Skip to content

The scene graph

A scene is a tree of Object3D. Every object has a position, a rotation and a scale relative to its parent, and children placed relative to it. The renderer reads where each one ends up, matrixWorld, which updateMatrixWorld() computes. render calls it on the scene and the camera before every frame.

import { DiffuseMaterial, Mesh, Object3D, Scene, SphereGeometry } from '@typeshade/radiance';
const scene = new Scene();
const pivot = new Object3D();
pivot.position.set(0, 1, 0);
const ball = new Mesh(new SphereGeometry(0.3), new DiffuseMaterial({ color: 0xf0ad4e }));
ball.position.set(0.8, 0, 0);
pivot.add(ball);
scene.add(pivot);
// Turning the pivot carries the ball round it.
pivot.rotation.y = Math.PI / 4;
  • position is a Vector3. rotation is an Euler in radians, applied X, then Y, then Z. scale is a Vector3.
  • add and remove change the tree. Adding an object that has a parent moves it.
  • traverse visits an object and every descendant. visible = false leaves an object and its children out of the render.
  • lookAt(target) turns the object so that its -z axis points at the target, with no roll.

A Mesh joins a geometry, the shape in its own space, and a material. Every geometry is a triangle mesh, a BufferGeometry, with three.js’s vertex order and uv layout:

Geometry Shape
SphereGeometry(radius, widthSegments, heightSegments) a sphere about the origin. More segments give a rounder outline
PlaneGeometry(width, height) a rectangle in the xy plane, centred on the origin, facing +z
BoxGeometry(width, height, depth) a box about the origin
BufferGeometry your own triangles: position, index, and normal and uv if any

The path tracer builds a BVH for each geometry once. Many meshes can share one geometry, and each mesh is an instance of it. After you edit a geometry’s arrays in place, add 1 to its version, so the BVH is built again. glTF arrives later in milestone M2 (the plan).

Material Surface
DiffuseMaterial({ color }) matte: light leaves it in every direction alike
MirrorMaterial({ color }) a perfect mirror, tinted by its colour
EmissiveMaterial({ color, intensity }) a light: it gives off color times intensity from its front face
PhysicalMaterial({ color, roughness }) three.js’s physical parameters. At M2 it renders as a diffuse

A colour is a Color in linear RGB, or a 0xRRGGBB number in sRGB, which the material decodes. A light is any mesh with an emissive material. At every diffuse bounce the path tracer picks one of its triangles, by the share of the light it gives off, and samples a point on it. So a scene with three lights costs about what a scene with one does. Set doubleSided on the material to make a light give off light from both faces.

PerspectiveCamera(fov, aspect) takes the vertical field of view in degrees. Keep its aspect equal to the frame’s width over its height: the examples set both from the canvas’s size in a ResizeObserver.

The scene graph does not know which renderer draws it. The path tracer draws it today. The real-time tier in the plan draws the same scene later.