using UnityEngine; namespace CW.Common { /// This component will change the light intensity based on the current render pipeline. [ExecuteInEditMode] [RequireComponent(typeof(Light))] [HelpURL(CwShared.HelpUrlPrefix + "CwLightIntensity")] [AddComponentMenu(CwShared.ComponentMenuPrefix + "Light Intensity")] public class CwLightIntensity : MonoBehaviour { /// All light values will be multiplied by this before use. public float Multiplier { set { multiplier = value; } get { return multiplier; } } [SerializeField] private float multiplier = 1.0f; /// This allows you to control the intensity of the attached light when using the Standard rendering pipeline. /// -1 = The attached light intensity will not be modified. public float IntensityInStandard { set { intensityInStandard = value; } get { return intensityInStandard; } } [SerializeField] private float intensityInStandard = 1.0f; /// This allows you to control the intensity of the attached light when using the URP rendering pipeline. /// -1 = The attached light intensity will not be modified. public float IntensityInURP { set { intensityInURP = value; } get { return intensityInURP; } } [SerializeField] private float intensityInURP = 1.0f; /// This allows you to control the intensity of the attached light when using the HDRP rendering pipeline. /// -1 = The attached light intensity will not be modified. public float IntensityInHDRP { set { intensityInHDRP = value; } get { return intensityInHDRP; } } [SerializeField] private float intensityInHDRP = 120000.0f; [System.NonSerialized] private Light cachedLight; [System.NonSerialized] private bool cachedLightSet; #if __HDRP__ [System.NonSerialized] private UnityEngine.Rendering.HighDefinition.HDAdditionalLightData cachedLightData; #endif public Light CachedLight { get { if (cachedLightSet == false) { cachedLight = GetComponent(); cachedLightSet = true; } return cachedLight; } } protected virtual void Update() { var pipe = CwShaderBundle.DetectProjectPipeline(); if (CwShaderBundle.IsStandard(pipe) == true) { ApplyIntensity(intensityInStandard); } else if (CwShaderBundle.IsURP(pipe) == true) { ApplyIntensity(intensityInURP); } else if (CwShaderBundle.IsHDRP(pipe) == true) { ApplyIntensity(intensityInHDRP); } } private void ApplyIntensity(float intensity) { if (intensity >= 0.0f) { if (cachedLightSet == false) { cachedLight = GetComponent(); cachedLightSet = true; } #if __HDRP__ if (cachedLightData == null) { cachedLightData = GetComponent(); } if (cachedLightData != null) { cachedLightData.SetIntensity(intensity * multiplier, UnityEngine.Rendering.HighDefinition.LightUnit.Lux); } #else cachedLight.intensity = intensity * multiplier; #endif } } } } #if UNITY_EDITOR namespace CW.Common { using UnityEditor; using TARGET = CwLightIntensity; [CanEditMultipleObjects] [CustomEditor(typeof(TARGET))] public class P3dLight_Editor : CwEditor { protected override void OnInspector() { Draw("multiplier", "All light values will be multiplied by this before use."); Draw("intensityInStandard", "This allows you to control the intensity of the attached light when using the Standard rendering pipeline.\n\n-1 = The attached light intensity will not be modified."); Draw("intensityInURP", "This allows you to control the intensity of the attached light when using the URP rendering pipeline.\n\n-1 = The attached light intensity will not be modified."); Draw("intensityInHDRP", "This allows you to control the intensity of the attached light when using the HDRP rendering pipeline.\n\n-1 = The attached light intensity will not be modified."); } } } #endif