Mirror & array
Model one side and get the whole, or one piece and get the row: Blender's Mirror and Array modifiers, as functions. They work on meshes (joined into one new mesh) and on scene objects (separate copies that share the mesh and material). Nothing here changes its input.
import {
mirrorMesh, symmetrizeMesh, bisectMesh, arrayMesh, radialArrayMesh,
mirrorObject, arrayObject, radialArrayObject,
} from "./trid/index.js";import {
Scene, makeShape, extrude, displaceMesh, transformMesh, fbm3,
mirrorMesh, symmetrizeMesh, arrayMesh, radialArrayMesh, OrbitCamera,
} from "./trid/index.js";
// Mirror: half a heart, drawn on the right of x = 0, mirrored to the left and welded at the seam
const halfHeart = extrude(
[{ x: 0, y: -0.2 }, { x: 0.13, y: -0.05 }, { x: 0.19, y: 0.07 }, { x: 0.16, y: 0.16 }, { x: 0.08, y: 0.18 }, { x: 0, y: 0.11 }],
0.08,
);
const heart = mirrorMesh(halfHeart, { axis: "x" });
// Symmetrize: a lumpy, lopsided rock, made exactly symmetric from its right side
const lumpy = displaceMesh(makeShape("sphere", { r: 0.17 }), (v) => {
const k = 1 + 0.5 * (fbm3(v.x * 6 + 3, v.y * 6, v.z * 6) - 0.5);
return { x: v.x * k, y: v.y * k * 1.2, z: v.z * k };
});
const mask = symmetrizeMesh(lumpy, { axis: "x", keep: "+" });
// Array: a spiral staircase, each step a bit higher and turned 1/12 of a turn
const step = transformMesh(makeShape("box", { w: 0.15, h: 0.02, d: 0.05 }), { pos: { x: 0.1, y: -0.2, z: 0 } });
const stairs = arrayMesh(step, { count: 12, offset: { y: 0.035 }, rotate: { y: Math.PI / 6 } });
// Radial array: 7 petals around the middle of a flower
const petal = transformMesh(makeShape("sphere", { r: 0.1 }), { pos: { x: 0.12, y: 0, z: 0 }, scale: { x: 1, y: 0.25, z: 0.45 } });
const flower = radialArrayMesh(petal, { count: 7, axis: "y" });
export default function (canvas) {
const scene = new Scene(canvas, { width: 800, height: 320, lineWidth: 1, camera: { z: -2, fov: 0.6 } });
const colors = ["#E9806E", "#a0a8b8", "#e5c07b", "#c678dd"];
[heart, mask, stairs, flower].forEach((mesh, i) => {
scene.add({
mesh,
pos: { x: -0.75 + i * 0.5, y: i === 3 ? 0.05 : 0, z: 0 },
rot: { x: i === 3 ? -1.1 : 0, y: 0, z: 0 },
material: { fill: colors[i], smooth: i === 1 || i === 3 },
});
});
scene.use(new OrbitCamera({ pitch: -0.25, autoRotate: true, autoRotateSpeed: 0.4, autoRotateDelay: 2 }));
scene.start();
return () => scene.destroy();
}Planes
Mirroring and cutting happen across a plane:
| Option | Default | Meaning |
|---|---|---|
axis | "x" | The plane where that coordinate stays the same: "x" mirrors left and right, "y" up and down, "z" front and back |
normal | none | Any plane instead, by the direction it faces (replaces axis) |
center | the origin | A point the plane goes through. For meshes, the mesh's own origin; for objects, the parent's origin. { x: 0.2 } is enough for an "x" plane |
The "+" side of a plane is the side its normal points to: x > 0 for axis: "x".
mirrorMesh(mesh, opts?)
Mirrors a mesh and keeps both halves. Build the right half of a chair, a face or a car, and mirror it to get the whole.
| Option | Default | Meaning |
|---|---|---|
axis, normal, center | "x" through the origin | The plane. axis can be a list: ["x", "z"] mirrors across both, 4 copies |
weld | true | Points on the plane are shared by both halves, so the seam is closed and smooth, and faces lying on the plane (now inside) are removed. true uses a hundred-thousandth of the mesh's size as "on the plane"; a number sets that distance; false keeps two separate copies |
bisect | false | First cut away what's past the plane, so the result is exactly symmetric (see symmetrizeMesh) |
keep | "+" | The side bisect keeps |
const legs = mirrorMesh(leftLegs, { axis: ["x", "z"] }); // one leg → four
const body = mirrorMesh(halfBody); // welded at x = 0The mirrored half has the same texture coordinates (so the texture is mirrored too) and the same materials. Its faces are turned around, so they still face outward.
Put the seam on the plane
For a closed result, the half should end exactly on the plane: an open edge at x = 0, or a flat side there (it's removed). Points a hair away are moved onto it. A half that crosses the plane comes out overlapping its mirror image. Use bisect: true to cut that part off first.
symmetrizeMesh(mesh, opts?)
Makes a mesh exactly symmetric: cuts it along the plane, then copies the kept side over the other, welded at the seam. It's the same as mirrorMesh(mesh, { ...opts, bisect: true }), and it's Blender's Symmetrize. Good after noise or a deformer made a shape lopsided:
const head = symmetrizeMesh(lumpyHead, { axis: "x", keep: "+" }); // the right side winsbisectMesh(mesh, opts?)
Cuts a mesh along a plane and keeps one side (keep: "+" by default, or "-"). Faces crossing the plane are cut, with their texture coordinates and normals carried over. The cut is left open:
const dome = bisectMesh(makeShape("sphere"), { axis: "y" }); // the top half
const cup = bisectMesh(makeShape("sphere"), { axis: "y", center: { y: 0.1 }, keep: "-" });Faces are cut as flat, convex polygons, like the ones the renderers draw. Edges are cut too, and an edge loop that gets cut comes out as separate segments. distance sets how close to the plane a point is moved onto it (default: a hundred-thousandth of the mesh's size).
arrayMesh(mesh, opts?)
Repeats a mesh, each copy a step further than the one before, and joins the copies into one mesh. A step is any mix of:
| Option | Default | Meaning |
|---|---|---|
count | 2 | Number of copies, the first where the mesh is |
relative | { x: 1 } when no step is given | A move in sizes of the mesh: { x: 1 } puts the copies side by side, touching; { x: 1.2 } leaves a gap of a fifth |
offset | none | A move in units |
rotate | none | A turn per copy, in radians, like an object's rot, around center |
scale | none | A size factor per copy (0.9 = each a bit smaller), around center |
center | the origin | The point rotate and scale work around |
weld | false | Copies that touch share their points and the faces between them are removed, so touching copies become one solid. true or a distance |
const fence = arrayMesh(plank, { count: 8, relative: { x: 1.3 } });
const stairs = arrayMesh(step, { count: 16, offset: { y: 0.2 }, rotate: { y: Math.PI / 8 } }); // a spiral
const shell = arrayMesh(chamber, { count: 20, rotate: { y: 0.5 }, scale: 0.9 }); // smaller and smaller
const beam = arrayMesh(block, { count: 5, weld: true }); // one long boxradialArrayMesh(mesh, opts?)
Copies of a mesh around an axis: wheel spokes, petals, gear teeth, columns around a temple. Place the first copy off the axis (on +x for a "y" axis), and the others follow around.
| Option | Default | Meaning |
|---|---|---|
count | 6 | Number of copies |
axis | "y" | Axis they turn around, in the same direction as rot |
angle | 2π | A full turn spreads them evenly; a smaller angle goes from the first copy to the last (Math.PI with 3 copies: at 0°, 90° and 180°) |
center | the origin | A point the axis goes through |
weld | false | As in arrayMesh |
const wheel = radialArrayMesh(spoke, { count: 12, axis: "z" });Objects: mirrorObject, arrayObject, radialArrayObject
The same for scene objects. The copies are separate objects that share the original's mesh and material, so nothing is duplicated, and each one can still be moved, hidden or picked on its own. They aren't added to the scene: add them where you want them.
// The right arm from the left one
robot.children.push(mirrorObject(leftArm, { axis: "x" }));
// A row of 5 columns, 2 apart; four legs around a round table
scene.add({ children: arrayObject(column, { count: 5, offset: { x: 2 } }) });
table.children.push(...radialArrayObject(leg, { count: 4 }));mirrorObject(obj, opts?)takes a plane in the object's parent's space. The copy is mirrored with a negative scale on its local axis that points most along the plane's normal (an unturned object mirrored in"x"getsscale.x = -1), and it draws exactly the mirror image, light and shadows included.arrayObject(obj, opts?)andradialArrayObject(obj, opts?)return a list ofcountobjects, the first where the original is. Add them instead of the original, or skip the first.relativeis in sizes of the object as it stands (with its scale and turn), andscaleis a single factor.
Children come along as copies, with their own transforms; their meshes are shared too.