Skip to content

Modifier stack ​

A list of modeling steps applied in order, like Blender's modifier stack. The list is plain data: keep the original mesh and the list, change a number, apply again. Save it, send it, write it by hand, or have it written.

js
import { applyModifiers, MODIFIERS } from "./trid/index.js";
one stack, step by step: a box · subdivided · shaped with a lattice · bent · roughened — drag to orbit
js
import { Scene, makeShape, applyModifiers, WebGLRenderer, OrbitCamera } from "./trid/index.js";

// One stack, as data: a box made soft, shaped like a gourd, bent, and roughened
const stack = [
  { type: "subdivide", levels: 3 },
  { type: "lattice", div: 3, interpolation: "cardinal", edits: [
    { select: { y: -1 }, scale: { x: 0.45, z: 0.45 } },
    { select: { y: 1 }, scale: { x: 1.25, z: 1.25 } },
  ] },
  { type: "bend", axis: "y", angle: 0.9, toward: "x", from: 0.4 },
  { type: "noise", amount: 0.02, size: 0.06, seed: 4 },
];
const base = makeShape("box", { w: 0.3, h: 0.5, d: 0.3 });

export default function (canvas) {
  const scene = new Scene(canvas, {
    width: 800, height: 280, camera: { fov: 0.6 },
    renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
  });
  // The stack applied step by step: none, the first, the first two, ...
  for (let n = 0; n <= stack.length; n++) {
    scene.add({
      mesh: applyModifiers(base, stack.slice(0, n)),
      pos: { x: (n - 2) * 0.5, y: 0, z: 0 },
      material: { fill: "#E9806E", smooth: n > 0 },
    });
  }
  scene.use(new OrbitCamera({ distance: 2.2, pitch: -0.25 }));
  scene.start();
  return () => scene.destroy();
}

applyModifiers(mesh, modifiers) ​

Applies the steps in order and returns a new mesh: the original is never changed. Each step is { type, ...options }, with the options of the function it stands for. enabled: false skips a step, like Blender's eye icon, without removing it.

js
const vase = applyModifiers(lathe(profile), [
  { type: "lattice", div: 3, edits: [{ select: { y: 1 }, scale: 0.7 }] },
  { type: "twist", angle: 0.4 },
  { type: "noise", amount: 0.004, enabled: false }, // off for now
]);

Order matters, as in Blender: tapering then mirroring isn't mirroring then tapering. When a step fails, the error says which one and why, like Modifier 3 ("lattice"): latticePoints: no y index 5 (the lattice has 3). The original error is kept as its cause.

Steps ​

typeDoesOptions
"mirror"Mirrors across a plane, keeping both halvesas mirrorMesh
"symmetrize"Copies one side over the otheras symmetrizeMesh
"bisect"Cuts along a plane, keeps one sideas bisectMesh
"array"Copies a step apartas arrayMesh
"radialArray"Copies around an axisas radialArrayMesh
"subdivide"Smooths and adds facesas subdivideMesh
"bevel"Rounds or cuts edgesas bevelMesh
"boolean"Union, subtract or intersectop (default "union"), with: a mesh, an object or a list; as booleanMesh
"taper", "twist", "bend", "bulge"Deformsas the deformers
"sculpt"One brushbrush, then as sculptMesh
"noise"Lumps all over, along the surfaceamount: their height (default a fifth of size); size: about how wide (default a fifth of the mesh); seed
"lattice"Reshapes with a cage of pointssee below
"remesh"Even, sculptable facesas remeshMesh
"weld"Merges points at the same placedistance
"clean"Removes what's brokenas cleanMesh
"fixWinding"Turns faces outward
"transform"Moves, turns, scales the verticespos, rot, scale

Lattice steps ​

A lattice made in code can be given as { type: "lattice", lattice }. As data, a step makes its own lattice around the mesh as it is at that step, and edits it:

js
{
  type: "lattice", div: 3, padding: 0, interpolation: "cardinal",
  edits: [
    { select: { y: -1 }, scale: { x: 1.5, z: 1.5 } },  // the top layer, wider
    { select: { y: 1 }, scale: 0.7 },                   // the middle layer, smaller
    { select: { x: -1, y: -1 }, move: { y: 0.1 } },     // one row, lifted
    { select: { y: -1 }, rotate: { y: 0.5 } },          // the top layer, turned
  ],
}

Each edit picks points like latticePoints (one index: a layer, two: a row, three: a point; negative: from the end), then scales and turns them around their middle, and moves them, in that order. Edits run one after the other.

Your own steps ​

MODIFIERS holds the step types by name. Add a function of the mesh so far and the step: return a new mesh, or change the mesh in place and return nothing (it's the stack's own copy, never the original).

js
MODIFIERS.lift = (mesh, { by = 0.1 }) => {
  for (const v of mesh.vertices) v.y += by;
};
applyModifiers(base, [{ type: "lift", by: 0.5 }]);

Good to know ​

  • A stack is data. Steps hold numbers, names and lists, so a stack survives JSON.stringify and gives the same mesh when read back. The exceptions are steps that hold other meshes (boolean's with, a lattice object): store those your own way.
  • Each apply starts over. The whole list runs on a copy of the original every time, so a stack with remesh or boolean steps takes as long as those steps. For live editing, keep heavy steps early and apply them once, then run the rest of the list on their result.
  • Lattice steps fit the mesh at their place in the list: after a mirror, a lattice surrounds the whole mirrored mesh.