UniversalUnlitSubTarget.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.ShaderGraph;
  5. using UnityEditor.ShaderGraph.Legacy;
  6. using static UnityEditor.Rendering.Universal.ShaderGraph.SubShaderUtils;
  7. using static Unity.Rendering.Universal.ShaderUtils;
  8. namespace UnityEditor.Rendering.Universal.ShaderGraph
  9. {
  10. sealed class UniversalUnlitSubTarget : UniversalSubTarget, ILegacyTarget
  11. {
  12. static readonly GUID kSourceCodeGuid = new GUID("97c3f7dcb477ec842aa878573640313a"); // UniversalUnlitSubTarget.cs
  13. public UniversalUnlitSubTarget()
  14. {
  15. displayName = "Unlit";
  16. }
  17. protected override ShaderID shaderID => ShaderID.SG_Unlit;
  18. public override bool IsActive() => true;
  19. public override void Setup(ref TargetSetupContext context)
  20. {
  21. context.AddAssetDependency(kSourceCodeGuid, AssetCollection.Flags.SourceDependency);
  22. base.Setup(ref context);
  23. var universalRPType = typeof(UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset);
  24. if (!context.HasCustomEditorForRenderPipeline(universalRPType))
  25. {
  26. var gui = typeof(ShaderGraphUnlitGUI);
  27. #if HAS_VFX_GRAPH
  28. if (TargetsVFX())
  29. gui = typeof(VFXShaderGraphUnlitGUI);
  30. #endif
  31. context.AddCustomEditorForRenderPipeline(gui.FullName, universalRPType);
  32. }
  33. // Process SubShaders
  34. context.AddSubShader(PostProcessSubShader(SubShaders.UnlitDOTS(target, target.renderType, target.renderQueue)));
  35. context.AddSubShader(PostProcessSubShader(SubShaders.Unlit(target, target.renderType, target.renderQueue)));
  36. }
  37. public override void ProcessPreviewMaterial(Material material)
  38. {
  39. if (target.allowMaterialOverride)
  40. {
  41. // copy our target's default settings into the material
  42. // (technically not necessary since we are always recreating the material from the shader each time,
  43. // which will pull over the defaults from the shader definition)
  44. // but if that ever changes, this will ensure the defaults are set
  45. material.SetFloat(Property.SurfaceType, (float)target.surfaceType);
  46. material.SetFloat(Property.BlendMode, (float)target.alphaMode);
  47. material.SetFloat(Property.AlphaClip, target.alphaClip ? 1.0f : 0.0f);
  48. material.SetFloat(Property.CullMode, (int)target.renderFace);
  49. material.SetFloat(Property.CastShadows, target.castShadows ? 1.0f : 0.0f);
  50. material.SetFloat(Property.ZWriteControl, (float)target.zWriteControl);
  51. material.SetFloat(Property.ZTest, (float)target.zTestMode);
  52. }
  53. // We always need these properties regardless of whether the material is allowed to override
  54. // Queue control & offset enable correct automatic render queue behavior
  55. // Control == 0 is automatic, 1 is user-specified render queue
  56. material.SetFloat(Property.QueueOffset, 0.0f);
  57. material.SetFloat(Property.QueueControl, (float)BaseShaderGUI.QueueControl.Auto);
  58. // call the full unlit material setup function
  59. ShaderGraphUnlitGUI.UpdateMaterial(material, MaterialUpdateType.CreatedNewMaterial);
  60. }
  61. public override void GetFields(ref TargetFieldContext context)
  62. {
  63. base.GetFields(ref context);
  64. }
  65. public override void GetActiveBlocks(ref TargetActiveBlockContext context)
  66. {
  67. context.AddBlock(BlockFields.SurfaceDescription.Alpha, (target.surfaceType == SurfaceType.Transparent || target.alphaClip) || target.allowMaterialOverride);
  68. context.AddBlock(BlockFields.SurfaceDescription.AlphaClipThreshold, target.alphaClip || target.allowMaterialOverride);
  69. }
  70. public override void CollectShaderProperties(PropertyCollector collector, GenerationMode generationMode)
  71. {
  72. if (target.allowMaterialOverride)
  73. {
  74. collector.AddFloatProperty(Property.CastShadows, target.castShadows ? 1.0f : 0.0f);
  75. collector.AddFloatProperty(Property.SurfaceType, (float)target.surfaceType);
  76. collector.AddFloatProperty(Property.BlendMode, (float)target.alphaMode);
  77. collector.AddFloatProperty(Property.AlphaClip, target.alphaClip ? 1.0f : 0.0f);
  78. collector.AddFloatProperty(Property.SrcBlend, 1.0f); // always set by material inspector
  79. collector.AddFloatProperty(Property.DstBlend, 0.0f); // always set by material inspector
  80. collector.AddToggleProperty(Property.ZWrite, (target.surfaceType == SurfaceType.Opaque));
  81. collector.AddFloatProperty(Property.ZWriteControl, (float)target.zWriteControl);
  82. collector.AddFloatProperty(Property.ZTest, (float)target.zTestMode); // ztest mode is designed to directly pass as ztest
  83. collector.AddFloatProperty(Property.CullMode, (float)target.renderFace); // render face enum is designed to directly pass as a cull mode
  84. }
  85. // We always need these properties regardless of whether the material is allowed to override other shader properties.
  86. // Queue control & offset enable correct automatic render queue behavior. Control == 0 is automatic, 1 is user-specified.
  87. // We initialize queue control to -1 to indicate to UpdateMaterial that it needs to initialize it properly on the material.
  88. collector.AddFloatProperty(Property.QueueOffset, 0.0f);
  89. collector.AddFloatProperty(Property.QueueControl, -1.0f);
  90. }
  91. public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)
  92. {
  93. var universalTarget = (target as UniversalTarget);
  94. universalTarget.AddDefaultMaterialOverrideGUI(ref context, onChange, registerUndo);
  95. universalTarget.AddDefaultSurfacePropertiesGUI(ref context, onChange, registerUndo, showReceiveShadows: false);
  96. }
  97. public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary<BlockFieldDescriptor, int> blockMap)
  98. {
  99. blockMap = null;
  100. if (!(masterNode is UnlitMasterNode1 unlitMasterNode))
  101. return false;
  102. // Set blockmap
  103. blockMap = new Dictionary<BlockFieldDescriptor, int>()
  104. {
  105. { BlockFields.VertexDescription.Position, 9 },
  106. { BlockFields.VertexDescription.Normal, 10 },
  107. { BlockFields.VertexDescription.Tangent, 11 },
  108. { BlockFields.SurfaceDescription.BaseColor, 0 },
  109. { BlockFields.SurfaceDescription.Alpha, 7 },
  110. { BlockFields.SurfaceDescription.AlphaClipThreshold, 8 },
  111. };
  112. return true;
  113. }
  114. #region SubShader
  115. static class SubShaders
  116. {
  117. public static SubShaderDescriptor Unlit(UniversalTarget target, string renderType, string renderQueue)
  118. {
  119. var result = new SubShaderDescriptor()
  120. {
  121. pipelineTag = UniversalTarget.kPipelineTag,
  122. customTags = UniversalTarget.kUnlitMaterialTypeTag,
  123. renderType = renderType,
  124. renderQueue = renderQueue,
  125. generatesPreview = true,
  126. passes = new PassCollection()
  127. };
  128. result.passes.Add(UnlitPasses.Forward(target));
  129. if (target.mayWriteDepth)
  130. result.passes.Add(CorePasses.DepthOnly(target));
  131. result.passes.Add(CorePasses.DepthNormalOnly(target));
  132. if (target.castShadows || target.allowMaterialOverride)
  133. result.passes.Add(CorePasses.ShadowCaster(target));
  134. // Currently neither of these passes (selection/picking) can be last for the game view for
  135. // UI shaders to render correctly. Verify [1352225] before changing this order.
  136. result.passes.Add(CorePasses.SceneSelection(target));
  137. result.passes.Add(CorePasses.ScenePicking(target));
  138. result.passes.Add(UnlitPasses.DepthNormalOnly(target));
  139. result.passes.Add(CorePasses.MotionVectors(target));
  140. return result;
  141. }
  142. public static SubShaderDescriptor UnlitDOTS(UniversalTarget target, string renderType, string renderQueue)
  143. {
  144. var result = new SubShaderDescriptor()
  145. {
  146. pipelineTag = UniversalTarget.kPipelineTag,
  147. customTags = UniversalTarget.kUnlitMaterialTypeTag,
  148. renderType = renderType,
  149. renderQueue = renderQueue,
  150. generatesPreview = true,
  151. passes = new PassCollection()
  152. };
  153. result.passes.Add(PassVariant(UnlitPasses.Forward(target), CorePragmas.DOTSForward));
  154. if (target.mayWriteDepth)
  155. result.passes.Add(PassVariant(CorePasses.DepthOnly(target), CorePragmas.DOTSInstanced));
  156. result.passes.Add(PassVariant(CorePasses.DepthNormalOnly(target), CorePragmas.DOTSInstanced));
  157. if (target.castShadows || target.allowMaterialOverride)
  158. result.passes.Add(PassVariant(CorePasses.ShadowCaster(target), CorePragmas.DOTSInstanced));
  159. // Currently neither of these passes (selection/picking) can be last for the game view for
  160. // UI shaders to render correctly. Verify [1352225] before changing this order.
  161. result.passes.Add(PassVariant(CorePasses.SceneSelection(target), CorePragmas.DOTSDefault));
  162. result.passes.Add(PassVariant(CorePasses.ScenePicking(target), CorePragmas.DOTSDefault));
  163. result.passes.Add(PassVariant(UnlitPasses.DepthNormalOnly(target), CorePragmas.DOTSInstanced));
  164. return result;
  165. }
  166. }
  167. #endregion
  168. #region Pass
  169. static class UnlitPasses
  170. {
  171. public static PassDescriptor Forward(UniversalTarget target)
  172. {
  173. var result = new PassDescriptor
  174. {
  175. // Definition
  176. displayName = "Universal Forward",
  177. referenceName = "SHADERPASS_UNLIT",
  178. useInPreview = true,
  179. // Template
  180. passTemplatePath = UniversalTarget.kUberTemplatePath,
  181. sharedTemplateDirectories = UniversalTarget.kSharedTemplateDirectories,
  182. // Port Mask
  183. validVertexBlocks = CoreBlockMasks.Vertex,
  184. validPixelBlocks = CoreBlockMasks.FragmentColorAlpha,
  185. // Fields
  186. structs = CoreStructCollections.Default,
  187. requiredFields = UnlitRequiredFields.Unlit,
  188. fieldDependencies = CoreFieldDependencies.Default,
  189. // Conditional State
  190. renderStates = CoreRenderStates.UberSwitchedRenderState(target),
  191. pragmas = CorePragmas.Forward,
  192. defines = new DefineCollection() { CoreDefines.UseFragmentFog },
  193. keywords = new KeywordCollection() { UnlitKeywords.UnlitBaseKeywords },
  194. includes = UnlitIncludes.Unlit,
  195. // Custom Interpolator Support
  196. customInterpolators = CoreCustomInterpDescriptors.Common
  197. };
  198. CorePasses.AddTargetSurfaceControlsToPass(ref result, target);
  199. return result;
  200. }
  201. public static PassDescriptor DepthNormalOnly(UniversalTarget target)
  202. {
  203. var result = new PassDescriptor
  204. {
  205. // Definition
  206. displayName = "DepthNormals",
  207. referenceName = "SHADERPASS_DEPTHNORMALSONLY",
  208. lightMode = "DepthNormalsOnly",
  209. useInPreview = false,
  210. // Template
  211. passTemplatePath = UniversalTarget.kUberTemplatePath,
  212. sharedTemplateDirectories = UniversalTarget.kSharedTemplateDirectories,
  213. // Port Mask
  214. validVertexBlocks = CoreBlockMasks.Vertex,
  215. validPixelBlocks = UnlitBlockMasks.FragmentDepthNormals,
  216. // Fields
  217. structs = CoreStructCollections.Default,
  218. requiredFields = UnlitRequiredFields.DepthNormalsOnly,
  219. fieldDependencies = CoreFieldDependencies.Default,
  220. // Conditional State
  221. renderStates = CoreRenderStates.DepthNormalsOnly(target),
  222. pragmas = CorePragmas.Forward,
  223. defines = new DefineCollection(),
  224. keywords = new KeywordCollection(),
  225. includes = CoreIncludes.DepthNormalsOnly,
  226. // Custom Interpolator Support
  227. customInterpolators = CoreCustomInterpDescriptors.Common
  228. };
  229. CorePasses.AddTargetSurfaceControlsToPass(ref result, target);
  230. return result;
  231. }
  232. #region PortMasks
  233. static class UnlitBlockMasks
  234. {
  235. public static readonly BlockFieldDescriptor[] FragmentDepthNormals = new BlockFieldDescriptor[]
  236. {
  237. BlockFields.SurfaceDescription.NormalWS,
  238. BlockFields.SurfaceDescription.Alpha,
  239. BlockFields.SurfaceDescription.AlphaClipThreshold,
  240. };
  241. }
  242. #endregion
  243. #region RequiredFields
  244. static class UnlitRequiredFields
  245. {
  246. public static readonly FieldCollection Unlit = new FieldCollection()
  247. {
  248. StructFields.Varyings.positionWS,
  249. StructFields.Varyings.normalWS,
  250. StructFields.Varyings.viewDirectionWS,
  251. };
  252. public static readonly FieldCollection DepthNormalsOnly = new FieldCollection()
  253. {
  254. StructFields.Varyings.normalWS,
  255. };
  256. }
  257. #endregion
  258. }
  259. #endregion
  260. #region Keywords
  261. static class UnlitKeywords
  262. {
  263. public static readonly KeywordCollection UnlitBaseKeywords = new KeywordCollection()
  264. {
  265. // This contain lightmaps because without a proper custom lighting solution in Shadergraph,
  266. // people start with the unlit then add lightmapping nodes to it.
  267. // If we removed lightmaps from the unlit target this would ruin a lot of peoples days.
  268. CoreKeywordDescriptors.StaticLightmap,
  269. CoreKeywordDescriptors.DirectionalLightmapCombined,
  270. CoreKeywordDescriptors.SampleGI,
  271. CoreKeywordDescriptors.DBuffer,
  272. CoreKeywordDescriptors.DebugDisplay,
  273. };
  274. }
  275. #endregion
  276. #region Includes
  277. static class UnlitIncludes
  278. {
  279. const string kUnlitPass = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/UnlitPass.hlsl";
  280. public static IncludeCollection Unlit = new IncludeCollection
  281. {
  282. // Pre-graph
  283. { CoreIncludes.CorePregraph },
  284. { CoreIncludes.ShaderGraphPregraph },
  285. { CoreIncludes.DBufferPregraph },
  286. // Post-graph
  287. { CoreIncludes.CorePostgraph },
  288. { kUnlitPass, IncludeLocation.Postgraph },
  289. };
  290. }
  291. #endregion
  292. }
  293. }