Overview
The trace core, for renderers that need rays but do their own shading.
@pmndrs/raytracer traces rays against a triangle BVH on WebGPU. Rays go in, hits come out. What you do with a hit is up to you.
Use it when your renderer already exists and needs rays for one job:
- Reflections: trace from the G-buffer, shade the hit with your own lighting or with
rtShade. - Shadows: one visibility ray per light sample.
- Ambient occlusion: a few short visibility rays per pixel.
- GI probes and caches: rays from probe positions, or from surfaces, with radiance at the hit.
- Baking: lightmaps or probe data, offline or at load time.
If you want a rendered image instead, use @pmndrs/pathtracer, which is built on this package.
Three ways to trace
All three run the same WGSL walk over the same buffers, so a scene uploaded once serves all of them.
What it is built on
@pmndrs/bvhbuilds the trees and provides the WGSL walk. Its WGSL names start withbvh; everything this package adds starts withrt,RtorRT_.@pmndrs/downsamplerfills texture mip levels on the GPU when a scene has textures.
Both are regular dependencies; Installation has the bundler note for bvh's workers.
The pieces
A scene on the GPU has two halves:
- The trace buffers (bvh): nodes, triangles and the top level. This is all
rtVisibleandrtClosestneed. - The scene copy: instance transforms, vertex attributes, materials, lights and textures.
rtSurface,rtMaterial, light sampling andrtShaderead it.
buildGpuScene builds and uploads both from one input. See Your first render and Loading glTF for how to get there from triangles or a .glb.
import { buildGpuScene, detectCaps, requestDeviceForCaps } from '@pmndrs/raytracer';
const adapter = await navigator.gpu.requestAdapter();
const { device, caps } = await requestDeviceForCaps(adapter!, detectCaps(adapter!));
const scene = await buildGpuScene(device, input);
// scene.trace: bvh buffers. scene.buffers: the scene copy. scene.format, scene.packing: what kernels compile for.
Pages in this section
Examples
- Hit inspector:
rtClosestandrtSurfacein your own compute shader. - Ambient occlusion: short
rtVisiblerays from each pixel. - Batch shadow rays:
RtBatchTracerover a buffer of rays. - One-bounce shading:
rtShadelighting the hits.