Skip to content

Core concepts

Scene graph ​

Arrange objects in a tree, where each child moves, turns and scales with its parent.

Every node in a scene is an Obj: it has a position, rotation and scale, a parent, and children. Cameras are Objs, and so are meshes, through Drawable, the kind of Obj the renderer draws. A scene is the root Obj you pass to render(), usually an empty one, and everything under it is drawn.

In the example, the planet is a child of an empty Obj at the centre, so turning that Obj carries the planet round the sun. The moon is a child of the planet, so as the planet spins, the moon goes round with it.

js
import {
  Renderer,
  Camera,
  Obj,
  Mesh,
  Sphere,
  Lights,
  LitProgram,
  Vec2,
  Vec3
} from 'wtc-gl'

// A sun, a planet and a moon. Each child moves with its parent: turning the
// empty `orbit` carries the planet round the sun, and turning the planet
// carries the moon round the planet.
export default function (container) {
  const renderer = new Renderer({
    dpr: Math.min(devicePixelRatio, 2),
    alpha: true,
    premultipliedAlpha: true,
    antialias: true,
    clearColour: [0, 0, 0, 0]
  })
  const { gl } = renderer
  container.appendChild(gl.canvas)

  const camera = new Camera({ fov: 35 })
  const scene = new Obj()
  const lights = new Lights({ direction: [0, 1, 0.6] })

  // One geometry, shared by all three bodies and scaled to size
  const sphere = new Sphere(gl, { radius: 0.5, widthSegments: 32 })
  const body = (colour, emissive = [0, 0, 0]) =>
    new Mesh(gl, {
      geometry: sphere,
      program: new LitProgram(gl, { lights, colour, emissive })
    })

  const sun = body([1, 0.7, 0.3], [0.5, 0.25, 0])
  sun.scale.reset(1.6, 1.6, 1.6)
  sun.setParent(scene)

  const orbit = new Obj()
  orbit.setParent(scene)

  const planet = body([0.29, 0.62, 1])
  planet.position.x = 3
  planet.setParent(orbit)

  const moon = body([0.85, 0.85, 0.8])
  moon.position.x = 0.9
  moon.scale.reset(0.35, 0.35, 0.35)
  moon.setParent(planet)

  return {
    frame(time) {
      orbit.rotation.y = time * 0.4
      orbit.updateRotation()
      planet.rotation.y = time * 1.5
      planet.updateRotation()
      renderer.render({ scene, camera })
    },
    resize(width, height) {
      renderer.dimensions = new Vec2(width, height)
      camera.perspective({ aspect: width / height })
      const distance = Math.max(10, 17 / camera.aspect)
      camera.position.reset(0, distance * 0.4, distance)
      camera.lookAt(new Vec3(0, 0, 0))
    },
    dispose() {
      // Programs are one per mesh; the geometry is shared, so it goes once
      scene.traverse((node) => {
        if (node instanceof Mesh) node.program.remove()
      })
      sphere.remove()
      renderer.dispose()
      gl.canvas.remove()
    }
  }
}

Parents and children ​

setParent puts an object under another, and setParent(null) takes it out of the tree. An object has one parent, so setting a new one moves it. addChild and removeChild do the same from the parent's side:

js
import { Obj } from 'wtc-gl'

const scene = new Obj()
const group = new Obj()
group.setParent(scene)

mesh.setParent(group) // or group.addChild(mesh)
mesh.setParent(null) // or group.removeChild(mesh)

Taking a mesh out of the tree stops it being drawn, but doesn't free its geometry or program; see Cleaning up.

parent and children hold the links. Change them through these methods, which keep both sides in step, rather than directly.

Position, rotation and scale ​

position and scale are Vec3s, relative to the parent. Change them in place and they take effect on the next render:

js
mesh.position.y = 1.5
mesh.position.reset(0, 1.5, -2)
mesh.scale.reset(2, 2, 2)

The object's matrix is built from its quaternion. rotation is a friendlier way to set it: Euler angles in radians, applied in YXZ order. After changing rotation, call updateRotation() to turn it into the quaternion:

js
mesh.rotation.x = Math.PI / 4
mesh.rotation.y += 0.01
mesh.updateRotation()

To work with quaternions instead, set quaternion and call updateQuaternion() if you also want rotation to match:

js
import { Quat, Vec3 } from 'wtc-gl'

mesh.quaternion = Quat.fromAxisAngle(new Vec3(1, 0, 0), Math.PI / 4)
mesh.updateQuaternion()

TIP

updateQuaternion() and lookAt() replace rotation and quaternion with new objects, so don't keep references to them across those calls.

Looking at a point ​

lookAt(target) turns an object to face a Vec3. A mesh turns its +z axis towards the target, and a camera its view direction, −z, so a camera looks straight at it. It uses the object's own position, so the target should be in the parent's space; for objects directly under an unmoved scene root, that's world space.

js
camera.position.reset(0, 4, 8)
camera.lookAt(new Vec3(0, 0, 0))

Matrices ​

Each object has two matrices:

  • matrix, its transform relative to its parent, built from position, quaternion and scale.
  • worldMatrix, its transform in the world: the parent's worldMatrix times its own matrix.

updateMatrixWorld() rebuilds both for an object and everything under it. The renderer calls it on the scene every render, and then on the camera, so normally you don't. Call it yourself when you need world positions before drawing, such as for a raycast, and render with update: false to avoid doing it twice:

js
scene.updateMatrixWorld()
const worldPosition = moon.worldMatrix.translation // a Vec3
renderer.render({ scene, camera, update: false })

To set matrix yourself, turn off matrixAutoUpdate so it isn't rebuilt from the position, quaternion and scale. decompose() goes the other way, and sets position, quaternion, scale and rotation from matrix.

Finding things ​

name is an optional label; models loaded with GLTFLoader get their node names. traverse calls a function for an object and everything under it. Returning true skips that object's children:

js
let wheel
scene.traverse((node) => {
  if (node.name === 'Wheel') wheel = node
  return !!wheel
})

Visibility ​

Set visible to false to stop drawing an object and everything under it. Its children's own visible is left alone, so they reappear with it.

Drawables and meshes ​

Drawable is an Obj the renderer collects and draws. Mesh is the drawable you'll use most: it binds a geometry to a program. Before drawing, it sets uniforms on the program from its world matrix and the camera: u_modelMatrix, u_viewMatrix, u_modelViewMatrix, u_projectionMatrix, u_normalMatrix, u_cameraPosition and u_objectPosition. Programs and shaders shows them in use.

Two options on every drawable change how the renderer treats it:

  • frustumCulled (default true) skips the mesh when its bounds are outside the camera's view. A drawable that isn't a Mesh has no bounds, so it's always drawn. See frustum culling.
  • renderOrder (default 0) sets its place in the draw order.
js
const sky = new Mesh(gl, { geometry, program, renderOrder: -1 })

Draw order ​

With sort on, which it is by default, the renderer draws in three groups:

  1. Opaque meshes, whose program isn't transparent. They're grouped by program, so the GPU switches programs as rarely as possible, then drawn nearest first, so hidden pixels fail the depth test early.
  2. Transparent meshes, whose program is transparent and tests depth. They're drawn furthest first, so each blends over what's behind it.
  3. Transparent meshes whose program has depthTest: false, such as overlays, drawn last.

Within each group, renderOrder comes first: lower numbers draw earlier. A mesh with a renderOrder other than 0 isn't sorted by depth.

With sort: false, meshes draw in tree order: each object, then its children.

See also ​