Babylon.js

Screen-space reflections and probes on a Babylon.js WebGPUEngine, as frame-graph tasks. Experimental.
Warning

The Babylon adapter is experimental. The API can change.

The adapter runs the core screen-space tracer and the probe set on Babylon's own device. It supports:

  • Probes. BabylonProbeSet captures them with Babylon cube renders, plus an environment slot.
  • Bounce light from the probes. probeIrradiance(material, probes) lights a PBR material's diffuse from the probes.
  • Screen-space reflections. ScreenSpaceReflections, on a G-buffer from Babylon's geometry renderer.
  • A composite. It adds the reflections to the lit image, with debug views.
  • Frame-graph tasks. FrameGraphReflectionsTask and FrameGraphReflectionsCompositeTask.

It does not have the ray-traced fallback, the denoiser, presets, the hybrid resolve or a one-call setup. The adapter comparison has the full table. The adapter handles Babylon's conventions (handedness, Y-flipped targets, velocity, gloss and F0) when it builds the frame contract.

Install

pnpm add @pmndrs/reflections @babylonjs/core
PackageRangeWhen
@babylonjs/core>=9.35.0 <10Always. The tested version is 9.35.0.

three, pixi.js, @pmndrs/raytracer, @pmndrs/bvh and denoiser are not used by this adapter.

Minimal setup

The complete code is examples/babylon. These are its reflection parts. Import from the Babylon entry:

import { FrameGraphReflectionsCompositeTask, FrameGraphReflectionsTask, BabylonProbeSet, ScreenSpaceReflections, stripSpecularIBL } from '@pmndrs/reflections/babylon';

1. The engine

export async function createEngine(canvas: HTMLCanvasElement): Promise<WebGPUEngine> {
  // enableAllFeatures: subgroups for the tracer's faster variant. setMaximumLimits: four colour targets.
  const engine = new WebGPUEngine(canvas, { antialias: false, enableAllFeatures: true, setMaximumLimits: true });
  await engine.initAsync();
  return engine;
}

2. Probes and the tracer

// The composite adds the specular IBL term back from the sources, so take it out of the materials.
materials.forEach((m) => stripSpecularIBL(m));

// One probe over the room, plus the environment as a slot.
const probes = new BabylonProbeSet(scene, { capacity: 1, sourceSize: 128, environment: [skybox] });
probes.setProbes([{ position: [0, 2, 0], influenceSize: [16, 4.2, 16], projectionSize: [16, 4.2, 12] }]);
await scene.whenReadyAsync();
await probes.bake();

const ssr = new ScreenSpaceReflections({ engine, probes });

A probe can capture from a point other than its influence box's centre (originOffset) and project onto a box offset from it (projectionOffset), as three's ReflectionProbe does: that lets a room's influence box reach below the floor, so the floor gets the probe's full weight, while the capture point and the walls stay where the room is.

For bounce light, give each PBR material the probes' irradiance. three's materials get it from scene.environmentNode; Babylon has no per-pixel environment hook, so a material plugin runs the probe lookup in the material's shader. It replaces the diffuse part of Babylon's image-based lighting, and Babylon still weights it as usual (albedo, specular energy, scene.environmentIntensity, ambient occlusion). It works with or without scene.environmentTexture.

for (const m of scene.materials) if (m instanceof PBRMaterial) probeIrradiance(m, probes);

Attach it before the first render. bake() captures with the probes switched off, so a capture never sees the probes' own light. Without the plugin, a scene with no environment texture gets direct light only: the rooms of engines/babylon read about 60 / 255 darker than three's.

Without a denoiser in this adapter, a glossy surface shows the tracer's raw samples, and they move every frame. Set ssr.settings.frameIndex (for example to 0) to fix the noise pattern, so a still view stays still. The adapter also lets every glossy ray march to the end (minTraversalOccupancy: 0; core's default stops rays early in a way that changes from frame to frame). Leave it at 0 unless you trade a still image for a faster tracer.

BabylonProbeSet.bake() needs scene.activeCamera to be set, because the captures take its near and far planes. ssr.configure(width, height) allocates the tracer's textures. Call it again, and rebuild the frame graph, when the size changes.

3. The frame graph

Build a frame graph the usual Babylon way: clear, shadows, a geometry renderer task that writes world normals, reflectivity and linear velocity, the sky. Then add the two reflection tasks and present the composite:

// The geometry task writes world normals, reflectivity and linear velocity. `sky` holds the lit colour and depth.
const trace = new FrameGraphReflectionsTask('reflections', graph, ssr);
trace.camera = camera;
trace.colorTexture = sky.outputTexture;
trace.depthTexture = sky.outputDepthTexture;
trace.worldNormalTexture = geometry.geometryWorldNormalTexture;
trace.reflectivityTexture = geometry.geometryReflectivityTexture;
trace.velocityTexture = geometry.geometryLinearVelocityTexture;
graph.addTask(trace);

const composite = new FrameGraphReflectionsCompositeTask('composite', graph, scene);
composite.camera = camera;
composite.sourceTexture = sky.outputTexture;
composite.depthTexture = sky.outputDepthTexture;
composite.worldNormalTexture = geometry.geometryWorldNormalTexture;
composite.reflectivityTexture = geometry.geometryReflectivityTexture;
composite.velocityTexture = geometry.geometryLinearVelocityTexture;
composite.radianceTexture = trace.radianceTexture;
composite.confidenceTexture = trace.confidenceTexture;
composite.hitDistanceTexture = trace.hitDistanceTexture;
graph.addTask(composite);

The geometry task needs disableImageProcessing = true, so the tracer and the composite get linear HDR. Feed the composite's outputTexture to your image-processing task. examples/babylon/src/main.ts has the whole graph.

The parts

PieceWhat it does
BabylonProbeSetProbes captured with Babylon cube renders, backed by the core probe set. Add the environment as a slot.
ScreenSpaceReflectionsThe core tracer on Babylon's device. With probes, misses and rough pixels take the probe radiance.
FrameGraphReflectionsTaskRuns the tracer inside a frame graph.
FrameGraphReflectionsCompositeTaskAdds the reflections to the lit image, with debug views (COMPOSITE_VIEWS).
stripSpecularIBL(material)Takes the specular IBL term out of a PBR material, because the composite adds it back from the sources.
probeIrradiance(material, probes)Lights a PBR material's diffuse IBL from the probes, per pixel (bounce light). WGSL only.

Device limits

The engine needs setMaximumLimits: true for four colour targets, and enableAllFeatures: true if you want the subgroup variant of the tracer. The adapter reads Babylon's GPUDevice and does not make its own. There is no ray-traced pass here, so the storage-buffer count from the three.js adapter does not apply.

Known gaps

  • No ray-traced fallback, no denoiser, no presets and no hybrid resolve. Probes and screen space only.
  • No one-call setup like Reflections.create. You build the frame graph.
  • Materials need stripSpecularIBL, and probeIrradiance for bounce light. Both work on PBR materials.
  • The bounce light samples the probes along the normal at roughness 1, as three's does. It is not a cosine convolution like Babylon's own spherical harmonics.
  • Experimental: names and options can change.

Live example

examples/engines/babylon builds the two rooms of the other engine pages in Babylon, with the three probes and screen space as frame-graph tasks; its live page sits next to the three.js page at the same views. examples/babylon is the source the snippets above come from, and its live page takes ?ssr=0, ?probes=0, ?view= and ?variant=workgroup.