No Description
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.

ShaderUtils.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Linq;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. /// <summary>
  6. /// Options to get a shader path to URP shaders when calling ShaderUtils.GetShaderGUID();
  7. /// <see cref="ShaderUtils"/>.
  8. /// </summary>
  9. public enum ShaderPathID
  10. {
  11. /// <summary>
  12. /// Use this for URP Lit shader.
  13. /// </summary>
  14. Lit,
  15. /// <summary>
  16. /// Use this for URP Simple Lit shader.
  17. /// </summary>
  18. SimpleLit,
  19. /// <summary>
  20. /// Use this for URP Unlit shader.
  21. /// </summary>
  22. Unlit,
  23. /// <summary>
  24. /// Use this for URP Terrain Lit shader.
  25. /// </summary>
  26. TerrainLit,
  27. /// <summary>
  28. /// Use this for URP Particles Lit shader.
  29. /// </summary>
  30. ParticlesLit,
  31. /// <summary>
  32. /// Use this for URP Particles Simple Lit shader.
  33. /// </summary>
  34. ParticlesSimpleLit,
  35. /// <summary>
  36. /// Use this for URP Particles Simple Unlit shader.
  37. /// </summary>
  38. ParticlesUnlit,
  39. /// <summary>
  40. /// Use this for URP Baked Lit shader.
  41. /// </summary>
  42. BakedLit,
  43. /// <summary>
  44. /// Use this for URP SpeedTree 7 shader.
  45. /// </summary>
  46. SpeedTree7,
  47. /// <summary>
  48. /// Use this for URP SpeedTree 7 Billboard shader.
  49. /// </summary>
  50. SpeedTree7Billboard,
  51. /// <summary>
  52. /// Use this for URP SpeedTree 8 shader.
  53. /// </summary>
  54. SpeedTree8,
  55. /// <summary>
  56. /// Use this for URP SpeedTree 9 shader.
  57. /// </summary>
  58. SpeedTree9,
  59. // If you add a value here, also add it to ShaderID in Editor/ShaderUtils.cs
  60. /// <summary>
  61. /// Use this for URP Complex Lit shader.
  62. /// </summary>
  63. ComplexLit,
  64. }
  65. /// <summary>
  66. /// Various utility functions for shaders in URP.
  67. /// </summary>
  68. public static class ShaderUtils
  69. {
  70. static readonly string[] s_ShaderPaths =
  71. {
  72. "Universal Render Pipeline/Lit",
  73. "Universal Render Pipeline/Simple Lit",
  74. "Universal Render Pipeline/Unlit",
  75. "Universal Render Pipeline/Terrain/Lit",
  76. "Universal Render Pipeline/Particles/Lit",
  77. "Universal Render Pipeline/Particles/Simple Lit",
  78. "Universal Render Pipeline/Particles/Unlit",
  79. "Universal Render Pipeline/Baked Lit",
  80. "Universal Render Pipeline/Nature/SpeedTree7",
  81. "Universal Render Pipeline/Nature/SpeedTree7 Billboard",
  82. "Universal Render Pipeline/Nature/SpeedTree8_PBRLit",
  83. "Universal Render Pipeline/Complex Lit",
  84. };
  85. /// <summary>
  86. /// Retrieves a shader path for the given URP Shader Path ID.
  87. /// </summary>
  88. /// <param name="id">The URP Shader Path ID.</param>
  89. /// <returns>The path to the URP shader.</returns>
  90. public static string GetShaderPath(ShaderPathID id)
  91. {
  92. int index = (int)id;
  93. int arrayLength = s_ShaderPaths.Length;
  94. if (arrayLength > 0 && index >= 0 && index < arrayLength)
  95. return s_ShaderPaths[index];
  96. Debug.LogError("Trying to access universal shader path out of bounds: (" + id + ": " + index + ")");
  97. return "";
  98. }
  99. /// <summary>
  100. /// Retrieves a URP Shader Path ID from a path given.
  101. /// </summary>
  102. /// <param name="path">The path to the shader.</param>
  103. /// <returns>The URP Shader Path ID.</returns>
  104. public static ShaderPathID GetEnumFromPath(string path)
  105. {
  106. var index = Array.FindIndex(s_ShaderPaths, m => m == path);
  107. return (ShaderPathID)index;
  108. }
  109. /// <summary>
  110. /// Checks if a given shader is a URP shader or not.
  111. /// </summary>
  112. /// <param name="shader">The shader.</param>
  113. /// <returns>True or false if it's a URP shader or not.</returns>
  114. public static bool IsLWShader(Shader shader)
  115. {
  116. return s_ShaderPaths.Contains(shader.name);
  117. }
  118. #if UNITY_EDITOR
  119. private static float s_MostRecentValidDeltaTime = 0.0f;
  120. #endif
  121. // A delta time that does not get reset to zero when stepping paused Play Mode or using the FrameDebugger
  122. // (unless Time.timeScale is zero)
  123. // * Can be zero on the first frame after domain reload (if Time.deltaTime is also zero)
  124. // * The value depends on when it was last called
  125. // * In in practice it should not get stale as it's called at least once during a URP frame
  126. // * Currently only used when calculating '_LastTimeParameters' for shader upload
  127. // * Please validate your use case if trying to reuse this somewhere else (as it might not transfer)
  128. internal static float PersistentDeltaTime
  129. {
  130. get
  131. {
  132. #if UNITY_EDITOR
  133. float deltaTime = Time.deltaTime;
  134. // The only case I'm aware of when a deltaTime of 0 is valid is when Time.timeScale is 0
  135. if (deltaTime > 0.0f || Time.timeScale == 0.0f)
  136. s_MostRecentValidDeltaTime = deltaTime;
  137. return s_MostRecentValidDeltaTime;
  138. #else
  139. return Time.deltaTime;
  140. #endif
  141. }
  142. }
  143. #if UNITY_EDITOR
  144. static readonly string[] s_ShaderGUIDs =
  145. {
  146. "933532a4fcc9baf4fa0491de14d08ed7",
  147. "8d2bb70cbf9db8d4da26e15b26e74248",
  148. "650dd9526735d5b46b79224bc6e94025",
  149. "69c1f799e772cb6438f56c23efccb782",
  150. "b7839dad95683814aa64166edc107ae2",
  151. "8516d7a69675844a7a0b7095af7c46af",
  152. "0406db5a14f94604a8c57ccfbc9f3b46",
  153. "0ca6dca7396eb48e5849247ffd444914",
  154. "0f4122b9a743b744abe2fb6a0a88868b",
  155. "5ec81c81908db34429b4f6ddecadd3bd",
  156. "9920c1f1781549a46ba081a2a15a16ec",
  157. "ee7e4c9a5f6364b688a332c67fc32cca",
  158. };
  159. /// <summary>
  160. /// Returns a GUID for a URP shader from Shader Path ID.
  161. /// </summary>
  162. /// <param name="id">ID of shader path.</param>
  163. /// <returns>GUID for the shader.</returns>
  164. public static string GetShaderGUID(ShaderPathID id)
  165. {
  166. int index = (int)id;
  167. int arrayLength = s_ShaderGUIDs.Length;
  168. if (arrayLength > 0 && index >= 0 && index < arrayLength)
  169. return s_ShaderGUIDs[index];
  170. Debug.LogError("Trying to access universal shader GUID out of bounds: (" + id + ": " + index + ")");
  171. return "";
  172. }
  173. #endif
  174. }
  175. }