暫無描述
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.

UniversalUnlitSubTarget.cs 19KB

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