|
15 | 15 | ``--stray-vert``, ``--lift-z``, ``--float-spout``, ``--float-flange``, |
16 | 16 | ``--skinny-col``. |
17 | 17 |
|
18 | | -No RNG. Construction is closed-form. DECIMATE COLLAPSE triangle counts |
| 18 | +Construction is closed-form; the only RNG is the seeded per-piece wood |
| 19 | +tone. DECIMATE COLLAPSE triangle counts |
19 | 20 | are not byte-identical across Blender versions — the LOD gate is a |
20 | 21 | ratio band, not an exact count. |
21 | 22 |
|
|
26 | 27 | import argparse |
27 | 28 | import math |
28 | 29 | import os |
| 30 | +import random |
29 | 31 | import sys |
30 | 32 | import tempfile |
31 | 33 | import traceback |
|
87 | 89 | WOOD_IDX = 0 |
88 | 90 | METAL_IDX = 1 |
89 | 91 |
|
| 92 | +# Per-piece wood tone jitter and grain frequency, as in shipping-crate. |
| 93 | +PLANK_TONE_JITTER = 0.28 |
| 94 | +TONE_SEED = 29 |
| 95 | +WOOD_GRAIN_SCALE = 30.0 |
| 96 | + |
90 | 97 | DOUBLES_EPS = 1e-5 |
91 | 98 | AREA_EPS = 1e-10 |
92 | 99 | ZMIN_EPS = 1e-4 |
@@ -397,6 +404,10 @@ def build_hand_pump_mesh( |
397 | 404 | ) |
398 | 405 | if bevel_offset > 0.0: |
399 | 406 | edges = list({e for v in wood for e in v.link_edges}) |
| 407 | + # set order follows memory addresses; sort so the bevel, and the |
| 408 | + # face order it produces, are the same on every run |
| 409 | + bm.edges.index_update() |
| 410 | + edges.sort(key=lambda e: e.index) |
400 | 411 | ret = bmesh.ops.bevel( |
401 | 412 | bm, |
402 | 413 | geom=edges, |
@@ -572,6 +583,132 @@ def principled(name, color, metallic, roughness, noise_scale=0.0, wear=None): |
572 | 583 | return mat |
573 | 584 |
|
574 | 585 |
|
| 586 | +def _long_axis(pts): |
| 587 | + """Principal axis of a point set, by power iteration on its covariance.""" |
| 588 | + c = sum(pts, Vector()) / len(pts) |
| 589 | + cov = [[0.0] * 3 for _ in range(3)] |
| 590 | + for p in pts: |
| 591 | + d = p - c |
| 592 | + for i in range(3): |
| 593 | + for j in range(3): |
| 594 | + cov[i][j] += d[i] * d[j] |
| 595 | + v = Vector((1.0, 0.3, 0.1)) |
| 596 | + for _ in range(30): |
| 597 | + w = Vector([sum(cov[i][j] * v[j] for j in range(3)) for i in range(3)]) |
| 598 | + if w.length < 1e-12: |
| 599 | + break |
| 600 | + v = w.normalized() |
| 601 | + return v |
| 602 | + |
| 603 | + |
| 604 | +def paint_planks(me): |
| 605 | + """Per-shell ``PlankTone`` and ``GrainDir`` face attributes for the wood shader. |
| 606 | +
|
| 607 | + Each plinth slab and the handle grip is its own shell, so each gets |
| 608 | + one tone and grain running along its own long axis. |
| 609 | + """ |
| 610 | + tone = [0.5] * len(me.polygons) |
| 611 | + grain = [(0.0, 0.0, 1.0)] * len(me.polygons) |
| 612 | + owner = {} |
| 613 | + rng = random.Random(TONE_SEED) |
| 614 | + for g in shells(me): |
| 615 | + pts = [me.vertices[i].co.copy() for i in g] |
| 616 | + d = _long_axis(pts) if len(pts) > 2 else Vector((0.0, 0.0, 1.0)) |
| 617 | + t = 0.5 + rng.uniform(-PLANK_TONE_JITTER, PLANK_TONE_JITTER) |
| 618 | + for i in g: |
| 619 | + owner[i] = (t, tuple(d)) |
| 620 | + for poly in me.polygons: |
| 621 | + t, d = owner[poly.vertices[0]] |
| 622 | + tone[poly.index] = t |
| 623 | + grain[poly.index] = d |
| 624 | + a = me.attributes.new("PlankTone", "FLOAT", "FACE") |
| 625 | + a.data.foreach_set("value", tone) |
| 626 | + b = me.attributes.new("GrainDir", "FLOAT_VECTOR", "FACE") |
| 627 | + b.data.foreach_set("vector", [c for v in grain for c in v]) |
| 628 | + |
| 629 | + |
| 630 | +def _sock(sockets, identifier): |
| 631 | + """A Mix-node socket by identifier; its A/B/Result names repeat per type.""" |
| 632 | + return next(sk for sk in sockets if sk.identifier == identifier) |
| 633 | + |
| 634 | + |
| 635 | +def wood_material(name): |
| 636 | + """Grain along each slab and the grip (``GrainDir``), tone per piece (``PlankTone``).""" |
| 637 | + mat = bpy.data.materials.new(name) |
| 638 | + mat.use_nodes = True |
| 639 | + nt = mat.node_tree |
| 640 | + bsdf = nt.nodes["Principled BSDF"] |
| 641 | + coord = nt.nodes.new("ShaderNodeTexCoord") |
| 642 | + gdir = nt.nodes.new("ShaderNodeAttribute") |
| 643 | + gdir.attribute_name = "GrainDir" |
| 644 | + tone = nt.nodes.new("ShaderNodeAttribute") |
| 645 | + tone.attribute_name = "PlankTone" |
| 646 | + dot = nt.nodes.new("ShaderNodeVectorMath") |
| 647 | + dot.operation = "DOT_PRODUCT" |
| 648 | + nt.links.new(coord.outputs["Object"], dot.inputs[0]) |
| 649 | + nt.links.new(gdir.outputs["Vector"], dot.inputs[1]) |
| 650 | + squash = nt.nodes.new("ShaderNodeMath") |
| 651 | + squash.operation = "MULTIPLY" |
| 652 | + squash.inputs[1].default_value = 0.94 |
| 653 | + nt.links.new(dot.outputs["Value"], squash.inputs[0]) |
| 654 | + along = nt.nodes.new("ShaderNodeVectorMath") |
| 655 | + along.operation = "SCALE" |
| 656 | + nt.links.new(gdir.outputs["Vector"], along.inputs[0]) |
| 657 | + nt.links.new(squash.outputs["Value"], along.inputs["Scale"]) |
| 658 | + grain_co = nt.nodes.new("ShaderNodeVectorMath") |
| 659 | + grain_co.operation = "SUBTRACT" |
| 660 | + nt.links.new(coord.outputs["Object"], grain_co.inputs[0]) |
| 661 | + nt.links.new(along.outputs["Vector"], grain_co.inputs[1]) |
| 662 | + shift = nt.nodes.new("ShaderNodeVectorMath") |
| 663 | + shift.operation = "ADD" |
| 664 | + nt.links.new(grain_co.outputs["Vector"], shift.inputs[0]) |
| 665 | + nt.links.new(tone.outputs["Fac"], shift.inputs[1]) |
| 666 | + noise = nt.nodes.new("ShaderNodeTexNoise") |
| 667 | + noise.inputs["Scale"].default_value = WOOD_GRAIN_SCALE |
| 668 | + noise.inputs["Detail"].default_value = 6.0 |
| 669 | + noise.inputs["Roughness"].default_value = 0.62 |
| 670 | + nt.links.new(shift.outputs["Vector"], noise.inputs["Vector"]) |
| 671 | + ramp = nt.nodes.new("ShaderNodeValToRGB") |
| 672 | + ramp.color_ramp.elements[0].position = 0.30 |
| 673 | + ramp.color_ramp.elements[0].color = (0.12, 0.052, 0.018, 1.0) |
| 674 | + ramp.color_ramp.elements[1].position = 0.72 |
| 675 | + ramp.color_ramp.elements[1].color = (0.38, 0.18, 0.065, 1.0) |
| 676 | + nt.links.new(noise.outputs["Fac"], ramp.inputs["Fac"]) |
| 677 | + gain = nt.nodes.new("ShaderNodeMath") |
| 678 | + gain.operation = "MULTIPLY_ADD" |
| 679 | + gain.inputs[1].default_value = 1.1 |
| 680 | + gain.inputs[2].default_value = 0.45 |
| 681 | + nt.links.new(tone.outputs["Fac"], gain.inputs[0]) |
| 682 | + mix = nt.nodes.new("ShaderNodeMix") |
| 683 | + mix.data_type = "RGBA" |
| 684 | + mix.blend_type = "MULTIPLY" |
| 685 | + _sock(mix.inputs, "Factor_Float").default_value = 1.0 |
| 686 | + nt.links.new(ramp.outputs["Color"], _sock(mix.inputs, "A_Color")) |
| 687 | + nt.links.new(gain.outputs["Value"], _sock(mix.inputs, "B_Color")) |
| 688 | + nt.links.new(_sock(mix.outputs, "Result_Color"), bsdf.inputs["Base Color"]) |
| 689 | + rough = nt.nodes.new("ShaderNodeMapRange") |
| 690 | + rough.inputs["To Min"].default_value = 0.72 |
| 691 | + rough.inputs["To Max"].default_value = 0.52 |
| 692 | + nt.links.new(noise.outputs["Fac"], rough.inputs["Value"]) |
| 693 | + nt.links.new(rough.outputs["Result"], bsdf.inputs["Roughness"]) |
| 694 | + return mat |
| 695 | + |
| 696 | + |
| 697 | +def pump_materials(): |
| 698 | + """(wood, iron): shared by the check, the render and inspection. |
| 699 | +
|
| 700 | + A cast-iron pump is painted near-black and weathers to rust at the |
| 701 | + edges and pores; the first build was polished metal (metallic 1.0, |
| 702 | + roughness 0.38) and read as chrome. |
| 703 | + """ |
| 704 | + wood = wood_material("HandPumpWood") |
| 705 | + metal = principled( |
| 706 | + "HandPumpIron", (0.045, 0.047, 0.050, 1.0), 0.55, 0.52, |
| 707 | + noise_scale=24.0, wear=(0.09, 0.05, 0.03, 1.0), |
| 708 | + ) |
| 709 | + return wood, metal |
| 710 | + |
| 711 | + |
575 | 712 | def assign_slots(obj, wood, metal): |
576 | 713 | mats = obj.data.materials |
577 | 714 | wanted = (wood, metal) |
@@ -902,14 +1039,9 @@ def check( |
902 | 1039 | ) |
903 | 1040 | low = build_hand_pump_mesh("HandPumpLow", bevel_offset=0.004, bevel_segments=2, **kw) |
904 | 1041 | high = build_hand_pump_mesh("HandPumpHigh", bevel_offset=0.004, bevel_segments=4, **kw) |
905 | | - wood = principled( |
906 | | - "HandPumpWood", (0.36, 0.19, 0.07, 1.0), 0.0, 0.60, |
907 | | - noise_scale=14.0, wear=(0.22, 0.12, 0.05, 1.0), |
908 | | - ) |
909 | | - metal = principled( |
910 | | - "HandPumpIron", (0.18, 0.175, 0.17, 1.0), 1.0, 0.38, |
911 | | - noise_scale=18.0, wear=(0.28, 0.24, 0.18, 1.0), |
912 | | - ) |
| 1042 | + wood, metal = pump_materials() |
| 1043 | + paint_planks(low.data) |
| 1044 | + paint_planks(high.data) |
913 | 1045 | assign_slots(low, wood, metal) |
914 | 1046 | assign_slots(high, wood, metal) |
915 | 1047 |
|
@@ -965,6 +1097,10 @@ def check( |
965 | 1097 | os.remove(export_path) |
966 | 1098 | export_unity(export_path, [low, collider]) |
967 | 1099 | export_size = os.path.getsize(export_path) if os.path.isfile(export_path) else 0 |
| 1100 | + # Blender points TMPDIR at the working directory, so the export must not |
| 1101 | + # outlive the measurement. |
| 1102 | + if os.path.exists(export_path): |
| 1103 | + os.remove(export_path) |
968 | 1104 |
|
969 | 1105 | hyg = hygiene_audit(low.data) |
970 | 1106 | zf = zfight_pairs(low.data) |
@@ -1120,7 +1256,9 @@ def render_still(low, wood, tex, path, engine): |
1120 | 1256 | floor_me = bpy.data.meshes.new("Floor") |
1121 | 1257 | bm = bmesh.new() |
1122 | 1258 | try: |
1123 | | - bmesh.ops.create_grid(bm, x_segments=1, y_segments=1, size=14.0) |
| 1259 | + # Oversized so no edge of the set can enter frame; at 14 m the wall's |
| 1260 | + # left edge showed as a bright band in the corner of the hero. |
| 1261 | + bmesh.ops.create_grid(bm, x_segments=1, y_segments=1, size=60.0) |
1124 | 1262 | bm.to_mesh(floor_me) |
1125 | 1263 | finally: |
1126 | 1264 | bm.free() |
|
0 commit comments