-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathgenerate_UnityToon.py
More file actions
305 lines (244 loc) · 10.5 KB
/
generate_UnityToon.py
File metadata and controls
305 lines (244 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
import os
import re
from datetime import datetime, timezone
PROPERTY_NAME_PATTERN = re.compile(r'(?:\]\s*|^)([A-Za-z_][A-Za-z0-9_]*)\s*\(')
def extract_properties_from_shader_content(content):
properties_match = re.search(r"Properties\s*\{", content)
if not properties_match:
return content.strip()
open_brace_index = content.find('{', properties_match.start())
if open_brace_index == -1:
return content.strip()
brace_count = 1
close_brace_index = -1
for index in range(open_brace_index + 1, len(content)):
char = content[index]
if char == '{':
brace_count += 1
elif char == '}':
brace_count -= 1
if brace_count == 0:
close_brace_index = index
break
if close_brace_index == -1:
return content.strip()
block = content[open_brace_index + 1:close_brace_index]
lines = block.split('\n')
cleaned_lines = [line.strip() for line in lines]
while cleaned_lines and cleaned_lines[0] == "":
cleaned_lines.pop(0)
while cleaned_lines and cleaned_lines[-1] == "":
cleaned_lines.pop()
return '\n'.join(cleaned_lines)
def get_property_name(line: str):
if not line or line.startswith("//"):
return None
matches = PROPERTY_NAME_PATTERN.findall(line)
if not matches:
return None
candidate = matches[-1]
if candidate.startswith('['):
return None
return candidate
def cleanup_lines(lines):
lines = list(lines)
while lines and lines[0] == "":
lines.pop(0)
while lines and lines[-1] == "":
lines.pop()
cleaned = []
for line in lines:
if line == "" and cleaned and cleaned[-1] == "":
continue
cleaned.append(line)
return cleaned
def build_property_sections(common_properties, tessellation_properties, indent):
entries = []
name_to_index = {}
def add_lines(raw_text, source):
if not raw_text:
return
for raw_line in raw_text.splitlines():
stripped = raw_line.strip()
entry = {
"line": stripped,
"source": source,
"is_hidden": "[HideInInspector]" in stripped,
}
if stripped == "":
entry["name"] = None
entries.append(entry)
continue
name = get_property_name(stripped)
entry["name"] = name
source_map = name_to_index.setdefault(source, {})
if name and name in source_map:
existing_index = source_map[name]
existing_entry = entries[existing_index]
def should_replace(existing, new):
if existing["is_hidden"] and not new["is_hidden"]:
return True
if not existing["is_hidden"] and new["is_hidden"]:
return False
return True
if should_replace(existing_entry, entry):
entries[existing_index] = entry
else:
entries.append(entry)
if name:
source_map[name] = len(entries) - 1
add_lines(common_properties, "common")
add_lines(tessellation_properties, "tess")
seen_pairs = set()
property_count = 0
tess_override_names = set(name_to_index.get("tess", {}).keys())
for entry in entries:
entry["skip_for_tess"] = entry["source"] == "common" and entry.get("name") in tess_override_names
for entry in entries:
name = entry.get("name")
if not name:
continue
key = (entry["source"], name)
if key in seen_pairs:
continue
seen_pairs.add(key)
property_count += 1
def render(section_source, skip_overrides=False):
lines = []
for entry in entries:
if entry["source"] != section_source:
continue
if skip_overrides and entry.get("skip_for_tess"):
continue
if entry["line"]:
lines.append(indent + entry["line"])
else:
lines.append("")
return "\n".join(cleanup_lines(lines))
common_block = render("common")
common_block_for_tess = render("common", skip_overrides=True)
tess_block = render("tess")
return property_count, common_block, tess_block, common_block_for_tess
def load_properties_from_shader(shader_path, descriptor):
with open(shader_path, 'r', encoding='utf-8') as f:
shader_content = f.read()
if not shader_content:
print(f"ERROR: {descriptor} shader file is empty or could not be read")
return None
shader_content = shader_content.lstrip('\ufeff')
properties = extract_properties_from_shader_content(shader_content)
if not properties:
print(f"ERROR: Could not extract {descriptor} properties from {shader_path}")
return None
print(f"Successfully extracted {descriptor} properties. Length: {len(properties)} characters")
return properties
def generate_shader_files():
try:
# Read the common properties shader
common_properties_path = "com.unity.toonshader/Runtime/Shaders/Common/Parts/CommonProperties.shaderblock"
common_properties = load_properties_from_shader(common_properties_path, "common")
if not common_properties:
return False
# Read the tessellation properties shader
tessellation_properties_path = "com.unity.toonshader/Runtime/Shaders/Common/Parts/TessellationProperties.shaderblock"
tessellation_properties = load_properties_from_shader(tessellation_properties_path, "tessellation")
if tessellation_properties is None:
return False
timestamp = datetime.now(timezone.utc).strftime("%a %b %d %H:%M:%S UTC %Y")
auto_comment_line = f"//Auto-generated on {timestamp}"
# Generate UnityToon.shader
print("\nGenerating UnityToon.shader...")
success1 = generate_shader(
"com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader",
"com.unity.toonshader/Runtime/Shaders/Common/Parts/UnityToon.shadertemplate",
common_properties,
"",
auto_comment_line,
)
# Generate UnityToonTessellation.shader
print("\nGenerating UnityToonTessellation.shader...")
success2 = generate_shader(
"com.unity.toonshader/Runtime/Integrated/Shaders/UnityToonTessellation.shader",
"com.unity.toonshader/Runtime/Shaders/Common/Parts/UnityToonTessellation.shadertemplate",
common_properties,
tessellation_properties,
auto_comment_line,
)
if success1 and success2:
print("\nBoth shader files generated successfully!")
return True
else:
print("\nSome shader files failed to generate.")
return False
except Exception as e:
print(f"ERROR: {e}")
import traceback
traceback.print_exc()
return False
def generate_shader(shader_path, template_path, common_properties, tessellation_properties, auto_comment_line):
try:
# Read the template or existing shader file
if template_path and os.path.exists(template_path):
with open(template_path, 'r', encoding='utf-8') as f:
original_content = f.read()
source_path = template_path
elif os.path.exists(shader_path):
with open(shader_path, 'r', encoding='utf-8') as f:
original_content = f.read()
source_path = shader_path
else:
print(f"ERROR: Could not locate shader or template for {shader_path}")
return False
if not original_content:
print(f"ERROR: {shader_path} file is empty or could not be read")
return False
original_content = original_content.lstrip('\ufeff')
print(f"Successfully read {source_path}. Length: {len(original_content)} characters")
property_count, common_block, tess_block, common_block_for_tess = build_property_sections(common_properties, tessellation_properties, " ")
use_common_block = common_block_for_tess if tessellation_properties else common_block
new_content = original_content
if " [COMMON_PROPERTIES]" in new_content:
new_content = new_content.replace(" [COMMON_PROPERTIES]", use_common_block or "", 1)
elif "[COMMON_PROPERTIES]" in new_content:
new_content = new_content.replace("[COMMON_PROPERTIES]", use_common_block or "", 1)
else:
print(f"ERROR: Common properties placeholder not found in template for {shader_path}")
return False
if "[TESSELLATION_PROPERTIES]" in new_content:
replacement = tess_block or ""
new_content = new_content.replace(" [TESSELLATION_PROPERTIES]", replacement, 1)
new_content = new_content.replace("[TESSELLATION_PROPERTIES]", replacement, 1)
print(f"Generated properties with {property_count} entries. Common length: {len(common_block)}, Tess length: {len(tess_block)}")
new_content = apply_auto_generated_comment(new_content, auto_comment_line)
print(f"Generated new shader content. Original length: {len(original_content)}, New length: {len(new_content)}")
# Write the new shader file
new_content = new_content.lstrip('\ufeff')
new_content = new_content.replace('\r\n', '\n').replace('\r', '\n')
if not new_content.endswith('\n'):
new_content += '\n'
os.makedirs(os.path.dirname(shader_path), exist_ok=True)
with open(shader_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Successfully wrote {shader_path}")
return True
except Exception as e:
print(f"ERROR generating {shader_path}: {e}")
return False
def apply_auto_generated_comment(content, comment_line):
auto_prefix = "//Auto-generated on "
lines = content.splitlines()
if lines and lines[0].startswith(auto_prefix):
lines[0] = comment_line
else:
lines.insert(0, comment_line)
joined = "\n".join(lines)
if not joined.endswith("\n"):
joined += "\n"
return joined
if __name__ == "__main__":
success = generate_shader_files()
if success:
print("\nAll shader files generated successfully!")
else:
print("\nShader generation failed!")