UniversalSpriteUnlitSubShader.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.ShaderGraph;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Rendering.Universal;
  6. using UnityEditor.Rendering.Universal;
  7. using Data.Util;
  8. using UnityEditor.ShaderGraph.Internal;
  9. namespace UnityEditor.Experimental.Rendering.Universal
  10. {
  11. [Serializable]
  12. [FormerName("UnityEditor.Experimental.Rendering.LWRP.LightWeightSpriteUnlitSubShader")]
  13. class UniversalSpriteUnlitSubShader : ISpriteUnlitSubShader
  14. {
  15. #region Passes
  16. ShaderPass m_UnlitPass = new ShaderPass
  17. {
  18. // Definition
  19. referenceName = "SHADERPASS_SPRITEUNLIT",
  20. passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/SpriteUnlitPass.hlsl",
  21. varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
  22. useInPreview = true,
  23. // Port mask
  24. vertexPorts = new List<int>()
  25. {
  26. SpriteUnlitMasterNode.PositionSlotId,
  27. SpriteUnlitMasterNode.VertNormalSlotId,
  28. SpriteUnlitMasterNode.VertTangentSlotId
  29. },
  30. pixelPorts = new List<int>
  31. {
  32. SpriteUnlitMasterNode.ColorSlotId,
  33. },
  34. // Required fields
  35. requiredAttributes = new List<string>()
  36. {
  37. "Attributes.color",
  38. "Attributes.uv0",
  39. },
  40. requiredVaryings = new List<string>()
  41. {
  42. "Varyings.color",
  43. "Varyings.texCoord0",
  44. },
  45. // Pass setup
  46. includes = new List<string>()
  47. {
  48. "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
  49. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
  50. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
  51. "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.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. },
  63. };
  64. #endregion
  65. #region Keywords
  66. static KeywordDescriptor s_ETCExternalAlphaKeyword = new KeywordDescriptor()
  67. {
  68. displayName = "ETC External Alpha",
  69. referenceName = "ETC1_EXTERNAL_ALPHA",
  70. type = KeywordType.Boolean,
  71. definition = KeywordDefinition.MultiCompile,
  72. scope = KeywordScope.Global,
  73. };
  74. #endregion
  75. public int GetPreviewPassIndex() { return 0; }
  76. private static ActiveFields GetActiveFieldsFromMasterNode(SpriteUnlitMasterNode masterNode, ShaderPass pass)
  77. {
  78. var activeFields = new ActiveFields();
  79. var baseActiveFields = activeFields.baseInstance;
  80. // Graph Vertex
  81. if(masterNode.IsSlotConnected(SpriteUnlitMasterNode.PositionSlotId) ||
  82. masterNode.IsSlotConnected(SpriteUnlitMasterNode.VertNormalSlotId) ||
  83. masterNode.IsSlotConnected(SpriteUnlitMasterNode.VertTangentSlotId))
  84. {
  85. baseActiveFields.Add("features.graphVertex");
  86. }
  87. // Graph Pixel (always enabled)
  88. baseActiveFields.Add("features.graphPixel");
  89. baseActiveFields.Add("SurfaceType.Transparent");
  90. baseActiveFields.Add("BlendMode.Alpha");
  91. return activeFields;
  92. }
  93. private static bool GenerateShaderPass(SpriteUnlitMasterNode masterNode, ShaderPass pass, GenerationMode mode, ShaderGenerator result, List<string> sourceAssetDependencyPaths)
  94. {
  95. UniversalShaderGraphUtilities.SetRenderState(SurfaceType.Transparent, AlphaMode.Alpha, true, ref pass);
  96. // apply master node options to active fields
  97. var activeFields = GetActiveFieldsFromMasterNode(masterNode, pass);
  98. // use standard shader pass generation
  99. return ShaderGraph.GenerationUtils.GenerateShaderPass(masterNode, pass, mode, activeFields, result, sourceAssetDependencyPaths,
  100. UniversalShaderGraphResources.s_Dependencies, UniversalShaderGraphResources.s_ResourceClassName, UniversalShaderGraphResources.s_AssemblyName);
  101. }
  102. public string GetSubshader(IMasterNode masterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
  103. {
  104. if (sourceAssetDependencyPaths != null)
  105. {
  106. // LightWeightSpriteUnlitSubShader.cs
  107. sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("3ef30c5c1d5fc412f88511ef5818b654"));
  108. }
  109. // Master Node data
  110. var unlitMasterNode = masterNode as SpriteUnlitMasterNode;
  111. var subShader = new ShaderGenerator();
  112. subShader.AddShaderChunk("SubShader", true);
  113. subShader.AddShaderChunk("{", true);
  114. subShader.Indent();
  115. {
  116. var surfaceTags = ShaderGenerator.BuildMaterialTags(SurfaceType.Transparent);
  117. var tagsBuilder = new ShaderStringBuilder(0);
  118. surfaceTags.GetTags(tagsBuilder, "UniversalPipeline");
  119. subShader.AddShaderChunk(tagsBuilder.ToString());
  120. GenerateShaderPass(unlitMasterNode, m_UnlitPass, mode, subShader, sourceAssetDependencyPaths);
  121. }
  122. subShader.Deindent();
  123. subShader.AddShaderChunk("}", true);
  124. return subShader.GetShaderString(0);
  125. }
  126. public bool IsPipelineCompatible(RenderPipelineAsset renderPipelineAsset)
  127. {
  128. return renderPipelineAsset is UniversalRenderPipelineAsset;
  129. }
  130. public UniversalSpriteUnlitSubShader() { }
  131. }
  132. }