123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Text;
- namespace ShaderControl {
- public partial class SCWindow : EditorWindow {
-
- void ScanShaderNonGraph(SCShader shader) {
-
- string[] shaderLines = File.ReadAllLines(shader.path);
- string[] separator = new string[] { " " };
- SCShaderPass currentPass = new SCShaderPass();
- SCShaderPass basePass = null;
- int pragmaControl = 0;
- int pass = -1;
- bool blockComment = false;
- SCKeywordLine keywordLine = new SCKeywordLine();
- for (int k = 0; k < shaderLines.Length; k++) {
- string line = shaderLines[k].Trim();
- if (line.Length == 0)
- continue;
- int lineCommentIndex = line.IndexOf("//");
- int blocCommentIndex = line.IndexOf("/*");
- int endCommentIndex = line.IndexOf("*/");
- if (blocCommentIndex > 0 && (lineCommentIndex > blocCommentIndex || lineCommentIndex < 0)) {
- blockComment = true;
- }
- if (endCommentIndex > blocCommentIndex && (lineCommentIndex > endCommentIndex || lineCommentIndex < 0)) {
- blockComment = false;
- }
- if (blockComment)
- continue;
- string lineUPPER = line.ToUpper();
- if (lineUPPER.Equals("PASS") || lineUPPER.StartsWith("PASS ")) {
- if (pass >= 0) {
- currentPass.pass = pass;
- if (basePass != null)
- currentPass.Add(basePass.keywordLines);
- shader.Add(currentPass);
- } else if (currentPass.keywordCount > 0) {
- basePass = currentPass;
- }
- currentPass = new SCShaderPass();
- pass++;
- continue;
- }
- int j = line.IndexOf(PRAGMA_COMMENT_MARK);
- if (j >= 0) {
- pragmaControl = 1;
- } else {
- j = line.IndexOf(PRAGMA_DISABLED_MARK);
- if (j >= 0)
- pragmaControl = 3;
- }
- if (lineCommentIndex == 0 && pragmaControl != 1 && pragmaControl != 3) {
- continue;
- }
- PragmaType pragmaType = PragmaType.Unknown;
- int offset = 0;
- j = line.IndexOf(SCKeywordLine.PRAGMA_MULTICOMPILE_GLOBAL);
- if (j >= 0) {
- pragmaType = PragmaType.MultiCompileGlobal;
- offset = SCKeywordLine.PRAGMA_MULTICOMPILE_GLOBAL.Length;
- } else {
- j = line.IndexOf(SCKeywordLine.PRAGMA_FEATURE_GLOBAL);
- if (j >= 0) {
- pragmaType = PragmaType.FeatureGlobal;
- offset = SCKeywordLine.PRAGMA_FEATURE_GLOBAL.Length;
- } else {
- j = line.IndexOf(SCKeywordLine.PRAGMA_MULTICOMPILE_LOCAL);
- if (j >= 0) {
- pragmaType = PragmaType.MultiCompileLocal;
- offset = SCKeywordLine.PRAGMA_MULTICOMPILE_LOCAL.Length;
- } else {
- j = line.IndexOf(SCKeywordLine.PRAGMA_FEATURE_LOCAL);
- if (j >= 0) {
- pragmaType = PragmaType.FeatureLocal;
- offset = SCKeywordLine.PRAGMA_FEATURE_LOCAL.Length;
- }
- }
- }
- }
- if (j >= 0) {
- if (pragmaControl != 2) {
- keywordLine = new SCKeywordLine();
- }
- keywordLine.pragmaType = pragmaType;
-
- int lastStringPos = line.IndexOf("//", j + offset);
- if (lastStringPos < 0) {
- lastStringPos = line.Length;
- }
- int length = lastStringPos - j - offset;
- string[] kk = line.Substring(j + offset, length).Split(separator, StringSplitOptions.RemoveEmptyEntries);
-
- for (int i = 0; i < kk.Length; i++) {
- kk[i] = kk[i].Trim();
- }
-
- switch (pragmaControl) {
- case 1:
- shader.editedByShaderControl = true;
-
- for (int s = 0; s < kk.Length; s++) {
- keywordLine.Add(shader.GetKeyword(kk[s]), k);
- }
- pragmaControl = 2;
- break;
- case 2:
-
- keywordLine.DisableKeywords();
- for (int s = 0; s < kk.Length; s++) {
- SCKeyword keyword = keywordLine.GetKeyword(kk[s]);
- if (keyword != null)
- keyword.enabled = true;
- }
- currentPass.Add(keywordLine);
- pragmaControl = 0;
- break;
- case 3:
- shader.editedByShaderControl = true;
-
- for (int s = 0; s < kk.Length; s++) {
- SCKeyword keyword = shader.GetKeyword(kk[s]);
- keyword.enabled = false;
- keywordLine.Add(keyword, k);
- }
- currentPass.Add(keywordLine);
- pragmaControl = 0;
- break;
- case 0:
-
- for (int s = 0; s < kk.Length; s++) {
- keywordLine.Add(shader.GetKeyword(kk[s]), k);
- }
- currentPass.Add(keywordLine);
- break;
- }
- }
- }
- currentPass.pass = Mathf.Max(pass, 0);
- if (basePass != null) {
- currentPass.Add(basePass.keywordLines);
- }
- shader.Add(currentPass);
- shader.UpdateVariantCount();
- }
- void UpdateShaderNonGraph(SCShader shader) {
-
- string[] shaderLines = File.ReadAllLines(shader.path);
- string[] separator = new string[] { " " };
- StringBuilder sb = new StringBuilder();
- int pragmaControl = 0;
- shader.editedByShaderControl = false;
- SCKeywordLine keywordLine = new SCKeywordLine();
- bool blockComment = false;
- for (int k = 0; k < shaderLines.Length; k++) {
- int lineCommentIndex = shaderLines[k].IndexOf("//");
- int blocCommentIndex = shaderLines[k].IndexOf("/*");
- int endCommentIndex = shaderLines[k].IndexOf("*/");
- if (blocCommentIndex > 0 && (lineCommentIndex > blocCommentIndex || lineCommentIndex < 0)) {
- blockComment = true;
- }
- if (endCommentIndex > blocCommentIndex && (lineCommentIndex > endCommentIndex || lineCommentIndex < 0)) {
- blockComment = false;
- }
- int j = -1;
- PragmaType pragmaType = PragmaType.Unknown;
- if (!blockComment) {
- j = shaderLines[k].IndexOf(PRAGMA_COMMENT_MARK);
- if (j >= 0) {
- pragmaControl = 1;
- }
- j = shaderLines[k].IndexOf(SCKeywordLine.PRAGMA_MULTICOMPILE_GLOBAL);
- if (j >= 0) {
- pragmaType = PragmaType.MultiCompileGlobal;
- } else {
- j = shaderLines[k].IndexOf(SCKeywordLine.PRAGMA_FEATURE_GLOBAL);
- if (j >= 0) {
- pragmaType = PragmaType.FeatureGlobal;
- } else {
- j = shaderLines[k].IndexOf(SCKeywordLine.PRAGMA_MULTICOMPILE_LOCAL);
- if (j >= 0) {
- pragmaType = PragmaType.MultiCompileLocal;
- } else {
- j = shaderLines[k].IndexOf(SCKeywordLine.PRAGMA_FEATURE_LOCAL);
- if (j >= 0) {
- pragmaType = PragmaType.FeatureLocal;
- }
- }
- }
- }
- if (pragmaControl != 1 && lineCommentIndex == 0 && shaderLines[k].IndexOf(PRAGMA_DISABLED_MARK) < 0) {
-
- j = -1;
- }
- }
- if (j >= 0) {
- if (pragmaControl != 2) {
- keywordLine.Clear();
- }
- keywordLine.pragmaType = pragmaType;
- j = shaderLines[k].IndexOf(' ', j + 20) + 1;
- if (j >= shaderLines[k].Length) continue;
-
- int lastStringPos = shaderLines[k].IndexOf("//", j);
- if (lastStringPos < 0) {
- lastStringPos = shaderLines[k].Length;
- }
- int length = lastStringPos - j;
- string[] kk = shaderLines[k].Substring(j, length).Split(separator, StringSplitOptions.RemoveEmptyEntries);
-
- for (int i = 0; i < kk.Length; i++) {
- kk[i] = kk[i].Trim();
- }
-
- switch (pragmaControl) {
- case 1:
-
- for (int s = 0; s < kk.Length; s++) {
- SCKeyword keyword = shader.GetKeyword(kk[s]);
- keywordLine.Add(keyword);
- }
- pragmaControl = 2;
- break;
- case 0:
- case 2:
- if (pragmaControl == 0) {
- for (int s = 0; s < kk.Length; s++) {
- SCKeyword keyword = shader.GetKeyword(kk[s]);
- keywordLine.Add(keyword);
- }
- }
- int kCount = keywordLine.keywordCount;
- int kEnabledCount = keywordLine.keywordsEnabledCount;
- if (kEnabledCount < kCount) {
-
- if (kEnabledCount == 0) {
- sb.Append(PRAGMA_DISABLED_MARK);
- } else {
- sb.Append(PRAGMA_COMMENT_MARK);
- }
- shader.editedByShaderControl = true;
- sb.Append(keywordLine.GetPragma());
- if (keywordLine.hasUnderscoreVariant)
- sb.Append(PRAGMA_UNDERSCORE);
- for (int s = 0; s < kCount; s++) {
- SCKeyword keyword = keywordLine.keywords[s];
- sb.Append(keyword.name);
- if (s < kCount - 1)
- sb.Append(" ");
- }
- sb.AppendLine();
- }
- if (kEnabledCount > 0) {
-
- sb.Append(keywordLine.GetPragma());
- if (keywordLine.hasUnderscoreVariant)
- sb.Append(PRAGMA_UNDERSCORE);
- for (int s = 0; s < kCount; s++) {
- SCKeyword keyword = keywordLine.keywords[s];
- if (keyword.enabled) {
- sb.Append(keyword.name);
- if (s < kCount - 1)
- sb.Append(" ");
- }
- }
- sb.AppendLine();
- }
- pragmaControl = 0;
- break;
- }
- } else {
- sb.AppendLine(shaderLines[k]);
- }
- }
-
- File.WriteAllText(shader.path, sb.ToString());
- AssetDatabase.Refresh();
- }
- void ConvertToLocalNonGraph(SCKeyword keyword, SCShader shader) {
- string path = shader.path;
- if (!File.Exists(path)) return;
- string[] lines = File.ReadAllLines(path);
- bool changed = false;
- for (int k = 0; k < lines.Length; k++) {
-
- if (lines[k].IndexOf(SCKeywordLine.PRAGMA_FEATURE_GLOBAL, StringComparison.InvariantCultureIgnoreCase) >= 0 && lines[k].IndexOf(keyword.name, StringComparison.InvariantCultureIgnoreCase) >= 0) {
- lines[k] = lines[k].Replace(SCKeywordLine.PRAGMA_FEATURE_GLOBAL, SCKeywordLine.PRAGMA_FEATURE_LOCAL);
- lines[k] = lines[k].Replace(SCKeywordLine.PRAGMA_FEATURE_GLOBAL.ToUpper(), SCKeywordLine.PRAGMA_FEATURE_LOCAL);
- changed = true;
- }
- }
- if (changed) {
- MakeBackup(shader);
- File.WriteAllLines(path, lines, Encoding.UTF8);
- }
- }
- }
- }
|