Skip to content

Scene graph

A turning group of spheres: the path tracer starts again every frame.

Scene graph, rendered by the engine

Source

Open the file on GitHub

examples/scene-graph.ts
// The scene graph in motion: a group turns in the Cornell box, and two spheres turn on it. Each
// frame moves them, so the path tracer starts again every frame and shows what one or two
// samples a pixel look like: a game's frame, before denoising (docs/plan.md, M4). Paused, the
// motion stops and the samples add up again on the frame it stopped on.
import {
Clock,
DiffuseMaterial,
Mesh,
MirrorMaterial,
Object3D,
PathTracer,
Sphere,
SphereGeometry,
} from '@typeshade/radiance';
import { OrbitControls, createCornellBox } from '@typeshade/radiance-addons';
import type { ExampleRun } from './types.ts';
export default async function sceneGraph(canvas: HTMLCanvasElement): Promise<ExampleRun> {
const { scene, camera, target, bounds } = createCornellBox();
// Take the box's own spheres out, and put a turning group in their place. The box's balls are
// `Sphere` objects. A mesh sphere is taken out too.
for (const child of [...scene.children])
if (
child instanceof Sphere ||
(child instanceof Mesh && child.geometry instanceof SphereGeometry)
)
scene.remove(child);
const pivot = new Object3D();
pivot.position.set(0, 0.8, 0);
const arm = new Object3D();
pivot.add(arm);
const mirror = new Mesh(new SphereGeometry(0.28), new MirrorMaterial());
mirror.position.set(0.55, 0, 0);
const ball = new Mesh(new SphereGeometry(0.22), new DiffuseMaterial({ color: 0xf0ad4e }));
ball.position.set(-0.55, 0, 0);
const moon = new Mesh(new SphereGeometry(0.08), new DiffuseMaterial({ color: 0xffffff }));
moon.position.set(0, 0, 0.4);
ball.add(moon);
arm.add(mirror, ball);
scene.add(pivot);
const renderer = await new PathTracer({ canvas, seed: 1, samplesPerFrame: 2 }).init();
const controls = new OrbitControls(camera, canvas);
controls.target.copy(target);
controls.targetBounds = bounds;
controls.minDistance = 0.05;
controls.maxDistance = 50;
controls.update();
controls.saveState();
// Fill the canvas: the frame follows its size, and the camera its shape.
const resize = (): void => {
renderer.setSize(canvas.clientWidth, canvas.clientHeight);
camera.aspect = canvas.clientWidth / Math.max(1, canvas.clientHeight);
};
const observer = new ResizeObserver(resize);
observer.observe(canvas);
resize();
const clock = new Clock();
const run: ExampleRun = {
renderer,
controls,
playing: true,
dispose() {
observer.disconnect();
controls.dispose();
renderer.dispose();
},
};
renderer.setAnimationLoop(async () => {
const dt = clock.getDelta();
controls.update(dt);
if (run.playing) {
pivot.rotation.y += dt * 0.6;
ball.rotation.y += dt * 2;
}
renderer.preview = controls.moving ? 2 : 1;
await renderer.render(scene, camera);
});
return run;
}