Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LensFlareComponentSRP.cs 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. #endif
  4. using System;
  5. using System.Dynamic;
  6. using UnityEngine.Serialization;
  7. namespace UnityEngine.Rendering
  8. {
  9. /// <summary>
  10. /// Data-Driven Lens Flare can be added on any gameobject
  11. /// </summary>
  12. [ExecuteAlways]
  13. [AddComponentMenu("Rendering/Lens Flare (SRP)")]
  14. public sealed class LensFlareComponentSRP : MonoBehaviour
  15. {
  16. [SerializeField]
  17. private LensFlareDataSRP m_LensFlareData = null;
  18. enum Version
  19. {
  20. Initial,
  21. }
  22. #pragma warning disable 414
  23. [SerializeField]
  24. Version version = Version.Initial;
  25. #pragma warning restore 414
  26. /// <summary>
  27. /// Lens flare asset used on this component
  28. /// </summary>
  29. public LensFlareDataSRP lensFlareData
  30. {
  31. get
  32. {
  33. return m_LensFlareData;
  34. }
  35. set
  36. {
  37. m_LensFlareData = value;
  38. OnValidate();
  39. }
  40. }
  41. /// <summary>
  42. /// Intensity
  43. /// </summary>
  44. [Min(0.0f)]
  45. public float intensity = 1.0f;
  46. /// <summary>
  47. /// Distance used to scale the Distance Attenuation Curve
  48. /// </summary>
  49. [Min(1e-5f)]
  50. public float maxAttenuationDistance = 100.0f;
  51. /// <summary>
  52. /// Distance used to scale the Scale Attenuation Curve
  53. /// </summary>
  54. [Min(1e-5f)]
  55. public float maxAttenuationScale = 100.0f;
  56. /// <summary>
  57. /// Attenuation by distance
  58. /// </summary>
  59. public AnimationCurve distanceAttenuationCurve = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 0.0f));
  60. /// <summary>
  61. /// Scale by distance, use the same distance as distanceAttenuationCurve
  62. /// </summary>
  63. public AnimationCurve scaleByDistanceCurve = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 0.0f));
  64. /// <summary>
  65. /// If component attached to a light, attenuation the lens flare per light type
  66. /// </summary>
  67. public bool attenuationByLightShape = true;
  68. /// <summary>
  69. /// Attenuation used radially, which allow for instance to enable flare only on the edge of the screen
  70. /// </summary>
  71. public AnimationCurve radialScreenAttenuationCurve = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 1.0f));
  72. /// <summary>
  73. /// Enable Occlusion feature
  74. /// </summary>
  75. public bool useOcclusion = false;
  76. /// <summary>
  77. /// Enable Occlusion using Background Cloud (for instance: CloudLayer)
  78. /// Please use useFogOpacityOcclusion instead.
  79. /// </summary>
  80. [Obsolete("Replaced by environmentOcclusion.")]
  81. public bool useBackgroundCloudOcclusion = false;
  82. /// <summary>Enable occlusion from environment effects supported by the render pipeline. This may include opacity from volumetric clouds, background clouds, fog and water.</summary>
  83. [FormerlySerializedAs("volumetricCloudOcclusion")]
  84. [FormerlySerializedAs("useFogOpacityOcclusion")]
  85. public bool environmentOcclusion = false;
  86. /// <summary>
  87. /// Enable Occlusion with Water
  88. /// </summary>
  89. [Obsolete("Replaced by environmentOcclusion.")]
  90. public bool useWaterOcclusion = false;
  91. /// <summary>
  92. /// Radius around the light used to occlude the flare (value in world space)
  93. /// </summary>
  94. [Min(0.0f)]
  95. public float occlusionRadius = 0.1f;
  96. /// <summary>
  97. /// Random Samples Count used inside the disk with 'occlusionRadius'
  98. /// </summary>
  99. [Range(1, 64)]
  100. public uint sampleCount = 32;
  101. /// <summary>
  102. /// Z Occlusion Offset allow us to offset the plane where the disc of occlusion is place (closer to camera), value on world space.
  103. /// Useful for instance to sample occlusion outside a light bulb if we place a flare inside the light bulb
  104. /// </summary>
  105. public float occlusionOffset = 0.05f;
  106. /// <summary>
  107. /// Global Scale
  108. /// </summary>
  109. [Min(0.0f)]
  110. public float scale = 1.0f;
  111. /// <summary>
  112. /// If allowOffScreen is true then If the lens flare is outside the screen we still emit the flare on screen
  113. /// </summary>
  114. public bool allowOffScreen = false;
  115. /// <summary>
  116. /// If volumetricCloudOcclusion is true then use the volumetric cloud (on HDRP only) for the occlusion
  117. /// Please use useFogOpacityOcclusion instead.
  118. /// </summary>
  119. [Obsolete("Please use environmentOcclusion instead.")]
  120. public bool volumetricCloudOcclusion = false;
  121. /// Our default celestial body will have an angular radius of 3.3 degrees. This is an arbitrary number, but must be kept constant
  122. /// so the occlusion radius for direct lights is consistent regardless of near / far clip plane configuration.
  123. private static float sCelestialAngularRadius = 3.3f * Mathf.PI / 180.0f;
  124. /// <summary>
  125. /// OcclusionRemapCurve allow the occlusion [from 0 to 1] to be remap with any desired shape.
  126. /// </summary>
  127. public TextureCurve occlusionRemapCurve = new TextureCurve(AnimationCurve.Linear(0.0f, 0.0f, 1.0f, 1.0f), 1.0f, false, new Vector2(0.0f, 1.0f));
  128. /// <summary>
  129. /// Light override, change the light which influences the flares including "modulate by light color" and "Attenuation By Light Shape" but not the position.
  130. /// </summary>
  131. public Light lightOverride = null;
  132. /// <summary>
  133. /// Retrieves the projected occlusion radius from a particular celestial in the infinity plane with an angular radius.
  134. /// This is used for directional lights which require to have consistent occlusion radius regardless of the near/farplane configuration.
  135. /// </summary>
  136. /// <param name="mainCam">The camera utilized to calculate the occlusion radius</param>
  137. /// <returns>The value, in world units, of the occlusion angular radius.</returns>
  138. public float celestialProjectedOcclusionRadius(Camera mainCam)
  139. {
  140. float projectedRadius = (float)Math.Tan(sCelestialAngularRadius) * mainCam.farClipPlane;
  141. return occlusionRadius * projectedRadius;
  142. }
  143. void Awake()
  144. {
  145. #if UNITY_EDITOR
  146. if (!lensFlareData)
  147. lensFlareData = AssetDatabase.LoadAssetAtPath<LensFlareDataSRP>("Packages/com.unity.render-pipelines.core/Runtime/RenderPipelineResources/Default Lens Flare (SRP).asset");
  148. #endif
  149. }
  150. /// <summary>
  151. /// Add or remove the lens flare to the queue of PostProcess
  152. /// </summary>
  153. void OnEnable()
  154. {
  155. if (lensFlareData)
  156. LensFlareCommonSRP.Instance.AddData(this);
  157. else
  158. LensFlareCommonSRP.Instance.RemoveData(this);
  159. }
  160. /// <summary>
  161. /// Remove the lens flare from the queue of PostProcess
  162. /// </summary>
  163. void OnDisable()
  164. {
  165. LensFlareCommonSRP.Instance.RemoveData(this);
  166. }
  167. /// <summary>
  168. /// Add or remove the lens flare from the queue of PostProcess
  169. /// </summary>
  170. void OnValidate()
  171. {
  172. if (isActiveAndEnabled && lensFlareData != null)
  173. {
  174. LensFlareCommonSRP.Instance.AddData(this);
  175. }
  176. else
  177. {
  178. LensFlareCommonSRP.Instance.RemoveData(this);
  179. }
  180. }
  181. private void OnDestroy()
  182. {
  183. occlusionRemapCurve.Release();
  184. }
  185. #if UNITY_EDITOR
  186. private float sDebugClippingSafePercentage = 0.9f; //for debug gizmo, only push 90% further so we avoid clipping of debug lines.
  187. void OnDrawGizmosSelected()
  188. {
  189. Camera mainCam = Camera.current;
  190. if (mainCam != null && useOcclusion)
  191. {
  192. Vector3 positionWS;
  193. float adjustedOcclusionRadius = occlusionRadius;
  194. Light light = GetComponent<Light>();
  195. if (light != null && light.type == LightType.Directional)
  196. {
  197. positionWS = -transform.forward * (mainCam.farClipPlane * sDebugClippingSafePercentage) + mainCam.transform.position;
  198. adjustedOcclusionRadius = celestialProjectedOcclusionRadius(mainCam);
  199. }
  200. else
  201. {
  202. positionWS = transform.position;
  203. }
  204. Color previousH = Handles.color;
  205. Color previousG = Gizmos.color;
  206. Handles.color = Color.red;
  207. Gizmos.color = Color.red;
  208. Vector3 dir = (mainCam.transform.position - positionWS).normalized;
  209. Handles.DrawWireDisc(positionWS + dir * occlusionOffset, dir, adjustedOcclusionRadius, 1.0f);
  210. Gizmos.DrawWireSphere(positionWS, occlusionOffset);
  211. Gizmos.color = previousG;
  212. Handles.color = previousH;
  213. }
  214. }
  215. #endif
  216. }
  217. }