SCWindow.Project.Shaders.Graph.v2.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /// <summary>
  2. /// Shader Control - (C) Copyright 2016-2022 Ramiro Oliva (Kronnect)
  3. /// </summary>
  4. ///
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System;
  8. using System.IO;
  9. using System.Text;
  10. using System.Collections.Generic;
  11. namespace ShaderControl {
  12. public partial class SCWindow : EditorWindow {
  13. const string JSON_NODE_DATA_V2 = "JSONnodeData";
  14. const string JSON_KEYWORD_SCOPE_V2 = "m_KeywordScope";
  15. [Serializable]
  16. public struct ShaderGraphChunkDataV2 {
  17. public string m_Type;
  18. public string m_ObjectId;
  19. public string m_Name;
  20. public string m_DefaultReferenceName;
  21. public int m_KeywordScope;
  22. public int m_KeywordDefinition;
  23. }
  24. List<ShaderGraphChunkDataV2> graphKeywords;
  25. void ScanShaderGraphV2(SCShader shader, string shaderContents) {
  26. // Only extract info from first JSON chunk
  27. ExtractJSONChunks(shaderContents);
  28. SCShaderPass currentPass = new SCShaderPass();
  29. for (int k = 0; k < graphKeywords.Count; k++) {
  30. ShaderGraphChunkDataV2 kw = graphKeywords[k];
  31. PragmaType pragmaType = PragmaType.Unknown;
  32. if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_MULTI_COMPILE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_GLOBAL) {
  33. pragmaType = PragmaType.MultiCompileGlobal;
  34. } else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_MULTI_COMPILE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_LOCAL) {
  35. pragmaType = PragmaType.MultiCompileLocal;
  36. } else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_SHADER_FEATURE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_GLOBAL) {
  37. pragmaType = PragmaType.FeatureGlobal;
  38. } else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_SHADER_FEATURE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_LOCAL) {
  39. pragmaType = PragmaType.FeatureLocal;
  40. }
  41. SCKeywordLine keywordLine = new SCKeywordLine();
  42. keywordLine.pragmaType = pragmaType;
  43. SCKeyword keyword = new SCKeyword(kw.m_DefaultReferenceName, kw.m_Name, kw.m_ObjectId);
  44. keywordLine.Add(keyword);
  45. currentPass.Add(keywordLine);
  46. }
  47. shader.Add(currentPass);
  48. shader.UpdateVariantCount();
  49. }
  50. void ConvertToLocalGraphV2(SCKeyword keyword, SCShader shader) {
  51. string contents = File.ReadAllText(shader.path, Encoding.UTF8);
  52. int pos = contents.IndexOf("\"m_ObjectId\": \"" + keyword.shaderGraphObjectId);
  53. bool changed = false;
  54. if (pos > 0) {
  55. int scopePos = contents.IndexOf(JSON_KEYWORD_SCOPE, pos);
  56. if (scopePos > pos) {
  57. scopePos += JSON_KEYWORD_SCOPE.Length + 2;
  58. int valuePos = contents.IndexOf("1", scopePos);
  59. int safetyPos = contents.IndexOf("\"", scopePos);
  60. if (valuePos > scopePos && valuePos < safetyPos && safetyPos > valuePos) {
  61. contents = contents.Substring(0, valuePos) + "0" + contents.Substring(valuePos + 1);
  62. changed = true;
  63. }
  64. }
  65. }
  66. if (changed) {
  67. MakeBackup(shader);
  68. File.WriteAllText(shader.path, contents, Encoding.UTF8);
  69. }
  70. }
  71. static readonly char[] jsonClosures = { '{', '}' };
  72. void ExtractJSONChunks(string json) {
  73. if (graphKeywords == null) {
  74. graphKeywords = new List<ShaderGraphChunkDataV2>();
  75. } else {
  76. graphKeywords.Clear();
  77. }
  78. int count = 0;
  79. int startIndex = 0, lastIndex = 0;
  80. do {
  81. int nextClosure = json.IndexOfAny(jsonClosures, lastIndex);
  82. if (nextClosure < 0) break;
  83. if (json[nextClosure] == '{') count++; else if (json[nextClosure] == '}') count--;
  84. lastIndex = nextClosure + 1;
  85. if (count == 0) {
  86. string jsonChunk = json.Substring(startIndex, lastIndex - startIndex);
  87. ShaderGraphChunkDataV2 chunk = JsonUtility.FromJson<ShaderGraphChunkDataV2>(jsonChunk);
  88. if (chunk.m_Type.Equals("UnityEditor.ShaderGraph.ShaderKeyword")) {
  89. graphKeywords.Add(chunk);
  90. }
  91. startIndex = lastIndex;
  92. }
  93. } while (true);
  94. }
  95. }
  96. }