Skip to content

Groups ​

Any object can hold other objects in children. Children are placed relative to their parent: move, turn or scale the parent and its whole subtree follows. A model made of parts (a sword: blade, guard, grip) moves as one, and a moon can orbit a planet that orbits a sun.

Drag a ring to turn the sword · click a part: first the sword, then a level deeper each click · drag to orbit
js
import { Scene, makeShape, extrude, OrbitCamera, TransformGizmo, pickHit } from "./trid/index.js";

export default function (canvas) {
  const scene = new Scene(canvas, {
    width: 800, height: 380, lineWidth: 1, camera: { y: 0.5, z: -2, fov: 0.7 },
  });
  const mat = (fill) => ({ fill, smooth: true });

  // A sword made of parts, in two levels of groups
  const blade = {
    name: "blade",
    mesh: extrude([{ x: -0.04, y: 0 }, { x: 0.04, y: 0 }, { x: 0.04, y: 0.7 }, { x: 0, y: 0.8 }, { x: -0.04, y: 0.7 }], 0.02),
    material: mat("#d8dce6"),
  };
  const hilt = {
    name: "hilt",
    children: [
      { name: "guard", mesh: makeShape("box", { w: 0.3, h: 0.04, d: 0.06 }), material: mat("#c9a227") },
      { name: "grip", mesh: makeShape("cylinder", { r: 0.02, h: 0.18, segments: 12 }), pos: { x: 0, y: -0.1, z: 0 }, material: mat("#6b4a2e") },
      { name: "pommel", mesh: makeShape("sphere", { r: 0.035, rings: 8, segments: 12 }), pos: { x: 0, y: -0.21, z: 0 }, material: mat("#c9a227") },
    ],
  };
  const sword = scene.add({ name: "sword", pos: { x: 0, y: -0.2, z: 0 }, children: [blade, hilt] });

  scene.use(new OrbitCamera({ pitch: -0.2 }));
  const gizmo = scene.use(new TransformGizmo({ mode: "rotate", targets: [sword] }));

  // Click a part: the first click selects the whole sword, the next one a level deeper
  let down = null;
  const at = (e) => ({ x: (e.offsetX * canvas.width) / canvas.clientWidth, y: (e.offsetY * canvas.height) / canvas.clientHeight });
  canvas.addEventListener("pointerdown", (e) => (down = at(e)));
  canvas.addEventListener("pointerup", (e) => {
    const p = at(e);
    if (!down || gizmo.dragging || Math.hypot(p.x - down.x, p.y - down.y) > 4) return;
    const hit = pickHit(scene, p.x, p.y);
    if (!hit) return void (gizmo.targets = []);
    const levels = [...hit.path].reverse(); // [sword, hilt, guard]
    const now = levels.indexOf(gizmo.targets[0]);
    gizmo.targets = [levels[Math.min(now + 1, levels.length - 1)]];
  });

  // Turning the group turns every part with it
  scene.start((dt) => {
    if (!gizmo.dragging && gizmo.targets[0] === sword) sword.rot.y += dt * 0.3;
  });

  return () => scene.destroy();
}

Making a group ​

Give an object children. Leave out mesh when it's only there to hold them:

js
const sword = scene.add({
  pos: { x: 0, y: 1, z: 0 },
  children: [
    { mesh: blade, pos: { x: 0, y: 0.45, z: 0 } },
    { name: "hilt", children: [
      { mesh: guard },
      { mesh: grip, pos: { x: 0, y: -0.12, z: 0 } },
    ]},
  ],
});

sword.rot.y += dt;                // the whole sword turns
sword.children[1].rot.z = 0.3;    // only the hilt tilts, around the sword's center
  • A child's pos, rot and scale are in its parent's space: pos: { x: 0, y: 0.45, z: 0 } is 0.45 above the sword's origin, wherever the sword is and however it's turned. Scaling a group scales its children's sizes and distances.
  • Groups nest as deep as you like.
  • Missing pos, rot and scale get defaults, down the whole tree.
  • Each object keeps its own material, color and lineWidth; they aren't inherited.

Changing the tree ​

children is a plain array, read every frame: push, splice and replace it freely.

js
sword.children.push({ mesh: gem, pos: { x: 0, y: 0.1, z: 0.03 } });  // shows up next frame
scene.remove(gem);              // takes a child out of its group (or a top-level object out of the scene)
hilt.visible = false;           // hides the hilt and everything in it

An object should only be in one place at a time: one parent, or the scene's top level.

Where things are: parent and world ​

Every object gets two fields, kept up to date by the scene each frame (and set right away by scene.add):

FieldMeaning
obj.parentThe group it's in, or null at the top level
obj.worldIts world matrix: its parents' transforms times its own, from the last frame

Both are hidden from Object.keys, JSON.stringify and { ...obj }, so objects stay easy to save and copy. To work them out now (after changing a transform, between frames):

js
worldPosition(grip)                       // → { x, y, z } of its origin in the world
transformPoint(worldMatrix(grip), point)  // → a point of its mesh, in the world
ancestors(grip)                           // → [grip, hilt, sword]
worldObjects(scene)                       // → every visible object, parents first, with world set

Clicking and moving parts ​

  • Picking finds the part that was hit, even deep in a group. pickHit also returns its path, the part then its groups up to the top ([guard, hilt, sword]), so you can choose what a click selects. The demo above selects the whole sword first, then one level deeper each click.
  • DragObject moves the part that was clicked, inside its group: it follows the cursor however the group is turned or scaled. With root: true it drags the whole group instead.
  • TransformGizmo takes groups and parts as targets. Handles work in world space and each target is changed in its parent's space. With apply: false, turn the reported operation into a target's space with transformOpToLocal(op, worldMatrix(target.parent)).

Uneven scales ​

Groups use 4 × 4 matrices, so everything is exact, even a group scaled unevenly (scale: { x: 2, y: 1, z: 1 }) with turned children inside: the children come out stretched along the group's X, skewed if they're turned, just as you'd expect. (That's something a single pos, rot and scale can't describe, so a child's own transform can't be "flattened" to one in that case.)

Exporting and merging ​

  • exportOBJ writes every mesh in a group where it's drawn, one OBJ object per part (named after name).
  • mergeMeshes([group]) bakes a whole group into one mesh, placed by the group's own transform.
  • meshBounds(group) measures a group with everything in it.

Matrices ​

From trid/math/matrix.js, for working with world matrices yourself. A matrix is 16 numbers in column-major order (m[col * 4 + row]), the order WebGL uses.

js
identityMat4()                   // → matrix that changes nothing
mat4FromTransform({ pos, rot, scale })
mulMat4(a, b)                    // → a · b (b first): world = mulMat4(parentWorld, local)
invertMat4(m)                    // → world → local
determinantMat4(m)               // → < 0 when it mirrors
transformPoint(m, p)             // → point (moved)
transformDirection(m, v)         // → direction or offset (not moved)
normalMatrix(m)                  // → 3 × 3 normal matrix, column-major
transformNormal(nm, n)           // → unit normal
transformOpToLocal(op, parentWorld)