UniversalUnlitSubShader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Data.Util;
  5. using UnityEditor.Graphing;
  6. using UnityEditor.ShaderGraph;
  7. using UnityEditor.ShaderGraph.Internal;
  8. using UnityEngine.Rendering;
  9. using UnityEngine.Rendering.Universal;
  10. namespace UnityEditor.Rendering.Universal
  11. {
  12. [Serializable]
  13. [FormerName("UnityEngine.Experimental.Rendering.LightweightPipeline.LightWeightUnlitSubShader")]
  14. [FormerName("UnityEditor.ShaderGraph.LightWeightUnlitSubShader")]
  15. [FormerName("UnityEditor.Rendering.LWRP.LightWeightUnlitSubShader")]
  16. [FormerName("UnityEngine.Rendering.LWRP.LightWeightUnlitSubShader")]
  17. class UniversalUnlitSubShader : IUnlitSubShader
  18. {
  19. #region Passes
  20. ShaderPass m_UnlitPass = new ShaderPass
  21. {
  22. // Definition
  23. displayName = "Pass",
  24. referenceName = "SHADERPASS_UNLIT",
  25. passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/UnlitPass.hlsl",
  26. varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
  27. useInPreview = true,
  28. // Port mask
  29. vertexPorts = new List<int>()
  30. {
  31. UnlitMasterNode.PositionSlotId,
  32. UnlitMasterNode.VertNormalSlotId,
  33. UnlitMasterNode.VertTangentSlotId
  34. },
  35. pixelPorts = new List<int>
  36. {
  37. UnlitMasterNode.ColorSlotId,
  38. UnlitMasterNode.AlphaSlotId,
  39. UnlitMasterNode.AlphaThresholdSlotId
  40. },
  41. // Pass setup
  42. includes = new List<string>()
  43. {
  44. "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
  45. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
  46. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
  47. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl",
  48. "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl"
  49. },
  50. pragmas = new List<string>()
  51. {
  52. "prefer_hlslcc gles",
  53. "exclude_renderers d3d11_9x",
  54. "target 2.0",
  55. "multi_compile_fog",
  56. "multi_compile_instancing",
  57. },
  58. keywords = new KeywordDescriptor[]
  59. {
  60. s_LightmapKeyword,
  61. s_DirectionalLightmapCombinedKeyword,
  62. s_SampleGIKeyword,
  63. },
  64. };
  65. ShaderPass m_DepthOnlyPass = new ShaderPass()
  66. {
  67. // Definition
  68. displayName = "DepthOnly",
  69. referenceName = "SHADERPASS_DEPTHONLY",
  70. lightMode = "DepthOnly",
  71. passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/DepthOnlyPass.hlsl",
  72. varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
  73. useInPreview = true,
  74. // Port mask
  75. vertexPorts = new List<int>()
  76. {
  77. UnlitMasterNode.PositionSlotId,
  78. UnlitMasterNode.VertNormalSlotId,
  79. UnlitMasterNode.VertTangentSlotId
  80. },
  81. pixelPorts = new List<int>()
  82. {
  83. UnlitMasterNode.AlphaSlotId,
  84. UnlitMasterNode.AlphaThresholdSlotId
  85. },
  86. // Render State Overrides
  87. ZWriteOverride = "ZWrite On",
  88. ColorMaskOverride = "ColorMask 0",
  89. // Pass setup
  90. includes = new List<string>()
  91. {
  92. "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
  93. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
  94. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
  95. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl",
  96. "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl"
  97. },
  98. pragmas = new List<string>()
  99. {
  100. "prefer_hlslcc gles",
  101. "exclude_renderers d3d11_9x",
  102. "target 2.0",
  103. "multi_compile_instancing",
  104. },
  105. };
  106. ShaderPass m_ShadowCasterPass = new ShaderPass()
  107. {
  108. // Definition
  109. displayName = "ShadowCaster",
  110. referenceName = "SHADERPASS_SHADOWCASTER",
  111. lightMode = "ShadowCaster",
  112. passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShadowCasterPass.hlsl",
  113. varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
  114. // Port mask
  115. vertexPorts = new List<int>()
  116. {
  117. UnlitMasterNode.PositionSlotId,
  118. UnlitMasterNode.VertNormalSlotId,
  119. UnlitMasterNode.VertTangentSlotId
  120. },
  121. pixelPorts = new List<int>()
  122. {
  123. UnlitMasterNode.AlphaSlotId,
  124. UnlitMasterNode.AlphaThresholdSlotId
  125. },
  126. // Required fields
  127. requiredAttributes = new List<string>()
  128. {
  129. "Attributes.normalOS",
  130. },
  131. // Render State Overrides
  132. ZWriteOverride = "ZWrite On",
  133. ZTestOverride = "ZTest LEqual",
  134. // Pass setup
  135. includes = new List<string>()
  136. {
  137. "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
  138. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
  139. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
  140. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl",
  141. "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl"
  142. },
  143. pragmas = new List<string>()
  144. {
  145. "prefer_hlslcc gles",
  146. "exclude_renderers d3d11_9x",
  147. "target 2.0",
  148. "multi_compile_instancing",
  149. },
  150. keywords = new KeywordDescriptor[]
  151. {
  152. s_SmoothnessChannelKeyword,
  153. },
  154. };
  155. #endregion
  156. #region Keywords
  157. static KeywordDescriptor s_LightmapKeyword = new KeywordDescriptor()
  158. {
  159. displayName = "Lightmap",
  160. referenceName = "LIGHTMAP_ON",
  161. type = KeywordType.Boolean,
  162. definition = KeywordDefinition.MultiCompile,
  163. scope = KeywordScope.Global,
  164. };
  165. static KeywordDescriptor s_DirectionalLightmapCombinedKeyword = new KeywordDescriptor()
  166. {
  167. displayName = "Directional Lightmap Combined",
  168. referenceName = "DIRLIGHTMAP_COMBINED",
  169. type = KeywordType.Boolean,
  170. definition = KeywordDefinition.MultiCompile,
  171. scope = KeywordScope.Global,
  172. };
  173. static KeywordDescriptor s_SampleGIKeyword = new KeywordDescriptor()
  174. {
  175. displayName = "Sample GI",
  176. referenceName = "_SAMPLE_GI",
  177. type = KeywordType.Boolean,
  178. definition = KeywordDefinition.ShaderFeature,
  179. scope = KeywordScope.Global,
  180. };
  181. static KeywordDescriptor s_SmoothnessChannelKeyword = new KeywordDescriptor()
  182. {
  183. displayName = "Smoothness Channel",
  184. referenceName = "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A",
  185. type = KeywordType.Boolean,
  186. definition = KeywordDefinition.ShaderFeature,
  187. scope = KeywordScope.Global,
  188. };
  189. #endregion
  190. public int GetPreviewPassIndex() { return 0; }
  191. private static ActiveFields GetActiveFieldsFromMasterNode(UnlitMasterNode masterNode, ShaderPass pass)
  192. {
  193. var activeFields = new ActiveFields();
  194. var baseActiveFields = activeFields.baseInstance;
  195. // Graph Vertex
  196. if(masterNode.IsSlotConnected(UnlitMasterNode.PositionSlotId) ||
  197. masterNode.IsSlotConnected(UnlitMasterNode.VertNormalSlotId) ||
  198. masterNode.IsSlotConnected(UnlitMasterNode.VertTangentSlotId))
  199. {
  200. baseActiveFields.Add("features.graphVertex");
  201. }
  202. // Graph Pixel (always enabled)
  203. baseActiveFields.Add("features.graphPixel");
  204. if (masterNode.IsSlotConnected(UnlitMasterNode.AlphaThresholdSlotId) ||
  205. masterNode.GetInputSlots<Vector1MaterialSlot>().First(x => x.id == UnlitMasterNode.AlphaThresholdSlotId).value > 0.0f)
  206. {
  207. baseActiveFields.Add("AlphaClip");
  208. }
  209. // Keywords for transparent
  210. // #pragma shader_feature _SURFACE_TYPE_TRANSPARENT
  211. if (masterNode.surfaceType != ShaderGraph.SurfaceType.Opaque)
  212. {
  213. // transparent-only defines
  214. baseActiveFields.Add("SurfaceType.Transparent");
  215. // #pragma shader_feature _ _BLENDMODE_ALPHA _BLENDMODE_ADD _BLENDMODE_PRE_MULTIPLY
  216. if (masterNode.alphaMode == AlphaMode.Alpha)
  217. {
  218. baseActiveFields.Add("BlendMode.Alpha");
  219. }
  220. else if (masterNode.alphaMode == AlphaMode.Additive)
  221. {
  222. baseActiveFields.Add("BlendMode.Add");
  223. }
  224. else if (masterNode.alphaMode == AlphaMode.Premultiply)
  225. {
  226. baseActiveFields.Add("BlendMode.Premultiply");
  227. }
  228. }
  229. return activeFields;
  230. }
  231. private static bool GenerateShaderPass(UnlitMasterNode masterNode, ShaderPass pass, GenerationMode mode, ShaderGenerator result, List<string> sourceAssetDependencyPaths)
  232. {
  233. UniversalShaderGraphUtilities.SetRenderState(masterNode.surfaceType, masterNode.alphaMode, masterNode.twoSided.isOn, ref pass);
  234. // apply master node options to active fields
  235. var activeFields = GetActiveFieldsFromMasterNode(masterNode, pass);
  236. // use standard shader pass generation
  237. return ShaderGraph.GenerationUtils.GenerateShaderPass(masterNode, pass, mode, activeFields, result, sourceAssetDependencyPaths,
  238. UniversalShaderGraphResources.s_Dependencies, UniversalShaderGraphResources.s_ResourceClassName, UniversalShaderGraphResources.s_AssemblyName);
  239. }
  240. public string GetSubshader(IMasterNode masterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
  241. {
  242. if (sourceAssetDependencyPaths != null)
  243. {
  244. // LightWeightPBRSubShader.cs
  245. sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("62511ee827d14492a8c78ba0ef167e7f"));
  246. }
  247. // Master Node data
  248. var unlitMasterNode = masterNode as UnlitMasterNode;
  249. var subShader = new ShaderGenerator();
  250. subShader.AddShaderChunk("SubShader", true);
  251. subShader.AddShaderChunk("{", true);
  252. subShader.Indent();
  253. {
  254. var surfaceTags = ShaderGenerator.BuildMaterialTags(unlitMasterNode.surfaceType);
  255. var tagsBuilder = new ShaderStringBuilder(0);
  256. surfaceTags.GetTags(tagsBuilder, "UniversalPipeline");
  257. subShader.AddShaderChunk(tagsBuilder.ToString());
  258. GenerateShaderPass(unlitMasterNode, m_UnlitPass, mode, subShader, sourceAssetDependencyPaths);
  259. GenerateShaderPass(unlitMasterNode, m_ShadowCasterPass, mode, subShader, sourceAssetDependencyPaths);
  260. GenerateShaderPass(unlitMasterNode, m_DepthOnlyPass, mode, subShader, sourceAssetDependencyPaths);
  261. }
  262. subShader.Deindent();
  263. subShader.AddShaderChunk("}", true);
  264. return subShader.GetShaderString(0);
  265. }
  266. public bool IsPipelineCompatible(RenderPipelineAsset renderPipelineAsset)
  267. {
  268. return renderPipelineAsset is UniversalRenderPipelineAsset;
  269. }
  270. public UniversalUnlitSubShader() { }
  271. }
  272. }