Skip to content

Curves, sweeps & lofts ​

Smooth curves from a few points, and surfaces built along them: a section swept along a path (sweep), or a surface through several cross-sections (loft).

js
import { spline, resamplePath, sweep, loft } from "./trid/index.js";

Curves ​

A few control points in, a smooth curve out: paths for sweeps, profiles for lathe, outlines for extrude. Write 5 points, get a smooth curve of 40.

a lathe profile without and with spline · a 3D spline — drag to orbit
js
import { Scene, lathe, spline, OrbitCamera } from "./trid/index.js";

// The same 5 profile points: straight lines (left) and a spline through them (right)
const points = [{ r: 0.1, y: -0.4 }, { r: 0.3, y: -0.25 }, { r: 0.12, y: 0.05 }, { r: 0.08, y: 0.25 }, { r: 0.18, y: 0.4 }];
const rough = lathe(points, { segments: 24, capBottom: true });
const smooth = lathe(spline(points, { segments: 8 }), { segments: 24, capBottom: true });

// A 3D curve through 6 points, drawn as a line (vertices + edges)
const knot = spline([
  { x: -0.3, y: -0.35, z: 0 }, { x: 0.2, y: -0.2, z: 0.3 }, { x: 0.3, y: 0.1, z: -0.2 },
  { x: -0.2, y: 0.2, z: -0.3 }, { x: -0.25, y: 0.4, z: 0.25 }, { x: 0.25, y: 0.4, z: 0.1 },
], { count: 60 });
const curve = { vertices: knot, edges: knot.slice(1).map((_, i) => [i, i + 1]) };

export default function (canvas) {
  const scene = new Scene(canvas, { width: 800, height: 320, lineWidth: 1, camera: { z: -2.2, fov: 0.6 } });
  scene.add({ mesh: rough, pos: { x: -0.8, y: 0, z: 0 }, material: { fill: "#8a8f98", wireframe: true, doubleSided: true } }); // open at the top: draw the inside too
  scene.add({ mesh: smooth, pos: { x: 0, y: 0, z: 0 }, material: { fill: "#5f8fd8", wireframe: true, doubleSided: true } });
  scene.add({ mesh: curve, pos: { x: 0.8, y: 0, z: 0 }, color: "#e5c07b" });

  scene.use(new OrbitCamera({ pitch: -0.25, autoRotate: true, autoRotateSpeed: 0.4, autoRotateDelay: 2 }));
  scene.start();
  return () => scene.destroy();
}

Points can be 2D ({ x, y }), 3D ({ x, y, z }) or lathe profile points ({ r, y }). Every number field of the first point is a coordinate, and the result has the same fields.

spline(points, opts?) ​

A smooth curve that passes through every point (a Catmull-Rom spline). Returns new points.

OptionDefaultMeaning
segments8Points made between two control points
closedfalseThe last point joins the first (a loop). The result doesn't repeat the first point
alpha0.5How the curve bends: 0 uniform, 0.5 centripetal (never loops or overshoots at sharp turns), 1 chordal (hugs the points)
tension00 smooth, 1 straight lines between the points
countnoneInstead of segments: this many points in all, evenly spaced by length
spacingnoneInstead of segments: points about this far apart, evenly spaced
js
// A smooth vase profile from 5 points
const vase = lathe(spline([
  { r: 0.1, y: -0.4 }, { r: 0.3, y: -0.25 }, { r: 0.12, y: 0.05 }, { r: 0.08, y: 0.25 }, { r: 0.18, y: 0.4 },
]), { capBottom: true });

// A tail path with 40 evenly spaced points
const tail = spline([
  { x: 0, y: 0, z: 0 }, { x: 0.2, y: 0.3, z: -0.4 }, { x: 0, y: 0.6, z: -0.7 }, { x: -0.2, y: 0.5, z: -1 },
], { count: 40 });

segments or count?

With segments, close control points get as many points between them as far ones, so points bunch up where control points are close. count and spacing space the points evenly along the whole curve, which gives sweeps and lofts faces of even size.

Repeated points in a row and points with NaN or infinite numbers are skipped. With fewer than 2 points, the points are returned as they are.

resamplePath(points, opts?) ​

The same points along a polyline (straight lines between them), evenly spaced by length. The ends stay where they are. Use it to even out a path without smoothing it.

OptionDefaultMeaning
countnoneNumber of points in all
spacingnoneDistance between points (used when count isn't given)
closedfalseThe points form a loop

Without count or spacing, it returns a copy of the points.

sweep(path, opts?) ​

Moves a cross-section along a 3D path: tails, horns, tentacles, handles, pipes, cables, rails, picture frames. Without a profile, it's a round tube.

a tapered tail · a twisted star knot · a level road — drag to orbit
js
import { Scene, sweep, spline, OrbitCamera } from "./trid/index.js";

// A tail: a smooth path from 5 points, thick at the base, a point at the tip
const tail = sweep(spline([
  { x: 0, y: -0.35, z: 0 }, { x: 0.1, y: -0.1, z: 0.1 }, { x: -0.05, y: 0.1, z: 0 },
  { x: 0.1, y: 0.3, z: -0.1 }, { x: 0.2, y: 0.35, z: 0 },
], { count: 40 }), { radius: (t) => 0.09 * (1 - t) ** 0.7, segments: 12 });

// A star section twisted along a closed trefoil knot
const star = Array.from({ length: 10 }, (_, i) => {
  const a = (i / 10) * Math.PI * 2, r = i % 2 ? 0.03 : 0.07;
  return { x: Math.cos(a) * r, y: Math.sin(a) * r };
});
const knotPath = Array.from({ length: 150 }, (_, i) => {
  const t = (i / 150) * Math.PI * 2;
  return { x: 0.1 * (Math.sin(t) + 2 * Math.sin(2 * t)), y: 0.1 * (Math.cos(t) - 2 * Math.cos(2 * t)), z: -0.1 * Math.sin(3 * t) };
});
const knot = sweep(knotPath, { profile: star, closed: true, twist: Math.PI * 4 });

// A road: an open, flat profile kept level along a winding path
const road = sweep(spline([
  { x: -0.3, y: -0.2, z: -0.3 }, { x: 0.2, y: -0.1, z: -0.1 }, { x: -0.2, y: 0, z: 0.1 }, { x: 0.3, y: 0.1, z: 0.3 },
], { count: 50 }), {
  profile: [{ x: 0.08, y: 0 }, { x: -0.08, y: 0 }], profileClosed: false, up: { x: 0, y: 1, z: 0 },
});

export default function (canvas) {
  const scene = new Scene(canvas, { width: 800, height: 320, lineWidth: 1, camera: { z: -2.2, fov: 0.6 } });
  scene.add({ mesh: tail, pos: { x: -0.8, y: 0, z: 0 }, material: { fill: "#e5c07b", smooth: true } });
  scene.add({ mesh: knot, pos: { x: 0, y: 0, z: 0 }, material: { fill: "#5f8fd8", wireframe: true } });
  scene.add({ mesh: road, pos: { x: 0.8, y: 0, z: 0 }, material: { fill: "#98c379", wireframe: true, doubleSided: true } }); // a flat strip: both sides show

  scene.use(new OrbitCamera({ pitch: -0.25, autoRotate: true, autoRotateSpeed: 0.4, autoRotateDelay: 2 }));
  scene.start();
  return () => scene.destroy();
}

The path is used as given, so pass it through spline for a smooth one (with count or spacing for faces of even size).

OptionDefaultMeaning
profilea circleThe cross-section, a list of { x, y }: x goes to the path's side, y to its "up"
profileClosedtruefalse: the profile is an open line (a ribbon, a leaf, a road surface)
radius0.1Radius of the default circle. Ignored with a profile
segments16Points around the default circle
scale1Size of the section along the path
twist0Turn of the section from start to end, in radians
closedfalseThe path is a loop (a ring, a handle closed on itself)
capstrueClose the ends with flat caps
upnoneKeep the section's y pointed this way
mitertrueStretch the section at sharp corners so the tube keeps its thickness

radius and scale can be:

  • a number;
  • a list of numbers spread evenly from the start to the end ([0.1, 0.05, 0]);
  • a function of t, which is 0 at the start and 1 at the end ((t) => 0.1 * (1 - t)).

An end whose size is 0 closes into a single point, for horns and tail tips.

js
// A horn: curved, from thick to a point
const horn = sweep(spline([
  { x: 0, y: 0, z: 0 }, { x: 0.05, y: 0.2, z: 0.05 }, { x: 0.2, y: 0.35, z: 0.1 }, { x: 0.35, y: 0.38, z: 0.05 },
], { count: 30 }), { radius: (t) => 0.08 * (1 - t) ** 0.8 });

// A picture frame: a square path, a molding profile kept upright, sharp mitered corners
const molding = [{ x: -0.1, y: 0 }, { x: 0.1, y: 0 }, { x: 0.1, y: 0.05 }, { x: 0, y: 0.08 }, { x: -0.1, y: 0.04 }];
const frame = sweep(
  [{ x: -1, y: 0, z: -1 }, { x: 1, y: 0, z: -1 }, { x: 1, y: 0, z: 1 }, { x: -1, y: 0, z: 1 }],
  { profile: molding, closed: true, up: { x: 0, y: 1, z: 0 } },
);

How the section turns ​

By default, the section is carried along the path without spinning, so it never flips at bends, even on knotted 3D paths. On a closed loop, the small turn it picks up around the loop is spread evenly, so there's no seam where the ends meet.

With up, the section's y stays pointed that way instead (it only turns around up). Use it for things that must stay level: roads, rails, shelves, frames. Where the path goes straight along up, the section is carried as usual.

On a closed path, twist is rounded to whole turns so the ends still meet.

Faces and textures ​

Faces are clockwise from outside like the built-in shapes, whichever way the path and a closed profile go. An open profile faces its right side (like a lathe profile). Texture coordinates: u goes around the section and v along the path, both by length. Caps map the section's box.

loft(sections, opts?) ​

A surface through cross-sections: bodies, bottles, boat hulls, car bodies, heads, fuselages. Each section is a closed outline, and the surface joins them in order, smoothly.

a bottle along Y · a boat hull from a flat stern to a pointed bow · a horn along a curved path — drag to orbit
js
import { Scene, loft, spline, OrbitCamera } from "./trid/index.js";

const circle = (r, n = 24) => Array.from({ length: n }, (_, i) => {
  const a = (i / n) * Math.PI * 2;
  return { x: Math.cos(a) * r, y: Math.sin(a) * r };
});

// A bottle along Y: a few round sections, smooth between them
const bottle = loft([
  { at: -0.4, outline: circle(0.16) }, { at: -0.1, outline: circle(0.18) }, { at: 0.12, outline: circle(0.17) },
  { at: 0.25, outline: circle(0.06) }, { at: 0.42, outline: circle(0.055) },
], { axis: "y", segments: 8 });

// A boat hull along X: each section is a flat deck over a round bottom,
// from a flat stern (left) to a raised, pointed bow (right)
const section = (w, depth, n = 12) => Array.from({ length: n + 1 }, (_, i) => {
  const a = (i / n) * Math.PI;
  return { x: Math.cos(a) * w, y: -Math.sin(a) * depth };
});
const hull = loft([
  { at: -0.4, outline: section(0.13, 0.07), offset: { x: 0, y: 0.02 } },
  { at: -0.15, outline: section(0.16, 0.13) },
  { at: 0.15, outline: section(0.15, 0.13) },
  { at: 0.36, outline: section(0.07, 0.1), offset: { x: 0, y: 0.03 } },
  { at: 0.46, outline: [{ x: 0, y: 0.07 }] },
], { axis: "x", segments: 6 });

// A horn along a curved path, from round to a flattened tip
const path = spline([{ x: 0, y: -0.35, z: 0 }, { x: 0.05, y: 0, z: 0.1 }, { x: 0.25, y: 0.25, z: 0 }, { x: 0.3, y: 0.4, z: -0.15 }], { count: 40 });
const horn = loft([
  { at: 0, outline: circle(0.12) }, { at: 0.6, outline: circle(0.07), scale: { x: 1.4, y: 0.7 } }, { at: 1, outline: [{ x: 0, y: 0 }] },
], { path });

export default function (canvas) {
  const scene = new Scene(canvas, { width: 800, height: 320, lineWidth: 1, camera: { z: -2.2, fov: 0.6 } });
  scene.add({ mesh: bottle, pos: { x: -0.8, y: 0, z: 0 }, material: { fill: "#98c379", smooth: true } });
  scene.add({ mesh: hull, pos: { x: 0.02, y: 0, z: 0 }, rot: { x: 0, y: 0.5, z: 0 }, material: { fill: "#c8925a", smooth: true } });
  scene.add({ mesh: horn, pos: { x: 0.65, y: 0, z: 0 }, material: { fill: "#e5c07b", smooth: true } });

  scene.use(new OrbitCamera({ pitch: -0.25, autoRotate: true, autoRotateSpeed: 0.4, autoRotateDelay: 2 }));
  scene.start();
  return () => scene.destroy();
}

Sections come in two kinds (all sections must be the same kind):

KindFieldsPlaced
Outlineoutline (2D points), at, and optional scale, rotate, offsetAlong a straight axis, or along a 3D path
Ringpoints (3D points)Where they are

For outlines, at is where the section sits: the position on the axis, or from 0 (the path's start) to 1 (its end) along a path. Sections are joined in order of at; without it, they're spread evenly. scale (a number, or { x, y }), rotate (radians) and offset ({ x, y }) change a section in its own plane, so one outline can be reused at many sizes.

OptionDefaultMeaning
axis"z"Straight axis for outlines. An outline's x and y go to X and Y for "z", X and Z for "y", Z and Y for "x"
pathnoneA 3D path to place the outlines along instead (use spline for a smooth one)
upnoneWith path: keep the outlines' y pointed this way, like sweep
closedfalseThe surface loops back to its first section: a closed path, or rings in a loop
capstrueClose the ends with flat caps
segments6Rows between two sections, on average (shared out by distance, so faces stay even)
pointsautoPoints around each section
smoothtrueSmooth between sections; false joins them with straight lines
aligntrueLine the sections up so the surface doesn't twist
js
const circle = (r, n = 24) => Array.from({ length: n }, (_, i) => {
  const a = (i / n) * Math.PI * 2;
  return { x: Math.cos(a) * r, y: Math.sin(a) * r };
});

// A boat hull: a flat stern, a round middle, a sharp bow
const hull = loft([
  { at: -1, outline: [{ x: -0.3, y: 0.2 }, { x: -0.3, y: -0.1 }, { x: 0.3, y: -0.1 }, { x: 0.3, y: 0.2 }] },
  { at: 0, outline: spline([{ x: -0.4, y: 0.2 }, { x: -0.35, y: -0.15 }, { x: 0, y: -0.3 }, { x: 0.35, y: -0.15 }, { x: 0.4, y: 0.2 }]) },
  { at: 1, outline: [{ x: 0, y: 0.25 }] },
]);

// A neck along a curve, from round to oval, kept upright
const neckPath = spline([{ x: 0, y: 0, z: 0 }, { x: 0, y: 0.3, z: 0.1 }, { x: 0, y: 0.5, z: 0.35 }], { count: 30 });
const neck = loft([
  { at: 0, outline: circle(0.2) },
  { at: 1, outline: circle(0.2), scale: { x: 0.7, y: 1 } },
], { path: neckPath, up: { x: 0, y: 1, z: 0 } });

How sections are joined ​

  • Point counts. When every section has the same number of points, they're kept as they are, so you decide which points join. Otherwise each section is resampled to the same count (points, by default the most any section has, at least 24). A section's own points are kept, so a square keeps its corners.
  • Lining up. Each section is turned to match the one before it, so the surface doesn't twist even if the outlines start at different points. Rings of 3D points can also be walked either way. Turn it off with align: false to join point k to point k exactly.
  • Smooth, never past the sections. Between sections the surface follows smooth curves that never go past the sections themselves: the widest section is the widest point, with no bulge between two equal sections. smooth: false joins them with straight lines.
  • Tips. A section of one point (or scale: 0) closes the surface into a point: a nose, a bow, a tail.

Faces and textures ​

Faces are clockwise from outside like the built-in shapes, however the outlines and the axis or path go. Texture coordinates: u goes around the sections and v along the surface (by distance). Caps map their own box.