GenerationUtils.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. using UnityEditor;
  8. using UnityEditor.Graphing;
  9. using UnityEditor.ShaderGraph;
  10. using UnityEditor.ShaderGraph.Internal;
  11. using Data.Util;
  12. namespace UnityEditor.ShaderGraph
  13. {
  14. static class GenerationUtils
  15. {
  16. const string kDebugSymbol = "SHADERGRAPH_DEBUG";
  17. public static bool GenerateShaderPass(AbstractMaterialNode masterNode, ShaderPass pass, GenerationMode mode,
  18. ActiveFields activeFields, ShaderGenerator result, List<string> sourceAssetDependencyPaths,
  19. List<Dependency[]> dependencies, string resourceClassName, string assemblyName)
  20. {
  21. // --------------------------------------------------
  22. // Debug
  23. // Get scripting symbols
  24. BuildTargetGroup buildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
  25. string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
  26. bool isDebug = defines.Contains(kDebugSymbol);
  27. // --------------------------------------------------
  28. // Setup
  29. // Initiailize Collectors
  30. var propertyCollector = new PropertyCollector();
  31. var keywordCollector = new KeywordCollector();
  32. masterNode.owner.CollectShaderKeywords(keywordCollector, mode);
  33. // Get upstream nodes from ShaderPass port mask
  34. List<AbstractMaterialNode> vertexNodes;
  35. List<AbstractMaterialNode> pixelNodes;
  36. GetUpstreamNodesForShaderPass(masterNode, pass, out vertexNodes, out pixelNodes);
  37. // Track permutation indices for all nodes
  38. List<int>[] vertexNodePermutations = new List<int>[vertexNodes.Count];
  39. List<int>[] pixelNodePermutations = new List<int>[pixelNodes.Count];
  40. // Get active fields from upstream Node requirements
  41. ShaderGraphRequirementsPerKeyword graphRequirements;
  42. GetActiveFieldsAndPermutationsForNodes(masterNode, pass, keywordCollector, vertexNodes, pixelNodes,
  43. vertexNodePermutations, pixelNodePermutations, activeFields, out graphRequirements);
  44. // GET CUSTOM ACTIVE FIELDS HERE!
  45. // Get active fields from ShaderPass
  46. AddRequiredFields(pass.requiredAttributes, activeFields.baseInstance);
  47. AddRequiredFields(pass.requiredVaryings, activeFields.baseInstance);
  48. // Get Port references from ShaderPass
  49. var pixelSlots = FindMaterialSlotsOnNode(pass.pixelPorts, masterNode);
  50. var vertexSlots = FindMaterialSlotsOnNode(pass.vertexPorts, masterNode);
  51. // Function Registry
  52. var functionBuilder = new ShaderStringBuilder();
  53. var functionRegistry = new FunctionRegistry(functionBuilder);
  54. // Hash table of named $splice(name) commands
  55. // Key: splice token
  56. // Value: string to splice
  57. Dictionary<string, string> spliceCommands = new Dictionary<string, string>();
  58. // --------------------------------------------------
  59. // Dependencies
  60. // Propagate active field requirements using dependencies
  61. // Must be executed before types are built
  62. foreach (var instance in activeFields.all.instances)
  63. ShaderSpliceUtil.ApplyDependencies(instance, dependencies);
  64. // --------------------------------------------------
  65. // Pass Setup
  66. // Name
  67. if(!string.IsNullOrEmpty(pass.displayName))
  68. {
  69. spliceCommands.Add("PassName", $"Name \"{pass.displayName}\"");
  70. }
  71. else
  72. {
  73. spliceCommands.Add("PassName", "// Name: <None>");
  74. }
  75. // Tags
  76. if(!string.IsNullOrEmpty(pass.lightMode))
  77. {
  78. spliceCommands.Add("LightMode", $"\"LightMode\" = \"{pass.lightMode}\"");
  79. }
  80. else
  81. {
  82. spliceCommands.Add("LightMode", "// LightMode: <None>");
  83. }
  84. // Render state
  85. BuildRenderStatesFromPass(pass, ref spliceCommands);
  86. // --------------------------------------------------
  87. // Pass Code
  88. // Pragmas
  89. using (var passPragmaBuilder = new ShaderStringBuilder())
  90. {
  91. if(pass.pragmas != null)
  92. {
  93. foreach(string pragma in pass.pragmas)
  94. {
  95. passPragmaBuilder.AppendLine($"#pragma {pragma}");
  96. }
  97. }
  98. if(passPragmaBuilder.length == 0)
  99. passPragmaBuilder.AppendLine("// PassPragmas: <None>");
  100. spliceCommands.Add("PassPragmas", passPragmaBuilder.ToCodeBlack());
  101. }
  102. // Includes
  103. using (var passIncludeBuilder = new ShaderStringBuilder())
  104. {
  105. if(pass.includes != null)
  106. {
  107. foreach(string include in pass.includes)
  108. {
  109. passIncludeBuilder.AppendLine($"#include \"{include}\"");
  110. }
  111. }
  112. if(passIncludeBuilder.length == 0)
  113. passIncludeBuilder.AppendLine("// PassIncludes: <None>");
  114. spliceCommands.Add("PassIncludes", passIncludeBuilder.ToCodeBlack());
  115. }
  116. // Keywords
  117. using (var passKeywordBuilder = new ShaderStringBuilder())
  118. {
  119. if(pass.keywords != null)
  120. {
  121. foreach(KeywordDescriptor keyword in pass.keywords)
  122. {
  123. passKeywordBuilder.AppendLine(keyword.ToDeclarationString());
  124. }
  125. }
  126. if(passKeywordBuilder.length == 0)
  127. passKeywordBuilder.AppendLine("// PassKeywords: <None>");
  128. spliceCommands.Add("PassKeywords", passKeywordBuilder.ToCodeBlack());
  129. }
  130. // --------------------------------------------------
  131. // Graph Vertex
  132. var vertexBuilder = new ShaderStringBuilder();
  133. // If vertex modification enabled
  134. if (activeFields.baseInstance.Contains("features.graphVertex"))
  135. {
  136. // Setup
  137. string vertexGraphInputName = "VertexDescriptionInputs";
  138. string vertexGraphOutputName = "VertexDescription";
  139. string vertexGraphFunctionName = "VertexDescriptionFunction";
  140. var vertexGraphInputGenerator = new ShaderGenerator();
  141. var vertexGraphFunctionBuilder = new ShaderStringBuilder();
  142. var vertexGraphOutputBuilder = new ShaderStringBuilder();
  143. // Build vertex graph inputs
  144. ShaderSpliceUtil.BuildType(GetTypeForStruct("VertexDescriptionInputs", resourceClassName, assemblyName), activeFields, vertexGraphInputGenerator, isDebug);
  145. // Build vertex graph outputs
  146. // Add struct fields to active fields
  147. SubShaderGenerator.GenerateVertexDescriptionStruct(vertexGraphOutputBuilder, vertexSlots, vertexGraphOutputName, activeFields.baseInstance);
  148. // Build vertex graph functions from ShaderPass vertex port mask
  149. SubShaderGenerator.GenerateVertexDescriptionFunction(
  150. masterNode.owner as GraphData,
  151. vertexGraphFunctionBuilder,
  152. functionRegistry,
  153. propertyCollector,
  154. keywordCollector,
  155. mode,
  156. masterNode,
  157. vertexNodes,
  158. vertexNodePermutations,
  159. vertexSlots,
  160. vertexGraphInputName,
  161. vertexGraphFunctionName,
  162. vertexGraphOutputName);
  163. // Generate final shader strings
  164. vertexBuilder.AppendLines(vertexGraphInputGenerator.GetShaderString(0, false));
  165. vertexBuilder.AppendNewLine();
  166. vertexBuilder.AppendLines(vertexGraphOutputBuilder.ToString());
  167. vertexBuilder.AppendNewLine();
  168. vertexBuilder.AppendLines(vertexGraphFunctionBuilder.ToString());
  169. }
  170. // Add to splice commands
  171. if(vertexBuilder.length == 0)
  172. vertexBuilder.AppendLine("// GraphVertex: <None>");
  173. spliceCommands.Add("GraphVertex", vertexBuilder.ToCodeBlack());
  174. // --------------------------------------------------
  175. // Graph Pixel
  176. // Setup
  177. string pixelGraphInputName = "SurfaceDescriptionInputs";
  178. string pixelGraphOutputName = "SurfaceDescription";
  179. string pixelGraphFunctionName = "SurfaceDescriptionFunction";
  180. var pixelGraphInputGenerator = new ShaderGenerator();
  181. var pixelGraphOutputBuilder = new ShaderStringBuilder();
  182. var pixelGraphFunctionBuilder = new ShaderStringBuilder();
  183. // Build pixel graph inputs
  184. ShaderSpliceUtil.BuildType(GetTypeForStruct("SurfaceDescriptionInputs", resourceClassName, assemblyName), activeFields, pixelGraphInputGenerator, isDebug);
  185. // Build pixel graph outputs
  186. // Add struct fields to active fields
  187. SubShaderGenerator.GenerateSurfaceDescriptionStruct(pixelGraphOutputBuilder, pixelSlots, pixelGraphOutputName, activeFields.baseInstance);
  188. // Build pixel graph functions from ShaderPass pixel port mask
  189. SubShaderGenerator.GenerateSurfaceDescriptionFunction(
  190. pixelNodes,
  191. pixelNodePermutations,
  192. masterNode,
  193. masterNode.owner as GraphData,
  194. pixelGraphFunctionBuilder,
  195. functionRegistry,
  196. propertyCollector,
  197. keywordCollector,
  198. mode,
  199. pixelGraphFunctionName,
  200. pixelGraphOutputName,
  201. null,
  202. pixelSlots,
  203. pixelGraphInputName);
  204. using (var pixelBuilder = new ShaderStringBuilder())
  205. {
  206. // Generate final shader strings
  207. pixelBuilder.AppendLines(pixelGraphInputGenerator.GetShaderString(0, false));
  208. pixelBuilder.AppendNewLine();
  209. pixelBuilder.AppendLines(pixelGraphOutputBuilder.ToString());
  210. pixelBuilder.AppendNewLine();
  211. pixelBuilder.AppendLines(pixelGraphFunctionBuilder.ToString());
  212. // Add to splice commands
  213. if(pixelBuilder.length == 0)
  214. pixelBuilder.AppendLine("// GraphPixel: <None>");
  215. spliceCommands.Add("GraphPixel", pixelBuilder.ToCodeBlack());
  216. }
  217. // --------------------------------------------------
  218. // Graph Functions
  219. if(functionBuilder.length == 0)
  220. functionBuilder.AppendLine("// GraphFunctions: <None>");
  221. spliceCommands.Add("GraphFunctions", functionBuilder.ToCodeBlack());
  222. // --------------------------------------------------
  223. // Graph Keywords
  224. using (var keywordBuilder = new ShaderStringBuilder())
  225. {
  226. keywordCollector.GetKeywordsDeclaration(keywordBuilder, mode);
  227. if(keywordBuilder.length == 0)
  228. keywordBuilder.AppendLine("// GraphKeywords: <None>");
  229. spliceCommands.Add("GraphKeywords", keywordBuilder.ToCodeBlack());
  230. }
  231. // --------------------------------------------------
  232. // Graph Properties
  233. using (var propertyBuilder = new ShaderStringBuilder())
  234. {
  235. propertyCollector.GetPropertiesDeclaration(propertyBuilder, mode, masterNode.owner.concretePrecision);
  236. if(propertyBuilder.length == 0)
  237. propertyBuilder.AppendLine("// GraphProperties: <None>");
  238. spliceCommands.Add("GraphProperties", propertyBuilder.ToCodeBlack());
  239. }
  240. // --------------------------------------------------
  241. // Graph Defines
  242. using (var graphDefines = new ShaderStringBuilder())
  243. {
  244. graphDefines.AppendLine("#define {0}", pass.referenceName);
  245. if (graphRequirements.permutationCount > 0)
  246. {
  247. List<int> activePermutationIndices;
  248. // Depth Texture
  249. activePermutationIndices = graphRequirements.allPermutations.instances
  250. .Where(p => p.requirements.requiresDepthTexture)
  251. .Select(p => p.permutationIndex)
  252. .ToList();
  253. if (activePermutationIndices.Count > 0)
  254. {
  255. graphDefines.AppendLine(KeywordUtil.GetKeywordPermutationSetConditional(activePermutationIndices));
  256. graphDefines.AppendLine("#define REQUIRE_DEPTH_TEXTURE");
  257. graphDefines.AppendLine("#endif");
  258. }
  259. // Opaque Texture
  260. activePermutationIndices = graphRequirements.allPermutations.instances
  261. .Where(p => p.requirements.requiresCameraOpaqueTexture)
  262. .Select(p => p.permutationIndex)
  263. .ToList();
  264. if (activePermutationIndices.Count > 0)
  265. {
  266. graphDefines.AppendLine(KeywordUtil.GetKeywordPermutationSetConditional(activePermutationIndices));
  267. graphDefines.AppendLine("#define REQUIRE_OPAQUE_TEXTURE");
  268. graphDefines.AppendLine("#endif");
  269. }
  270. }
  271. else
  272. {
  273. // Depth Texture
  274. if (graphRequirements.baseInstance.requirements.requiresDepthTexture)
  275. graphDefines.AppendLine("#define REQUIRE_DEPTH_TEXTURE");
  276. // Opaque Texture
  277. if (graphRequirements.baseInstance.requirements.requiresCameraOpaqueTexture)
  278. graphDefines.AppendLine("#define REQUIRE_OPAQUE_TEXTURE");
  279. }
  280. // Add to splice commands
  281. spliceCommands.Add("GraphDefines", graphDefines.ToCodeBlack());
  282. }
  283. // --------------------------------------------------
  284. // Main
  285. // Main include is expected to contain vert/frag definitions for the pass
  286. // This must be defined after all graph code
  287. using (var mainBuilder = new ShaderStringBuilder())
  288. {
  289. mainBuilder.AppendLine($"#include \"{pass.varyingsInclude}\"");
  290. mainBuilder.AppendLine($"#include \"{pass.passInclude}\"");
  291. // Add to splice commands
  292. spliceCommands.Add("MainInclude", mainBuilder.ToCodeBlack());
  293. }
  294. // --------------------------------------------------
  295. // Debug
  296. // Debug output all active fields
  297. using(var debugBuilder = new ShaderStringBuilder())
  298. {
  299. if (isDebug)
  300. {
  301. // Active fields
  302. debugBuilder.AppendLine("// ACTIVE FIELDS:");
  303. foreach (string field in activeFields.baseInstance.fields)
  304. {
  305. debugBuilder.AppendLine("// " + field);
  306. }
  307. }
  308. if(debugBuilder.length == 0)
  309. debugBuilder.AppendLine("// <None>");
  310. // Add to splice commands
  311. spliceCommands.Add("Debug", debugBuilder.ToCodeBlack());
  312. }
  313. // --------------------------------------------------
  314. // Finalize
  315. // Get Template
  316. string templateLocation = GetTemplatePath("PassMesh.template");
  317. if (!File.Exists(templateLocation))
  318. return false;
  319. // Get Template preprocessor
  320. string templatePath = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Templates";
  321. var templatePreprocessor = new ShaderSpliceUtil.TemplatePreprocessor(activeFields, spliceCommands,
  322. isDebug, templatePath, sourceAssetDependencyPaths, assemblyName, resourceClassName);
  323. // Process Template
  324. templatePreprocessor.ProcessTemplateFile(templateLocation);
  325. result.AddShaderChunk(templatePreprocessor.GetShaderCode().ToString(), false);
  326. return true;
  327. }
  328. public static Type GetTypeForStruct(string structName, string resourceClassName, string assemblyName)
  329. {
  330. // 'C# qualified assembly type names' for $buildType() commands
  331. string assemblyQualifiedTypeName = $"{resourceClassName}+{structName}, {assemblyName}";
  332. return Type.GetType(assemblyQualifiedTypeName);
  333. }
  334. static void GetUpstreamNodesForShaderPass(AbstractMaterialNode masterNode, ShaderPass pass, out List<AbstractMaterialNode> vertexNodes, out List<AbstractMaterialNode> pixelNodes)
  335. {
  336. // Traverse Graph Data
  337. vertexNodes = Graphing.ListPool<AbstractMaterialNode>.Get();
  338. NodeUtils.DepthFirstCollectNodesFromNode(vertexNodes, masterNode, NodeUtils.IncludeSelf.Include, pass.vertexPorts);
  339. pixelNodes = Graphing.ListPool<AbstractMaterialNode>.Get();
  340. NodeUtils.DepthFirstCollectNodesFromNode(pixelNodes, masterNode, NodeUtils.IncludeSelf.Include, pass.pixelPorts);
  341. }
  342. static void GetActiveFieldsAndPermutationsForNodes(AbstractMaterialNode masterNode, ShaderPass pass,
  343. KeywordCollector keywordCollector, List<AbstractMaterialNode> vertexNodes, List<AbstractMaterialNode> pixelNodes,
  344. List<int>[] vertexNodePermutations, List<int>[] pixelNodePermutations,
  345. ActiveFields activeFields, out ShaderGraphRequirementsPerKeyword graphRequirements)
  346. {
  347. // Initialize requirements
  348. ShaderGraphRequirementsPerKeyword pixelRequirements = new ShaderGraphRequirementsPerKeyword();
  349. ShaderGraphRequirementsPerKeyword vertexRequirements = new ShaderGraphRequirementsPerKeyword();
  350. graphRequirements = new ShaderGraphRequirementsPerKeyword();
  351. // Evaluate all Keyword permutations
  352. if (keywordCollector.permutations.Count > 0)
  353. {
  354. for(int i = 0; i < keywordCollector.permutations.Count; i++)
  355. {
  356. // Get active nodes for this permutation
  357. var localVertexNodes = Graphing.ListPool<AbstractMaterialNode>.Get();
  358. var localPixelNodes = Graphing.ListPool<AbstractMaterialNode>.Get();
  359. NodeUtils.DepthFirstCollectNodesFromNode(localVertexNodes, masterNode, NodeUtils.IncludeSelf.Include, pass.vertexPorts, keywordCollector.permutations[i]);
  360. NodeUtils.DepthFirstCollectNodesFromNode(localPixelNodes, masterNode, NodeUtils.IncludeSelf.Include, pass.pixelPorts, keywordCollector.permutations[i]);
  361. // Track each vertex node in this permutation
  362. foreach(AbstractMaterialNode vertexNode in localVertexNodes)
  363. {
  364. int nodeIndex = vertexNodes.IndexOf(vertexNode);
  365. if(vertexNodePermutations[nodeIndex] == null)
  366. vertexNodePermutations[nodeIndex] = new List<int>();
  367. vertexNodePermutations[nodeIndex].Add(i);
  368. }
  369. // Track each pixel node in this permutation
  370. foreach(AbstractMaterialNode pixelNode in localPixelNodes)
  371. {
  372. int nodeIndex = pixelNodes.IndexOf(pixelNode);
  373. if(pixelNodePermutations[nodeIndex] == null)
  374. pixelNodePermutations[nodeIndex] = new List<int>();
  375. pixelNodePermutations[nodeIndex].Add(i);
  376. }
  377. // Get requirements for this permutation
  378. vertexRequirements[i].SetRequirements(ShaderGraphRequirements.FromNodes(localVertexNodes, ShaderStageCapability.Vertex, false));
  379. pixelRequirements[i].SetRequirements(ShaderGraphRequirements.FromNodes(localPixelNodes, ShaderStageCapability.Fragment, false));
  380. // Add active fields
  381. AddActiveFieldsFromGraphRequirements(activeFields[i], vertexRequirements[i].requirements, "VertexDescriptionInputs");
  382. AddActiveFieldsFromGraphRequirements(activeFields[i], pixelRequirements[i].requirements, "SurfaceDescriptionInputs");
  383. }
  384. }
  385. // No Keywords
  386. else
  387. {
  388. // Get requirements
  389. vertexRequirements.baseInstance.SetRequirements(ShaderGraphRequirements.FromNodes(vertexNodes, ShaderStageCapability.Vertex, false));
  390. pixelRequirements.baseInstance.SetRequirements(ShaderGraphRequirements.FromNodes(pixelNodes, ShaderStageCapability.Fragment, false));
  391. // Add active fields
  392. AddActiveFieldsFromGraphRequirements(activeFields.baseInstance, vertexRequirements.baseInstance.requirements, "VertexDescriptionInputs");
  393. AddActiveFieldsFromGraphRequirements(activeFields.baseInstance, pixelRequirements.baseInstance.requirements, "SurfaceDescriptionInputs");
  394. }
  395. // Build graph requirements
  396. graphRequirements.UnionWith(pixelRequirements);
  397. graphRequirements.UnionWith(vertexRequirements);
  398. }
  399. static void AddActiveFieldsFromGraphRequirements(IActiveFields activeFields, ShaderGraphRequirements requirements, string structName)
  400. {
  401. if (requirements.requiresScreenPosition)
  402. {
  403. activeFields.Add($"{structName}.ScreenPosition");
  404. }
  405. if (requirements.requiresVertexColor)
  406. {
  407. activeFields.Add($"{structName}.VertexColor");
  408. }
  409. if (requirements.requiresFaceSign)
  410. {
  411. activeFields.Add($"{structName}.FaceSign");
  412. }
  413. if (requirements.requiresNormal != 0)
  414. {
  415. if ((requirements.requiresNormal & NeededCoordinateSpace.Object) > 0)
  416. activeFields.Add($"{structName}.ObjectSpaceNormal");
  417. if ((requirements.requiresNormal & NeededCoordinateSpace.View) > 0)
  418. activeFields.Add($"{structName}.ViewSpaceNormal");
  419. if ((requirements.requiresNormal & NeededCoordinateSpace.World) > 0)
  420. activeFields.Add($"{structName}.WorldSpaceNormal");
  421. if ((requirements.requiresNormal & NeededCoordinateSpace.Tangent) > 0)
  422. activeFields.Add($"{structName}.TangentSpaceNormal");
  423. }
  424. if (requirements.requiresTangent != 0)
  425. {
  426. if ((requirements.requiresTangent & NeededCoordinateSpace.Object) > 0)
  427. activeFields.Add($"{structName}.ObjectSpaceTangent");
  428. if ((requirements.requiresTangent & NeededCoordinateSpace.View) > 0)
  429. activeFields.Add($"{structName}.ViewSpaceTangent");
  430. if ((requirements.requiresTangent & NeededCoordinateSpace.World) > 0)
  431. activeFields.Add($"{structName}.WorldSpaceTangent");
  432. if ((requirements.requiresTangent & NeededCoordinateSpace.Tangent) > 0)
  433. activeFields.Add($"{structName}.TangentSpaceTangent");
  434. }
  435. if (requirements.requiresBitangent != 0)
  436. {
  437. if ((requirements.requiresBitangent & NeededCoordinateSpace.Object) > 0)
  438. activeFields.Add($"{structName}.ObjectSpaceBiTangent");
  439. if ((requirements.requiresBitangent & NeededCoordinateSpace.View) > 0)
  440. activeFields.Add($"{structName}.ViewSpaceBiTangent");
  441. if ((requirements.requiresBitangent & NeededCoordinateSpace.World) > 0)
  442. activeFields.Add($"{structName}.WorldSpaceBiTangent");
  443. if ((requirements.requiresBitangent & NeededCoordinateSpace.Tangent) > 0)
  444. activeFields.Add($"{structName}.TangentSpaceBiTangent");
  445. }
  446. if (requirements.requiresViewDir != 0)
  447. {
  448. if ((requirements.requiresViewDir & NeededCoordinateSpace.Object) > 0)
  449. activeFields.Add($"{structName}.ObjectSpaceViewDirection");
  450. if ((requirements.requiresViewDir & NeededCoordinateSpace.View) > 0)
  451. activeFields.Add($"{structName}.ViewSpaceViewDirection");
  452. if ((requirements.requiresViewDir & NeededCoordinateSpace.World) > 0)
  453. activeFields.Add($"{structName}.WorldSpaceViewDirection");
  454. if ((requirements.requiresViewDir & NeededCoordinateSpace.Tangent) > 0)
  455. activeFields.Add($"{structName}.TangentSpaceViewDirection");
  456. }
  457. if (requirements.requiresPosition != 0)
  458. {
  459. if ((requirements.requiresPosition & NeededCoordinateSpace.Object) > 0)
  460. activeFields.Add($"{structName}.ObjectSpacePosition");
  461. if ((requirements.requiresPosition & NeededCoordinateSpace.View) > 0)
  462. activeFields.Add($"{structName}.ViewSpacePosition");
  463. if ((requirements.requiresPosition & NeededCoordinateSpace.World) > 0)
  464. activeFields.Add($"{structName}.WorldSpacePosition");
  465. if ((requirements.requiresPosition & NeededCoordinateSpace.Tangent) > 0)
  466. activeFields.Add($"{structName}.TangentSpacePosition");
  467. if ((requirements.requiresPosition & NeededCoordinateSpace.AbsoluteWorld) > 0)
  468. activeFields.Add($"{structName}.AbsoluteWorldSpacePosition");
  469. }
  470. foreach (var channel in requirements.requiresMeshUVs.Distinct())
  471. {
  472. activeFields.Add($"{structName}.{channel.GetUVName()}");
  473. }
  474. if (requirements.requiresTime)
  475. {
  476. activeFields.Add($"{structName}.TimeParameters");
  477. }
  478. if (requirements.requiresVertexSkinning)
  479. {
  480. activeFields.Add($"{structName}.BoneWeights");
  481. activeFields.Add($"{structName}.BoneIndices");
  482. }
  483. }
  484. static void AddRequiredFields(
  485. List<string> passRequiredFields, // fields the pass requires
  486. IActiveFieldsSet activeFields)
  487. {
  488. if (passRequiredFields != null)
  489. {
  490. foreach (var requiredField in passRequiredFields)
  491. {
  492. activeFields.AddAll(requiredField);
  493. }
  494. }
  495. }
  496. static List<MaterialSlot> FindMaterialSlotsOnNode(IEnumerable<int> slots, AbstractMaterialNode node)
  497. {
  498. var activeSlots = new List<MaterialSlot>();
  499. if (slots != null)
  500. {
  501. foreach (var id in slots)
  502. {
  503. MaterialSlot slot = node.FindSlot<MaterialSlot>(id);
  504. if (slot != null)
  505. {
  506. activeSlots.Add(slot);
  507. }
  508. }
  509. }
  510. return activeSlots;
  511. }
  512. static void BuildRenderStatesFromPass(ShaderPass pass, ref Dictionary<string, string> spliceCommands)
  513. {
  514. spliceCommands.Add("Blending", pass.BlendOverride != null ? pass.BlendOverride : "// Blending: <None>");
  515. spliceCommands.Add("Culling", pass.CullOverride != null ? pass.CullOverride : "// Culling: <None>");
  516. spliceCommands.Add("ZTest", pass.ZTestOverride != null ? pass.ZTestOverride : "// ZTest: <None>");
  517. spliceCommands.Add("ZWrite", pass.ZWriteOverride != null ? pass.ZWriteOverride : "// ZWrite: <None>");
  518. spliceCommands.Add("ZClip", pass.ZClipOverride != null ? pass.ZClipOverride : "// ZClip: <None>");
  519. spliceCommands.Add("ColorMask", pass.ColorMaskOverride != null ? pass.ColorMaskOverride : "// ColorMask: <None>");
  520. using(var stencilBuilder = new ShaderStringBuilder())
  521. {
  522. if (pass.StencilOverride != null)
  523. {
  524. foreach (var str in pass.StencilOverride)
  525. stencilBuilder.AppendLine(str);
  526. }
  527. spliceCommands.Add("Stencil", stencilBuilder.ToCodeBlack());
  528. }
  529. }
  530. static string GetTemplatePath(string templateName)
  531. {
  532. var basePath = "Packages/com.unity.shadergraph/Editor/Templates/";
  533. string templatePath = Path.Combine(basePath, templateName);
  534. if (File.Exists(templatePath))
  535. return templatePath;
  536. throw new FileNotFoundException(string.Format(@"Cannot find a template with name ""{0}"".", templateName));
  537. }
  538. }
  539. }