{
  "version": 1,
  "description": "Frame-by-frame coral growth: a round seed of seed_size, meshed at edge_length, grows in a solver. 22 bud centres sit evenly over the upper seed (seed turns them), each starting at its own frame; every frame each point moves out by rate × its growth, half along its normal and half away from the centre. Growth peaks at the bud centres, so knobs bulge out on narrow necks; lumpiness sets how much faster the buds grow than the surface between them (0 swells like a balloon) and splits each knob into lobes. Knobs that grow into each other push apart where they meet and leave a fold. The underside barely grows, so the form sits on its base. Each frame the surface is relaxed (Taubin, no shrinking) and remeshed to edge_length; past about 10,000 triangles the edges lengthen to keep the cook light. The form sits on the ground in a pale coral material, with the folds and valleys a deeper coral in Cd (vertex colour; the render multiplies it in). Cook any frame (24 fps): frame 1 is the seed.",
  "params": {
    "seed_size": { "type": "float", "default": 0.5, "min": 0.2, "max": 2, "step": 0.05, "label": "seed size", "desc": "diameter of the round seed it grows from (m)" },
    "rate": { "type": "float", "default": 0.02, "min": 0.005, "max": 0.05, "step": 0.001, "label": "rate", "desc": "how far the surface grows each frame (m)" },
    "lumpiness": { "type": "float", "default": 0.6, "min": 0, "max": 1, "step": 0.05, "label": "lumpiness", "desc": "how unevenly it grows: 0 swells like a balloon, 1 throws out knobs" },
    "edge_length": { "type": "float", "default": 0.05, "min": 0.02, "max": 0.2, "step": 0.005, "label": "edge length", "desc": "edge length the surface keeps as it grows (m); longer only past about 10,000 triangles" },
    "seed": { "type": "int", "default": 1, "min": 0, "max": 9999, "label": "seed", "desc": "random seed of where the knobs come out" }
  },
  "vars": { "seed_size": 0.5, "rate": 0.02, "lumpiness": 0.6, "edge_length": 0.05, "seed": 1 },
  "meta": { "frame": 24, "render": { "views": ["persp"], "azimuth": 30, "elevation": 22, "size": "1600x900", "shading": "pbr", "cd": "multiply" } },
  "nodes": {
    "ball": { "type": "sphere", "params": { "type": "ico", "level": 4, "radius": "=seed_size / 2" } },
    "seed_mesh": { "type": "remesh_isotropic", "in": ["ball"], "note": "the seed at edge_length; finer on a seed too small for it (4 edges across, never below 0.35 × edge_length), coarser past about 10,000 triangles", "params": { "target_length": "=max(edge_length * 0.35, min(edge_length, seed_size * 0.25), sqrt(PI * seed_size * seed_size / 3200))", "iterations": 4, "feature_angle": 0 } },
    "anchor": { "type": "wrangle", "in": ["seed_mesh"], "note": "each point remembers its direction on the seed: the growth noise is read there, so knobs stay put as the form grows", "params": { "run_over": "points", "code": "v@dir = normalize(@P);" } },
    "buds": { "type": "wrangle", "in": ["anchor"], "note": "22 bud centres spread evenly over the upper seed (a Fibonacci spiral turned by seed), each with its own size and the frame (2 to 9) it starts budding", "params": { "run_over": "detail", "code": "const N = 22;\nf[]@bud_x = []; f[]@bud_y = []; f[]@bud_z = []; f[]@bud_c = []; f[]@bud_t = [];\nfor (let i = 0; i < N; i++) {\n  const y = 1 - 1.55 * (i + 0.5) / N, r = sqrt(max(0, 1 - y * y)), a = i * 2.399963 + TAU * rand(seed, 1);\n  const c = normalize(add([r * cos(a), y, r * sin(a)], mul(randdir(i * 31 + seed), 0.12)));\n  @bud_x.push(c.x); @bud_y.push(c.y); @bud_z.push(c.z);\n  @bud_c.push(1 - cos(0.3 * (0.75 + 0.5 * rand(i, seed, 2))));\n  @bud_t.push(floor(8 * rand(i, seed, 3)));\n}" } },
    "grow": { "type": "solver", "in": ["buds"], "params": { "start_frame": 2, "body": { "nodes": { "prev": { "type": "input", "params": { "index": 0 } }, "area": { "type": "measure", "in": ["prev"], "params": { "measure": "total_area", "name": "area" }, "note": "surface area of the last frame, for the triangle budget" }, "nrm": { "type": "normal", "in": ["prev"], "params": { "class": "point" } }, "push": { "type": "wrangle", "in": ["nrm"], "params": { "run_over": "points", "code": "const bx = detail(0, 'bud_x'), by = detail(0, 'bud_y'), bz = detail(0, 'bud_z'), bc = detail(0, 'bud_c'), bt = detail(0, 'bud_t');\nconst dx = v@dir.x, dy = v@dir.y, dz = v@dir.z;\nlet bud = 0;\nfor (let i = 0; i < bx.length; i++) {\n  if (iteration < bt[i]) continue;\n  const u = (1 - (dx * bx[i] + dy * by[i] + dz * bz[i])) / bc[i];\n  if (u < 1) { const s = max(0, u - 0.0625) / 0.9375, f = 1 - s * s * (3 - 2 * s); if (f > bud) bud = f; }\n}\nconst lobe = 1 + 0.7 * lumpiness * snoise(mul(v@dir, 12), seed + 9);\nconst g = (1 - lumpiness) * 0.6 + lumpiness * lobe * 2.4 * bud * bud;\nconst base = smoothstep(-0.95, -0.4, dy);\nconst d = normalize(add(mul(normalize(@P), 0.5), mul(@N, 0.5)));\n@P = add(@P, mul(d, rate * g * base));" }, "note": "buds grow fastest at their centres (a smooth cap around each centre on the seed directions), so they bulge out on narrow necks; lumpiness also splits each bud into lobes (noise) and slows the growth between the buds" }, "apart": { "type": "wrangle", "in": ["push"], "note": "lobes that grow into each other push apart where they meet, leaving a fold instead of crossing", "params": { "run_over": "points", "code": "const R = 1.6 * edge_length;\nlet push = [0, 0, 0];\nfor (const j of nearpoints(0, @P, R, 8)) {\n  if (j === @ptnum) continue;\n  const q = point(0, 'P', j), nq = point(0, 'N', j);\n  const dv = sub(@P, q), dist = length(dv);\n  if (dist > 1e-9 && dot(@N, nq) < -0.3 && dot(nq, dv) > 0) push = add(push, mul(dv, 0.5 * (R - dist) / dist));\n}\n@P = add(@P, push);" } }, "relax": { "type": "smooth", "in": ["apart"], "params": { "iterations": 2, "strength": 0.3, "method": "taubin" } }, "even": { "type": "remesh_isotropic", "in": ["relax"], "params": { "target_length": "=max(edge_length * 0.35, min(edge_length, 0.25 * max(bbox('prev').size.x, bbox('prev').size.z)), sqrt(prim('area', 0, 'area') / 3200))", "iterations": 2, "feature_angle": 0 }, "note": "back to edge_length (sqrt(area / 3200) keeps it under about 10,000 triangles)" } }, "output": "even" } } },
    "grounded": { "type": "match_size", "in": ["grow"], "note": "sits on the ground, centred" },
    "shaded": { "type": "normal", "in": ["grounded"], "params": { "class": "point" } },
    "curved": { "type": "measure", "in": ["shaded"], "params": { "measure": "curvature" } },
    "tint": { "type": "color", "in": ["curved"], "note": "folds and valleys a deeper coral, knob tops pale (vertex colour; the render multiplies it into the material)", "params": { "mode": "ramp", "attrib": "curvature", "range": [-8, 10], "ramp": [[0, "#dc9482"], [0.22, "#f3cfc2"], [0.4, "#fff6f0"], [1, "#ffffff"]] } },
    "named": { "type": "attrib_create", "in": ["tint"], "params": { "name": "name", "class": "prim", "value": "coral" } },
    "coral": { "type": "material", "in": ["named"], "params": { "name": "coral", "base_color": "#f7e1d5", "roughness": 0.6 } },
    "clean": { "type": "attrib_delete", "in": ["coral"], "params": { "names": "dir curvature bud_x bud_y bud_z bud_c bud_t" } },
    "closed": { "type": "assert", "in": ["clean"], "params": { "label": "closed", "watertight": true, "no_flipped": true, "expr": "pieces == 1" } },
    "seed_volume": { "type": "measure", "in": ["seed_mesh"], "params": { "measure": "volume", "name": "volume" } },
    "form_volume": { "type": "measure", "in": ["closed"], "params": { "measure": "volume", "name": "volume" } },
    "grows": { "type": "assert", "in": ["form_volume"], "note": "the form holds at least the seed's volume (1e-6: float rounding only; at frame 1 the form is the seed)", "params": { "label": "grows", "attrib": "volume", "attrib_class": "prim", "attrib_min": "=prim('seed_volume', 0, 'volume') * (1 - 1e-6)" } },
    "even": { "type": "assert", "in": ["grows"], "params": { "label": "even", "min_edge_length": "=edge_length / 4" } },
    "form_clean": { "type": "attrib_delete", "in": ["even"], "params": { "names": "volume" } },
    "form": { "type": "null", "in": ["form_clean"] }
  },
  "output": "form",
  "outputs": {
    "form": { "node": "form", "label": "Form", "kind": "mesh", "tags": ["coral", "growth", "organic", "animated"], "description": "The form at the current frame: one closed, outward-facing triangle mesh with edges of about edge_length, sitting on the ground, in a pale coral material; point Cd darkens the folds and valleys." }
  }
}
