Skip to content

Instantly share code, notes, and snippets.

@maoyeedy
Last active June 28, 2025 07:09
Show Gist options
  • Save maoyeedy/a8d66b314dfe671a6a9336eb4a2d15ef to your computer and use it in GitHub Desktop.
Save maoyeedy/a8d66b314dfe671a6a9336eb4a2d15ef to your computer and use it in GitHub Desktop.
[Unity] Override Camera Ambient Color for SRP
using UnityEngine;
using UnityEngine.Rendering;
namespace CameraAmbientOverride
{
/// <summary>
/// Attach it to camera. Supports both URP and HDRP.
/// </summary>
[DisallowMultipleComponent]
[RequireComponent(typeof(Camera))]
public class CameraAmbientOverrideSrp : MonoBehaviour
{
[SerializeField] private bool _useCustomAmbientColor;
[SerializeField] private Color _customAmbientColor;
private Camera _cam;
private Color _defaultAmbientColor;
private bool _isSubscribed;
private void Awake()
{
_defaultAmbientColor = RenderSettings.ambientLight;
_cam = GetComponent<Camera>();
}
private void OnEnable()
{
SubscribeToEvents();
}
private void OnDisable()
{
UnsubscribeFromEvents();
}
private void SubscribeToEvents()
{
if (_isSubscribed) return;
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
_isSubscribed = true;
}
private void UnsubscribeFromEvents()
{
if (!_isSubscribed) return;
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
_isSubscribed = false;
}
private void OnBeginCameraRendering(ScriptableRenderContext ctx, Camera cam)
{
if (cam != _cam) return;
if (_useCustomAmbientColor) RenderSettings.ambientLight = _customAmbientColor;
}
private void OnEndCameraRendering(ScriptableRenderContext ctx, Camera cam)
{
if (cam != _cam) return;
if (_useCustomAmbientColor) RenderSettings.ambientLight = _defaultAmbientColor;
}
}
}
@maoyeedy
Copy link
Author

maoyeedy commented Apr 28, 2025

Usage scenario

  • To have different environment lighting for certain objects
  • To set ambient light per camera

@maoyeedy
Copy link
Author

using UnityEngine;

namespace CameraAmbientOverride
{
    /// <summary>
    /// Works for built-in render pipeline.
    /// </summary>
    [DisallowMultipleComponent]
    [RequireComponent(typeof(Camera))]
    public class CameraAmbientOverrideBIT : MonoBehaviour
    {
        [SerializeField] private bool _useCustomAmbientColor;
        [SerializeField] private Color _customAmbientColor;

        private Color _defaultAmbientColor;

        private void Awake()
        {
            _defaultAmbientColor = RenderSettings.ambientLight;
        }

        private void OnPostRender()
        {
            if (_useCustomAmbientColor) RenderSettings.ambientLight = _defaultAmbientColor;
        }

        private void OnPreCull()
        {
            if (_useCustomAmbientColor) RenderSettings.ambientLight = _customAmbientColor;
        }
    }
}

Here's the built-in pipeline version, posted by peachpieproductions.

I got inspiration from it and write the SRP version (as SRP don't use OnPostRender but use endCameraRendering) (Source)

@maoyeedy
Copy link
Author

Demo

Desktop.2025.01.23.-.23.58.03.19.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment