Lattice
A coarse cage of control points around a mesh, like Blender's Lattice modifier: move a few points and the whole shape follows smoothly. Widen a top, pinch a waist, lean a tower, twist a column, bulge a belly, with a handful of numbers instead of one per vertex.
import { makeLattice, latticePoints, latticeMesh, latticeCage } from "./trid/index.js";import {
Scene, lathe, cloneMesh, makeLattice, latticePoints, latticeMesh, latticeCage, WebGLRenderer, OrbitCamera,
} from "./trid/index.js";
// Columns with many rows (a lattice only moves vertices, so it needs plenty): round, and square
const rows = (r) => Array.from({ length: 31 }, (_, i) => ({ r, y: -0.45 + (i / 30) * 0.9 }));
const column = lathe(rows(0.2), { segments: 48, capBottom: true, capTop: true });
const square = lathe(rows(0.2), { segments: 4, capBottom: true, capTop: true });
// A vase: the top layer wider, the middle pinched ("cardinal": the shape passes through the points)
const vaseLattice = makeLattice(column, { div: { x: 3, y: 4, z: 3 }, padding: 0.02 });
for (const p of latticePoints(vaseLattice, { y: -1 })) { p.x *= 1.6; p.z *= 1.6; }
for (const p of latticePoints(vaseLattice, { y: 1 })) { p.x *= 0.55; p.z *= 0.55; }
const vase = latticeMesh(cloneMesh(column), vaseLattice, { interpolation: "cardinal" });
// A creature-ish lean: the upper layers pushed forward and to the side, one corner pulled out
const leanLattice = makeLattice(column, { div: 3, padding: 0.02 });
for (const p of latticePoints(leanLattice, { y: 2 })) { p.x += 0.18; p.z -= 0.1; }
for (const p of latticePoints(leanLattice, { y: 1 })) p.x += 0.06;
latticePoints(leanLattice, { x: 0, y: 0, z: 0 })[0].x -= 0.25;
const lean = latticeMesh(cloneMesh(column), leanLattice);
// Live: the top layer of a square column turns back and forth, and the mesh follows every frame
const twistLattice = makeLattice(square, { div: 3, padding: 0.02 });
const rest = twistLattice.points.map((p) => ({ ...p }));
const twist = { mesh: cloneMesh(square) };
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 340, camera: { fov: 0.6 }, color: "#8ab4f8",
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
});
const shapes = [vase, lean, null];
const lattices = [vaseLattice, leanLattice, twistLattice];
let twistCage;
["#E9806E", "#98c379", "#e5c07b"].forEach((fill, i) => {
const pos = { x: (i - 1) * 1.05, y: 0, z: 0 };
const obj = i === 2 ? twist : { mesh: shapes[i] };
scene.add(Object.assign(obj, { pos, material: { fill, smooth: i < 2 } }));
const cage = scene.add({ mesh: latticeCage(lattices[i]), pos }); // the cage, as lines
if (i === 2) twistCage = cage;
});
scene.use(new OrbitCamera({ distance: 3.6, pitch: -0.3 }));
const top = latticePoints(twistLattice, { y: -1 });
scene.start((dt, time) => {
// Turn the top layer from its rest places, then apply to a fresh copy
const a = Math.sin(time * 1.2) * 0.9;
top.forEach((p) => {
const r = rest[twistLattice.points.indexOf(p)];
p.x = r.x * Math.cos(a) - r.z * Math.sin(a);
p.z = r.x * Math.sin(a) + r.z * Math.cos(a);
});
twist.mesh = latticeMesh(cloneMesh(square), twistLattice);
twistCage.mesh.dirty = true; // the cage's points moved
});
return () => scene.destroy();
}const lattice = makeLattice(vase, { div: 3 }); // 3 × 3 × 3 points around it
for (const p of latticePoints(lattice, { y: -1 })) { p.x *= 1.5; p.z *= 1.5; } // the top layer, wider
latticeMesh(vase, lattice); // the vase followsA lattice only moves vertices, so the mesh needs plenty of them where it bends: build it with rows (lathe, loft, sweep), subdivide it, or remeshMesh it.
makeLattice(input, opts?)
A lattice around a mesh's box (or around any box { min, max }), its points spread evenly over it.
| Option | Default | Meaning |
|---|---|---|
div | 3 | Points per side: a number, or { x, y, z }. At least 2 (a side of no size, like a flat sheet's, gets 1) |
padding | 0 | Make the box this much bigger on every side, so the cage sits a little off the surface |
A lattice is plain data, in the mesh's own space: { min, max, div, points }. points[i + div.x * (j + div.y * k)] is the point i along x, j along y and k along z, counted from the low corner. Move the points freely; where they started is remembered by the box and div, so they can always be compared with their rest places.
latticePoints(lattice, which?)
Picks a lattice's points to move: one index gives a layer, two a row, three a single point. Negative indices count from the end. The points are the lattice's own, so changing them changes the lattice.
latticePoints(lattice, { y: 0 }); // the bottom layer
latticePoints(lattice, { y: -1 }); // the top layer
latticePoints(lattice, { x: 0, z: -1 }); // one vertical row: left, back
latticePoints(lattice, { x: 1, y: 1, z: 1 })[0] // the middle point of a 3 × 3 × 3 latticelatticeMesh(mesh, lattice, opts?)
Deforms a mesh by its lattice, in place, like the deformers: each vertex moves by how far the points around it moved from their rest places, blended smoothly. An untouched lattice changes nothing; a lattice shifted, scaled or sheared as a whole moves the mesh exactly that way. Vertices outside the lattice's box move like the nearest place on it.
interpolation sets how the points' moves blend:
| Interpolation | Shape | A vertex at a moved point |
|---|---|---|
"bspline" (default, like Blender) | The smoothest, soft | Moves less than the point: 2/3 as far as its layer when a whole layer inside the lattice moves, about 0.3 as far when one point moves alone |
"cardinal" | Smooth, follows the points | Moves exactly with it: easiest to aim |
"linear" | Straight between points, creased at them | Moves exactly with it |
Make the lattice for the mesh as it is, move its points, and apply it once. To try other moves, apply the lattice to a fresh copy of the original each time (cloneMesh), like the live column in the demo.
latticeCage(lattice)
The lattice as a wireframe mesh, to show it: lines between neighboring points. Its vertices are the lattice's points (the same objects), so it always shows the lattice as it is. After moving points, set the cage's dirty so it's drawn again, and apply the lattice again to a copy of the original mesh.
const cage = scene.add({ mesh: latticeCage(lattice), pos: vase.pos }); // no material: drawn as lines