{
  "version": 1,
  "description": "A tablecloth draped over a table: a square cloth of cloth_size laid flat and centred (shifted a little by seed) on a wooden table 0.68 m high with round corners (corner radius 0.35 × its shorter side; a top with rounded edges, four legs and an apron). The cloth over the table top stays where it lies; the rest falls over the edges for 2.7 seconds around a collision proxy of the top 4 cm wider, gathers into vertical folds where it turns the round corners, its corners falling into points, and pools a little where it reaches the floor (at the defaults the long sides and the corners do). stiffness sets the bending rigidity from silk through linen and cotton to canvas, thickness the cloth's thickness (the output is its mid-surface, half the thickness off the table and the floor). cloth: the draped sheet with uv 0..1 over the whole cloth, in linen (double-sided); table: the table, in wood.",
  "params": {
    "table_width": { "type": "float", "default": 1.6, "min": 0.6, "max": 3, "step": 0.05, "label": "table width", "desc": "width of the table along X (m)" },
    "table_depth": { "type": "float", "default": 0.9, "min": 0.6, "max": 2, "step": 0.05, "label": "table depth", "desc": "depth of the table along Z (m)" },
    "cloth_size": { "type": "float", "default": 2.4, "min": 1, "max": 4, "step": 0.05, "label": "cloth size", "desc": "side of the square cloth (m)" },
    "stiffness": { "type": "float", "default": 0.3, "min": 0, "max": 1, "step": 0.05, "label": "stiffness", "desc": "how stiff the cloth is: 0 hangs like silk, 0.5 like cotton, 1 like canvas" },
    "thickness": { "type": "float", "default": 0.004, "min": 0.002, "max": 0.02, "step": 0.001, "label": "thickness", "desc": "thickness of the cloth (m): how far it keeps off the table and the floor" },
    "seed": { "type": "int", "default": 1, "min": 0, "max": 9999, "label": "seed", "desc": "random seed of how the cloth is dropped, so of how the folds fall" }
  },
  "vars": { "table_width": 1.6, "table_depth": 0.9, "cloth_size": 2.4, "stiffness": 0.3, "thickness": 0.004, "seed": 1 },
  "meta": { "render": { "node": "preview", "views": ["persp"], "azimuth": 55, "elevation": 18, "size": "1600x900", "shading": "pbr" } },
  "nodes": {
    "top_slab": { "type": "shape2d", "note": "the outline of the table top, a rounded rectangle: round corners gather the cloth that hangs over them into vertical folds (over sharp corners a square cloth hangs in flat panels with a flat flap at each corner)", "params": { "shape": "rounded_rect", "size": ["=table_width", "=table_depth"], "corner_radius": "=0.35 * min(table_width, table_depth)", "corner_segments": 8, "center": [0, 0.64, 0] } },
    "top_solid": { "type": "extrude", "in": ["top_slab"], "params": { "height": 0.04 } },
    "top_round": { "type": "poly_bevel", "in": ["top_solid"], "params": { "group": "angle>80", "offset": 0.012, "divisions": 2 } },
    "top_named": { "type": "attrib_create", "in": ["top_round"], "params": { "name": "name", "class": "prim", "value": "table_top" } },
    "leg": { "type": "box", "params": { "size": [0.06, 0.64, 0.06], "center": [0, 0.32, 0] } },
    "leg_spots": { "type": "points", "params": { "positions": [["=-(table_width / 2 - (0.105 * min(table_width, table_depth) + 0.06))", 0, "=-(table_depth / 2 - (0.105 * min(table_width, table_depth) + 0.06))"], ["=table_width / 2 - (0.105 * min(table_width, table_depth) + 0.06)", 0, "=-(table_depth / 2 - (0.105 * min(table_width, table_depth) + 0.06))"], ["=-(table_width / 2 - (0.105 * min(table_width, table_depth) + 0.06))", 0, "=table_depth / 2 - (0.105 * min(table_width, table_depth) + 0.06)"], ["=table_width / 2 - (0.105 * min(table_width, table_depth) + 0.06)", 0, "=table_depth / 2 - (0.105 * min(table_width, table_depth) + 0.06)"]] }, "note": "the legs stand in from the round corners" },
    "legs": { "type": "copy_to_points", "in": ["leg", "leg_spots"] },
    "legs_named": { "type": "attrib_create", "in": ["legs"], "params": { "name": "name", "class": "prim", "value": "table_legs" } },
    "apron_long": { "type": "box", "params": { "size": ["=table_width - 2 * (0.105 * min(table_width, table_depth) + 0.06)", 0.09, 0.022], "center": [0, 0.595, 0] } },
    "apron_front_back": { "type": "copy_xform", "in": ["apron_long"], "params": { "count": 2, "translate": [0, 0, "=table_depth - 2 * (0.105 * min(table_width, table_depth) + 0.06)"], "copy_attrib": "" } },
    "apron_fb_at": { "type": "transform", "in": ["apron_front_back"], "params": { "translate": [0, 0, "=-(table_depth / 2 - (0.105 * min(table_width, table_depth) + 0.06))"] } },
    "apron_short": { "type": "box", "params": { "size": [0.022, 0.09, "=table_depth - 2 * (0.105 * min(table_width, table_depth) + 0.06)"], "center": [0, 0.595, 0] } },
    "apron_sides": { "type": "copy_xform", "in": ["apron_short"], "params": { "count": 2, "translate": ["=table_width - 2 * (0.105 * min(table_width, table_depth) + 0.06)", 0, 0], "copy_attrib": "" } },
    "apron_sides_at": { "type": "transform", "in": ["apron_sides"], "params": { "translate": ["=-(table_width / 2 - (0.105 * min(table_width, table_depth) + 0.06))", 0, 0] } },
    "apron": { "type": "merge", "in": ["apron_fb_at", "apron_sides_at"] },
    "apron_named": { "type": "attrib_create", "in": ["apron"], "params": { "name": "name", "class": "prim", "value": "table_apron" } },
    "table_parts": { "type": "merge", "in": ["top_named", "legs_named", "apron_named"] },
    "wood": { "type": "material", "in": ["table_parts"], "params": { "name": "wood", "base_color": "#7a4a2a", "roughness": 0.55 } },
    "table": { "type": "null", "in": ["wood"] },
    "collider_slab": { "type": "shape2d", "note": "the cloth's collision proxy: the table top grown by 4 cm, 10 cm thick, with 3 cm round edges. Workaround: drape collides the cloth's points only, so a table edge passes between the points of the cloth and pokes through", "params": { "shape": "rounded_rect", "size": ["=table_width + 0.08", "=table_depth + 0.08"], "corner_radius": "=0.35 * min(table_width, table_depth) + 0.04", "corner_segments": 8, "center": [0, 0.58, 0] } },
    "collider_solid": { "type": "extrude", "in": ["collider_slab"], "params": { "height": 0.1 } },
    "collider": { "type": "poly_bevel", "in": ["collider_solid"], "params": { "group": "angle>80", "offset": 0.03, "divisions": 3 } },
    "sheet": { "type": "grid", "params": { "size": ["=cloth_size", "=cloth_size"], "divisions": ["=clamp(round(cloth_size / 0.03), 25, 120)", "=clamp(round(cloth_size / 0.03), 25, 120)"], "center": [0, 0, 0] }, "note": "3 cm quads: fine enough for the folds at the corners" },
    "laid": { "type": "transform", "in": ["sheet"], "note": "laid flat on the table top, its mid-surface half the thickness above it, shifted a little (by seed) so the folds fall differently", "params": { "translate": ["=(rand(1, seed) - 0.5) * 0.06", "=0.68 + thickness / 2", "=(rand(2, seed) - 0.5) * 0.06"] } },
    "on_top": { "type": "wrangle", "in": ["laid"], "note": "the cloth over the table top (inside its rounded outline) stays where it lies (static friction): pinned; the rest drapes over the edges. Workaround: unpinned, drape lets a cloth lying on a flat top buckle into small ears (and a stiff one lifts a few mm before the edge), and the top must stay flat (rests)", "params": { "run_over": "points", "code": "const r = 0.35 * min(table_width, table_depth);\nconst dx = max(abs(@P.x) - (table_width / 2 - r), 0), dz = max(abs(@P.z) - (table_depth / 2 - r), 0);\ni@pin = dx * dx + dz * dz < r * r ? 1 : 0;" } },
    "drape": { "type": "drape", "in": ["on_top", "collider"], "params": { "steps": 64, "substeps": 6, "thickness": "=thickness / 2", "bend_stiffness": "=pow(10, -5.5 + 2.5 * stiffness)", "friction": 0.6, "damping": 1.0, "seed": "=seed" }, "note": "6 substeps, a workaround: at drape's default 4, a cloth this fine keeps jittering where it hangs (column by column, about 0.2 m/s after 3 s), which reads as dents and false folds" },
    "smoothed": { "type": "subdivide", "in": ["drape"], "note": "one level of smoothing: 1.5 cm quads, soft folds", "params": { "levels": 1, "boundary": "sharp" } },
    "cloth_normals": { "type": "normal", "in": ["smoothed"], "params": { "class": "point" } },
    "cloth_named": { "type": "attrib_create", "in": ["cloth_normals"], "params": { "name": "name", "class": "prim", "value": "tablecloth" } },
    "linen": { "type": "material", "in": ["cloth_named"], "params": { "name": "linen", "base_color": "#e9e2d0", "roughness": 0.85, "double_sided": true } },
    "cloth_clean": { "type": "attrib_delete", "in": ["linen"], "params": { "names": "v rest_P pin pinned" } },
    "above_floor": { "type": "assert", "in": ["cloth_clean"], "params": { "label": "above_floor", "expr": "bbox.min.y >= 0" } },
    "rests": { "type": "assert", "in": ["above_floor"], "note": "assert expr cannot read other nodes, so the table top height 0.68 m (top_slab) is written in", "params": { "label": "rests", "expr": "abs(bbox.max.y - 0.68) <= thickness" } },
    "uv": { "type": "assert", "in": ["rests"], "params": { "label": "uv", "attrib": "uv", "attrib_min": 0, "attrib_max": 1 } },
    "cloth": { "type": "null", "in": ["uv"] },
    "preview": { "type": "merge", "in": ["cloth", "table"], "note": "for the render: the cloth on its table" }
  },
  "output": "cloth",
  "outputs": {
    "cloth": { "node": "cloth", "label": "Cloth", "kind": "mesh", "tags": ["tablecloth", "cloth", "linen", "set-dressing"], "description": "The tablecloth draped over the table: one quad sheet (1.5 cm quads at the defaults) with uv 0..1 over the whole cloth, in a double-sided linen material." },
    "table": { "node": "table", "label": "Table", "kind": "mesh", "tags": ["table", "furniture", "wood", "set-dressing"], "description": "The table the cloth lies on: a top with round corners and rounded edges, four legs and an apron, 0.68 m high, in wood." }
  }
}
