DecalProjector.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System;
  2. using UnityEditor;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. /// <summary>The scaling mode to apply to decals that use the Decal Projector.</summary>
  6. public enum DecalScaleMode
  7. {
  8. /// <summary>Ignores the transformation hierarchy and uses the scale values in the Decal Projector component directly.</summary>
  9. ScaleInvariant,
  10. /// <summary>Multiplies the lossy scale of the Transform with the Decal Projector's own scale then applies this to the decal.</summary>
  11. [InspectorName("Inherit from Hierarchy")]
  12. InheritFromHierarchy,
  13. }
  14. /// <summary>
  15. /// Decal Projector component.
  16. /// </summary>
  17. [ExecuteAlways]
  18. #if UNITY_EDITOR
  19. [CanEditMultipleObjects]
  20. #endif
  21. [AddComponentMenu("Rendering/URP Decal Projector")]
  22. public class DecalProjector : MonoBehaviour
  23. {
  24. internal delegate void DecalProjectorAction(DecalProjector decalProjector);
  25. internal static event DecalProjectorAction onDecalAdd;
  26. internal static event DecalProjectorAction onDecalRemove;
  27. internal static event DecalProjectorAction onDecalPropertyChange;
  28. internal static event DecalProjectorAction onDecalMaterialChange;
  29. internal static Material defaultMaterial { get; set; }
  30. internal static bool isSupported => onDecalAdd != null;
  31. internal DecalEntity decalEntity { get; set; }
  32. [SerializeField]
  33. private Material m_Material = null;
  34. /// <summary>
  35. /// The material used by the decal.
  36. /// </summary>
  37. public Material material
  38. {
  39. get
  40. {
  41. return m_Material;
  42. }
  43. set
  44. {
  45. m_Material = value;
  46. OnValidate();
  47. }
  48. }
  49. [SerializeField]
  50. private float m_DrawDistance = 1000.0f;
  51. /// <summary>
  52. /// Distance from camera at which the Decal is not rendered anymore.
  53. /// </summary>
  54. public float drawDistance
  55. {
  56. get
  57. {
  58. return m_DrawDistance;
  59. }
  60. set
  61. {
  62. m_DrawDistance = Mathf.Max(0f, value);
  63. OnValidate();
  64. }
  65. }
  66. [SerializeField]
  67. [Range(0, 1)]
  68. private float m_FadeScale = 0.9f;
  69. /// <summary>
  70. /// Percent of the distance from the camera at which this Decal start to fade off.
  71. /// </summary>
  72. public float fadeScale
  73. {
  74. get
  75. {
  76. return m_FadeScale;
  77. }
  78. set
  79. {
  80. m_FadeScale = Mathf.Clamp01(value);
  81. OnValidate();
  82. }
  83. }
  84. [SerializeField]
  85. [Range(0, 180)]
  86. private float m_StartAngleFade = 180.0f;
  87. /// <summary>
  88. /// Angle between decal backward orientation and vertex normal of receiving surface at which the Decal start to fade off.
  89. /// </summary>
  90. public float startAngleFade
  91. {
  92. get
  93. {
  94. return m_StartAngleFade;
  95. }
  96. set
  97. {
  98. m_StartAngleFade = Mathf.Clamp(value, 0.0f, 180.0f);
  99. OnValidate();
  100. }
  101. }
  102. [SerializeField]
  103. [Range(0, 180)]
  104. private float m_EndAngleFade = 180.0f;
  105. /// <summary>
  106. /// Angle between decal backward orientation and vertex normal of receiving surface at which the Decal end to fade off.
  107. /// </summary>
  108. public float endAngleFade
  109. {
  110. get
  111. {
  112. return m_EndAngleFade;
  113. }
  114. set
  115. {
  116. m_EndAngleFade = Mathf.Clamp(value, m_StartAngleFade, 180.0f);
  117. OnValidate();
  118. }
  119. }
  120. [SerializeField]
  121. private Vector2 m_UVScale = new Vector2(1, 1);
  122. /// <summary>
  123. /// Tilling of the UV of the projected texture.
  124. /// </summary>
  125. public Vector2 uvScale
  126. {
  127. get
  128. {
  129. return m_UVScale;
  130. }
  131. set
  132. {
  133. m_UVScale = value;
  134. OnValidate();
  135. }
  136. }
  137. [SerializeField]
  138. private Vector2 m_UVBias = new Vector2(0, 0);
  139. /// <summary>
  140. /// Offset of the UV of the projected texture.
  141. /// </summary>
  142. public Vector2 uvBias
  143. {
  144. get
  145. {
  146. return m_UVBias;
  147. }
  148. set
  149. {
  150. m_UVBias = value;
  151. OnValidate();
  152. }
  153. }
  154. [SerializeField]
  155. private DecalScaleMode m_ScaleMode = DecalScaleMode.ScaleInvariant;
  156. /// <summary>
  157. /// The scaling mode to apply to decals that use this Decal Projector.
  158. /// </summary>
  159. public DecalScaleMode scaleMode
  160. {
  161. get => m_ScaleMode;
  162. set
  163. {
  164. m_ScaleMode = value;
  165. OnValidate();
  166. }
  167. }
  168. [SerializeField]
  169. internal Vector3 m_Offset = new Vector3(0, 0, 0.5f);
  170. /// <summary>
  171. /// Change the offset position.
  172. /// Do not expose: Could be changed by the inspector when manipulating the gizmo.
  173. /// </summary>
  174. public Vector3 pivot
  175. {
  176. get
  177. {
  178. return m_Offset;
  179. }
  180. set
  181. {
  182. m_Offset = value;
  183. OnValidate();
  184. }
  185. }
  186. [SerializeField]
  187. internal Vector3 m_Size = new Vector3(1, 1, 1);
  188. /// <summary>
  189. /// The size of the projection volume.
  190. /// </summary>
  191. public Vector3 size
  192. {
  193. get
  194. {
  195. return m_Size;
  196. }
  197. set
  198. {
  199. m_Size = value;
  200. OnValidate();
  201. }
  202. }
  203. [SerializeField]
  204. [Range(0, 1)]
  205. private float m_FadeFactor = 1.0f;
  206. /// <summary>
  207. /// Controls the transparency of the decal.
  208. /// </summary>
  209. public float fadeFactor
  210. {
  211. get
  212. {
  213. return m_FadeFactor;
  214. }
  215. set
  216. {
  217. m_FadeFactor = Mathf.Clamp01(value);
  218. OnValidate();
  219. }
  220. }
  221. private Material m_OldMaterial = null;
  222. /// <summary>A scale that should be used for rendering and handles.</summary>
  223. internal Vector3 effectiveScale => m_ScaleMode == DecalScaleMode.InheritFromHierarchy ? transform.lossyScale : Vector3.one;
  224. /// <summary>current size in a way the DecalSystem will be able to use it</summary>
  225. internal Vector3 decalSize => new Vector3(m_Size.x, m_Size.z, m_Size.y);
  226. /// <summary>current size in a way the DecalSystem will be able to use it</summary>
  227. internal Vector3 decalOffset => new Vector3(m_Offset.x, -m_Offset.z, m_Offset.y);
  228. /// <summary>current uv parameters in a way the DecalSystem will be able to use it</summary>
  229. internal Vector4 uvScaleBias => new Vector4(m_UVScale.x, m_UVScale.y, m_UVBias.x, m_UVBias.y);
  230. void InitMaterial()
  231. {
  232. if (m_Material == null)
  233. {
  234. #if UNITY_EDITOR
  235. m_Material = defaultMaterial;
  236. #endif
  237. }
  238. }
  239. void OnEnable()
  240. {
  241. InitMaterial();
  242. m_OldMaterial = m_Material;
  243. onDecalAdd?.Invoke(this);
  244. #if UNITY_EDITOR
  245. // Handle scene visibility
  246. UnityEditor.SceneVisibilityManager.visibilityChanged += UpdateDecalVisibility;
  247. #endif
  248. }
  249. #if UNITY_EDITOR
  250. void UpdateDecalVisibility()
  251. {
  252. // Fade out the decal when it is hidden by the scene visibility
  253. if (UnityEditor.SceneVisibilityManager.instance.IsHidden(gameObject))
  254. {
  255. onDecalRemove?.Invoke(this);
  256. }
  257. else
  258. {
  259. onDecalAdd?.Invoke(this);
  260. onDecalPropertyChange?.Invoke(this); // Scene culling mask may have changed.
  261. }
  262. }
  263. #endif
  264. void OnDisable()
  265. {
  266. onDecalRemove?.Invoke(this);
  267. #if UNITY_EDITOR
  268. UnityEditor.SceneVisibilityManager.visibilityChanged -= UpdateDecalVisibility;
  269. #endif
  270. }
  271. internal void OnValidate()
  272. {
  273. if (!isActiveAndEnabled)
  274. return;
  275. if (m_Material != m_OldMaterial)
  276. {
  277. onDecalMaterialChange?.Invoke(this);
  278. m_OldMaterial = m_Material;
  279. }
  280. else
  281. onDecalPropertyChange?.Invoke(this);
  282. }
  283. public bool IsValid()
  284. {
  285. if (material == null)
  286. return false;
  287. if (material.FindPass(DecalShaderPassNames.DBufferProjector) != -1)
  288. return true;
  289. if (material.FindPass(DecalShaderPassNames.DecalProjectorForwardEmissive) != -1)
  290. return true;
  291. if (material.FindPass(DecalShaderPassNames.DecalScreenSpaceProjector) != -1)
  292. return true;
  293. if (material.FindPass(DecalShaderPassNames.DecalGBufferProjector) != -1)
  294. return true;
  295. return false;
  296. }
  297. }
  298. }