Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DecalProjector.cs 9.9KB

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