React Three Fiber

Reflections in a React Three Fiber v10 canvas, with a useReflections hook.

React Three Fiber (R3F) v10 renders with WebGPURenderer, so the three.js adapter works under a plain <Canvas>. Everything Reflections.create does is available: probes, screen-space reflections, the ray-traced fallback, the denoiser, presets and the hybrid resolve. The package has no React code. A small hook of about thirty lines, copied from the example, wraps Reflections.create. See the adapter comparison.

Install

R3F v10 is on the alpha tag at the time of writing. The last tested version is 10.0.0-alpha.6. You also need React 19.

pnpm add @react-three/fiber@alpha react react-dom three @pmndrs/reflections
PackageRangeWhen
three>=0.187.0Always.
@react-three/fiber10.0.0-alpha.6 testedAlways.
@pmndrs/raytracer^0.1.0 (optional)rayTracing: true.
@pmndrs/bvh^0.1.0 (optional)Install with @pmndrs/raytracer.
denoiser^2.0.0 (optional, not a peer)denoiser/ffx, for steady glossy reflections.
pnpm add @pmndrs/raytracer @pmndrs/bvh denoiser

Minimal setup

Two things differ from a plain canvas:

  1. The renderer needs the device limits the reflection passes ask for. R3F's renderer prop takes a factory, so pass createReflectionsRenderer through it.
  2. The frame is drawn by Reflections, not by R3F's default render. A useFrame job in the render phase takes over.

The useReflections hook

This is examples/r3f/src/useReflections.ts:

import { useEffect, useRef, useState } from 'react';
import { useFrame, useThree } from '@react-three/fiber';
import type * as THREE from 'three/webgpu';
import { Reflections, type ReflectionsOptions } from '@pmndrs/reflections/three';

export function useReflections(options: ReflectionsOptions & { enabled?: boolean } = {}) {
  const { renderer, scene } = useThree() as unknown as { renderer: THREE.WebGPURenderer; scene: THREE.Scene };
  const [reflections, setReflections] = useState<Reflections | null>(null);
  const optionsRef = useRef(options);
  optionsRef.current = options;
  const enabled = options.enabled ?? true;

  useEffect(() => {
    if (!enabled) return;
    let cancelled = false;
    let made: Reflections | null = null;
    Reflections.create(renderer, scene, optionsRef.current).then((r) => {
      if (cancelled) r.dispose(); else { made = r; setReflections(r); }
    }, (e) => console.error('useReflections:', e));
    return () => { cancelled = true; made?.dispose(); setReflections(null); };
  }, [renderer, scene, enabled]);

  // A job in the 'render' phase takes over R3F's own render call; this draws the frame instead.
  useFrame(({ renderer, scene, camera }) => {
    if (reflections) reflections.render(camera);
    else (renderer as THREE.WebGPURenderer).render(scene, camera);
  }, { phase: 'render' });

  return reflections;
}

Reflections.create is async (the cube-array check, the probe bake, the BVH), so the hook returns null until it is ready and draws the scene the plain way meanwhile. It builds once per renderer and scene. To rebuild with other options, remount the component with a key. If creation fails, the error goes to the console and the plain frame stays.

The canvas

import { useEffect, useState } from 'react';
import { Canvas, useThree } from '@react-three/fiber';
import * as THREE from 'three/webgpu';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
import { createReflectionsRenderer } from '@pmndrs/reflections/three';
import { FfxReflectionDenoiser } from 'denoiser/ffx';
import { useReflections } from './useReflections';

function Scene() {
  const { renderer, scene } = useThree() as unknown as { renderer: THREE.WebGPURenderer; scene: THREE.Scene };
  const [envReady, setEnvReady] = useState(false);

  // The probe captures what the scene shows, so the environment has to be there first.
  useEffect(() => {
    scene.environment = new THREE.PMREMGenerator(renderer).fromScene(new RoomEnvironment(), 0.04).texture;
    scene.background = scene.environment;
    setEnvReady(true);
  }, [renderer, scene]);

  useReflections({
    enabled: envReady,
    denoiser: (device, { roughnessThreshold }) =>
      new FfxReflectionDenoiser(device, { roughnessThreshold: roughnessThreshold ** 2 }),
  });

  return (
    <>
      <directionalLight position={[3, 6, 2]} intensity={1.6} />
      <mesh rotation-x={-Math.PI / 2}>
        <circleGeometry args={[6, 64]} />
        <meshStandardMaterial color={0x20242b} roughness={0.08} />
      </mesh>
      <mesh position={[-1.3, 0.7, 0]}>
        <sphereGeometry args={[0.7, 64, 32]} />
        <meshStandardMaterial color={0xffc766} roughness={0.18} metalness={1} />
      </mesh>
    </>
  );
}

export default function App() {
  return (
    <Canvas
      renderer={(defaults) => createReflectionsRenderer({ ...(defaults as object), antialias: false })}
      camera={{ position: [3.4, 2.2, 4.6], fov: 45 }}
    >
      <Scene />
    </Canvas>
  );
}

Device limits

createReflectionsRenderer asks the adapter for the limits the passes need. Keep it in the renderer prop. A renderer built without it can throw in Reflections.create for lack of colour-attachment bytes. The ray-traced pass fits WebGPU's default 8 storage buffers per stage in this setup. If a configuration does not fit, it warns once, sets pass.traceError, and screen space and probes continue (Troubleshooting).

Known gaps

  • R3F v10 is an alpha. The hook lives in the example, not in the package, and there is no @pmndrs/reflections/react entry.
  • enabled is for anything the probe bakes that arrives late: an <Environment> that is still loading, a model. Call reflections.bake() after the scene changes.
  • Moving objects: screen-space reflections follow them. The probe is a snapshot, and with rayTracing the BVH holds the meshes as they were at build time (reflections.trace.refit() each frame updates the transforms of the same meshes).
  • One Reflections per canvas.

Live example

examples/r3f is the full page, and the live version runs it. It takes ?rt=1 for the ray-traced fallback, ?preset=performance and ?denoise=0.

engines/r3f is the larger page: the two rooms written as JSX, with every feature of the three.js page behind the same hook.