SCWindow.Project.Shaders.Graph.v1.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace ShaderControl {
  11. public partial class SCWindow : EditorWindow {
  12. const string JSON_NODE_DATA = "JSONnodeData";
  13. const string JSON_KEYWORD_SCOPE = "m_KeywordScope";
  14. [Serializable]
  15. public struct SerializedKeywordData {
  16. public string typeInfo;
  17. public string JSONnodeData;
  18. }
  19. [Serializable]
  20. public struct SerializedKeywordProxy {
  21. public string m_Name;
  22. public string m_DefaultReferenceName;
  23. public int m_KeywordScope;
  24. public int m_KeywordDefinition;
  25. }
  26. [Serializable]
  27. public struct ShaderGraphProxy {
  28. public SerializedKeywordData[] m_SerializedKeywords;
  29. }
  30. void ScanShaderGraphV1(SCShader shader, string shaderContents) {
  31. shaderContents = shaderContents.Replace("UnityEditor.ShaderGraph.ShaderKeyword", "ShaderControl.SCWindow.SerializedKeyword");
  32. ShaderGraphProxy graph = JsonUtility.FromJson<ShaderGraphProxy>(shaderContents);
  33. SCShaderPass currentPass = new SCShaderPass();
  34. if (graph.m_SerializedKeywords != null) {
  35. for (int k = 0; k < graph.m_SerializedKeywords.Length; k++) {
  36. SerializedKeywordData skw = graph.m_SerializedKeywords[k];
  37. if (string.IsNullOrEmpty(skw.JSONnodeData)) continue;
  38. SerializedKeywordProxy kw = JsonUtility.FromJson<SerializedKeywordProxy>(skw.JSONnodeData);
  39. PragmaType pragmaType = PragmaType.Unknown;
  40. if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_MULTI_COMPILE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_GLOBAL) {
  41. pragmaType = PragmaType.MultiCompileGlobal;
  42. } else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_MULTI_COMPILE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_LOCAL) {
  43. pragmaType = PragmaType.MultiCompileLocal;
  44. } else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_SHADER_FEATURE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_GLOBAL) {
  45. pragmaType = PragmaType.FeatureGlobal;
  46. } else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_SHADER_FEATURE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_LOCAL) {
  47. pragmaType = PragmaType.FeatureLocal;
  48. }
  49. SCKeywordLine keywordLine = new SCKeywordLine();
  50. keywordLine.pragmaType = pragmaType;
  51. SCKeyword keyword = new SCKeyword(kw.m_DefaultReferenceName, kw.m_Name);
  52. keywordLine.Add(keyword);
  53. currentPass.Add(keywordLine);
  54. }
  55. }
  56. shader.Add(currentPass);
  57. shader.UpdateVariantCount();
  58. }
  59. void ConvertToLocalGraphV1(SCKeyword keyword, SCShader shader) {
  60. string contents = File.ReadAllText(shader.path, Encoding.UTF8);
  61. int i = contents.IndexOf("m_SerializedKeywords");
  62. if (i < 0) return;
  63. int j = contents.IndexOf("m_SerializedNodes");
  64. if (j < 0) j = contents.Length - 1;
  65. int pos = contents.IndexOf(keyword.name, i);
  66. bool changed = false;
  67. if (pos > i && pos < j) {
  68. int dataBlockPos = contents.LastIndexOf(JSON_NODE_DATA, pos);
  69. if (dataBlockPos > 0) {
  70. int scopePos = contents.IndexOf(JSON_KEYWORD_SCOPE, dataBlockPos);
  71. if (scopePos > dataBlockPos && scopePos < j) {
  72. scopePos += JSON_KEYWORD_SCOPE.Length + 2;
  73. int valuePos = contents.IndexOf("1", scopePos);
  74. int safetyPos = contents.IndexOf("\"", scopePos);
  75. if (valuePos > scopePos && valuePos < safetyPos && safetyPos > valuePos) {
  76. contents = contents.Substring(0, valuePos) + "0" + contents.Substring(valuePos + 1);
  77. changed = true;
  78. }
  79. }
  80. }
  81. }
  82. if (changed) {
  83. MakeBackup(shader);
  84. File.WriteAllText(shader.path, contents, Encoding.UTF8);
  85. }
  86. }
  87. }
  88. }