agent-manifesto-kit 1.8.1 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +18 -0
  2. package/collection/skills/concept-asset-builder/SKILL.md +124 -0
  3. package/collection/skills/concept-asset-builder/agents/openai.yaml +4 -0
  4. package/collection/skills/concept-asset-builder/assets/templates/agent_brief.md +56 -0
  5. package/collection/skills/concept-asset-builder/assets/templates/rubric.json +50 -0
  6. package/collection/skills/concept-asset-builder/assets/templates/rubric_example_vending.json +14 -0
  7. package/collection/skills/concept-asset-builder/assets/templates/scene_style_example.md +30 -0
  8. package/collection/skills/concept-asset-builder/assets/templates/wear_description_example.md +50 -0
  9. package/collection/skills/concept-asset-builder/assets/templates/zones.json +10 -0
  10. package/collection/skills/concept-asset-builder/assets/templates/zones_example_vending.json +69 -0
  11. package/collection/skills/concept-asset-builder/references/examples/postbox_blender.py +1112 -0
  12. package/collection/skills/concept-asset-builder/references/examples/postbox_wear_three.js +742 -0
  13. package/collection/skills/concept-asset-builder/references/examples/vending_materials_blender.py +487 -0
  14. package/collection/skills/concept-asset-builder/references/examples/vending_wear_three.js +798 -0
  15. package/collection/skills/concept-asset-builder/references/field-lessons.md +36 -0
  16. package/collection/skills/concept-asset-builder/references/wear-language.md +36 -0
  17. package/collection/skills/concept-asset-builder/scripts/blender/bake_export.py +174 -0
  18. package/collection/skills/concept-asset-builder/scripts/blender/render_views.py +100 -0
  19. package/collection/skills/concept-asset-builder/scripts/blender/studio.py +73 -0
  20. package/collection/skills/concept-asset-builder/scripts/capture.mjs +44 -0
  21. package/collection/skills/concept-asset-builder/scripts/codex_review.sh +88 -0
  22. package/collection/skills/concept-asset-builder/scripts/compare_to.py +24 -0
  23. package/collection/skills/concept-asset-builder/scripts/composite_godot.py +13 -0
  24. package/collection/skills/concept-asset-builder/scripts/crop.py +9 -0
  25. package/collection/skills/concept-asset-builder/scripts/gen_texture.sh +18 -0
  26. package/collection/skills/concept-asset-builder/scripts/godot/capture.gd +122 -0
  27. package/collection/skills/concept-asset-builder/scripts/godot/project.godot +18 -0
  28. package/collection/skills/concept-asset-builder/scripts/init_workspace.sh +17 -0
  29. package/collection/skills/concept-asset-builder/scripts/measure_silhouette.py +40 -0
  30. package/collection/skills/concept-asset-builder/scripts/metrics.py +144 -0
  31. package/collection/skills/concept-asset-builder/scripts/sheet.py +19 -0
  32. package/collection/skills/concept-asset-builder/scripts/test_tools.py +121 -0
  33. package/collection/skills/concept-asset-builder/scripts/threejs/index.html +23 -0
  34. package/collection/skills/concept-asset-builder/scripts/threejs/model.js +9 -0
  35. package/collection/skills/concept-asset-builder/scripts/threejs/studio.js +178 -0
  36. package/collection/skills/concept-asset-builder/scripts/wear_stats.py +130 -0
  37. package/collection/skills/concept-scene-builder/SKILL.md +16 -3
  38. package/package.json +1 -1
@@ -0,0 +1,1112 @@
1
+ """Build the Japanese post box from spec/postbox.json and save blender/postbox.blend.
2
+
3
+ Run: /Applications/Blender.app/Contents/MacOS/Blender -b --factory-startup --python blender/build_postbox.py
4
+ Spec coordinates are Y-up, front +Z. Blender is Z-up, so spec (x, y, z) -> Blender (x, -z, y): front faces -Y.
5
+ """
6
+ import bpy, bmesh, json, math, os
7
+ from mathutils import Vector
8
+
9
+ ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
10
+ SPEC = json.load(open(os.path.join(ROOT, "spec/postbox.json")))
11
+ TEX = os.path.join(ROOT, "textures")
12
+
13
+
14
+ def P(x, up, fwd):
15
+ """spec (x, y-up, z-front) -> blender"""
16
+ return Vector((x, -fwd, up))
17
+
18
+
19
+ def srgb_to_lin(c):
20
+ return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
21
+
22
+
23
+ def hex_rgba(h):
24
+ h = h.lstrip("#")
25
+ return tuple(srgb_to_lin(int(h[i:i + 2], 16) / 255) for i in (0, 2, 4)) + (1.0,)
26
+
27
+
28
+ # ---------------------------------------------------------------- scene reset
29
+ bpy.ops.wm.read_factory_settings(use_empty=True)
30
+ scene = bpy.context.scene
31
+ col = scene.collection
32
+
33
+
34
+ def link(obj):
35
+ col.objects.link(obj)
36
+ return obj
37
+
38
+
39
+ def new_obj(name, bm):
40
+ me = bpy.data.meshes.new(name)
41
+ bm.to_mesh(me)
42
+ bm.free()
43
+ return link(bpy.data.objects.new(name, me))
44
+
45
+
46
+ # ---------------------------------------------------------------- geometry helpers
47
+ def lathe(name, profile, sides, v_range=None, sharp_deg=20.0):
48
+ """profile: list of (apothem, y) bottom->top. Flat face centred at the front.
49
+ UV: u = angle (seam at the back), v = (y - v0)/(v1 - v0)."""
50
+ bm = bmesh.new()
51
+ uv = bm.loops.layers.uv.new("UVMap")
52
+ rad_scale = 1.0 / math.cos(math.pi / sides) # apothem -> vertex radius
53
+ v0, v1 = v_range or (profile[0][1], profile[-1][1])
54
+ rings = []
55
+ for r, y in profile:
56
+ if r <= 1e-6:
57
+ rings.append(None)
58
+ continue
59
+ ring = []
60
+ for i in range(sides):
61
+ th = (i + 0.5) / sides * 2 * math.pi # theta 0 = front (+Z spec)
62
+ rr = r * rad_scale
63
+ ring.append(bm.verts.new(P(rr * math.sin(th), y, rr * math.cos(th))))
64
+ rings.append(ring)
65
+
66
+ def u_of(i): # seam at the back (theta = pi)
67
+ th = (i + 0.5) / sides # fraction of turn from front
68
+ return (th + 0.5) % 1.0
69
+
70
+ for k in range(len(profile) - 1):
71
+ a, b = rings[k], rings[k + 1]
72
+ ya, yb = profile[k][1], profile[k + 1][1]
73
+ va, vb = (ya - v0) / (v1 - v0), (yb - v0) / (v1 - v0)
74
+ for i in range(sides):
75
+ j = (i + 1) % sides
76
+ ui, uj = u_of(i), u_of(j)
77
+ if uj < ui:
78
+ uj += 1.0
79
+ if a is None and b is None:
80
+ continue
81
+ if a is None: # bottom cap fan
82
+ c = bm.verts.new(P(0, ya, 0))
83
+ f = bm.faces.new((c, b[j], b[i]))
84
+ uvs = [((ui + uj) / 2, va), (uj, vb), (ui, vb)]
85
+ elif b is None:
86
+ c = bm.verts.new(P(0, yb, 0))
87
+ f = bm.faces.new((a[i], a[j], c))
88
+ uvs = [(ui, va), (uj, va), ((ui + uj) / 2, vb)]
89
+ else:
90
+ f = bm.faces.new((a[i], a[j], b[j], b[i]))
91
+ uvs = [(ui, va), (uj, va), (uj, vb), (ui, vb)]
92
+ for loop, t in zip(f.loops, uvs):
93
+ loop[uv].uv = t
94
+ bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=1e-6)
95
+ bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
96
+ obj = new_obj(name, bm)
97
+ smooth_by_angle(obj, sharp_deg)
98
+ return obj
99
+
100
+
101
+ def box(name, x0, x1, y0, y1, z0, z1):
102
+ """axis-aligned box in spec coords."""
103
+ bm = bmesh.new()
104
+ bmesh.ops.create_cube(bm, size=1.0)
105
+ for v in bm.verts:
106
+ sx = x0 if v.co.x < 0 else x1
107
+ sy = y0 if v.co.y < 0 else y1
108
+ sz = z0 if v.co.z < 0 else z1
109
+ v.co = P(sx, sy, sz)
110
+ bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
111
+ bm.loops.layers.uv.new("UVMap")
112
+ return new_obj(name, bm)
113
+
114
+
115
+ def prism_xy(name, pts, z0, z1):
116
+ """Extrude a front-profile polygon (spec x, y) between spec z0..z1."""
117
+ bm = bmesh.new()
118
+ back = [bm.verts.new(P(x, y, z0)) for x, y in pts]
119
+ front = [bm.verts.new(P(x, y, z1)) for x, y in pts]
120
+ bm.faces.new(front)
121
+ bm.faces.new(list(reversed(back)))
122
+ n = len(pts)
123
+ for i in range(n):
124
+ j = (i + 1) % n
125
+ bm.faces.new((back[i], back[j], front[j], front[i]))
126
+ bmesh.ops.recalc_face_normals(bm, faces=bm.faces)
127
+ bm.loops.layers.uv.new("UVMap")
128
+ return new_obj(name, bm)
129
+
130
+
131
+ def smooth_by_angle(obj, deg):
132
+ me = obj.data
133
+ for p in me.polygons:
134
+ p.use_smooth = True
135
+ bm = bmesh.new()
136
+ bm.from_mesh(me)
137
+ lim = math.radians(deg)
138
+ for e in bm.edges:
139
+ if len(e.link_faces) == 2:
140
+ e.smooth = e.calc_face_angle(0.0) < lim
141
+ else:
142
+ e.smooth = False
143
+ bm.to_mesh(me)
144
+ bm.free()
145
+
146
+
147
+ def bevel(obj, width, segments=3, angle=30.0):
148
+ m = obj.modifiers.new("Bevel", "BEVEL")
149
+ m.width = width
150
+ m.segments = segments
151
+ m.limit_method = "ANGLE"
152
+ m.angle_limit = math.radians(angle)
153
+ m.profile = 0.5
154
+ m.miter_outer = "MITER_ARC"
155
+ return m
156
+
157
+
158
+ def weighted(obj):
159
+ m = obj.modifiers.new("WeightedNormal", "WEIGHTED_NORMAL")
160
+ m.keep_sharp = True
161
+ m.weight = 50
162
+
163
+
164
+ def boolean_cut(obj, cutter):
165
+ m = obj.modifiers.new("Cut_" + cutter.name, "BOOLEAN")
166
+ m.operation = "DIFFERENCE"
167
+ m.object = cutter
168
+ try:
169
+ m.solver = "EXACT"
170
+ except Exception:
171
+ pass
172
+ cutter.hide_render = True
173
+ cutter.display_type = "WIRE"
174
+ cutter.hide_set(True)
175
+ # move the boolean before the bevel
176
+ while obj.modifiers.find(m.name) > 0:
177
+ with bpy.context.temp_override(object=obj):
178
+ bpy.ops.object.modifier_move_up(modifier=m.name)
179
+
180
+
181
+ def finish(obj, bevel_w, segs=3, angle=30.0):
182
+ smooth_by_angle(obj, angle)
183
+ if bevel_w > 0:
184
+ bevel(obj, bevel_w, segs, angle)
185
+ weighted(obj)
186
+
187
+
188
+ # ---------------------------------------------------------------- materials
189
+ def mat_nodes(name):
190
+ m = bpy.data.materials.new(name)
191
+ try:
192
+ m.use_nodes = True
193
+ except Exception:
194
+ pass
195
+ nt = m.node_tree
196
+ for n in list(nt.nodes):
197
+ nt.nodes.remove(n)
198
+ out = nt.nodes.new("ShaderNodeOutputMaterial")
199
+ bsdf = nt.nodes.new("ShaderNodeBsdfPrincipled")
200
+ nt.links.new(bsdf.outputs["BSDF"], out.inputs["Surface"])
201
+ return m, nt, bsdf
202
+
203
+
204
+ def mix_rgb(nt, fac, a, b, blend="MIX"):
205
+ n = nt.nodes.new("ShaderNodeMix")
206
+ n.data_type = "RGBA"
207
+ n.blend_type = blend
208
+ link_or_set(nt, n.inputs[0], fac)
209
+ link_or_set(nt, n.inputs[6], a)
210
+ link_or_set(nt, n.inputs[7], b)
211
+ return n.outputs[2]
212
+
213
+
214
+ def link_or_set(nt, sock, val):
215
+ if hasattr(val, "is_output") or isinstance(val, bpy.types.NodeSocket):
216
+ nt.links.new(val, sock)
217
+ else:
218
+ sock.default_value = val
219
+
220
+
221
+ def math_node(nt, op, a, b=None, clamp=False):
222
+ n = nt.nodes.new("ShaderNodeMath")
223
+ n.operation = op
224
+ n.use_clamp = clamp
225
+ link_or_set(nt, n.inputs[0], a)
226
+ if b is not None:
227
+ link_or_set(nt, n.inputs[1], b)
228
+ return n.outputs[0]
229
+
230
+
231
+ def map_range(nt, val, a, b, c=0.0, d=1.0):
232
+ n = nt.nodes.new("ShaderNodeMapRange")
233
+ link_or_set(nt, n.inputs["Value"], val)
234
+ n.inputs["From Min"].default_value = a
235
+ n.inputs["From Max"].default_value = b
236
+ n.inputs["To Min"].default_value = c
237
+ n.inputs["To Max"].default_value = d
238
+ n.clamp = True
239
+ return n.outputs["Result"]
240
+
241
+
242
+ def albedo_source(nt, image_file, fallback_hex, mapping="UV", scale=1.0, loc=(0.0, 0.0, 0.0)):
243
+ path = os.path.join(TEX, image_file) if image_file else None
244
+ if path and os.path.exists(path):
245
+ tex = nt.nodes.new("ShaderNodeTexImage")
246
+ tex.image = bpy.data.images.load(path, check_existing=True)
247
+ tex.interpolation = "Cubic"
248
+ if mapping == "BOX":
249
+ tc = nt.nodes.new("ShaderNodeTexCoord")
250
+ mp = nt.nodes.new("ShaderNodeMapping")
251
+ mp.inputs["Scale"].default_value = (scale, scale, scale)
252
+ mp.inputs["Location"].default_value = loc
253
+ nt.links.new(tc.outputs["Object"], mp.inputs["Vector"])
254
+ nt.links.new(mp.outputs["Vector"], tex.inputs["Vector"])
255
+ tex.projection = "BOX"
256
+ tex.projection_blend = 0.25
257
+ else:
258
+ uvn = nt.nodes.new("ShaderNodeUVMap")
259
+ uvn.uv_map = "UVMap"
260
+ nt.links.new(uvn.outputs["UV"], tex.inputs["Vector"])
261
+ tex.extension = "EXTEND" # collar (v > 1) must not wrap into the mossy bottom rows
262
+ return tex.outputs["Color"], True
263
+ return hex_rgba(fallback_hex), False
264
+
265
+
266
+ def obj_xyz(nt):
267
+ tco = nt.nodes.new("ShaderNodeTexCoord")
268
+ sep = nt.nodes.new("ShaderNodeSeparateXYZ")
269
+ nt.links.new(tco.outputs["Object"], sep.inputs[0])
270
+ return tco, sep
271
+
272
+
273
+ def column_noise(nt, tco, scale, w=None, detail=3.0):
274
+ """noise that is constant along Z (vertical columns)."""
275
+ mp = nt.nodes.new("ShaderNodeMapping")
276
+ mp.inputs["Scale"].default_value = (scale, scale, 0.0)
277
+ nt.links.new(tco.outputs["Object"], mp.inputs["Vector"])
278
+ n = nt.nodes.new("ShaderNodeTexNoise")
279
+ if w is not None:
280
+ n.noise_dimensions = "4D"
281
+ n.inputs["W"].default_value = w
282
+ n.inputs["Scale"].default_value = 1.0
283
+ n.inputs["Detail"].default_value = detail
284
+ nt.links.new(mp.outputs["Vector"], n.inputs["Vector"])
285
+ return n.outputs["Fac"]
286
+
287
+
288
+ def noise1d(nt, w, scale=1.0, detail=1.0):
289
+ n = nt.nodes.new("ShaderNodeTexNoise")
290
+ n.noise_dimensions = "1D"
291
+ n.inputs["Scale"].default_value = scale
292
+ n.inputs["Detail"].default_value = detail
293
+ link_or_set(nt, n.inputs["W"], w)
294
+ return n.outputs["Fac"]
295
+
296
+
297
+ def clamp01(nt, v):
298
+ return math_node(nt, "MINIMUM", math_node(nt, "MAXIMUM", v, 0.0), 1.0)
299
+
300
+
301
+ def vmath(nt, op, a, b=None, scale=None):
302
+ n = nt.nodes.new("ShaderNodeVectorMath")
303
+ n.operation = op
304
+ link_or_set(nt, n.inputs[0], a)
305
+ if b is not None:
306
+ link_or_set(nt, n.inputs[1], b)
307
+ if scale is not None:
308
+ link_or_set(nt, n.inputs["Scale"], scale)
309
+ return n.outputs["Value"] if op in ("DOT_PRODUCT", "LENGTH", "DISTANCE") else n.outputs["Vector"]
310
+
311
+
312
+ def noise3(nt, vec, sx, sy=None, sz=None, detail=2.0, rough=0.5, out="Fac", loc=(0.0, 0.0, 0.0)):
313
+ """3D noise of a vector socket with per-axis scale (1/m)."""
314
+ mp = nt.nodes.new("ShaderNodeMapping")
315
+ mp.inputs["Scale"].default_value = (sx, sy if sy is not None else sx, sz if sz is not None else sx)
316
+ mp.inputs["Location"].default_value = loc
317
+ link_or_set(nt, mp.inputs["Vector"], vec)
318
+ n = nt.nodes.new("ShaderNodeTexNoise")
319
+ n.inputs["Scale"].default_value = 1.0
320
+ n.inputs["Detail"].default_value = detail
321
+ n.inputs["Roughness"].default_value = rough
322
+ nt.links.new(mp.outputs["Vector"], n.inputs["Vector"])
323
+ return n.outputs[out]
324
+
325
+
326
+ def edge_mask(nt, geo, radius, a=0.998, b=0.93, samples=8):
327
+ """convex/concave edge proximity from the Bevel node: 1 on the edge, 0 farther than ~radius."""
328
+ bev = nt.nodes.new("ShaderNodeBevel")
329
+ bev.samples = samples
330
+ bev.inputs["Radius"].default_value = radius
331
+ d = vmath(nt, "DOT_PRODUCT", bev.outputs["Normal"], geo.outputs["Normal"])
332
+ return map_range(nt, d, a, b)
333
+
334
+
335
+ def soft_gt(nt, a, b, k=2000.0):
336
+ """1 where a > b (sharp step), both may be sockets."""
337
+ return math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", a, b), k, clamp=True)
338
+
339
+
340
+ def flakes(nt, wco, cell, aniso, prob, erode=(0.1, 0.3), seed=0.0, rim_w=0.05):
341
+ """Paint flakes (r1): Voronoi cells (vertically elongated by `aniso`), a random subset (cell value < prob) is eroded
342
+ to an island by distance-to-edge + fine noise -> irregular, ragged, polygonal flakes, no round blobs.
343
+ wco: warped object coords. returns (chip, rim_outer) masks."""
344
+ mp = nt.nodes.new("ShaderNodeMapping")
345
+ mp.inputs["Scale"].default_value = (1.0 / cell, 1.0 / cell, 1.0 / (cell * aniso))
346
+ mp.inputs["Location"].default_value = (seed * 7.13, seed * 3.31, seed * 5.71)
347
+ link_or_set(nt, mp.inputs["Vector"], wco)
348
+ v1 = nt.nodes.new("ShaderNodeTexVoronoi")
349
+ v1.feature = "F1"
350
+ v1.inputs["Scale"].default_value = 1.0
351
+ nt.links.new(mp.outputs["Vector"], v1.inputs["Vector"])
352
+ v2 = nt.nodes.new("ShaderNodeTexVoronoi")
353
+ v2.feature = "DISTANCE_TO_EDGE"
354
+ v2.inputs["Scale"].default_value = 1.0
355
+ nt.links.new(mp.outputs["Vector"], v2.inputs["Vector"])
356
+ sc = nt.nodes.new("ShaderNodeSeparateColor")
357
+ nt.links.new(v1.outputs["Color"], sc.inputs["Color"])
358
+ sel = soft_gt(nt, prob, sc.outputs[0])
359
+ er = math_node(nt, "ADD", math_node(nt, "MULTIPLY", sc.outputs[1], erode[1] - erode[0]), erode[0])
360
+ fine = noise3(nt, mp.outputs["Vector"], 4.0, detail=4.0, rough=0.65, loc=(seed, 0, 0))
361
+ thr = math_node(nt, "ADD", er, math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", fine, 0.5), 0.45))
362
+ dist = v2.outputs["Distance"]
363
+ chip = math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", dist, thr), 40.0, clamp=True), sel)
364
+ rim = math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", math_node(nt, "ADD", dist, rim_w), thr), 40.0, clamp=True), sel)
365
+ return chip, rim
366
+
367
+
368
+ def facet_point(k, s, z, r=None):
369
+ """point on body facet k (0 front, 2 right +X, 4 back, 6 left -X), lateral offset s (toward increasing k), height z."""
370
+ r = r or SPEC["body"]["radius"]
371
+ ph = math.radians(45.0 * k)
372
+ n = Vector((math.sin(ph), -math.cos(ph), 0.0))
373
+ t = Vector((math.cos(ph), math.sin(ph), 0.0))
374
+ p = n * r + t * s
375
+ return (p.x, p.y, z)
376
+
377
+
378
+ def spot_masks(nt, tco, spots):
379
+ """hand-placed large flakes (r1): ellipses w x h (m) with a noise-warped ragged outline + a halo for satellite flecks."""
380
+ rag = noise3(nt, tco.outputs["Object"], 28.0, detail=8.0, rough=0.7)
381
+ rag2 = noise3(nt, tco.outputs["Object"], 110.0, detail=3.0, rough=0.6, loc=(3.0, 1.0, 2.0))
382
+ chip, halo, rim = None, None, None
383
+ for (k, s, z, w, h) in spots:
384
+ cx, cy, cz = facet_point(k, s, z)
385
+ dv = vmath(nt, "SUBTRACT", tco.outputs["Object"], (cx, cy, cz))
386
+ dv = vmath(nt, "MULTIPLY", dv, (2.0 / w, 2.0 / w, 2.0 / h))
387
+ e = vmath(nt, "LENGTH", dv)
388
+ e = math_node(nt, "ADD", e, math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", rag, 0.5), 1.9))
389
+ e = math_node(nt, "ADD", e, math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", rag2, 0.5), 0.9))
390
+ c = map_range(nt, e, 0.92, 0.88)
391
+ hl = map_range(nt, e, 1.9, 1.2)
392
+ rm = map_range(nt, e, 0.92 + 0.004 / w, 0.92 + 0.0015 / w) # ~1.5 mm rust rim outside the flake
393
+ rim = rm if rim is None else math_node(nt, "MAXIMUM", rim, rm)
394
+ chip = c if chip is None else math_node(nt, "MAXIMUM", chip, c)
395
+ halo = hl if halo is None else math_node(nt, "MAXIMUM", halo, hl)
396
+ return chip, halo, rim
397
+
398
+
399
+ def drips(nt, tco, sep, sources):
400
+ """Rust drip CLUSTERS: a dark stain at the source ~2-2.5x wider than the drip, then 1-5 thin strands whose lengths
401
+ follow a (skewed) bell curve across the cluster. sources: dicts z, maxlen, seed, [xhalf, cell, active, stain, radius,
402
+ ymax]. xhalf: front-only source of that half-width (cells along X). returns (all, stain)."""
403
+ ang = math_node(nt, "ARCTAN2", sep.outputs["Y"], sep.outputs["X"])
404
+ # seam of the angle at a facet edge (atan2 = -157.5 deg), not across a facet
405
+ ang = math_node(nt, "FLOORED_MODULO", math_node(nt, "ADD", ang, math.radians(157.5)), 2 * math.pi)
406
+ total, stain_t = None, None
407
+ for S_ in sources:
408
+ src, maxlen, seed = S_["z"], S_["maxlen"], S_["seed"]
409
+ xhalf = S_.get("xhalf")
410
+ cell = S_.get("cell", 0.06)
411
+ if xhalf:
412
+ s_ = math_node(nt, "ADD", sep.outputs["X"], S_.get("xoff", 0.0))
413
+ else:
414
+ rad = S_.get("radius", SPEC["body"]["radius"])
415
+ n_cells = max(1, round(2 * math.pi * rad / cell))
416
+ cell = 2 * math.pi * rad / n_cells # whole number of cells round the box: no half cluster at the seam
417
+ s_ = math_node(nt, "MULTIPLY", ang, rad)
418
+ k = math_node(nt, "FLOOR", math_node(nt, "DIVIDE", s_, cell))
419
+ u = math_node(nt, "FRACT", math_node(nt, "DIVIDE", s_, cell))
420
+ wn = nt.nodes.new("ShaderNodeTexWhiteNoise")
421
+ wn.noise_dimensions = "1D"
422
+ nt.links.new(math_node(nt, "ADD", k, seed * 31.7), wn.inputs["W"])
423
+ sc = nt.nodes.new("ShaderNodeSeparateColor")
424
+ nt.links.new(wn.outputs["Color"], sc.inputs["Color"])
425
+ r1, r2, r3, r4 = sc.outputs[0], sc.outputs[1], sc.outputs[2], wn.outputs["Value"]
426
+ act = S_.get("active", 0.5)
427
+ active = map_range(nt, r1, act + 0.01, act)
428
+ wn2 = nt.nodes.new("ShaderNodeTexWhiteNoise")
429
+ wn2.noise_dimensions = "1D"
430
+ nt.links.new(math_node(nt, "ADD", k, seed * 31.7 + 0.5), wn2.inputs["W"])
431
+ size = math_node(nt, "ADD", math_node(nt, "MULTIPLY", wn2.outputs["Value"], 0.45), 0.55) # per-cluster 0.55-1
432
+ c = math_node(nt, "ADD", math_node(nt, "MULTIPLY", r2, 0.4), 0.3) # cluster centre in the cell
433
+ hw = math_node(nt, "MULTIPLY", math_node(nt, "ADD", math_node(nt, "MULTIPLY", r3, 0.12), 0.2), math_node(nt, "ADD", math_node(nt, "MULTIPLY", size, 0.5), 0.5))
434
+ t = math_node(nt, "DIVIDE", math_node(nt, "SUBTRACT", u, c), hw)
435
+ tt = math_node(nt, "SUBTRACT", t, math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", r4, 0.5), 0.7)) # skew
436
+ env = math_node(nt, "EXPONENT", math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", tt, tt), -2.4))
437
+ in_group = map_range(nt, math_node(nt, "ABSOLUTE", t), 1.0, 0.75)
438
+ d = math_node(nt, "SUBTRACT", src, sep.outputs["Z"]) # metres below the source
439
+ below = map_range(nt, d, -0.001, 0.002)
440
+ hs = math_node(nt, "MULTIPLY", math_node(nt, "ADD", math_node(nt, "MULTIPLY", r4, 0.014), 0.012), S_.get("stain", 1.0)) # 12-26 mm
441
+ stain = clamp01(nt, math_node(nt, "DIVIDE", math_node(nt, "SUBTRACT", hs, d), math_node(nt, "MULTIPLY", hs, 0.7)))
442
+ stain = math_node(nt, "MULTIPLY", stain, map_range(nt, math_node(nt, "ABSOLUTE", t), 1.0, 0.8)) # soft sides
443
+ # stain edge is not a ruler line: break it with noise
444
+ sn = noise3(nt, tco.outputs["Object"], 90.0, detail=3.0, loc=(seed, 0, 0))
445
+ stain = math_node(nt, "MULTIPLY", stain, map_range(nt, sn, 0.3, 0.5, 0.35, 1.0))
446
+ # strands: 2K slots across the cluster (K 1-2.2 per cluster), each a ~3 mm line with its own length, slight wiggle
447
+ wn3 = nt.nodes.new("ShaderNodeTexWhiteNoise")
448
+ wn3.noise_dimensions = "1D"
449
+ nt.links.new(math_node(nt, "ADD", k, seed * 31.7 + 0.25), wn3.inputs["W"])
450
+ K = math_node(nt, "ADD", math_node(nt, "MULTIPLY", wn3.outputs["Value"], 1.0), 0.75)
451
+ wig = math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", noise1d(nt, math_node(nt, "MULTIPLY", sep.outputs["Z"], 35.0), 1.0, 2.0), 0.5), 0.35)
452
+ ts = math_node(nt, "ADD", math_node(nt, "ADD", math_node(nt, "MULTIPLY", t, K), r2), wig)
453
+ sid = math_node(nt, "FLOOR", ts)
454
+ sf = math_node(nt, "FRACT", ts)
455
+ wn4 = nt.nodes.new("ShaderNodeTexWhiteNoise")
456
+ wn4.noise_dimensions = "1D"
457
+ nt.links.new(math_node(nt, "ADD", math_node(nt, "ADD", sid, math_node(nt, "MULTIPLY", k, 7.3)), seed * 3.1), wn4.inputs["W"])
458
+ rs = wn4.outputs["Value"]
459
+ jitter = map_range(nt, rs, 0.0, 1.0, 0.6, 1.0)
460
+ strand_on = map_range(nt, rs, 0.12, 0.16) # some slots stay empty
461
+ length = math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", env, jitter), maxlen), size)
462
+ rel = clamp01(nt, math_node(nt, "DIVIDE", d, math_node(nt, "MAXIMUM", length, 0.003)))
463
+ hwm = math_node(nt, "MULTIPLY", hw, cell) # cluster half-width, m
464
+ wfrac = math_node(nt, "DIVIDE", math_node(nt, "MULTIPLY", K, 0.0045), hwm) # strand half-width 4.5 mm as slot fraction
465
+ wfrac = math_node(nt, "MINIMUM", wfrac, S_.get("wcap", 0.42)) # narrow clusters: strands stay lines, not wedges
466
+ wfrac = math_node(nt, "MULTIPLY", wfrac, map_range(nt, rel, 0.0, 1.0, 1.0, 0.8)) # slightly narrower to the tip
467
+ dc = math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sf, 0.5))
468
+ strands = clamp01(nt, math_node(nt, "DIVIDE", math_node(nt, "SUBTRACT", wfrac, dc), math_node(nt, "MULTIPLY", wfrac, 0.3)))
469
+ on = clamp01(nt, math_node(nt, "DIVIDE", math_node(nt, "SUBTRACT", length, d), 0.008))
470
+ fade = map_range(nt, rel, 0.0, 1.0, 1.0, 0.75)
471
+ strand = math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", strands, on), fade), strand_on)
472
+ gate = math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", active, below), in_group)
473
+ if xhalf:
474
+ ax = math_node(nt, "ABSOLUTE", sep.outputs["X"])
475
+ gate = math_node(nt, "MULTIPLY", gate, map_range(nt, ax, xhalf, xhalf - 0.008))
476
+ gate = math_node(nt, "MULTIPLY", gate, map_range(nt, sep.outputs["Y"], S_.get("ymax", 0.0), S_.get("ymax", 0.0) - 0.008)) # front = -Y
477
+ stain = math_node(nt, "MULTIPLY", stain, gate)
478
+ v = math_node(nt, "MULTIPLY", math_node(nt, "MAXIMUM", stain, strand), gate)
479
+ total = v if total is None else math_node(nt, "MAXIMUM", total, v)
480
+ stain_t = stain if stain_t is None else math_node(nt, "MAXIMUM", stain_t, stain)
481
+ return clamp01(nt, total), clamp01(nt, stain_t)
482
+
483
+
484
+ def rust_bands(nt, tco, sep, bands):
485
+ """rusty grime in grooves / under lips (r1): horizontal bands (z, below, above, strength), patchy and streaky."""
486
+ patch = map_range(nt, noise3(nt, tco.outputs["Object"], 22.0, 22.0, 7.0, detail=4.0, rough=0.6), 0.33, 0.52)
487
+ smudge = map_range(nt, noise3(nt, tco.outputs["Object"], 75.0, 75.0, 40.0, detail=3.0, rough=0.6, loc=(1.0, 2.0, 3.0)), 0.38, 0.55, 0.35, 1.0)
488
+ patch = math_node(nt, "MULTIPLY", patch, smudge)
489
+ total = None
490
+ for z, below, above, k in bands:
491
+ dz = math_node(nt, "SUBTRACT", sep.outputs["Z"], z)
492
+ m = math_node(nt, "MULTIPLY", map_range(nt, dz, -below - 0.004, -below), map_range(nt, dz, above + 0.004, above))
493
+ m = math_node(nt, "MULTIPLY", m, k)
494
+ total = m if total is None else math_node(nt, "MAXIMUM", total, m)
495
+ return math_node(nt, "MULTIPLY", total, patch)
496
+
497
+
498
+ def rect_groove(nt, sep, cx, cz, hx, hz, width, ymax=0.0):
499
+ """band just inside a front rectangle (|X| < hx, |Z - cz| < hz): the groove between door frame and panel."""
500
+ dx = math_node(nt, "SUBTRACT", hx, math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sep.outputs["X"], cx)))
501
+ dz = math_node(nt, "SUBTRACT", hz, math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sep.outputs["Z"], cz)))
502
+ dd = math_node(nt, "MINIMUM", dx, dz)
503
+ g = math_node(nt, "MULTIPLY", map_range(nt, dd, -0.003, 0.0), map_range(nt, dd, width, width * 0.4))
504
+ # post-r3: rust gathers in the corners of the groove (blobs up to ~3 cm), not an even dark frame
505
+ corner = map_range(nt, math_node(nt, "MAXIMUM", dx, dz), 0.04, 0.012)
506
+ g = math_node(nt, "MAXIMUM", g, math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", corner, map_range(nt, dd, 0.026, 0.0)),
507
+ map_range(nt, dd, -0.003, 0.0)))
508
+ return math_node(nt, "MULTIPLY", g, map_range(nt, sep.outputs["Y"], ymax, ymax - 0.005))
509
+
510
+
511
+ def rect_mask(nt, sep, cx, cz, hx, hz, soft=0.004):
512
+ dx = math_node(nt, "SUBTRACT", hx, math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sep.outputs["X"], cx)))
513
+ dz = math_node(nt, "SUBTRACT", hz, math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sep.outputs["Z"], cz)))
514
+ return map_range(nt, math_node(nt, "MINIMUM", dx, dz), 0.0, soft)
515
+
516
+
517
+ def weathered_paint(name, image_file, mapping="UV", scale=1.0, loc=(0.0, 0.0, 0.0), base_hex=None, bump=0.0,
518
+ clean_moss=False, facets=0,
519
+ small=(0.03, 0.2), medium=(0.01, 0.05), cells=(0.016, 0.034), near_r=0.025, spots=None,
520
+ edge_wear=0.6, wear_r=0.01, wear_gate=(14.0, 0.5), ring_zone=None, wear_mod=None, prob_mod=None,
521
+ drip_sources=None, bands=None, groove=None, crevice_rust=0.5, facet_grime=0.0,
522
+ wear_ab=(0.999, 0.9), near_ab=(0.9995, 0.985), fleck=0.15, dirt=0.2, ridge=None, wash=None, moss=None, ring_p=(0.0, 0.0), ao_mask=None, ring_wear=0.8, metal_thr=0.88,
523
+ groove_hex="#5E2914", clean_rust=False, floor=None):
524
+ """small / medium: (base probability, extra probability at edges) of Voronoi flakes (cells: cell size m).
525
+ wear_gate: (scale 1/m, coverage) of the interruptions along worn edges."""
526
+ pal = SPEC["palette"]
527
+ m, nt, bsdf = mat_nodes(name)
528
+ col_src, has_tex = albedo_source(nt, image_file, base_hex or pal["paint"], mapping, scale, loc)
529
+ color = col_src
530
+ if has_tex and clean_rust:
531
+ # the codex texture's own rust streaks (V < ~0.7) would show as a brown smear on the hood
532
+ hsv3 = nt.nodes.new("ShaderNodeSeparateColor")
533
+ hsv3.mode = "HSV"
534
+ nt.links.new(col_src, hsv3.inputs["Color"])
535
+ color = mix_rgb(nt, map_range(nt, hsv3.outputs[2], 0.74, 0.66), color, hex_rgba("#D45C40"))
536
+ if has_tex and clean_moss:
537
+ # no mould on the cap / slot box / door: replace olive-green texels with paint red
538
+ hsv = nt.nodes.new("ShaderNodeSeparateColor")
539
+ hsv.mode = "HSV"
540
+ nt.links.new(col_src, hsv.inputs["Color"])
541
+ green = math_node(nt, "MULTIPLY", map_range(nt, hsv.outputs[0], 0.038, 0.05), map_range(nt, hsv.outputs[0], 0.45, 0.4))
542
+ green = math_node(nt, "MULTIPLY", green, map_range(nt, hsv.outputs[1], 0.08, 0.18))
543
+ color = mix_rgb(nt, green, color, hex_rgba(pal["paint"]))
544
+ if has_tex:
545
+ # r1: the codex texture carries its own small round beige dots (evenly spread "stamps"): paint them over,
546
+ # chips come only from the procedural flakes below. beige = low saturation, red/orange hue (moss is hue > 0.12)
547
+ hsv2 = nt.nodes.new("ShaderNodeSeparateColor")
548
+ hsv2.mode = "HSV"
549
+ nt.links.new(col_src, hsv2.inputs["Color"])
550
+ beige = math_node(nt, "MULTIPLY", map_range(nt, hsv2.outputs[1], 0.63, 0.53), map_range(nt, hsv2.outputs[0], 0.13, 0.11))
551
+ beige = math_node(nt, "MULTIPLY", beige, map_range(nt, hsv2.outputs[2], 0.55, 0.65))
552
+ color = mix_rgb(nt, beige, color, hex_rgba(pal["paint"]))
553
+ # concept paint is darker / more orange than the codex texture
554
+ color = mix_rgb(nt, 1.0, color, hex_rgba("#F0E2C8"), "MULTIPLY")
555
+ tco, sep = obj_xyz(nt)
556
+ geo = nt.nodes.new("ShaderNodeNewGeometry")
557
+ ang = math_node(nt, "ARCTAN2", sep.outputs["Y"], sep.outputs["X"])
558
+ if facets:
559
+ # +-10% brightness per facet. r1 fix: facet edges are at atan2 = 22.5 + 45k deg (index used to change mid-facet)
560
+ idx = math_node(nt, "FLOOR", math_node(nt, "ADD", math_node(nt, "MULTIPLY", ang, facets / (2 * math.pi)), 0.5))
561
+ wn = nt.nodes.new("ShaderNodeTexWhiteNoise")
562
+ wn.noise_dimensions = "1D"
563
+ nt.links.new(idx, wn.inputs["W"])
564
+ k = map_range(nt, wn.outputs["Value"], 0.0, 1.0, 0.92, 1.08)
565
+ kc = nt.nodes.new("ShaderNodeCombineColor")
566
+ for i in range(3):
567
+ nt.links.new(k, kc.inputs[i])
568
+ color = mix_rgb(nt, 1.0, color, kc.outputs["Color"], "MULTIPLY")
569
+ if facet_grime > 0:
570
+ # concept: dark rusty dirt collects along the vertical facet edges, streaky
571
+ q = math_node(nt, "DIVIDE", math_node(nt, "SUBTRACT", ang, math.radians(22.5)), math.radians(45.0))
572
+ fd = math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", q, math_node(nt, "ROUND", q)))
573
+ dist_m = math_node(nt, "MULTIPLY", fd, 2 * SPEC["body"]["radius"] * math.tan(math.pi / 8))
574
+ streak = map_range(nt, noise3(nt, tco.outputs["Object"], 45.0, 45.0, 1.6, detail=3.0), 0.47, 0.62)
575
+ fg = math_node(nt, "MULTIPLY", map_range(nt, dist_m, 0.016, 0.004), streak)
576
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", fg, facet_grime), color, hex_rgba("#7A3A1E"))
577
+ if dirt > 0:
578
+ # concept paint is dustier (L 45, a 33 vs 50, 40): mottled grey-brown dirt film
579
+ dn = map_range(nt, noise3(nt, tco.outputs["Object"], 7.0, 7.0, 3.0, detail=5.0, rough=0.6, loc=(9.0, 0, 0)), 0.3, 0.7, 0.1, 1.0)
580
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", dn, dirt), color, hex_rgba("#76604F"))
581
+ # warped coordinates: ragged flake outlines
582
+ wv = noise3(nt, tco.outputs["Object"], 22.0, detail=3.0, out="Color")
583
+ wco = vmath(nt, "ADD", tco.outputs["Object"], vmath(nt, "SCALE", vmath(nt, "SUBTRACT", wv, (0.5, 0.5, 0.5)), scale=0.012))
584
+ near = edge_mask(nt, geo, near_r, *near_ab)
585
+ ridge_e = None
586
+ if ridge:
587
+ # r1: the pyramid ridges are only ~18 deg: the Bevel-node mask barely sees them -> analytic distance to the ridge
588
+ z0, z1, a0, a1 = ridge
589
+ q = math_node(nt, "DIVIDE", math_node(nt, "SUBTRACT", ang, math.radians(22.5)), math.radians(45.0))
590
+ fd = math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", q, math_node(nt, "ROUND", q)))
591
+ ap = map_range(nt, sep.outputs["Z"], z0, z1, a0, a1)
592
+ dist_r = math_node(nt, "MULTIPLY", fd, math_node(nt, "MULTIPLY", ap, 2 * math.tan(math.pi / 8)))
593
+ zone = math_node(nt, "MULTIPLY", map_range(nt, sep.outputs["Z"], z0 - 0.002, z0 + 0.004), map_range(nt, sep.outputs["Z"], z1 - 0.03, z1 - 0.055))
594
+ near = math_node(nt, "MAXIMUM", near, math_node(nt, "MULTIPLY", map_range(nt, dist_r, 0.022, 0.004), zone))
595
+ ridge_e = math_node(nt, "MULTIPLY", map_range(nt, dist_r, 0.009, 0.002), zone)
596
+ # dirt settles in a thin line beside each ridge (concept: ridges read as dark + light lines). red-brown, no green
597
+ rg = math_node(nt, "MULTIPLY", map_range(nt, dist_r, 0.016, 0.009), map_range(nt, dist_r, 0.003, 0.007))
598
+ rgn = map_range(nt, noise3(nt, tco.outputs["Object"], 30.0, detail=3.0, loc=(2.0, 0, 0)), 0.35, 0.55, 0.3, 1.0)
599
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", rg, zone), math_node(nt, "MULTIPLY", rgn, 0.55)), color, hex_rgba("#7A3519"))
600
+ ring = 0.0
601
+ if ring_zone: # the mid ring is the most worn belt
602
+ rc, rh = (ring_zone[0] + ring_zone[1]) / 2, (ring_zone[1] - ring_zone[0]) / 2
603
+ ring = map_range(nt, math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sep.outputs["Z"], rc)), rh + 0.004, rh)
604
+ # chips cluster: some areas are sound, some are flaking
605
+ cluster = map_range(nt, noise3(nt, tco.outputs["Object"], 4.0, detail=2.0, loc=(11.0, 0, 0)), 0.38, 0.62, 0.35, 1.65)
606
+ halo = 0.0
607
+ spot_chip = None
608
+ if spots:
609
+ spot_chip, halo, spot_rim = spot_masks(nt, tco, spots)
610
+ nr = math_node(nt, "MAXIMUM", near, ring) if not isinstance(ring, float) else near
611
+
612
+ def prob(base, extra, halo_k, ring_k=0.0):
613
+ p = math_node(nt, "ADD", base, math_node(nt, "MULTIPLY", nr, extra))
614
+ if ring_k and not isinstance(ring, float): # chips sit on the mid ring lips
615
+ p = math_node(nt, "ADD", p, math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", ring, near), ring_k))
616
+ p = math_node(nt, "MULTIPLY", p, cluster)
617
+ if not isinstance(halo, float):
618
+ p = math_node(nt, "ADD", p, math_node(nt, "MULTIPLY", halo, halo_k))
619
+ if prob_mod is not None:
620
+ p = math_node(nt, "MULTIPLY", p, prob_mod(nt, sep))
621
+ return p
622
+
623
+ chip, rim = flakes(nt, wco, cells[0], 1.5, prob(small[0], small[1], 0.12, ring_p[0]), (0.1, 0.32), seed=1.0)
624
+ if medium[0] > 0 or medium[1] > 0:
625
+ c2, r2_ = flakes(nt, wco, cells[1], 1.5, prob(medium[0], medium[1], 0.0, ring_p[1]), (0.06, 0.22), seed=2.0)
626
+ chip, rim = math_node(nt, "MAXIMUM", chip, c2), math_node(nt, "MAXIMUM", rim, r2_)
627
+ # tiny flecks (3-6 mm) next to chips
628
+ c3, r3_ = flakes(nt, wco, 0.007, 1.3, prob(small[0] * fleck, small[1] * fleck, 0.15), (0.12, 0.3), seed=3.0, rim_w=0.08)
629
+ chip, rim = math_node(nt, "MAXIMUM", chip, c3), math_node(nt, "MAXIMUM", rim, r3_)
630
+ if spot_chip is not None:
631
+ chip = math_node(nt, "MAXIMUM", chip, spot_chip)
632
+ rim = math_node(nt, "MAXIMUM", rim, spot_rim)
633
+ # edge wear: interrupted strip of undercoat on edges, bare metal in its core
634
+ worn, metal = 0.0, 0.0
635
+ if edge_wear > 0:
636
+ e2 = edge_mask(nt, geo, wear_r, *wear_ab)
637
+ if ridge_e is not None:
638
+ e2 = math_node(nt, "MAXIMUM", e2, ridge_e)
639
+ gsc, cov = wear_gate
640
+ gs3 = gsc if isinstance(gsc, tuple) else (gsc, gsc, gsc)
641
+ g = noise3(nt, tco.outputs["Object"], *gs3, detail=2.0, loc=(5.0, 7.0, 1.0))
642
+ t0 = 0.5 + (0.5 - cov) * 0.28
643
+ gate = map_range(nt, g, t0 - 0.015, t0 + 0.015)
644
+ strength = math_node(nt, "MULTIPLY", math_node(nt, "ADD", math_node(nt, "MULTIPLY", ring, ring_wear), 1.0), edge_wear) if not isinstance(ring, float) else edge_wear
645
+ if wear_mod is not None:
646
+ strength = math_node(nt, "MULTIPLY", strength, wear_mod(nt, sep))
647
+ rag = noise3(nt, tco.outputs["Object"], 160.0, detail=3.0, rough=0.6, loc=(2.0, 2.0, 2.0))
648
+ ev = math_node(nt, "ADD", math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", e2, gate), strength),
649
+ math_node(nt, "MULTIPLY", math_node(nt, "SUBTRACT", rag, 0.5), 0.9))
650
+ worn = map_range(nt, ev, 0.5, 0.54)
651
+ metal = map_range(nt, ev, metal_thr, metal_thr + 0.07)
652
+ rim = math_node(nt, "MAXIMUM", rim, math_node(nt, "MULTIPLY", map_range(nt, ev, 0.44, 0.5), 0.6))
653
+ chip = math_node(nt, "MAXIMUM", chip, worn)
654
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", rim, 0.65), color, hex_rgba(pal.get("chip_rim", "#9A5236")))
655
+ # undercoat with a little tonal variation
656
+ uc = mix_rgb(nt, map_range(nt, noise3(nt, tco.outputs["Object"], 60.0, detail=3.0), 0.35, 0.65), hex_rgba("#D9BCA2"), hex_rgba("#CDAA8E"))
657
+ color = mix_rgb(nt, chip, color, uc)
658
+ if not isinstance(metal, float):
659
+ color = mix_rgb(nt, metal, color, hex_rgba("#6E625A"))
660
+ # rust: grooves / lips, crevices (AO), drips
661
+ if moss:
662
+ # olive mould/dirt in patches over the bottom 5-8 cm of the body only
663
+ mz = map_range(nt, sep.outputs["Z"], moss + 0.075, moss + 0.01)
664
+ mn = noise3(nt, tco.outputs["Object"], 16.0, 16.0, 22.0, detail=6.0, rough=0.65, loc=(4.0, 0, 0))
665
+ mm = map_range(nt, math_node(nt, "ADD", mn, math_node(nt, "MULTIPLY", mz, 0.1)), 0.62, 0.66)
666
+ mm = math_node(nt, "MULTIPLY", mm, map_range(nt, sep.outputs["Z"], moss + 0.09, moss + 0.06))
667
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", mm, 0.85), color, hex_rgba("#6C6A3A"))
668
+ if floor is not None: # lower lip of the slot: lit, worn surface
669
+ fl = floor(nt, sep)
670
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", fl, 0.85), color, hex_rgba("#D99A7C"))
671
+ if bands:
672
+ rb = rust_bands(nt, tco, sep, bands)
673
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", rb, 0.85), color, hex_rgba("#6A2E15"))
674
+ if groove:
675
+ gr = rect_groove(nt, sep, *groove)
676
+ gp = map_range(nt, noise3(nt, tco.outputs["Object"], 70.0, detail=3.0), 0.3, 0.55, 0.6, 1.0)
677
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", gr, gp), color, hex_rgba(groove_hex))
678
+ ao = nt.nodes.new("ShaderNodeAmbientOcclusion")
679
+ ao.inputs["Distance"].default_value = 0.04
680
+ ao.samples = 16
681
+ ao_out = ao.outputs["AO"]
682
+ if ao_mask is not None: # surfaces that must stay readable (slot lip floor): no crevice darkening there
683
+ ao_out = math_node(nt, "MAXIMUM", ao_out, ao_mask(nt, sep))
684
+ if crevice_rust > 0:
685
+ crev = map_range(nt, ao_out, 0.9, 0.55)
686
+ cp_ = map_range(nt, noise3(nt, tco.outputs["Object"], 35.0, 35.0, 12.0, detail=3.0), 0.35, 0.6, 0.4, 1.0)
687
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", crev, cp_), crevice_rust), color, hex_rgba("#6A2E15"))
688
+ if wash:
689
+ # faint rusty wash streaks below the main sources (low contrast, fading over ~15 cm): concept's streaky look
690
+ wn_ = map_range(nt, noise3(nt, tco.outputs["Object"], 55.0, 55.0, 1.2, detail=3.0, loc=(0, 4.0, 0)), 0.46, 0.6)
691
+ wsum = None
692
+ for z_, ln_, k_ in wash:
693
+ dz_ = math_node(nt, "SUBTRACT", z_, sep.outputs["Z"])
694
+ w1 = math_node(nt, "MULTIPLY", map_range(nt, dz_, -0.001, 0.004), map_range(nt, dz_, 0.0, ln_, 1.0, 0.0))
695
+ w1 = math_node(nt, "MULTIPLY", w1, k_)
696
+ wsum = w1 if wsum is None else math_node(nt, "MAXIMUM", wsum, w1)
697
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", wsum, wn_), color, hex_rgba("#8A4220"))
698
+ if drip_sources:
699
+ dr, st = drips(nt, tco, sep, drip_sources)
700
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", dr, 0.95), color, hex_rgba("#7E3516"))
701
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", st, 0.9), color, hex_rgba("#62290F"))
702
+ if os.environ.get("DEBUG_DRIPS"):
703
+ color = mix_rgb(nt, dr, (0, 0, 0, 1), (1, 1, 1, 1))
704
+ if os.environ.get("DEBUG_CHIPS"):
705
+ color = mix_rgb(nt, chip, (0, 0, 0, 1), (1, 1, 1, 1))
706
+ if os.environ.get("DEBUG_EDGE"): # R = near-edge mask, G = edge-wear mask
707
+ dbg = nt.nodes.new("ShaderNodeCombineColor")
708
+ nt.links.new(near, dbg.inputs[0])
709
+ if edge_wear > 0:
710
+ nt.links.new(e2, dbg.inputs[1])
711
+ color = dbg.outputs["Color"]
712
+ # crevice darkening from AO (no green: moss lives only in the texture's bottom rows and on the base)
713
+ grime = map_range(nt, ao_out, 0.95, 0.45, 0.0, 0.25)
714
+ color = mix_rgb(nt, grime, color, hex_rgba("#6B2A1A"), "MULTIPLY")
715
+ nt.links.new(color, bsdf.inputs["Base Color"])
716
+ rough = map_range(nt, chip, 0.0, 1.0, 0.68, 0.86)
717
+ if not isinstance(metal, float):
718
+ rough = math_node(nt, "SUBTRACT", rough, math_node(nt, "MULTIPLY", metal, 0.35))
719
+ nt.links.new(math_node(nt, "MULTIPLY", metal, 0.6), bsdf.inputs["Metallic"])
720
+ link_or_set(nt, bsdf.inputs["Roughness"], rough)
721
+ if has_tex and bump > 0:
722
+ bw = nt.nodes.new("ShaderNodeRGBToBW")
723
+ nt.links.new(col_src, bw.inputs[0])
724
+ bp = nt.nodes.new("ShaderNodeBump")
725
+ bp.inputs["Strength"].default_value = bump
726
+ bp.inputs["Distance"].default_value = 0.002
727
+ nt.links.new(bw.outputs[0], bp.inputs["Height"])
728
+ nt.links.new(bp.outputs["Normal"], bsdf.inputs["Normal"])
729
+ # paint thickness: chips are a step down (subtle bump on the chip outline)
730
+ if not isinstance(chip, float):
731
+ bp2 = nt.nodes.new("ShaderNodeBump")
732
+ bp2.inputs["Strength"].default_value = 0.35
733
+ bp2.inputs["Distance"].default_value = 0.0006
734
+ nt.links.new(math_node(nt, "SUBTRACT", 1.0, chip), bp2.inputs["Height"])
735
+ nt.links.new(bp2.outputs["Normal"], bsdf.inputs["Normal"])
736
+ return m
737
+
738
+
739
+ def concrete_mat():
740
+ pal = SPEC["palette"]
741
+ m, nt, bsdf = mat_nodes("concrete")
742
+ color, has_tex = albedo_source(nt, "concrete_albedo.png", pal["concrete"], "BOX", 1.6)
743
+ # concept base is warmer/browner than the codex texture (median #987D65 vs #9C978C)
744
+ color = mix_rgb(nt, 1.0, color, hex_rgba("#FFE2C6"), "MULTIPLY")
745
+ tco, sep = obj_xyz(nt)
746
+ geo = nt.nodes.new("ShaderNodeNewGeometry")
747
+ # r1: dark water streaks running down the faces from the top chamfer
748
+ ws = map_range(nt, noise3(nt, tco.outputs["Object"], 25.0, 25.0, 3.0, detail=5.0, rough=0.65), 0.56, 0.66)
749
+ wz = map_range(nt, sep.outputs["Z"], 0.04, 0.2)
750
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", math_node(nt, "MULTIPLY", ws, wz), 0.4), color, hex_rgba("#5E5547"), "MULTIPLY")
751
+ # r1: olive moss patches, denser at the bottom edge and on the top chamfer
752
+ mz = math_node(nt, "MAXIMUM", map_range(nt, sep.outputs["Z"], 0.07, 0.01), map_range(nt, sep.outputs["Z"], 0.19, 0.212, 0.0, 0.4))
753
+ mn = noise3(nt, tco.outputs["Object"], 14.0, detail=6.0, rough=0.65, loc=(4.0, 0, 0))
754
+ moss = map_range(nt, math_node(nt, "ADD", mn, math_node(nt, "MULTIPLY", mz, 0.12)), 0.6, 0.65)
755
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", moss, 0.7), color, hex_rgba("#6E6B40"))
756
+ # r1: lighter weathered patches (concept base is blotchy light/dark)
757
+ lp = map_range(nt, noise3(nt, tco.outputs["Object"], 9.0, detail=5.0, rough=0.6, loc=(7.0, 1.0, 0)), 0.58, 0.64)
758
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", lp, 0.55), color, hex_rgba("#B9B1A3"))
759
+ # r1: small chips / spalls of lighter concrete on the edges
760
+ e = edge_mask(nt, geo, 0.012, 0.999, 0.93)
761
+ en = noise3(nt, tco.outputs["Object"], 70.0, detail=4.0, rough=0.6, loc=(1.0, 3.0, 0))
762
+ spall = map_range(nt, math_node(nt, "ADD", math_node(nt, "MULTIPLY", e, 0.4), en), 0.74, 0.78)
763
+ color = mix_rgb(nt, math_node(nt, "MULTIPLY", spall, 0.75), color, hex_rgba("#B8AD9C"))
764
+ ao = nt.nodes.new("ShaderNodeAmbientOcclusion")
765
+ ao.inputs["Distance"].default_value = 0.08
766
+ grime = map_range(nt, ao.outputs["AO"], 0.95, 0.4, 0.0, 0.5)
767
+ color = mix_rgb(nt, grime, color, hex_rgba(pal["grime"]), "MULTIPLY")
768
+ nt.links.new(color, bsdf.inputs["Base Color"])
769
+ bsdf.inputs["Roughness"].default_value = 0.93
770
+ if has_tex:
771
+ bw = nt.nodes.new("ShaderNodeRGBToBW")
772
+ nt.links.new(color, bw.inputs[0])
773
+ bp = nt.nodes.new("ShaderNodeBump")
774
+ bp.inputs["Strength"].default_value = 0.25
775
+ bp.inputs["Distance"].default_value = 0.003
776
+ nt.links.new(bw.outputs[0], bp.inputs["Height"])
777
+ nt.links.new(bp.outputs["Normal"], bsdf.inputs["Normal"])
778
+ return m
779
+
780
+
781
+ def flat_mat(name, hex_, rough=0.8, metal=0.0):
782
+ m, nt, bsdf = mat_nodes(name)
783
+ bsdf.inputs["Base Color"].default_value = hex_rgba(hex_)
784
+ bsdf.inputs["Roughness"].default_value = rough
785
+ bsdf.inputs["Metallic"].default_value = metal
786
+ return m
787
+
788
+
789
+ def assign(obj, mat):
790
+ obj.data.materials.clear()
791
+ obj.data.materials.append(mat)
792
+
793
+
794
+ # ---------------------------------------------------------------- build
795
+ S = SPEC
796
+ B = S["body"]
797
+ pal = S["palette"]
798
+
799
+ sb_, d_ = S["slot_box"], S["door"]
800
+ R_MID, R_LO, R_HI = S["rings"]
801
+ CP = S["cap"]
802
+ op_ = sb_["opening"]
803
+ SB_Y0 = sb_["center_y"] - sb_["height"] / 2
804
+ D_Y0, D_Y1 = d_["center_y"] - d_["height"] / 2, d_["center_y"] + d_["height"] / 2
805
+ ZF = B["radius"]
806
+ DRIPS_BODY = [
807
+ # under the collar: the main source, 10-15 clusters per view, strands 3-10 cm (few up to 13-15)
808
+ dict(z=B["y1"], maxlen=0.15, seed=1, cell=0.042, active=0.8, stain=1.4),
809
+ # under the mid ring: 6-12 clusters per view, 3-8 cm (up to 10)
810
+ dict(z=R_MID["y0"], maxlen=0.11, seed=2, cell=0.045, active=0.8, stain=1.6),
811
+ # under the slot box: 2-4 clusters 3-9 cm
812
+ dict(z=SB_Y0, maxlen=0.09, seed=3, xhalf=sb_["width"] / 2, cell=0.06, active=0.7),
813
+ # under the door: 2-4 strands 4-13 cm
814
+ dict(z=D_Y0, maxlen=0.13, seed=4, xhalf=d_["width"] / 2, cell=0.05, xoff=0.01, active=0.85, stain=1.2),
815
+ ]
816
+ BANDS_BODY = [ # (z, below, above, strength): rusty dirt in the collar grooves and on the mid ring lips
817
+ (B["y1"], 0.006, 0.01, 1.0), # collar foot groove + the strip right under it
818
+ (R_LO["y1"], 0.007, 0.004, 1.0), # groove between the two collar belts
819
+ (R_HI["y1"], 0.008, 0.001, 0.6), # under the cap slab
820
+ (R_MID["y0"], 0.005, 0.012, 1.0), # mid ring lower lip
821
+ (R_MID["y1"], 0.005, 0.005, 0.8), # mid ring upper lip
822
+ ]
823
+ # hand-placed large/medium flakes: (facet, lateral offset m, z m, width m, height m); 1-2 large per view
824
+ SPOTS_BODY = [
825
+ (1, 0.03, 0.84, 0.038, 0.068), # front-right, upper (front / right / 3q)
826
+ (1, -0.02, 0.40, 0.035, 0.06), # front-right lower
827
+ (6, 0.03, 0.87, 0.032, 0.055), # left upper (left / 3q)
828
+ (2, -0.03, 0.97, 0.04, 0.062), # right upper
829
+ (3, 0.02, 0.37, 0.034, 0.05), # right-back lower
830
+ (5, 0.01, 1.00, 0.04, 0.08), # back, viewer's right: 8 x 4 cm
831
+ (5, 0.05, 0.895, 0.032, 0.055), # and 5 x 3 cm
832
+ ]
833
+ def body_prob(nt, sep): # collar belts: almost no chips (< 1%), only small ones on the lips
834
+ return map_range(nt, sep.outputs["Z"], B["y1"] - 0.004, B["y1"] + 0.004, 1.0, 0.25)
835
+
836
+
837
+ M_BODY = weathered_paint("paint_body", "body_wrap_albedo.png", "UV", facets=B["sides"],
838
+ small=(0.003, 0.035), medium=(0.0015, 0.012), spots=SPOTS_BODY, ring_p=(0.03, 0.5), ring_wear=2.0, fleck=0.05,
839
+ prob_mod=body_prob,
840
+ edge_wear=0.75, wear_r=0.008, wear_gate=(30.0, 0.22),
841
+ ring_zone=(R_MID["y0"] - 0.004, R_MID["y1"] + 0.004),
842
+ drip_sources=DRIPS_BODY, bands=BANDS_BODY, crevice_rust=0.6, facet_grime=0.55,
843
+ wash=[(B["y1"], 0.14, 0.5), (R_MID["y0"], 0.12, 0.45)], moss=B["y0"])
844
+ # hood of the slot box: ridges worn like edges (2-6 mm), 1-3 small chips on the planes, no green / brown patches
845
+ M_PARTS = weathered_paint("paint_parts", "body_wrap_albedo.png", "BOX", scale=0.69, loc=(0.23, 0.78, 0.27), clean_moss=True,
846
+ small=(0.006, 0.1), medium=(0.0, 0.02), edge_wear=1.2, wear_r=0.006, wear_ab=(0.9995, 0.96), wear_gate=(25.0, 0.6),
847
+ crevice_rust=0.0, dirt=0.08, clean_rust=True)
848
+ PANEL = (0.0, d_["center_y"], d_["width"] / 2 - d_["inner_inset"], d_["height"] / 2 - d_["inner_inset"])
849
+ DRIPS_DOOR = [ # the door reads more drips than wear
850
+ dict(z=D_Y1 - d_["inner_inset"], maxlen=0.075, seed=8, xhalf=PANEL[2], cell=0.035, active=0.75, ymax=-ZF - 0.02),
851
+ dict(z=d_["center_y"] - d_["knob"]["radius"], maxlen=0.06, seed=6, xhalf=0.012, cell=0.024, xoff=0.012, active=1.0, stain=0.5, wcap=0.38, ymax=-ZF - 0.02),
852
+ ]
853
+
854
+
855
+ def door_prob(nt, sep): # the flat panel has almost no chips
856
+ return map_range(nt, rect_mask(nt, sep, 0.0, PANEL[1], PANEL[2] - 0.004, PANEL[3] - 0.004), 0.0, 1.0, 1.0, 0.08)
857
+
858
+
859
+ M_DOOR = weathered_paint("paint_door", "body_wrap_albedo.png", "BOX", scale=0.69, loc=(0.0, 0.45, 0.01), clean_moss=True,
860
+ small=(0.012, 0.2), medium=(0.0, 0.2), near_r=0.02, prob_mod=door_prob,
861
+ edge_wear=0.25, wear_r=0.005, wear_gate=(30.0, 0.25), drip_sources=DRIPS_DOOR,
862
+ groove=(0.0, PANEL[1], PANEL[2], PANEL[3], 0.016, -ZF - 0.02), crevice_rust=0.9, groove_hex="#6E2F14")
863
+ LIP_Z = op_["center_y"] - op_["height"] / 2
864
+
865
+
866
+ def slot_lip(nt, sep): # the lower lip of the slot is worn lighter than the rest of the box
867
+ reg = math_node(nt, "MULTIPLY", map_range(nt, math_node(nt, "ABSOLUTE", sep.outputs["X"]), op_["width"] / 2 + 0.006, op_["width"] / 2),
868
+ map_range(nt, math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sep.outputs["Z"], LIP_Z)), 0.008, 0.003))
869
+ return math_node(nt, "ADD", 1.0, math_node(nt, "MULTIPLY", reg, 1.2))
870
+
871
+
872
+ def slot_floor(nt, sep): # the floor of the opening (behind the lower lip): lit, not a black hole
873
+ return math_node(nt, "MULTIPLY", map_range(nt, math_node(nt, "ABSOLUTE", sep.outputs["X"]), op_["width"] / 2 - 0.002, op_["width"] / 2 - 0.008),
874
+ math_node(nt, "MULTIPLY", map_range(nt, math_node(nt, "ABSOLUTE", math_node(nt, "SUBTRACT", sep.outputs["Z"], LIP_Z)), 0.004, 0.0015),
875
+ map_range(nt, sep.outputs["Y"], -(ZF + sb_["depth"]) + 0.0003, -(ZF + sb_["depth"]) + 0.002))) # behind the front face
876
+
877
+
878
+ DRIPS_SLOT = [dict(z=LIP_Z - 0.003, maxlen=0.075, seed=5, xhalf=op_["width"] / 2, cell=0.04, active=0.8, stain=0.3, wcap=0.28,
879
+ ymax=-ZF - 0.02)]
880
+ M_SLOTBOX = weathered_paint("paint_slotbox", "body_wrap_albedo.png", "BOX", scale=0.69, loc=(0.0, 0.45, 0.01), clean_moss=True,
881
+ small=(0.012, 0.25), medium=(0.003, 0.22), near_r=0.02,
882
+ edge_wear=0.55, wear_r=0.006, wear_gate=(28.0, 0.4), wear_mod=slot_lip,
883
+ drip_sources=DRIPS_SLOT, crevice_rust=1.0, ao_mask=slot_floor, floor=slot_floor)
884
+ # cap: chips along the 8 pyramid ridges and the lower edge of the slopes (~70%), few flakes on the planes;
885
+ # ridges worn in interrupted strips 3-10 cm long over 40-60% of their length; short rust streaks at the slab bottom
886
+ M_CAP = weathered_paint("paint_cap", "body_wrap_albedo.png", "BOX", scale=0.69, loc=(0.5, 0.5, -0.39), clean_moss=True,
887
+ small=(0.005, 0.22), medium=(0.002, 0.22), cells=(0.016, 0.034), near_r=0.03, fleck=0.08,
888
+ edge_wear=1.2, wear_r=0.008, wear_ab=(0.999, 0.95), wear_gate=((7.0, 7.0, 14.0), 0.5), metal_thr=1.6,
889
+ ridge=(CP["slab_y1"], CP["pyramid_y1"], CP["slab_apothem"] - 0.012, CP["pyramid_top_apothem"]),
890
+ bands=[(CP["slab_y0"], 0.0, 0.006, 0.6)], wash=[(CP["slab_y0"] + 0.03, 0.03, 0.5)], crevice_rust=0.5,
891
+ drip_sources=[dict(z=CP["slab_y0"] + 0.026, maxlen=0.03, seed=12, cell=0.08, active=0.45, stain=0.05,
892
+ radius=CP["slab_apothem"])])
893
+ M_PLATE = weathered_paint("paint_plate", None, base_hex=pal["paint_dark"], small=(0.01, 0.1), medium=(0.0, 0.0),
894
+ edge_wear=0.4, wear_r=0.004, crevice_rust=0.5)
895
+ M_CONC = concrete_mat()
896
+ M_SLOT = flat_mat("slot_dark", "#2A1A12", 0.9)
897
+
898
+
899
+ def worn_mark():
900
+ """dull cream (#E4D8C4..#D8CDBC), even tone, a few small chips to red, light grime."""
901
+ m, nt, bsdf = mat_nodes("mark_cream")
902
+ tco, sep = obj_xyz(nt)
903
+ n = noise3(nt, tco.outputs["Object"], 90.0, detail=10.0, rough=0.7)
904
+ chip = map_range(nt, n, 0.64, 0.665)
905
+ grime = map_range(nt, noise3(nt, tco.outputs["Object"], 40.0, detail=4.0, loc=(3, 0, 0)), 0.35, 0.65, 0.0, 0.6)
906
+ color = mix_rgb(nt, grime, hex_rgba("#DDD1BD"), hex_rgba("#C4B59C"))
907
+ # post-r3: the top bar sits in the slot box shadow and read grey -> a touch lighter albedo there, same perceived cream
908
+ top = map_range(nt, sep.outputs["Z"], S["postal_mark"]["center_y"] + S["postal_mark"]["height"] / 2 - 0.0155,
909
+ S["postal_mark"]["center_y"] + S["postal_mark"]["height"] / 2 - 0.0135)
910
+ color = mix_rgb(nt, top, color, hex_rgba("#F2E8D6"))
911
+ color = mix_rgb(nt, chip, color, hex_rgba("#A8503A"))
912
+ nt.links.new(color, bsdf.inputs["Base Color"])
913
+ bsdf.inputs["Roughness"].default_value = 0.85
914
+ return m
915
+
916
+
917
+ M_MARK = worn_mark()
918
+ M_KNOB = flat_mat("knob_metal", S["door"]["knob"]["color"], 0.55, 0.6)
919
+
920
+ # body + rings + collars as one lathe so the paint wraps continuously
921
+ r = B["radius"]
922
+ c = 0.006 # chamfer size at ring transitions
923
+ prof = [(0.0, B["y0"]), (r, B["y0"])]
924
+ mid = S["rings"][0]
925
+ prof += [(r, mid["y0"] - c), (mid["radius"], mid["y0"] + c * 0.5), (mid["radius"], mid["y1"] - c * 0.5), (r, mid["y1"] + c)]
926
+ lo, hi = S["rings"][1], S["rings"][2]
927
+ prof += [(r, B["y1"]), (lo["radius"], lo["y0"] + c * 0.5), (lo["radius"], lo["y1"]),
928
+ (hi["radius"], hi["y0"] + c * 0.4), (hi["radius"], hi["y1"]), (0.0, hi["y1"])]
929
+ # v spans the whole lathe incl. the collar, so the collar never samples outside the texture (was a smeared band)
930
+ body = lathe("body", prof, B["sides"], v_range=(B["y0"], hi["y1"]), sharp_deg=20.0)
931
+ bevel(body, 0.004, 3, 20.0)
932
+ weighted(body)
933
+ assign(body, M_BODY)
934
+
935
+ ba = S["base"]
936
+ base = lathe("base", [(0.0, ba["y0"]), (ba["apothem"], ba["y0"]), (ba["apothem"], ba["y1"]),
937
+ (ba["chamfer_top_apothem"], ba["chamfer_top_y"]), (0.0, ba["chamfer_top_y"])], ba["sides"], sharp_deg=30)
938
+ bevel(base, 0.008, 3, 30)
939
+ weighted(base)
940
+ assign(base, M_CONC)
941
+
942
+ cp = S["cap"]
943
+ pyr_base = cp["slab_apothem"] - 0.012
944
+ cap = lathe("cap", [(0.0, cp["slab_y0"]), (cp["slab_apothem"], cp["slab_y0"]), (cp["slab_apothem"], cp["slab_y1"]),
945
+ (pyr_base, cp["slab_y1"]), (cp["pyramid_top_apothem"], cp["pyramid_y1"]), (0.0, cp["pyramid_y1"])],
946
+ cp["sides"], sharp_deg=12)
947
+ bevel(cap, cp["slab_bevel"], 3, 12) # r1: ridges of the pyramid (~18 deg) now sharp-shaded + rounded like the other edges
948
+ weighted(cap)
949
+ assign(cap, M_CAP)
950
+
951
+ if cp.get("knob_radius", 0) > 0:
952
+ bpy.ops.mesh.primitive_uv_sphere_add(segments=16, ring_count=8, radius=cp["knob_radius"],
953
+ location=P(0, cp["knob_top"] - cp["knob_radius"], 0))
954
+ knob_top = bpy.context.active_object
955
+ knob_top.name = "cap_knob"
956
+ knob_top.scale.z = 0.8
957
+ for p in knob_top.data.polygons:
958
+ p.use_smooth = True
959
+ assign(knob_top, M_CAP)
960
+
961
+ # mail slot box
962
+ sb = S["slot_box"]
963
+ zf = r # front face of the body (apothem)
964
+ y0 = sb["center_y"] - sb["height"] / 2
965
+ y1 = sb["center_y"] + sb["height"] / 2
966
+ hood_h = sb["hood"]["height"]
967
+ slot = box("slot_box", -sb["width"] / 2, sb["width"] / 2, y0, y1 - hood_h * 0.6, 0.12, zf + sb["depth"])
968
+ op = sb["opening"]
969
+ cutter = box("slot_cutter", -op["width"] / 2, op["width"] / 2, op["center_y"] - op["height"] / 2,
970
+ op["center_y"] + op["height"] / 2, zf + sb["depth"] - op["depth"], zf + sb["depth"] + 0.05)
971
+ boolean_cut(slot, cutter)
972
+ finish(slot, 0.006, 3, 30)
973
+ assign(slot, M_SLOTBOX)
974
+ oh = sb["hood"]["overhang"]
975
+ hw = sb["width"] / 2 + oh
976
+ zb, zfr = 0.12, zf + sb["depth"] + oh
977
+ run = hood_h * 0.55 / math.tan(math.radians(30)) # ~30 deg roof slope; keep a short vertical eave
978
+ eave = hood_h * 0.45
979
+ bmh = bmesh.new()
980
+ ring_lo = [P(-hw, y1 - hood_h, zb), P(hw, y1 - hood_h, zb), P(hw, y1 - hood_h, zfr), P(-hw, y1 - hood_h, zfr)]
981
+ ring_ea = [P(-hw, y1 - hood_h + eave, zb), P(hw, y1 - hood_h + eave, zb), P(hw, y1 - hood_h + eave, zfr), P(-hw, y1 - hood_h + eave, zfr)]
982
+ ring_hi = [P(-hw + run, y1, zb), P(hw - run, y1, zb), P(hw - run, y1, zfr - run), P(-hw + run, y1, zfr - run)]
983
+ rings_ = [[bmh.verts.new(v) for v in r] for r in (ring_lo, ring_ea, ring_hi)]
984
+ bmh.faces.new(list(reversed(rings_[0])))
985
+ bmh.faces.new(rings_[2])
986
+ for a_, b_ in ((rings_[0], rings_[1]), (rings_[1], rings_[2])):
987
+ for i in range(4):
988
+ j = (i + 1) % 4
989
+ bmh.faces.new((a_[i], a_[j], b_[j], b_[i]))
990
+ bmesh.ops.recalc_face_normals(bmh, faces=bmh.faces)
991
+ bmh.loops.layers.uv.new("UVMap")
992
+ hood = new_obj("slot_hood", bmh)
993
+ finish(hood, 0.005, 3, 25)
994
+ assign(hood, M_PARTS)
995
+ inner = box("slot_inner", -op["width"] / 2 - 0.005, op["width"] / 2 + 0.005, op["center_y"] - op["height"] / 2 - 0.005,
996
+ op["center_y"] + op["height"] / 2 + 0.005, zf + sb["depth"] - op["depth"] - 0.004, zf + sb["depth"] - op["depth"] + 0.002)
997
+ assign(inner, M_SLOT)
998
+
999
+ # postal mark 〒 (raised paint)
1000
+ pm = S["postal_mark"]
1001
+ w, h, cy = pm["width"], pm["height"], pm["center_y"]
1002
+ t = 0.014
1003
+ zm0, zm1 = zf - 0.001, zf + 0.0018
1004
+ bars = [box("mark_top", -w / 2, w / 2, cy + h / 2 - t, cy + h / 2, zm0, zm1),
1005
+ box("mark_mid", -w * 0.38, w * 0.38, cy + h / 2 - t * 2.6, cy + h / 2 - t * 1.6, zm0, zm1),
1006
+ box("mark_stem", -t / 2, t / 2, cy - h / 2, cy + h / 2 - t * 2.6, zm0, zm1)]
1007
+ for b_ in bars:
1008
+ finish(b_, 0.0022, 3, 30) # r2: slightly softer edges of the raised mark
1009
+ assign(b_, M_MARK)
1010
+
1011
+ # door
1012
+ d = S["door"]
1013
+ dy0, dy1 = d["center_y"] - d["height"] / 2, d["center_y"] + d["height"] / 2
1014
+ door = box("door", -d["width"] / 2, d["width"] / 2, dy0, dy1, 0.14, zf + d["depth"])
1015
+ ii = d["inner_inset"]
1016
+ dcut = box("door_cutter", -d["width"] / 2 + ii, d["width"] / 2 - ii, dy0 + ii, dy1 - ii, zf + d["depth"] - d["inner_depth"], zf + d["depth"] + 0.05)
1017
+ boolean_cut(door, dcut)
1018
+ finish(door, d["bevel"], 3, 30)
1019
+ assign(door, M_DOOR)
1020
+ kn = d["knob"]
1021
+ bpy.ops.mesh.primitive_cylinder_add(vertices=24, radius=kn["radius"], depth=kn["depth"],
1022
+ location=P(0, d["center_y"], zf + d["depth"] - d["inner_depth"] + kn["depth"] / 2),
1023
+ rotation=(math.pi / 2, 0, 0))
1024
+ dk = bpy.context.active_object
1025
+ dk.name = "door_knob"
1026
+ finish(dk, 0.005, 4, 30)
1027
+ assign(dk, M_KNOB)
1028
+
1029
+ # side plates (recessed darker panels at +-X)
1030
+ for sp in S["side_plates"]:
1031
+ sgn = -1 if sp["side"] == "left" else 1
1032
+ x_in, x_out = r - 0.01, r + 0.0015
1033
+ xa, xb = (x_in, x_out) if sgn > 0 else (-x_out, -x_in)
1034
+ pl = box("plate_" + sp["side"], xa, xb, sp["center_y"] - sp["height"] / 2, sp["center_y"] + sp["height"] / 2,
1035
+ -sp["width"] / 2, sp["width"] / 2)
1036
+ finish(pl, 0.003, 2, 30)
1037
+ assign(pl, M_PLATE)
1038
+
1039
+ # ---------------------------------------------------------------- studio
1040
+ scene.render.engine = "CYCLES"
1041
+ cy_ = scene.cycles
1042
+ cy_.samples = 128
1043
+ cy_.use_denoising = True
1044
+ try:
1045
+ prefs = bpy.context.preferences.addons["cycles"].preferences
1046
+ prefs.compute_device_type = "METAL"
1047
+ prefs.get_devices()
1048
+ for dev in prefs.devices:
1049
+ dev.use = True
1050
+ cy_.device = "GPU"
1051
+ except Exception as e:
1052
+ print("GPU setup failed, using CPU:", e)
1053
+ scene.render.film_transparent = True
1054
+ res = S["cameras"]["image"]
1055
+ scene.render.resolution_x, scene.render.resolution_y = res
1056
+ scene.render.resolution_percentage = 100
1057
+ scene.view_settings.view_transform = "Standard"
1058
+ try:
1059
+ scene.view_settings.look = "None"
1060
+ except Exception:
1061
+ pass
1062
+
1063
+ world = bpy.data.worlds.new("studio")
1064
+ scene.world = world
1065
+ try:
1066
+ world.use_nodes = True
1067
+ except Exception:
1068
+ pass
1069
+ bg = world.node_tree.nodes.get("Background") or world.node_tree.nodes.new("ShaderNodeBackground")
1070
+ bg.inputs["Color"].default_value = (1, 1, 1, 1)
1071
+ bg.inputs["Strength"].default_value = 0.16
1072
+ wout = world.node_tree.nodes.get("World Output") or world.node_tree.nodes.new("ShaderNodeOutputWorld")
1073
+ world.node_tree.links.new(bg.outputs["Background"], wout.inputs["Surface"])
1074
+
1075
+
1076
+ def area_light(name, az_deg, el_deg, dist, size, power):
1077
+ az, el = math.radians(az_deg), math.radians(el_deg)
1078
+ target = P(0, 0.7, 0)
1079
+ pos = target + P(dist * math.sin(az) * math.cos(el), dist * math.sin(el), dist * math.cos(az) * math.cos(el))
1080
+ ld = bpy.data.lights.new(name, "AREA")
1081
+ ld.shape = "DISK"
1082
+ ld.size = size
1083
+ ld.energy = power
1084
+ lo_ = link(bpy.data.objects.new(name, ld))
1085
+ lo_.location = pos
1086
+ lo_.rotation_euler = (target - pos).to_track_quat("-Z", "Y").to_euler()
1087
+ return lo_
1088
+
1089
+
1090
+ rig = link(bpy.data.objects.new("light_rig", None))
1091
+ rig.location = P(0, 0.7, 0)
1092
+ for l_ in [area_light("key", -40, 38, 4.0, 3.0, 210),
1093
+ area_light("fill", 55, 15, 4.0, 3.0, 85),
1094
+ area_light("rim", 170, 35, 4.0, 2.5, 70)]:
1095
+ l_.parent = rig
1096
+ l_.location = l_.location - rig.location
1097
+ scene.view_settings.exposure = -0.3
1098
+
1099
+ bpy.ops.mesh.primitive_plane_add(size=8, location=(0, 0, 0))
1100
+ ground = bpy.context.active_object
1101
+ ground.name = "shadow_catcher"
1102
+ ground.is_shadow_catcher = True
1103
+
1104
+ cam_data = bpy.data.cameras.new("cam")
1105
+ cam_data.sensor_fit = "VERTICAL"
1106
+ cam_data.angle_y = math.radians(S["cameras"]["fov_deg"])
1107
+ cam = link(bpy.data.objects.new("cam", cam_data))
1108
+ scene.camera = cam
1109
+
1110
+ os.makedirs(os.path.join(ROOT, "blender"), exist_ok=True)
1111
+ bpy.ops.wm.save_as_mainfile(filepath=os.path.join(ROOT, "blender/postbox.blend"))
1112
+ print("saved blender/postbox.blend")