Skip to content

Commit f62d5bf

Browse files
Refactor shader property generation and update UnityToon shader
Co-authored-by: sindharta.tanuwijaya <sindharta.tanuwijaya@unity3d.com>
1 parent 69fe417 commit f62d5bf

File tree

4 files changed

+76
-89
lines changed

4 files changed

+76
-89
lines changed

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

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -198,74 +198,59 @@ private string ReplacePropertiesBlock(string originalContent, string commonPrope
198198

199199
newProperties.Add($"{baseIndent}Properties {{");
200200

201-
// Add common properties
202-
string[] commonLines = commonProperties.Split('\n');
203-
foreach (string line in commonLines)
201+
void AppendPropertyLine(string trimmedLine, bool allowOverride)
204202
{
205-
string trimmed = line.Trim();
206-
if (trimmed.Length == 0)
203+
if (string.IsNullOrEmpty(trimmedLine))
207204
{
208205
newProperties.Add(string.Empty);
209-
continue;
206+
return;
210207
}
211208

212-
string propertyLine = $"{blockIndent}{trimmed}";
213-
string propertyName = GetPropertyName(trimmed);
209+
string propertyLine = $"{blockIndent}{trimmedLine}";
210+
string propertyName = GetPropertyName(trimmedLine);
214211

215-
if (propertyName != null)
212+
if (propertyName == null)
216213
{
217-
if (propertyLineIndices.TryGetValue(propertyName, out int existingIndex))
214+
newProperties.Add(propertyLine);
215+
return;
216+
}
217+
218+
if (propertyLineIndices.TryGetValue(propertyName, out int existingIndex))
219+
{
220+
if (allowOverride)
218221
{
219222
newProperties[existingIndex] = propertyLine;
223+
return;
220224
}
221-
else
225+
226+
string existingLine = newProperties[existingIndex];
227+
bool existingHidden = existingLine.Trim().StartsWith("[HideInInspector]", StringComparison.Ordinal);
228+
bool newHidden = trimmedLine.StartsWith("[HideInInspector]", StringComparison.Ordinal);
229+
230+
if (existingHidden && !newHidden)
222231
{
223-
propertyLineIndices[propertyName] = newProperties.Count;
224-
propertyCount++;
225-
newProperties.Add(propertyLine);
232+
newProperties[existingIndex] = propertyLine;
226233
}
234+
return;
227235
}
228-
else
229-
{
230-
newProperties.Add(propertyLine);
231-
}
236+
237+
propertyLineIndices[propertyName] = newProperties.Count;
238+
propertyCount++;
239+
newProperties.Add(propertyLine);
240+
}
241+
242+
foreach (string line in commonProperties.Split('\n'))
243+
{
244+
AppendPropertyLine(line.Trim(), allowOverride: false);
232245
}
233246

234-
// Add tessellation properties if provided
235247
if (!string.IsNullOrEmpty(tessellationProperties))
236248
{
237249
newProperties.Add(string.Empty);
238250
newProperties.Add($"{blockIndent}// Tessellation-specific properties");
239-
string[] tessellationLines = tessellationProperties.Split('\n');
240-
foreach (string line in tessellationLines)
251+
foreach (string line in tessellationProperties.Split('\n'))
241252
{
242-
string trimmed = line.Trim();
243-
if (trimmed.Length == 0)
244-
{
245-
newProperties.Add(string.Empty);
246-
continue;
247-
}
248-
249-
string propertyLine = $"{blockIndent}{trimmed}";
250-
string propertyName = GetPropertyName(trimmed);
251-
252-
if (propertyName != null)
253-
{
254-
if (propertyLineIndices.TryGetValue(propertyName, out int existingIndex))
255-
{
256-
newProperties[existingIndex] = propertyLine;
257-
}
258-
else
259-
{
260-
propertyLineIndices[propertyName] = newProperties.Count;
261-
propertyCount++;
262-
newProperties.Add(propertyLine);
263-
}
264-
}
265-
else
266-
{
267-
newProperties.Add(propertyLine);
268-
}
253+
AppendPropertyLine(line.Trim(), allowOverride: true);
269254
}
270255
}
271256

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//Auto-generated on Wed Nov 05 05:42:38 UTC 2025
1+
//Auto-generated on Wed Nov 05 06:07:09 UTC 2025
22
//Unity Toon Shader
33
//nobuyuki@unity3d.com
44
//toshiyuki@unity3d.com (Intengrated)
@@ -35,7 +35,7 @@ Shader "Toon" {
3535
[Enum(OFF, 0, ON, 1, TRANSMODE, 2)] _ClippingMode("CliippingMode", int) = 0
3636

3737
// Cull and ZWrite Properties
38-
[HideInInspector] _CullMode("__cullmode", Float) = 2.0
38+
[Enum(OFF, 0, FRONT, 1, BACK, 2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
3939
[Enum(OFF, 0, ONT, 1)] _ZWriteMode("ZWrite Mode", int) = 1 //OFF/ON
4040
[Enum(OFF, 0, ONT, 1)] _ZOverDrawMode("ZOver Draw Mode", Float) = 0 //OFF/ON
4141
_SPRDefaultUnlitColorMask("SPRDefaultUnlit Path Color Mask", int) = 15
@@ -47,7 +47,7 @@ Shader "Toon" {
4747
_IsBaseMapAlphaAsClippingMask("IsBaseMapAlphaAsClippingMask", Float) = 0
4848
//
4949
[Toggle(_)] _Inverse_Clipping("Inverse_Clipping", Float) = 0
50-
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
50+
_Clipping_Level("Clipping_Level", Range(0, 1)) = 0
5151
// ClippingMask paramaters to Here.
5252

5353
// Base Color Properties
@@ -56,7 +56,7 @@ Shader "Toon" {
5656
_BaseColor ("BaseColor", Color) = (1,1,1,1)
5757
//v.2.0.5 : Clipping/TransClipping for SSAO Problems in PostProcessing Stack.
5858
//If you want to go back the former SSAO results, comment out the below line.
59-
_Color("Color", Color) = (1,1,1,1)
59+
_Color ("Color", Color) = (1,1,1,1)
6060
//
6161
[Toggle(_)] _Is_LightColor_Base ("Is_LightColor_Base", Float ) = 1
6262

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//Auto-generated on Wed Nov 05 05:42:38 UTC 2025
1+
//Auto-generated on Wed Nov 05 06:07:09 UTC 2025
22
//Unity Toon Shader
33
//nobuyuki@unity3d.com
44
//toshiyuki@unity3d.com (Intengrated)
@@ -35,7 +35,7 @@ Shader "Toon(Tessellation)" {
3535
[Enum(OFF, 0, ON, 1, TRANSMODE, 2)] _ClippingMode("CliippingMode", int) = 0
3636

3737
// Cull and ZWrite Properties
38-
[HideInInspector] _CullMode("__cullmode", Float) = 2.0
38+
[Enum(OFF, 0, FRONT, 1, BACK, 2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
3939
[Enum(OFF, 0, ONT, 1)] _ZWriteMode("ZWrite Mode", int) = 1 //OFF/ON
4040
[Enum(OFF, 0, ONT, 1)] _ZOverDrawMode("ZOver Draw Mode", Float) = 0 //OFF/ON
4141
_SPRDefaultUnlitColorMask("SPRDefaultUnlit Path Color Mask", int) = 15
@@ -56,7 +56,7 @@ Shader "Toon(Tessellation)" {
5656
_BaseColor ("BaseColor", Color) = (1,1,1,1)
5757
//v.2.0.5 : Clipping/TransClipping for SSAO Problems in PostProcessing Stack.
5858
//If you want to go back the former SSAO results, comment out the below line.
59-
_Color("Color", Color) = (1,1,1,1)
59+
_Color ("Color", Color) = (1,1,1,1)
6060
//
6161
[Toggle(_)] _Is_LightColor_Base ("Is_LightColor_Base", Float ) = 1
6262

generate_UnityToon.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -180,46 +180,48 @@ def generate_shader(shader_path, common_properties, tessellation_properties, aut
180180
property_count = 0
181181
new_properties.append(f"{base_indent}Properties {{")
182182

183-
# Add common properties
184-
common_lines = common_properties.split('\n')
185-
for line in common_lines:
186-
stripped_line = line.strip()
187-
if stripped_line:
188-
new_line = f"{block_indent}{stripped_line}"
189-
property_name = get_property_name(stripped_line)
190-
if property_name:
191-
if property_name in property_map:
192-
new_properties[property_map[property_name]] = new_line
193-
else:
194-
property_map[property_name] = len(new_properties)
195-
property_count += 1
196-
new_properties.append(new_line)
197-
else:
198-
new_properties.append(new_line)
199-
else:
183+
def append_property_line(stripped_line, allow_override):
184+
nonlocal property_count
185+
186+
if not stripped_line:
200187
new_properties.append("")
188+
return
189+
190+
property_name = get_property_name(stripped_line)
191+
new_line = f"{block_indent}{stripped_line}"
192+
193+
if not property_name:
194+
new_properties.append(new_line)
195+
return
196+
197+
if property_name in property_map:
198+
if allow_override:
199+
new_properties[property_map[property_name]] = new_line
200+
return
201+
202+
existing_line = new_properties[property_map[property_name]]
203+
existing_hidden = existing_line.strip().startswith("[HideInInspector]")
204+
new_hidden = stripped_line.startswith("[HideInInspector]")
205+
206+
if existing_hidden and not new_hidden:
207+
new_properties[property_map[property_name]] = new_line
208+
# otherwise keep existing definition
209+
return
210+
211+
property_map[property_name] = len(new_properties)
212+
property_count += 1
213+
new_properties.append(new_line)
214+
215+
# Add common properties
216+
for line in common_properties.split('\n'):
217+
append_property_line(line.strip(), allow_override=False)
201218

202219
# Add tessellation properties if provided
203220
if tessellation_properties:
204221
new_properties.append("")
205222
new_properties.append(f"{block_indent}// Tessellation-specific properties")
206-
tessellation_lines = tessellation_properties.split('\n')
207-
for line in tessellation_lines:
208-
stripped_line = line.strip()
209-
if stripped_line:
210-
new_line = f"{block_indent}{stripped_line}"
211-
property_name = get_property_name(stripped_line)
212-
if property_name:
213-
if property_name in property_map:
214-
new_properties[property_map[property_name]] = new_line
215-
else:
216-
property_map[property_name] = len(new_properties)
217-
property_count += 1
218-
new_properties.append(new_line)
219-
else:
220-
new_properties.append(new_line)
221-
else:
222-
new_properties.append("")
223+
for line in tessellation_properties.split('\n'):
224+
append_property_line(line.strip(), allow_override=True)
223225

224226
new_properties.append(f"{base_indent}}}")
225227

0 commit comments

Comments
 (0)