{
  "description": "A stone wall of wall_width × wall_height × thickness, cut into exactly `pieces` Voronoi pieces, crumbles and comes to rest on the ground. About half the crack centres spread evenly over the wall (big slabs), the rest gather in tight clusters (small fragments), one of them where the wall is hit; seed moves them. Each piece breaks loose with a random turn, and the pieces drop one by one, lowest first, from where they were onto what has already fallen (rbd_stack), so they pile up in a heap at the foot of the wall. impact hits the middle of the wall from -Z and throws the pieces near it farther along +Z; 0 lets the wall simply collapse. intact is the standing wall already cut, broken the pieces at rest. Dressed faces are dark weathered stone, broken faces (group inside) light raw stone with a grain relief; uv run in metres on the standing wall, so the grain moves with each piece.",
  "params": {
    "wall_width": { "type": "float", "default": 5, "min": 2, "max": 12, "step": 0.1, "label": "wall width", "desc": "width of the wall (m)" },
    "wall_height": { "type": "float", "default": 2.5, "min": 1, "max": 6, "step": 0.1, "label": "wall height", "desc": "height of the wall (m)" },
    "thickness": { "type": "float", "default": 0.4, "min": 0.1, "max": 1, "step": 0.05, "label": "thickness", "desc": "thickness of the wall (m)" },
    "pieces": { "type": "int", "default": 40, "min": 10, "max": 200, "label": "pieces", "desc": "how many pieces it breaks into" },
    "impact": { "type": "float", "default": 0.5, "min": 0, "max": 1, "step": 0.05, "label": "impact", "desc": "how hard the wall is hit in its middle (from -Z): it throws the pieces there farther; 0 lets it collapse" },
    "seed": { "type": "int", "default": 1, "min": 0, "max": 9999, "label": "seed", "desc": "random seed of the cracks and the fall" }
  },
  "vars": { "wall_width": 5, "wall_height": 2.5, "thickness": 0.4, "pieces": 40, "impact": 0.5, "seed": 1 },
  "meta": { "render": { "views": ["persp"], "azimuth": 35, "elevation": 25, "size": "1600x900", "shading": "pbr" } },
  "nodes": {
    "wall": { "type": "box", "params": { "size": ["=wall_width", "=wall_height", "=thickness"], "center": [0, "=wall_height / 2", 0] } },
    "origin": { "type": "points", "params": { "positions": [[0, 0, 0]] } },
    "site_copies": { "type": "copy_xform", "in": ["origin"], "params": { "count": "=pieces", "translate": [0, 0, 0] } },
    "sites": { "type": "wrangle", "in": ["site_copies"], "note": "one crack centre per piece: about half spread evenly (a jittered R2 sequence) for the big slabs, the rest in tight clusters of small fragments, one of them where the wall is hit (impact > 0)", "params": { "run_over": "points", "code": "const n = pieces, nb = max(4, round(n * 0.55)), i = @ptnum;\nconst sp = sqrt(wall_width * wall_height / n);\nlet x, y;\nif (i < nb) {\n  const k = i + 0.5, jit = 0.3 / sqrt(nb);\n  x = (clamp(frac(rand(seed, 11) + k * 0.7548776662466927) + (rand(i, seed, 1) - 0.5) * jit, 0.01, 0.99) - 0.5) * wall_width;\n  y = clamp(frac(rand(seed, 12) + k * 0.5698402909980532) + (rand(i, seed, 2) - 0.5) * jit, 0.01, 0.99) * wall_height;\n} else {\n  const K = max(2, round(n / 12)), c = (i - nb) % K;\n  const hitc = c === 0 && impact > 0;\n  const cx = hitc ? 0 : (rand(c, seed, 21) - 0.5) * wall_width * 0.9;\n  const cy = hitc ? wall_height * 0.45 : (0.1 + 0.8 * rand(c, seed, 22)) * wall_height;\n  const a = TAU * rand(i, seed, 23), r = sp * (0.05 + 0.4 * sqrt(rand(i, seed, 24)));\n  x = clamp(cx + r * cos(a), -0.49 * wall_width, 0.49 * wall_width);\n  y = clamp(cy + r * sin(a), 0.01 * wall_height, 0.99 * wall_height);\n}\n@P = [x, y, (rand(i, seed, 3) - 0.5) * thickness * 0.3];" } },
    "cracked": { "type": "voronoi_fracture", "in": ["wall", "sites"], "note": "rbd takes pieces that touch exactly as starting inside each other, so the pieces keep a 0.5 mm gap", "params": { "gap": 0.0005, "seed": "=seed" } },
    "mapped": { "type": "uv_project", "in": ["cracked"], "note": "uv in metres on the standing wall, so the grain moves with each piece", "params": { "method": "box", "scale": [1.5, 1.5] } },
    "weathered": { "type": "tex_noise", "note": "the dressed faces: soft, low-contrast blotches", "params": { "resolution": [512, 512], "texture_size": 1.5, "noise": "perlin", "fractal": "fbm", "feature": 0.3, "octaves": 6, "roughness": 0.55, "seed": "=seed" } },
    "grain_stone": { "type": "tex_curves", "in": ["weathered"], "params": { "ramp": [[0, 0.27], [0.5, 0.35], [1, 0.45]] } },
    "grit": { "type": "tex_noise", "note": "the broken faces: fine, high-contrast grain", "params": { "resolution": [512, 512], "texture_size": 1.5, "noise": "perlin", "fractal": "fbm", "feature": 0.0625, "octaves": 5, "roughness": 0.7, "seed": "=seed + 1" } },
    "grain_raw": { "type": "tex_curves", "in": ["grit"], "params": { "ramp": [[0, 0.42], [0.5, 0.66], [1, 0.9]] } },
    "grit_relief": { "type": "tex_normal_from_height", "in": ["grit"], "note": "broken faces: 4 mm relief from the same grain", "params": { "depth": 0.004 } },
    "grains": { "type": "merge", "in": ["grain_stone", "grain_raw", "grit_relief"] },
    "stone": { "type": "texture_to_material", "in": ["mapped", "grains"], "params": { "material": "stone", "base_color": "grain_stone", "base_color_tint": "#e6d2b4", "roughness_value": 0.75 } },
    "raw_stone": { "type": "texture_to_material", "in": ["stone", "grains"], "note": "broken faces", "params": { "material": "raw_stone", "group": "inside", "base_color": "grain_raw", "base_color_tint": "#eef0f2", "roughness_value": 1, "normal": "grit_relief" } },
    "standing_normals": { "type": "normal", "in": ["raw_stone"], "params": { "cusp_angle": 20, "class": "vertex" } },
    "standing_parts": { "type": "connectivity", "in": ["standing_normals"], "params": { "attrib": "part" } },
    "standing_count": { "type": "wrangle", "in": ["standing_parts"], "note": "the assert expr cannot compare with the var pieces (its own piece count of that name comes first), so the connected pieces are counted into a detail attribute", "params": { "run_over": "detail", "code": "const seen = new Set();\nfor (let i = 0; i < nprims(0); i++) seen.add(prim(0, 'part', i));\ni@piece_count = seen.size;" } },
    "standing_pieces": { "type": "assert", "in": ["standing_count"], "params": { "label": "pieces", "watertight": true, "no_flipped": true, "attrib": "piece_count", "attrib_class": "detail", "attrib_min": "=pieces", "attrib_max": "=pieces" } },
    "standing_whole": { "type": "assert", "in": ["standing_pieces"], "note": "the pieces fill the wall but for their 0.5 mm gap: tolerance 0.0005 × surface area", "params": { "label": "whole", "expr": "abs(volume - wall_width * wall_height * thickness) <= 0.0005 * area" } },
    "standing_clean": { "type": "attrib_delete", "in": ["standing_whole"], "params": { "names": "part piece_count" } },
    "intact": { "type": "null", "in": ["standing_clean"] },
    "tumbled": { "type": "wrangle", "in": ["intact", "sites"], "note": "each piece turned at random about its centre and moved off the wall line a little, as it breaks loose", "params": { "run_over": "points", "code": "const c = prim(0, 'class', pointprims(0, @ptnum)[0]), s = point(1, 'P', c);\nconst r = rotate(sub(@P, s), PI * rand(c, seed, 34), randdir(c * 7 + seed + 0.5));\n@P = add(add(s, r), [0, 0, (rand(c, seed, 35) - 0.5) * 0.6]);" } },
    "kick": { "type": "wrangle", "in": ["tumbled", "sites"], "note": "per piece (class = its crack centre): the hit throws the pieces near the middle of the wall away along +Z and sets them spinning", "params": { "run_over": "prims", "code": "const id = @class, site = point(1, 'P', id);\nconst hit = [0, wall_height * 0.45, -thickness / 2];\nconst d = sub(site, hit);\nconst reach = 0.25 * min(wall_width, 2 * wall_height);\nconst f = impact * exp(-(d.x * d.x + d.y * d.y) / (reach * reach));\nconst dir = normalize([d.x * 0.15, 0.3, 1]);\nv@v = mul(dir, 6 * f * (0.7 + 0.6 * rand(id, seed, 7)));\nv@w = mul(randdir(id * 13 + seed), 1 + 6 * f);" } },
    "settled": { "type": "rbd_stack", "in": ["kick"], "params": { "order": "height", "interval": "=max(2, min(6, round(240 / pieces)))", "clearance": 0.2, "duration": 12, "angular_damping": 1.5, "friction": 0.2, "jitter": 0, "seed": "=seed", "rest_damping": 20.0, "solver_iterations": "=pieces > 100 ? 8 : 4" }, "note": "many small pieces need more solver iterations to rest on the ground instead of in it; the wall crumbles: the pieces drop one by one, lowest first, from where they were (raised only to clear what already lies under them), so they pile up at the foot of the wall" },
    "rest_normals": { "type": "normal", "in": ["settled"], "params": { "cusp_angle": 20, "class": "vertex" } },
    "rest_parts": { "type": "connectivity", "in": ["rest_normals"], "params": { "attrib": "part" } },
    "rest_count": { "type": "wrangle", "in": ["rest_parts"], "note": "the assert expr cannot compare with the var pieces (its own piece count of that name comes first), so the connected pieces are counted into a detail attribute", "params": { "run_over": "detail", "code": "const seen = new Set();\nfor (let i = 0; i < nprims(0); i++) seen.add(prim(0, 'part', i));\ni@piece_count = seen.size;" } },
    "pieces": { "type": "assert", "in": ["rest_count"], "params": { "label": "pieces", "watertight": true, "no_flipped": true, "attrib": "piece_count", "attrib_class": "detail", "attrib_min": "=pieces", "attrib_max": "=pieces" } },
    "whole": { "type": "assert", "in": ["pieces"], "note": "moved rigidly: same stone as the wall but for the 0.5 mm gap (0.0005 × surface area)", "params": { "label": "whole", "expr": "abs(volume - wall_width * wall_height * thickness) <= 0.0005 * area" } },
    "rest": { "type": "assert", "in": ["whole"], "note": "1 cm: contact slop of the solver", "params": { "label": "rest", "expr": "bbox.min.y >= -0.01" } },
    "rest_clean": { "type": "attrib_delete", "in": ["rest"], "params": { "names": "part piece_count v w settled contacts" } },
    "broken": { "type": "null", "in": ["rest_clean"] }
  },
  "output": "broken",
  "outputs": {
    "broken": { "node": "broken", "label": "Broken", "kind": "mesh", "tags": ["fracture", "rubble", "stone", "rbd"], "description": "The pieces where they come to rest, heaped at the foot of the wall: `pieces` closed convex pieces (prim name pieceN and class N, the same in both outputs), outer faces in stone, broken faces (prim group inside) in raw_stone with a normal map, uv in metres." },
    "intact": { "node": "intact", "label": "Intact", "kind": "mesh", "tags": ["fracture", "wall", "stone", "voronoi"], "description": "The wall standing before it falls, already cut into the same pieces with the same names, materials and uv: for a before/after switch or another simulation." }
  }
}
