Sculpting
Blender's sculpt brushes as one function: sculptMesh(mesh, brush, opts) pushes, pulls, smooths and carves a mesh around a point or along a path, fading out at a radius. To paint them with the mouse, see SculptTool. Like the deformers, it changes the mesh in place and returns it. The mesh's own normals (from an OBJ file) are removed, and the renderers compute new ones.
import { sculptMesh, SCULPT_BRUSHES, remeshMesh } from "./trid/index.js";import { Scene, makeShape, displaceMesh, fbm3, sculptMesh, WebGLRenderer, OrbitCamera } from "./trid/index.js";
// Dense balls to sculpt (a brush only moves vertices, so it needs many), facing the camera at -z
const R = 0.1;
const ball = () => makeShape("sphere", { r: R, rings: 48, segments: 96 });
const front = { x: 0, y: 0, z: -R };
const brushes = [
// A bump, pushed out along the surface
["inflate", sculptMesh(ball(), "inflate", { center: front, radius: 0.06, strength: 0.03 })],
// A snout, pulled forward and up
["grab", sculptMesh(ball(), "grab", { center: front, radius: 0.07, delta: { y: 0.02, z: -0.06 } })],
// A lumpy ball, smoothed on the front only
["smooth", sculptMesh(
displaceMesh(ball(), (v) => {
const k = 1 + 0.3 * (fbm3(v.x * 25, v.y * 25, v.z * 25, { octaves: 3 }) - 0.5);
return { x: v.x * k, y: v.y * k, z: v.z * k };
}),
"smooth", { center: front, radius: 0.1, strength: 1, iterations: 40 },
)],
// A flat face
["flatten", sculptMesh(ball(), "flatten", { center: front, radius: 0.08, strength: 1, falloff: "constant" })],
// A ridge raised down the front, then pinched into a sharp fin
["pinch", (() => {
const fin = [{ x: 0, y: 0.06, z: -0.08 }, { x: 0, y: 0, z: -0.1 }, { x: 0, y: -0.06, z: -0.08 }];
const m = sculptMesh(ball(), "inflate", { along: fin, radius: 0.03, strength: 0.015 });
return sculptMesh(m, "pinch", { along: fin.map((p) => ({ ...p, z: p.z - 0.015 })), radius: 0.035, strength: 1 });
})()],
// A smile: a groove along a curve
["crease", sculptMesh(ball(), "crease", {
along: [{ x: -0.05, y: 0, z: -0.087 }, { x: -0.025, y: -0.025, z: -0.093 }, { x: 0, y: -0.032, z: -0.095 }, { x: 0.025, y: -0.025, z: -0.093 }, { x: 0.05, y: 0, z: -0.087 }],
radius: 0.015, strength: 0.008,
})],
// Rough skin on the front
["noise", sculptMesh(ball(), "noise", { center: front, radius: 0.08, strength: 0.012, scale: 6 })],
];
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 240, camera: { fov: 0.6 },
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
});
const colors = ["#E9806E", "#e5c07b", "#a0a8b8", "#98c379", "#5f8fd8", "#c678dd", "#56b6c2"];
// Each ball turned a bit, so its sculpted front is seen from the side
brushes.forEach(([, mesh], i) => {
scene.add({ mesh, pos: { x: (i - 3) * 0.27, y: 0, z: 0 }, rot: { x: 0, y: -0.7, z: 0 }, material: { fill: colors[i], smooth: true } });
});
scene.use(new OrbitCamera({ target: { x: 0, y: 0, z: 0 }, distance: 1.05, pitch: -0.15 }));
scene.start();
return () => scene.destroy();
}Give it enough vertices
A brush only moves vertices, so the mesh needs plenty where it's sculpted: a sphere with many rings and segments, blobs, lofts and sweeps, or any shape after remeshMesh. A built-in box has 8 corners and nothing between them to move.
sculptMesh(mesh, brush, opts)
sculptMesh(head, "inflate", { center: cheek, radius: 0.1, strength: 0.02, symmetry: "x" }); // both cheeks
sculptMesh(head, "grab", { center: noseTip, radius: 0.08, delta: { z: -0.05 } }); // a longer nose
sculptMesh(head, "crease", { along: [a, b, c], radius: 0.02, strength: 0.01 }); // a mouth lineWhere the brush works:
| Option | Default | Meaning |
|---|---|---|
center | Middle of the brush, in the mesh's own space | |
along | Or a path (a list of points): the brush works along all of it at once, like a stroke. A groove along a path is the same depth everywhere on it | |
radius | required | Distance where the brush fades to nothing |
falloff | "smooth" | How it fades from the middle to the radius: "smooth", "sharp" (strong in the middle only), "linear", "constant" (full strength up to the radius: a hard edge), or a function of distance / radius (0 to 1) |
symmetry | none | "x" (or a list of axes): the brush also works mirrored across the mesh's origin, like Blender's X mirror. A symmetric mesh stays exactly symmetric, and points on the mirror plane stay on it |
Brushes
| Brush | What it does | strength (default) | Also |
|---|---|---|---|
"inflate" | Pushes the surface out along its normals; negative pushes it in. Cheeks, bellies, muscles, dents | How far the middle moves (radius / 5) | |
"grab" | Moves the area by delta, fading out: pull a snout, lift a shoulder, bend an ear | A factor on delta (1) | delta: the move at the middle |
"smooth" | Moves points toward the middle of their neighbors: softens noise, bumps and harsh edits | 0 to 1 per pass (0.5) | iterations: passes (1); more passes smooth wider bumps |
"flatten" | Presses the area onto the plane through it: a cut face, the sole of a foot, a seat | 0 to 1 (0.5); 1 with falloff: "constant" makes a clean flat face | |
"pinch" | Pulls points toward the brush, along the surface: makes ridges and grooves sharp | 0 to 1 (0.3) | |
"crease" | A groove: pushes in and pinches at once. Mouths, eyelids, wrinkles, the line between fingers. Negative makes a sharp ridge | Depth at the middle (radius × 0.15) | pinch: 0 to 1 (0.5) |
"noise" | Lumps the surface in and out with fbm3: skin, bark, rock | Height of the lumps (radius × 0.15) | scale: lumps per radius (3); seed |
SCULPT_BRUSHES lists the brush names.
A brush with symmetry reads the noise on each side as if it were the first, so "noise" is mirrored too.
Good to know
- Points at the same place move together. Meshes split at texture seams (and many OBJ files) have several vertices at one place. They're moved as one, so the surface doesn't tear.
- Order matters, like in Blender. Each call works on the mesh as the last one left it. Inflate a ridge, then pinch it, and you get a sharp fin (the blue ball in the demo).
- Strokes are exact. With
along, each point is weighed by its distance to the whole path, so there are no dabs to space out: a crease is equally deep from one end of the path to the other, and fades past its ends. - Speed. Only the points in the brush's reach are looked at, plus one pass over the faces. On a 33,000-vertex sphere, a call takes 10 to 30 ms, and a small dab about 4 ms.
remeshMesh(input, opts?)
Rebuilds any shape as one new mesh with small, even faces, like Blender's Voxel Remesh, so it can be sculpted everywhere: a box, a low-poly part, or several overlapping parts, which melt into one solid with no walls left inside.
import { Scene, makeShape, transformMesh, remeshMesh, sculptMesh, WebGLRenderer, OrbitCamera } from "./trid/index.js";
// A little creature from overlapping parts: a body, a head, four legs and two ears
const part = (shape, opts, place) => transformMesh(makeShape(shape, opts), place);
const parts = () => [
part("sphere", { r: 0.16 }, { scale: { x: 1.3, y: 0.9, z: 1 } }),
part("sphere", { r: 0.1 }, { pos: { x: 0, y: 0.13, z: -0.17 } }),
...[-1, 1].flatMap((x) => [-1, 1].map((z) => part("cylinder", { r: 0.035, h: 0.16 }, { pos: { x: x * 0.11, y: -0.15, z: z * 0.08 } }))),
...[-1, 1].map((x) => part("cone", { r: 0.035, h: 0.09 }, { pos: { x: x * 0.055, y: 0.24, z: -0.17 }, rot: { x: 0, y: 0, z: -x * 0.4 } })),
];
// One surface with small, even faces, where the parts were (without its smooth normals, so the faces show)
const clay = remeshMesh(parts(), { resolution: 40, normals: false });
// Now it can be sculpted anywhere: softened joins, round cheeks, a smile, a snout
const sculpted = remeshMesh(parts(), { resolution: 72 });
sculptMesh(sculpted, "smooth", { center: { x: 0, y: 0.04, z: -0.12 }, radius: 0.12, strength: 1, iterations: 10 });
for (const z of [-0.08, 0.08]) sculptMesh(sculpted, "smooth", { center: { x: 0.11, y: -0.08, z }, radius: 0.06, strength: 1, iterations: 6, symmetry: "x" });
sculptMesh(sculpted, "inflate", { center: { x: 0.06, y: 0.1, z: -0.24 }, radius: 0.06, strength: 0.025, symmetry: "x" });
sculptMesh(sculpted, "grab", { center: { x: 0, y: 0.1, z: -0.27 }, radius: 0.06, delta: { y: -0.01, z: -0.06 } });
sculptMesh(sculpted, "crease", {
along: [{ x: -0.05, y: 0.085, z: -0.28 }, { x: 0, y: 0.065, z: -0.32 }, { x: 0.05, y: 0.085, z: -0.28 }],
radius: 0.018, strength: 0.012,
});
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 300, camera: { fov: 0.6 },
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
});
const turn = { x: 0, y: -0.6, z: 0 }; // three-quarter view, face toward the camera
// The parts as they are, each its own color
const colors = ["#E9806E", "#E9806E", "#e5c07b", "#e5c07b", "#e5c07b", "#e5c07b", "#c678dd", "#c678dd"];
parts().forEach((mesh, i) => scene.add({ mesh, pos: { x: -0.6, y: 0, z: 0 }, rot: turn, material: { fill: colors[i] } }));
// Remeshed: one surface, its small faces showing with flat shading
scene.add({ mesh: clay, pos: { x: 0, y: 0, z: 0 }, rot: turn, material: { fill: "#a0a8b8" } });
scene.add({ mesh: sculpted, pos: { x: 0.6, y: 0, z: 0 }, rot: turn, material: { fill: "#E9806E", smooth: true } });
scene.use(new OrbitCamera({ target: { x: 0, y: 0, z: 0 }, distance: 1.6, pitch: -0.2 }));
scene.start();
return () => scene.destroy();
}const clay = remeshMesh(makeShape("box"), { resolution: 48 }); // a sculptable box
const body = remeshMesh([torso, head, ...legs], { size: 0.01 }); // parts → one surface, faces ~1 cm
sculptMesh(body, "smooth", { center: neck, radius: 0.05, strength: 1, iterations: 8 }); // soften a joininput is a mesh, a scene object, or a list of them. Objects are placed by their own pos, rot and scale (like mergeMeshes), so a group's children come out where they are in the group.
| Option | Default | Meaning |
|---|---|---|
resolution | 64 | Cells along the shape's longest side (at most 256): more cells, smaller faces |
size | Or the size of a cell, in units (replaces resolution) | |
method | "nets" | "nets": even faces. "cubes": marching cubes |
normals | true | Smooth normals taken from the surface |
uvs | "box" | Texture coordinates to make: a mapping, or false |
What to expect:
- The shape is kept, its details down to a cell. Flat sides stay flat and sizes stay the same; sharp edges and corners are softened a little (by less than a cell). A cube at the default resolution has the same volume to four decimals.
- Each closed piece counts as solid, and pieces turned inside out are fixed first. An open piece (a sheet, a cup without a bottom) becomes a thin shell two cells thick.
- Texture coordinates are made again, and materials per face aren't kept: the result is one surface.
- Speed. About half a second at the default resolution for one part, a second for eight overlapping parts, and about 4 seconds at
resolution: 128. Remesh once, then sculpt as much as you like. - Where two parts only just touch (closer than a cell), the surface can pinch into an edge shared by four faces. Nudge them together, or raise the resolution.