Getting started
Radiance renders a scene written in TypeScript on WebGPU. A scene is a graph of objects, a camera looks at it, and a renderer draws it. The first renderer is a path tracer: every frame adds samples to every pixel, so the picture starts noisy and sharpens while the camera rests.
Requirements
Section titled “Requirements”- A browser with WebGPU. Chrome and Edge have it from version 113.
- A bundler that compiles TypeShade modules. The engine’s kernels are
*.shade.tsfiles. The compiler’s Vite plugin,typeshade/vite, compiles them when your application imports the engine.
Install
Section titled “Install”npm install @typeshade/radiance @typeshade/radiance-addons typeshade-
Clone the repository with its compiler submodule.
Terminal window git clone --recurse-submodules https://github.com/typeshade/radiance -
Install the workspace.
Terminal window cd radiance && bun install -
Run the site, which runs every example.
Terminal window bun run site:dev
Configure Vite
Section titled “Configure Vite”Add the compiler’s plugin, so the engine’s kernels compile when Vite reads them.
import { defineConfig } from 'vite';import { typeshade } from 'typeshade/vite';
export default defineConfig({ plugins: [typeshade()] });A first scene
Section titled “A first scene”This is the file the front page runs. It builds the scene, places a camera, starts the renderer on a canvas, and gives the camera to the orbit controls.
// A floor, a lamp over it, a mirror ball and a matte one. This is the whole of a scene: objects// in a graph, a camera, a renderer and a loop. The front page draws this file.
import { Clock, DiffuseMaterial, EmissiveMaterial, Mesh, MirrorMaterial, PathTracer, PerspectiveCamera, PlaneGeometry, Scene, SphereGeometry,} from '@typeshade/radiance';import { OrbitControls } from '@typeshade/radiance-addons';import type { ExampleRun } from './types.ts';
export default async function firstScene(canvas: HTMLCanvasElement): Promise<ExampleRun> { const scene = new Scene();
const ground = new PlaneGeometry(6, 6); const grey = new DiffuseMaterial(); grey.color.setHex(0xbfbfbf); const floor = new Mesh(ground, grey); floor.rotation.x = -Math.PI / 2;
const panel = new PlaneGeometry(1.2, 1.2); const light = new EmissiveMaterial(); light.emissive.multiplyScalar(10); const lamp = new Mesh(panel, light); lamp.position.set(0, 2.2, 0); lamp.rotation.x = Math.PI / 2;
const sphere = new SphereGeometry(0.5); const chrome = new MirrorMaterial(); const mirror = new Mesh(sphere, chrome); mirror.position.set(-0.6, 0.5, 0);
const orange = new DiffuseMaterial(); orange.color.setHex(0xe8703a); const ball = new Mesh(sphere, orange); ball.position.set(0.6, 0.5, 0.2);
scene.add(floor, lamp, mirror, ball);
const camera = new PerspectiveCamera(40); camera.position.set(0, 1.4, 3.6); camera.lookAt(ball.position);
const renderer = await new PathTracer({ canvas, seed: 1, targetFrameTime: 30 }).init(); const controls = new OrbitControls(camera, canvas); controls.target.set(0, 0.5, 0); 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(); renderer.setAnimationLoop(async () => { controls.update(clock.getDelta()); // While the camera moves, trace one pixel in four by four: a quick preview. renderer.preview = controls.moving ? 4 : 1; await renderer.render(scene, camera); });
return { renderer, controls, dispose() { observer.disconnect(); controls.dispose(); renderer.dispose(); }, };}What each part does:
Sceneis the root of the graph.Meshjoins a geometry and a material and is placed withposition,rotationandscale, as in three.js.PerspectiveCameralooks down its own -z axis.lookAtturns it toward a point.PathTracerneedsawait init()before its first frame, because it requests a WebGPU device.render(scene, camera)adds samples and draws them.setAnimationLoopcalls the callback once for each frame the browser draws, and waits for the frame before.renderer.preview = 4while the camera moves traces one pixel for each 4 by 4 block, so a drag stays smooth. At rest the full frame accumulates again.
Next: The scene graph explains the objects, and The path tracer the renderer’s options.