Examples
Small complete programs. Each one is the exact code running above it.
Orbits
A moon orbiting a planet orbiting a sun, with groups: each frame only changes angles, and every position follows from the tree. The sun is an unlit sphere with a point light in its middle, and bloom makes it glow.
import { Scene, makeShape, WebGLRenderer, OrbitCamera, unlit, bloom } from "./trid/index.js";
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 380, camera: { fov: 0.7 }, lineWidth: 1, background: "#0d0d10",
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
// The sun is the light: a point light in the middle, and a little ambient
lights: [
{ type: "ambient", intensity: 0.08 },
{ type: "point", position: { x: 0, y: 0, z: 0 }, color: "#fff1d0", intensity: 1.6, range: 4 },
],
post: [bloom({ threshold: 0.8, strength: 0.9 })],
});
// Unlit, so it's fully bright and the bloom makes it glow
const sun = scene.add({ mesh: makeShape("sphere", { r: 0.3, rings: 24, segments: 36 }), material: { fill: "#ffd166", effects: [unlit()] } });
// Groups: the moon turns around the planet, the planet (with its moon) around the sun.
// Only angles change each frame; every position follows from the tree.
const planet = { mesh: makeShape("sphere", { r: 0.12, rings: 16, segments: 24 }), material: { fill: "#6ba8ff", smooth: true } };
const moonOrbit = {
rot: { x: 0.2, y: 0, z: 0 }, // tilted
children: [{ mesh: makeShape("sphere", { r: 0.04, rings: 10, segments: 14 }), pos: { x: 0.25, y: 0, z: 0 }, material: { fill: "#cccccc", smooth: true } }],
};
const orbit = scene.add({
children: [{ pos: { x: 1, y: 0, z: 0 }, children: [planet, moonOrbit] }],
});
// The planet's path, as one closed loop of lines
const path = { vertices: [], edges: [[]] };
for (let i = 0; i < 64; i++) {
const a = (i / 64) * Math.PI * 2;
path.edges[0].push(path.vertices.push({ x: Math.cos(a), y: 0, z: Math.sin(a) }) - 1);
}
scene.add({ mesh: path, color: "#333" });
scene.use(new OrbitCamera({ pitch: -0.55, distance: 2.8 }));
scene.start((dt) => {
sun.rot.y += dt * 0.2;
orbit.rot.y -= dt * 0.5; // the planet and its moon go around the sun
planet.rot.y += dt * 2; // the planet spins (its moon doesn't follow that)
moonOrbit.rot.y -= dt * 3; // the moon goes around the planet
});
return () => scene.destroy();
}Waves
Meshes are plain data, so you can move their vertices every frame. Set mesh.dirty = true after changing them in place, so the WebGL renderer sends them again and the smooth shading follows the new shape. Don't share a mesh you rewrite like this: every object using it would change too.
import { Scene, makeShape, WebGLRenderer, OrbitCamera } from "./trid/index.js";
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 380, camera: { z: -2, fov: 0.7 },
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
});
// A flat grid of 64 × 64 squares; smooth shading shows the ripples
const water = makeShape("plane", { size: 2, div: 64 });
scene.add({ mesh: water, material: { fill: "#5fb3d9", smooth: true, doubleSided: true } });
scene.use(new OrbitCamera({ pitch: -0.55, distance: 2.9 }));
scene.start((dt, t) => {
for (const v of water.vertices) {
const d = Math.hypot(v.x, v.z);
v.y = Math.sin(d * 8 - t * 3) * 0.06 / (1 + d * 2); // rings moving out from the middle
}
water.dirty = true; // changed in place: the WebGL renderer sends it again, and normals are worked out again
});
return () => scene.destroy();
}Labels with a custom tool
A tool's draw runs after the objects, so it can write text on top. worldToScreen finds where a 3D point lands on the canvas.
import {
Scene, makeShape, OrbitCamera, worldToScreen, worldToCamera, DEFAULT_NEAR,
} from "./trid/index.js";
// A tool that writes each object's label above it
const labels = {
draw(ctx, scene) {
const { camera, canvas } = scene;
ctx.font = "15px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillStyle = "#ddd";
for (const obj of scene.objects) {
const above = { x: obj.pos.x, y: obj.pos.y + 0.38, z: obj.pos.z };
// Skip points behind the camera
if (worldToCamera(above, camera).z < DEFAULT_NEAR) continue;
const p = worldToScreen(above, camera, canvas.width, canvas.height);
ctx.fillText(obj.label, p.x, p.y);
}
},
};
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 340, camera: { fov: 0.6 },
});
["cube", "sphere", "torus"].forEach((kind, i) => scene.add({
mesh: makeShape(kind, { size: 0.4, radius: 0.05, r: 0.25, R: 0.2, tube: 0.07 }),
pos: { x: (i - 1) * 0.75, y: 0, z: 0 },
material: { fill: ["#E9806E", "#7ae96e", "#8ab4f8"][i], smooth: true },
label: kind, // any field you like: the tool below reads it
}));
scene.use(new OrbitCamera({ pitch: -0.2, distance: 2.4 }));
scene.use(labels);
scene.start((dt) => scene.objects.forEach((o) => (o.rot.y += dt * 0.4)));
return () => scene.destroy();
}Tunnel
Rings rushing toward the camera, recycled when they pass it. The near plane clips them cleanly as they go by, and bloom makes the lines glow.
import { Scene, WebGLRenderer, bloom } from "./trid/index.js";
// A square ring in the XY plane
const ring = {
vertices: [{ x: -1, y: -0.6, z: 0 }, { x: 1, y: -0.6, z: 0 }, { x: 1, y: 0.6, z: 0 }, { x: -1, y: 0.6, z: 0 }],
edges: [[0, 1, 2, 3]],
};
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 340, lineWidth: 2, background: "#0b0b0b",
camera: { z: 0, near: 0.05 },
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
post: [bloom({ threshold: 0.4, strength: 1 })], // the bright lines glow
});
const COUNT = 16, GAP = 0.6;
const rings = Array.from({ length: COUNT }, (_, i) =>
scene.add({ mesh: ring, pos: { x: 0, y: 0, z: i * GAP } }));
scene.start((dt, t) => {
for (const r of rings) {
r.pos.z -= dt * 1.5;
if (r.pos.z < -0.2) r.pos.z += COUNT * GAP; // recycle behind the far end
r.rot.z = r.pos.z * 0.15 + t * 0.2; // twist
const fade = 1 - r.pos.z / (COUNT * GAP);
r.color = `hsla(${(t * 40 + r.pos.z * 30) % 360}, 80%, 65%, ${fade})`;
}
});
return () => scene.destroy();
}