Three.js From Zero · Article s17-02

Maurer Roses — Aliasing as an Art Form

Coarse-sample a rose curve and the chords weave thread-art: Line2 fat lines at last, the resolution-uniform pitfall, and a step-angle drift that turns number theory into a kaleidoscope. Part of the MasterAllArts Creative 3D Roadmap.

Part of the MasterAllArts Creative 3D Roadmap .

← Three.js From Zero S17-02 · Order & Chaos

Season 17 · Article 02 · Order & Chaos

Take a flower-shaped curve. Now sample it lazily — every 71 degrees instead of every one — and connect your samples with straight lines. Signal-processing class calls this aliasing, a bug. Peter Maurer's 1987 paper called it a rose garden. One integer controls whether you get a clean star, a woven basket, or thread-art chaos — and we're finally drawing it with thick lines, because this article pays off a debt from Season 16.

— fps · n=6 d=71°

The faint curve underneath is the true rose; the thick colored thread is the Maurer walk over it. drift d slides the step angle a hundredth of a degree per frame — that slow-motion kaleidoscope is the whole show, so let it run a minute. Then take the wheel: drag d yourself and feel how violently the weave reorganizes around special angles. Land exactly on 60, 72, 90 — sparse stars. A half-degree off — dense lace. Pull twist up and orbit: the flat thread becomes a woven 3D ribbon cage.

A rose, walked badly on purpose

The rose itself is one line of polar math — r = sin(n·θ) gives a flower with n petals (odd n) or 2n petals (even n). The Maurer construction walks it in 360 big, equal strides:

for (let k = 0; k <= 360; k++) {
  const theta = k * d * DEG;          // d = step angle, the magic number
  const r = Math.sin(n * theta) * R;  // radius from the rose
  points.push(r * Math.cos(theta), r * Math.sin(theta), 0);
}

Because both d·360 and the rose's period live on the same circle, the walk always closes after exactly 360 steps. Which 360 points of the rose it visits — and in what order it leaps between them — depends entirely on the arithmetic of d: its greatest common divisor with 360 sets how many distinct directions the chords take, and its relationship to n sets the weave. Rational resonances make stars; near-misses make moiré. It's the same number-theory machinery as the golden-angle story in phyllotaxis, wearing thread instead of seeds.

Step 1 — The debt: why your lines were 1px

Back in the harmonograph I told you LineBasicMaterial.linewidth is a lie — WebGL guarantees only 1-pixel lines — and promised the upgrade path. Here it is. Line2 doesn't draw GL lines at all: for every segment of your polyline it draws a little instanced quad, stretched between the two endpoints in screen space, mitered to its neighbors. Real geometry, so it can be any width, get per-vertex color, even dash.

import { Line2 } from 'three/addons/lines/Line2.js';
import { LineGeometry } from 'three/addons/lines/LineGeometry.js';
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';

const geometry = new LineGeometry();
geometry.setPositions(positions);        // flat [x,y,z, x,y,z, …]
geometry.setColors(colors);              // flat [r,g,b, …], same count

const material = new LineMaterial({
  linewidth: 3,                          // in PIXELS (worldUnits: false)
  vertexColors: true,
});
const line = new Line2(geometry, material);
Pitfall: the resolution uniform. Screen-space quads need to know the screen's size. LineMaterial carries a resolution uniform that YOU must set — at startup and on every resize: material.resolution.set(width, height). Forget it and the lines render at a bizarre wrong width or vanish entirely, with no warning. It is the single most-asked Line2 question on every Three.js forum, and now it will never bite you.

Step 2 — Rebuild cheap, restyle free

The walk is always 361 points, so the buffers never change size — moving a slider rewrites the same arrays and calls setPositions/setColors again. Meanwhile width is a material property: changing it costs nothing and touches no geometry. Same separation the whole series teaches — geometry when the shape changes, uniforms/material when the style does.

Color along the thread is the season's standing pattern (color = data): the walk index k maps to a gradient, so you can see the order in which the chords were laid down — that's what makes the weave readable instead of a tangle.

Step 3 — Drift: the kaleidoscope knob

if (drifting) {
  params.step += 0.01;                 // a hundredth of a degree per frame
  rebuildWalk();                        // 361 points — trivially cheap
}

Animating d instead of the artwork is the Julia-set lesson again: a slow path through parameter space reads as choreography. d drifts through irrational territory (lace), brushes a rational resonance (the lace suddenly snaps into a star), and melts again. Order and chaos taking turns, twice a minute.

Step 4 — The 3D twist

const z = Math.sin(2 * theta) * twist;   // lift the walk out of the plane

One sine in z and the flat thread becomes a saddle-shaped ribbon cage — and because Line2 quads are screen-space, they stay crisply thick from every camera angle (true 3D-thick lines would need TubeGeometry; this is the cheaper, usually-better-looking option). Orbit while twisted: the weave's hidden depth order becomes visible.

What the controls teach

  • d at resonances — try d = 72 with n = 5, d = 60 with n = 6: the walk visits only a handful of rose points and draws polygon stars. gcd(d, 360) is doing the counting.
  • d = 71 (Maurer's favorite) — one degree off resonance: the star unravels into the classic woven rose.
  • n odd vs even — odd roses trace each petal twice per cycle (the walk doubles back); even roses don't. Watch the thread density change.
  • width — a free style knob, finally. Notice it never re-triggers a geometry rebuild.

The skeleton

<script type="importmap">
{ "imports": {
    "three": "https://unpkg.com/three@0.170.0/build/three.module.js",
    "three/addons/": "https://unpkg.com/three@0.170.0/examples/jsm/" } }
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { Line2 } from 'three/addons/lines/Line2.js';
import { LineGeometry } from 'three/addons/lines/LineGeometry.js';
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';

// rose walk → setPositions/setColors                 (Steps 1–2)
// material.resolution.set(w, h) on init AND resize   (the pitfall)
// drift d through parameter space                     (Step 3)
</script>