Loading glTF
import { buildGpuScene, loadGltf } from '@pmndrs/raytracer';
import { PtMegakernel, ptSceneOptions } from '@pmndrs/pathtracer';
const input = await loadGltf('/models/helmet.glb');
input.environment = { radiance: [0.8, 0.9, 1] }; // glTF has no sky: add one
const scene = await buildGpuScene(device, input);
const pt = await PtMegakernel.create(device, caps, ptSceneOptions(scene));
pt.setScene(scene.trace, scene.buffers);
pt.setCamera(input.cameras[0]?.camera ?? { position: [0, 0, 3], target: [0, 0, 0], up: [0, 1, 0], yFovDeg: 40 });
Live example: Khronos sample models, or drop your own .glb onto the page.
Sources
loadGltf(source, options) takes:
- a URL (string or
URL): external buffers and images resolve next to it. - bytes (
ArrayBufferorUint8Array): a dropped file or an upload. PassbaseUrlif a.gltfnames external files.
GLB, .gltf with external files and data: URIs are all read. Files that require KHR_draco_mesh_compression, EXT_meshopt_compression or KHR_mesh_quantization are refused with an error. Decompress them first, for example with gltf-transform.
What it returns
A complete input for buildSceneCopy / buildGpuScene, plus:
Materials
Each glTF material becomes a set of OpenPBR parameters, then a scene-copy material. Unknown extensions and unmapped texture slots are listed in notes, never silently dropped.
Textures other than the four slots are factors only: occlusion, clearcoat, sheen and transmission textures. A material reads one uv set. If its textures use different sets or transforms, the first one wins and a note says so.
Options
The default decoder draws each image into a 2D canvas, and browsers may premultiply alpha there. The colour of nearly transparent texels can lose precision. Pass an exact PNG decoder if that matters (for example in a reference comparison).
In Node
There is no default image decoder in Node: pass decodeImage, or textures: false. Everything else works with Node's own fetch. File URLs need fetch replaced, or pass the bytes.
import { readFile } from 'node:fs/promises';
const input = await loadGltf(await readFile('model.glb'), { textures: false });