Skip to content

Commit 9c8ed8f

Browse files
Refactor shader generation and paths
This commit restructures the shader generation process by moving the shader generation script to the editor folder and updating paths. It also removes the separate property files and integrates them directly into the shader templates. Co-authored-by: sindharta.tanuwijaya <sindharta.tanuwijaya@unity3d.com>
1 parent c6711f0 commit 9c8ed8f

File tree

13 files changed

+3577
-82
lines changed

13 files changed

+3577
-82
lines changed
-9.59 KB
Binary file not shown.

com.unity.toon-graphics-test/Editor/README_ShaderGenerator.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@ This tool helps maintain consistency between `UnityToon.shader` and `UnityToonTe
44

55
## Files
66

7-
- **CommonPropertiesPart.shader**: Plain text list of shared property definitions (no Shader wrapper) with original comments preserved
8-
- **TessellationPropertiesPart.shader**: Plain text list of tessellation-only property definitions (no Shader wrapper)
7+
- **CommonPropertiesPart.shader**: Plain text list of shared property definitions (no Shader wrapper) with original comments preserved (`Packages/com.unity.toonshader/Runtime/Shaders/Common/Parts~/`)
8+
- **TessellationPropertiesPart.shader**: Plain text list of tessellation-only property definitions (no Shader wrapper) (`Packages/com.unity.toonshader/Runtime/Shaders/Common/Parts~/`)
99
- **ShaderGenerator.cs**: Unity Editor script (now located in the graphics test package) that generates the shader files from the property assets
1010

1111
## How to Use
1212

13-
1. **Open the Shader Generator Window**:
14-
- In Unity, go to `Unity Toon Shader > Generate Shader Files`
15-
- This opens the Shader Generator window
13+
1. **Edit Properties**:
14+
- Modify the plain-text part files under `Packages/com.unity.toonshader/Runtime/Shaders/Common/Parts~/`
15+
- The files contain only property declarations, so standard ShaderLab property syntax applies
1616

17-
2. **Edit Properties**:
18-
- Click "Open Common Properties File" to edit the shared properties shader (includes all original comments)
19-
- Click "Open Tessellation Properties File" to edit the tessellation-specific properties shader
20-
- Make your changes directly in the shader assets
21-
22-
3. **Generate Shader Files**:
23-
- Click "Generate Shader Files" button
17+
2. **Generate Shader Files**:
18+
- In Unity, choose `Toon Shader > Generate Shader Files`
2419
- The tool will automatically:
2520
- Replace the Properties blocks in both shader files
2621
- Preserve all other shader content (HLSLINCLUDE, SubShaders, etc.)
@@ -73,9 +68,11 @@ com.unity.toon-graphics-test/
7368
├── ShaderGeneratorTest.cs # Editor test harness
7469
└── README_ShaderGenerator.md # This file
7570
76-
com.unity.toonshader/Runtime/Integrated/Shaders/
71+
com.unity.toonshader/Runtime/Shaders/Common/Parts~/
7772
├── CommonPropertiesPart.shader # Shared properties (plain text)
78-
├── TessellationPropertiesPart.shader # Tessellation-specific properties (plain text)
73+
└── TessellationPropertiesPart.shader # Tessellation-specific properties (plain text)
74+
75+
com.unity.toonshader/Runtime/Integrated/Shaders/
7976
├── UnityToon.shader # Generated shader (regular)
8077
└── UnityToonTessellation.shader # Generated shader (tessellation)
8178

com.unity.toon-graphics-test/Editor/ShaderGenerator.cs

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,26 @@ namespace UnityEditor.Rendering.Toon
1414
/// This helps maintain consistency between UnityToon.shader and UnityToonTessellation.shader
1515
/// by using a single source of truth for shared properties.
1616
/// </summary>
17-
public class ShaderGenerator : EditorWindow
17+
public static class ShaderGenerator
1818
{
1919
private static readonly Regex PropertyNameRegex = new Regex(@"(?:\]\s*|^)([A-Za-z_][A-Za-z0-9_]*)\s*\(", RegexOptions.Compiled);
20-
private const string COMMON_PROPERTIES_PATH = "Packages/common.unity.toonshader/Runtime/Shaders/Common/Parts~/CommonPropertiesPart.shader";
21-
private const string TESSELATION_PROPERTIES_PATH = "Packages/common.unity.toonshader/Runtime/Shaders/Common/Parts~/TessellationPropertiesPart.shader";
20+
private const string COMMON_PROPERTIES_PATH = "Packages/com.unity.toonshader/Runtime/Shaders/Common/Parts~/CommonPropertiesPart.shader";
21+
private const string TESSELATION_PROPERTIES_PATH = "Packages/com.unity.toonshader/Runtime/Shaders/Common/Parts~/TessellationPropertiesPart.shader";
2222
private const string UNITY_TOON_SHADER_PATH = "Packages/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader";
2323
private const string UNITY_TOON_TESSELATION_SHADER_PATH = "Packages/com.unity.toonshader/Runtime/Integrated/Shaders/UnityToonTessellation.shader";
2424

25-
[MenuItem("Unity Toon Shader/Generate Shader Files")]
26-
public static void ShowWindow()
25+
[MenuItem("Toon Shader/Generate Shader Files")]
26+
public static void GenerateShaderFilesMenu()
2727
{
28-
GetWindow<ShaderGenerator>("Shader Generator");
28+
GenerateShaderFiles();
2929
}
30-
31-
private void OnGUI()
30+
31+
private static void GenerateShaderFiles()
3232
{
33-
GUILayout.Label("Unity Toon Shader Generator", EditorStyles.boldLabel);
34-
GUILayout.Space(10);
35-
36-
GUILayout.Label("This tool generates UnityToon.shader and UnityToonTessellation.shader from common property files.", EditorStyles.helpBox);
37-
GUILayout.Space(10);
38-
39-
if (GUILayout.Button("Generate Shader Files", GUILayout.Height(30)))
40-
{
41-
GenerateShaderFiles();
42-
}
43-
44-
GUILayout.Space(10);
45-
46-
if (GUILayout.Button("Open Common Properties File"))
47-
{
48-
var asset = AssetDatabase.LoadAssetAtPath<Shader>(COMMON_PROPERTIES_PATH);
49-
if (asset != null)
50-
{
51-
Selection.activeObject = asset;
52-
EditorGUIUtility.PingObject(asset);
53-
}
54-
}
55-
56-
if (GUILayout.Button("Open Tessellation Properties File"))
57-
{
58-
var asset = AssetDatabase.LoadAssetAtPath<Shader>(TESSELATION_PROPERTIES_PATH);
59-
if (asset != null)
60-
{
61-
Selection.activeObject = asset;
62-
EditorGUIUtility.PingObject(asset);
63-
}
64-
}
33+
GenerateShaderFilesInternal();
6534
}
6635

67-
private void GenerateShaderFiles()
36+
private static void GenerateShaderFilesInternal()
6837
{
6938
try
7039
{
@@ -116,35 +85,43 @@ private void GenerateShaderFiles()
11685
}
11786
}
11887

119-
private void GenerateUnityToonShader(string commonProperties, string autoCommentLine)
88+
private static void GenerateUnityToonShader(string commonProperties, string autoCommentLine)
12089
{
12190
// Read the original shader file to preserve the rest of the content
12291
string originalContent = ReadFile(UNITY_TOON_SHADER_PATH);
12392
if (string.IsNullOrEmpty(originalContent))
12493
{
125-
Debug.LogError($"Failed to read original shader from {UNITY_TOON_SHADER_PATH}");
126-
return;
94+
originalContent = ReadFile(UNITY_TOON_SHADER_PATH + ".template");
95+
if (string.IsNullOrEmpty(originalContent))
96+
{
97+
Debug.LogError($"Failed to read original shader from {UNITY_TOON_SHADER_PATH}");
98+
return;
99+
}
127100
}
128101
// Replace the Properties block
129102
string newContent = ReplacePropertiesBlock(originalContent, commonProperties, string.Empty, autoCommentLine);
130103
WriteFile(UNITY_TOON_SHADER_PATH, newContent);
131104
}
132105

133-
private void GenerateUnityToonTessellationShader(string commonProperties, string tessellationProperties, string autoCommentLine)
106+
private static void GenerateUnityToonTessellationShader(string commonProperties, string tessellationProperties, string autoCommentLine)
134107
{
135108
// Read the original shader file to preserve the rest of the content
136109
string originalContent = ReadFile(UNITY_TOON_TESSELATION_SHADER_PATH);
137110
if (string.IsNullOrEmpty(originalContent))
138111
{
139-
Debug.LogError($"Failed to read original tessellation shader from {UNITY_TOON_TESSELATION_SHADER_PATH}");
140-
return;
112+
originalContent = ReadFile(UNITY_TOON_TESSELATION_SHADER_PATH + ".template");
113+
if (string.IsNullOrEmpty(originalContent))
114+
{
115+
Debug.LogError($"Failed to read original tessellation shader from {UNITY_TOON_TESSELATION_SHADER_PATH}");
116+
return;
117+
}
141118
}
142119
// Replace the Properties block
143120
string newContent = ReplacePropertiesBlock(originalContent, commonProperties, tessellationProperties, autoCommentLine);
144121
WriteFile(UNITY_TOON_TESSELATION_SHADER_PATH, newContent);
145122
}
146123

147-
private string ReplacePropertiesBlock(string originalContent, string commonProperties, string tessellationProperties, string autoCommentLine)
124+
private static string ReplacePropertiesBlock(string originalContent, string commonProperties, string tessellationProperties, string autoCommentLine)
148125
{
149126
// Find the Properties block using a more robust regex that handles nested braces
150127
string propertiesPattern = @"Properties\s*\{";
@@ -264,7 +241,7 @@ void AppendPropertyLine(string trimmedLine, bool allowOverride)
264241
return ApplyAutoGeneratedComment(updatedContent, autoCommentLine);
265242
}
266243

267-
private string ExtractPropertiesBlockContent(string shaderContent)
244+
private static string ExtractPropertiesBlockContent(string shaderContent)
268245
{
269246
if (string.IsNullOrEmpty(shaderContent))
270247
{
@@ -349,7 +326,7 @@ private string ExtractPropertiesBlockContent(string shaderContent)
349326
return result.ToString();
350327
}
351328

352-
private string GetPropertyName(string line)
329+
private static string GetPropertyName(string line)
353330
{
354331
line = line.Trim();
355332
if (string.IsNullOrEmpty(line) || line.StartsWith("//", StringComparison.Ordinal))
@@ -372,7 +349,7 @@ private string GetPropertyName(string line)
372349
return candidate;
373350
}
374351

375-
private string ReadFile(string path)
352+
private static string ReadFile(string path)
376353
{
377354
if (File.Exists(path))
378355
{
@@ -381,7 +358,7 @@ private string ReadFile(string path)
381358
return null;
382359
}
383360

384-
private void WriteFile(string path, string content)
361+
private static void WriteFile(string path, string content)
385362
{
386363
// Ensure directory exists
387364
string directory = Path.GetDirectoryName(path);
@@ -393,7 +370,7 @@ private void WriteFile(string path, string content)
393370
File.WriteAllText(path, content.TrimStart('\uFEFF'));
394371
}
395372

396-
private string ApplyAutoGeneratedComment(string content, string commentLine)
373+
private static string ApplyAutoGeneratedComment(string content, string commentLine)
397374
{
398375
const string autoPrefix = "//Auto-generated on ";
399376
string[] lines = content.Split(new[] { "\n" }, StringSplitOptions.None);

com.unity.toon-graphics-test/Editor/ShaderGeneratorTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace UnityEditor.Rendering.Toon
1212
/// </summary>
1313
public class ShaderGeneratorTest
1414
{
15-
[MenuItem("Unity Toon Shader/Test Shader Generation")]
15+
[MenuItem("Toon Shader/Test Shader Generation")]
1616
public static void TestShaderGeneration()
1717
{
1818
try
1919
{
2020
// Test reading the common properties file
21-
string commonPropertiesPath = "Packages/common.unity.toonshader/Runtime/Shaders/Common/Parts~/CommonPropertiesPart.shader";
21+
string commonPropertiesPath = "Packages/com.unity.toonshader/Runtime/Shaders/Common/Parts~/CommonPropertiesPart.shader";
2222
string commonPropertiesShader = File.ReadAllText(commonPropertiesPath);
2323

2424
if (string.IsNullOrEmpty(commonPropertiesShader))
@@ -36,7 +36,7 @@ public static void TestShaderGeneration()
3636
Debug.Log($"Successfully extracted common properties. Length: {commonProperties.Length} characters");
3737

3838
// Test reading the tessellation properties file
39-
string tessellationPropertiesPath = "Packages/common.unity.toonshader/Runtime/Shaders/Common/Parts~/TessellationPropertiesPart.shader";
39+
string tessellationPropertiesPath = "Packages/com.unity.toonshader/Runtime/Shaders/Common/Parts~/TessellationPropertiesPart.shader";
4040
string tessellationPropertiesShader = File.ReadAllText(tessellationPropertiesPath);
4141

4242
if (string.IsNullOrEmpty(tessellationPropertiesShader))

com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//Auto-generated on Wed Nov 05 06:37:48 UTC 2025
1+
//Auto-generated on Wed Nov 05 08:37:28 UTC 2025
22
//Unity Toon Shader
33
//nobuyuki@unity3d.com
44
//toshiyuki@unity3d.com (Intengrated)

0 commit comments

Comments
 (0)