UniversalSpriteLitSubShader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.ShaderGraph;
  4. using UnityEditor.ShaderGraph.Internal;
  5. using UnityEngine.Rendering;
  6. using UnityEngine.Rendering.Universal;
  7. using UnityEditor.Rendering.Universal;
  8. using Data.Util;
  9. namespace UnityEditor.Experimental.Rendering.Universal
  10. {
  11. [Serializable]
  12. [FormerName("UnityEditor.Experimental.Rendering.LWRP.LightWeightSpriteLitSubShader")]
  13. class UniversalSpriteLitSubShader : ISpriteLitSubShader
  14. {
  15. #region Passes
  16. ShaderPass m_LitPass = new ShaderPass
  17. {
  18. // Definition
  19. displayName = "Sprite Lit",
  20. referenceName = "SHADERPASS_SPRITELIT",
  21. lightMode = "Universal2D",
  22. passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/SpriteLitPass.hlsl",
  23. varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
  24. useInPreview = true,
  25. // Port mask
  26. vertexPorts = new List<int>()
  27. {
  28. SpriteLitMasterNode.PositionSlotId,
  29. SpriteLitMasterNode.VertNormalSlotId,
  30. SpriteLitMasterNode.VertTangentSlotId
  31. },
  32. pixelPorts = new List<int>
  33. {
  34. SpriteLitMasterNode.ColorSlotId,
  35. SpriteLitMasterNode.MaskSlotId,
  36. },
  37. // Required fields
  38. requiredVaryings = new List<string>()
  39. {
  40. "Varyings.color",
  41. "Varyings.texCoord0",
  42. "Varyings.screenPosition",
  43. },
  44. // Pass setup
  45. includes = new List<string>()
  46. {
  47. "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
  48. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
  49. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
  50. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl",
  51. "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl",
  52. },
  53. pragmas = new List<string>()
  54. {
  55. "prefer_hlslcc gles",
  56. "exclude_renderers d3d11_9x",
  57. "target 2.0",
  58. },
  59. keywords = new KeywordDescriptor[]
  60. {
  61. s_ETCExternalAlphaKeyword,
  62. s_ShapeLightType0Keyword,
  63. s_ShapeLightType1Keyword,
  64. s_ShapeLightType2Keyword,
  65. s_ShapeLightType3Keyword,
  66. },
  67. };
  68. ShaderPass m_NormalPass = new ShaderPass
  69. {
  70. // Definition
  71. displayName = "Sprite Normal",
  72. referenceName = "SHADERPASS_SPRITENORMAL",
  73. lightMode = "NormalsRendering",
  74. passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/SpriteNormalPass.hlsl",
  75. varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
  76. useInPreview = true,
  77. // Port mask
  78. vertexPorts = new List<int>()
  79. {
  80. SpriteLitMasterNode.PositionSlotId,
  81. SpriteLitMasterNode.VertNormalSlotId,
  82. SpriteLitMasterNode.VertTangentSlotId
  83. },
  84. pixelPorts = new List<int>
  85. {
  86. SpriteLitMasterNode.ColorSlotId,
  87. SpriteLitMasterNode.NormalSlotId
  88. },
  89. // Required fields
  90. requiredVaryings = new List<string>()
  91. {
  92. "Varyings.normalWS",
  93. "Varyings.tangentWS",
  94. },
  95. // Pass setup
  96. includes = new List<string>()
  97. {
  98. "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
  99. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
  100. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
  101. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl",
  102. "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
  103. },
  104. pragmas = new List<string>()
  105. {
  106. "prefer_hlslcc gles",
  107. "exclude_renderers d3d11_9x",
  108. "target 2.0",
  109. },
  110. };
  111. ShaderPass m_ForwardPass = new ShaderPass
  112. {
  113. // Definition
  114. displayName = "Sprite Forward",
  115. referenceName = "SHADERPASS_SPRITEFORWARD",
  116. lightMode = "UniversalForward",
  117. passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/SpriteForwardPass.hlsl",
  118. varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
  119. useInPreview = true,
  120. // Port mask
  121. vertexPorts = new List<int>()
  122. {
  123. SpriteLitMasterNode.PositionSlotId,
  124. SpriteLitMasterNode.VertNormalSlotId,
  125. SpriteLitMasterNode.VertTangentSlotId
  126. },
  127. pixelPorts = new List<int>
  128. {
  129. SpriteLitMasterNode.ColorSlotId,
  130. SpriteLitMasterNode.NormalSlotId
  131. },
  132. // Required fields
  133. requiredVaryings = new List<string>()
  134. {
  135. "Varyings.color",
  136. "Varyings.texCoord0",
  137. },
  138. // Pass setup
  139. includes = new List<string>()
  140. {
  141. "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
  142. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
  143. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
  144. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl",
  145. },
  146. pragmas = new List<string>()
  147. {
  148. "prefer_hlslcc gles",
  149. "exclude_renderers d3d11_9x",
  150. "target 2.0",
  151. },
  152. keywords = new KeywordDescriptor[]
  153. {
  154. s_ETCExternalAlphaKeyword,
  155. },
  156. };
  157. #endregion
  158. #region Keywords
  159. static KeywordDescriptor s_ETCExternalAlphaKeyword = new KeywordDescriptor()
  160. {
  161. displayName = "ETC External Alpha",
  162. referenceName = "ETC1_EXTERNAL_ALPHA",
  163. type = KeywordType.Boolean,
  164. definition = KeywordDefinition.MultiCompile,
  165. scope = KeywordScope.Global,
  166. };
  167. static KeywordDescriptor s_ShapeLightType0Keyword = new KeywordDescriptor()
  168. {
  169. displayName = "Shape Light Type 0",
  170. referenceName = "USE_SHAPE_LIGHT_TYPE_0",
  171. type = KeywordType.Boolean,
  172. definition = KeywordDefinition.MultiCompile,
  173. scope = KeywordScope.Global,
  174. };
  175. static KeywordDescriptor s_ShapeLightType1Keyword = new KeywordDescriptor()
  176. {
  177. displayName = "Shape Light Type 1",
  178. referenceName = "USE_SHAPE_LIGHT_TYPE_1",
  179. type = KeywordType.Boolean,
  180. definition = KeywordDefinition.MultiCompile,
  181. scope = KeywordScope.Global,
  182. };
  183. static KeywordDescriptor s_ShapeLightType2Keyword = new KeywordDescriptor()
  184. {
  185. displayName = "Shape Light Type 2",
  186. referenceName = "USE_SHAPE_LIGHT_TYPE_2",
  187. type = KeywordType.Boolean,
  188. definition = KeywordDefinition.MultiCompile,
  189. scope = KeywordScope.Global,
  190. };
  191. static KeywordDescriptor s_ShapeLightType3Keyword = new KeywordDescriptor()
  192. {
  193. displayName = "Shape Light Type 3",
  194. referenceName = "USE_SHAPE_LIGHT_TYPE_3",
  195. type = KeywordType.Boolean,
  196. definition = KeywordDefinition.MultiCompile,
  197. scope = KeywordScope.Global,
  198. };
  199. #endregion
  200. public int GetPreviewPassIndex() { return 0; }
  201. private static ActiveFields GetActiveFieldsFromMasterNode(SpriteLitMasterNode masterNode, ShaderPass pass)
  202. {
  203. var activeFields = new ActiveFields();
  204. var baseActiveFields = activeFields.baseInstance;
  205. // Graph Vertex
  206. if(masterNode.IsSlotConnected(SpriteLitMasterNode.PositionSlotId) ||
  207. masterNode.IsSlotConnected(SpriteLitMasterNode.VertNormalSlotId) ||
  208. masterNode.IsSlotConnected(SpriteLitMasterNode.VertTangentSlotId))
  209. {
  210. baseActiveFields.Add("features.graphVertex");
  211. }
  212. // Graph Pixel (always enabled)
  213. baseActiveFields.Add("features.graphPixel");
  214. baseActiveFields.Add("SurfaceType.Transparent");
  215. baseActiveFields.Add("BlendMode.Alpha");
  216. return activeFields;
  217. }
  218. private static bool GenerateShaderPass(SpriteLitMasterNode masterNode, ShaderPass pass, GenerationMode mode, ShaderGenerator result, List<string> sourceAssetDependencyPaths)
  219. {
  220. UniversalShaderGraphUtilities.SetRenderState(SurfaceType.Transparent, AlphaMode.Alpha, true, ref pass);
  221. // apply master node options to active fields
  222. var activeFields = GetActiveFieldsFromMasterNode(masterNode, pass);
  223. // use standard shader pass generation
  224. return ShaderGraph.GenerationUtils.GenerateShaderPass(masterNode, pass, mode, activeFields, result, sourceAssetDependencyPaths,
  225. UniversalShaderGraphResources.s_Dependencies, UniversalShaderGraphResources.s_ResourceClassName, UniversalShaderGraphResources.s_AssemblyName);
  226. }
  227. public string GetSubshader(IMasterNode masterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
  228. {
  229. if (sourceAssetDependencyPaths != null)
  230. {
  231. // LightWeightSpriteUnlitSubShader.cs
  232. sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("ca91dbeb78daa054c9bbe15fef76361c"));
  233. }
  234. // Master Node data
  235. var litMasterNode = masterNode as SpriteLitMasterNode;
  236. var subShader = new ShaderGenerator();
  237. subShader.AddShaderChunk("SubShader", true);
  238. subShader.AddShaderChunk("{", true);
  239. subShader.Indent();
  240. {
  241. var surfaceTags = ShaderGenerator.BuildMaterialTags(SurfaceType.Transparent);
  242. var tagsBuilder = new ShaderStringBuilder(0);
  243. surfaceTags.GetTags(tagsBuilder, "UniversalPipeline");
  244. subShader.AddShaderChunk(tagsBuilder.ToString());
  245. GenerateShaderPass(litMasterNode, m_LitPass, mode, subShader, sourceAssetDependencyPaths);
  246. GenerateShaderPass(litMasterNode, m_NormalPass, mode, subShader, sourceAssetDependencyPaths);
  247. GenerateShaderPass(litMasterNode, m_ForwardPass, mode, subShader, sourceAssetDependencyPaths);
  248. }
  249. subShader.Deindent();
  250. subShader.AddShaderChunk("}", true);
  251. return subShader.GetShaderString(0);
  252. }
  253. public bool IsPipelineCompatible(RenderPipelineAsset renderPipelineAsset)
  254. {
  255. return renderPipelineAsset is UniversalRenderPipelineAsset;
  256. }
  257. public UniversalSpriteLitSubShader () { }
  258. }
  259. }