Skip to content

Subdivision & bevels ​

Two ways to soften a mesh: subdivide a rough cage into a smooth surface, or round only its sharp edges.

js
import { subdivideMesh, bevelMesh } from "./trid/index.js";

subdivideMesh(mesh, opts?) ​

Smooths a mesh by subdividing it (Catmull-Clark, like a subdivision surface modifier). Build a rough cage of a few boxes and faces, and get a smooth, organic shape from it: box modeling. Each level splits every face into one quad per corner (about 4 times the faces) and rounds the surface. Returns a new mesh; the original isn't changed.

a cage of boxes · smoothed twice · a box smoothed · a box with creaseAngle — drag to orbit
js
import { Scene, makeShape, mergeMeshes, subdivideMesh, OrbitCamera } from "./trid/index.js";

// A rough cage: a body box and a head box (a little character, box modeling style)
const cage = mergeMeshes([
  makeShape("box", { w: 0.36, h: 0.3, d: 0.26 }),
  { mesh: makeShape("cube", { size: 0.2 }), pos: { x: 0, y: 0.25, z: 0 } },
  { mesh: makeShape("box", { w: 0.08, h: 0.2, d: 0.08 }), pos: { x: -0.11, y: -0.22, z: 0 } },
  { mesh: makeShape("box", { w: 0.08, h: 0.2, d: 0.08 }), pos: { x: 0.11, y: -0.22, z: 0 } },
]);

const smooth = subdivideMesh(cage, { levels: 2 });
// The same, with every edge sharper than 60° kept hard
const creased = subdivideMesh(makeShape("box", { w: 0.4, h: 0.3, d: 0.3 }), { levels: 3, creaseAngle: 60 });
const soft = subdivideMesh(makeShape("box", { w: 0.4, h: 0.3, d: 0.3 }), { levels: 3 });

export default function (canvas) {
  const scene = new Scene(canvas, { width: 800, height: 320, lineWidth: 1, camera: { z: -2, fov: 0.6 } });
  scene.add({ mesh: cage, pos: { x: -0.8, y: 0, z: 0 }, material: { fill: "#8a8f98", wireframe: true } });
  scene.add({ mesh: smooth, pos: { x: -0.25, y: 0, z: 0 }, material: { fill: "#e5c07b", smooth: true } });
  scene.add({ mesh: soft, pos: { x: 0.3, y: 0, z: 0 }, material: { fill: "#5f8fd8", smooth: true } });
  scene.add({ mesh: creased, pos: { x: 0.8, y: 0, z: 0 }, material: { fill: "#98c379", smooth: true } });

  scene.use(new OrbitCamera({ pitch: -0.25, autoRotate: true, autoRotateSpeed: 0.4, autoRotateDelay: 2 }));
  scene.start();
  return () => scene.destroy();
}
OptionDefaultMeaning
levels1Times to subdivide (at most 6). 2 or 3 is usually plenty
smoothtruefalse: only split the faces, keep the shape (to give a deformer more rows)
creaseAnglenoneEdges where faces meet sharper than this (degrees) stay hard
creasesnoneEdges that stay hard: pairs or polylines of vertex indices
uvs"smooth""smooth": texture coordinates are smoothed with the surface, keeping their seams. "linear": only split
js
// A smooth body from a rough cage: a box with a smaller box on top
const cage = mergeMeshes([
  makeShape("box", { w: 0.6, h: 0.4, d: 0.4 }),
  { mesh: makeShape("cube", { size: 0.3 }), pos: { x: 0, y: 0.35, z: 0 } },
]);
const body = subdivideMesh(cage, { levels: 2 });

// A rounded box: smooth, but its 90° edges stay hard
const panel = subdivideMesh(box, { levels: 2, creaseAngle: 60 });

One piece or several

Pieces that don't share vertices (like boxes merged together) are smoothed one by one, each into its own rounded blob. For one continuous surface, the cage must be one connected mesh: neighboring faces sharing their edges.

Creases ​

A sharp edge keeps its shape: the surface stays creased along it at every level. A point with 3 sharp edges or more is a hard corner and stays put. A loop of sharp edges (each point on 2) stays sharp across, but rounds a little along the loop, like a creased edge in other modelers.

Open borders (a sheet, a hole) count as sharp: they stay where they are, smoothed along the border only, and an open sheet keeps its corners.

What the result keeps ​

  • Faces stay turned the way they were, and the result is welded (vertices at the same place merged first, so an unwelded mesh is still smoothed as one surface).
  • Each new face keeps its face's material (faceMaterials).
  • Texture coordinates come along when every face has them.
  • The mesh's own normals (from an OBJ file) are dropped: the renderers compute new ones, and material.smooth makes it look round.

bevelMesh(mesh, opts?) ​

Rounds the sharp edges of any mesh, or cuts them flat, like a bevel modifier. Manufactured things are rarely sharp-edged: furniture, devices, robots, panels, tools. Returns a new mesh; the original isn't changed.

an extruded star · a cylinder with rounded rims · one rounded edge · corners cut (vertices mode) — drag to orbit
js
import { Scene, makeShape, extrude, bevelMesh, OrbitCamera } from "./trid/index.js";

const star = Array.from({ length: 10 }, (_, i) => {
  const a = Math.PI / 2 + (i / 10) * Math.PI * 2, r = i % 2 ? 0.1 : 0.22;
  return { x: Math.cos(a) * r, y: Math.sin(a) * r };
});

// Every sharp edge rounded: caps, sides, and the concave inner corners
const starBevel = bevelMesh(extrude(star, 0.1), { radius: 0.02, segments: 3 });
// Only the rims are sharper than 30°: the sides stay smooth
const knob = bevelMesh(makeShape("cylinder", { r: 0.18, h: 0.16, segments: 32 }), { radius: 0.03, segments: 4 });
// Just one edge
const block = makeShape("box", { w: 0.3, h: 0.3, d: 0.3 });
const [a, b] = block.faces[2];
const oneEdge = bevelMesh(block, { edges: [[a, b]], radius: 0.08, segments: 6 });
// Corners cut, edges left sharp
const cut = bevelMesh(makeShape("cube", { size: 0.3 }), { mode: "vertices", radius: 0.07, segments: 1 });

export default function (canvas) {
  const scene = new Scene(canvas, { width: 800, height: 320, lineWidth: 1, camera: { z: -2, fov: 0.65 } });
  const items = [[starBevel, "#e5c07b"], [knob, "#5f8fd8"], [oneEdge, "#98c379"], [cut, "#E9806E"]];
  items.forEach(([mesh, fill], i) => scene.add({ mesh, pos: { x: -0.75 + i * 0.5, y: 0, z: 0 }, rot: { x: 0.5, y: 0.6, z: 0 }, material: { fill, smooth: true } }));
  scene.use(new OrbitCamera({ pitch: -0.25, autoRotate: true, autoRotateSpeed: 0.4, autoRotateDelay: 2 }));
  scene.start();
  return () => scene.destroy();
}
OptionDefaultMeaning
radiusa twentieth of the mesh's sizeHow round: the radius of a ball rolled along the edge
segments3Rows across each rounded edge. 1 cuts it flat (a chamfer)
angle30Edges where faces meet sharper than this (degrees) are beveled
edgesnoneInstead of angle: which edges, as pairs or polylines of vertex indices ([[0, 1], [4, 5, 6, 7, 4]])
mode"edges""edges" rounds the edges. "vertices" cuts only the corners
verticesnoneWith "vertices", instead of angle: which corners, as vertex indices
js
// A crate: every sharp edge rounded
const crate = bevelMesh(makeShape("box"), { radius: 0.03 });

// A cylinder: only its rims are sharper than 30°, so only they're rounded
const knob = bevelMesh(makeShape("cylinder", { r: 0.2, h: 0.1 }), { radius: 0.02, segments: 4 });

// One edge of a table top, cut flat
const top = makeShape("box", { w: 1.2, h: 0.05, d: 0.6 });
const edged = bevelMesh(top, { edges: [[top.faces[0][0], top.faces[0][1]]], radius: 0.02, segments: 1 });

How it rounds ​

Each beveled edge is rounded the way a ball of radius rolling along it would: the round surface meets both faces smoothly. Where 3 rounded edges meet (a box's corner), the corner is a piece of a sphere, exactly as on a rounded box. Where a rounded edge ends on a flat side, its end becomes part of that side. Concave edges (an inside corner) are rounded too, with the ball outside.

  • Overlap. On short edges, bevels from both ends would run into each other. The radius then shrinks everywhere (not only there), so the shape stays even.
  • Flat faces cut into triangles (an extruded star's caps) are treated as the flat face they make up.
  • Open borders (a sheet, a hole in a surface) stay sharp: there is no second face to round toward.
  • Texture coordinates follow each face (new points on a face get the coordinates the face's texture gives them there), and the rounded strips take them from the faces on either side. Materials follow their faces; the rounded strips and corners take a neighbor's.

The result is welded and closed (when the mesh was), with faces clockwise from outside, so validateMesh finds nothing on it.

makeShape or bevelMesh?

For a plain rounded box, makeShape("box", { radius }) is simpler and makes an even grid. bevelMesh is for everything else: extruded outlines, lathed shapes, meshes from code or files.

Vertices mode ​

With mode: "vertices", only the corners are cut: wherever 3 sharp edges or more meet (or each vertex in vertices), each edge is cut radius along from the corner. With segments: 1 the cut is flat; with more, it's a small dome.