|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | +#define PATCHING_ACTIVE |
| 4 | +using UnityEditor; |
| 5 | +using System.IO; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | + |
| 8 | +public class NonSrpShaderAssetGenerator : AssetPostprocessor |
| 9 | +{ |
| 10 | + |
| 11 | + static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload) |
| 12 | + { |
| 13 | +#if PATCHING_ACTIVE |
| 14 | + bool assetDBDirty = false; |
| 15 | + foreach (string str in importedAssets) |
| 16 | + { |
| 17 | + if (str.EndsWith(".shader") && !str.EndsWith("NonSrp.shader")) |
| 18 | + { |
| 19 | + string resultFile = copyAndPatchShader(str); |
| 20 | + if (resultFile != null) |
| 21 | + { |
| 22 | + assetDBDirty = true; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + if (assetDBDirty) { AssetDatabase.Refresh(); } |
| 27 | +#endif |
| 28 | + } |
| 29 | + |
| 30 | + static string copyAndPatchShader(string path) |
| 31 | + { |
| 32 | + if (!File.Exists(path)) { return null; } |
| 33 | + |
| 34 | + bool isPatched = false; |
| 35 | + |
| 36 | + string shaderText = File.ReadAllText(path); |
| 37 | + string sourceDirectory = Path.GetDirectoryName(path); |
| 38 | + string targetDirectory = sourceDirectory + "/NonSRP"; |
| 39 | + |
| 40 | + // Only work with shaders from Graphics Tools |
| 41 | + string nameDeclaration = Regex.Match(shaderText, " *Shader *\"Graphics Tools\\/.*\"").Value; |
| 42 | + if (nameDeclaration.Length <= 0 ) { return null; } |
| 43 | + |
| 44 | + string outputPath = Path.Combine( sourceDirectory, Path.GetFileNameWithoutExtension(path) +"NonSrp" + Path.GetExtension(path)); |
| 45 | + |
| 46 | + // Put "_NON_SRP" befor StandardProgram include (in Standard and StandardCanvas) |
| 47 | + MatchCollection matchesStandardProgram = Regex.Matches(shaderText, "(?<tab> *)(#include_with_pragmas \"GraphicsToolsStandardProgram.hlsl\")"); |
| 48 | + if (matchesStandardProgram.Count > 0) |
| 49 | + { |
| 50 | + string tab = matchesStandardProgram[1].Groups["tab"].Value; |
| 51 | + shaderText = Regex.Replace(shaderText, matchesStandardProgram[0].Value, tab + "#define _NON_SRP\r\n" + matchesStandardProgram[0].Value); |
| 52 | + isPatched = true; |
| 53 | + } |
| 54 | + |
| 55 | + // Remove CBUFFER statements |
| 56 | + MatchCollection matchCBuff = Regex.Matches(shaderText, " *(CBUFFER_START\\(UnityPerMaterial\\))| *(CBUFFER_END)"); |
| 57 | + if (matchCBuff.Count > 0) |
| 58 | + { |
| 59 | + shaderText = Regex.Replace(shaderText, " *(CBUFFER_START\\(UnityPerMaterial\\))| *(CBUFFER_END)", ""); |
| 60 | + isPatched = true; |
| 61 | + } |
| 62 | + |
| 63 | + // Rename shader, put it under NonSrp directory, write to file |
| 64 | + if (isPatched) |
| 65 | + { |
| 66 | + Match shaderDirAndName = Regex.Match(nameDeclaration, "(?<dir>.*\\/)(?<name>.*)\""); |
| 67 | + string newNameDeclaration = shaderDirAndName.Groups["dir"].Value + "Non-SRP/" + shaderDirAndName.Groups["name"].Value + "NonSrp\"" ; |
| 68 | + shaderText = Regex.Replace(shaderText, nameDeclaration, newNameDeclaration); |
| 69 | + |
| 70 | + File.WriteAllText(outputPath, shaderText); |
| 71 | + return outputPath; |
| 72 | + } |
| 73 | + else { return null; } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments