Your renderer and your own three.js
Game draws one canvas with three’s WebGLRenderer, made the way React Three Fiber makes it. Three props on Game change that renderer, and fiber’s useThree hands it to any component for what can change while the game runs. Each prop takes the shape fiber’s own <Canvas> takes, so a game that knows fiber knows them.
| Prop | What it sets |
|---|---|
gl |
The settings three’s WebGLRenderer is made with, over fiber’s defaults, or a factory that makes the renderer itself and may return a promise. A factory may return a WebGPURenderer. |
shadows |
How the shadow map filters a shadow’s edge: "basic", "percentage" (the default), "tent" or "variance", or false for no shadow map at all. |
onCreated |
A function Game calls once the canvas has made its renderer, scene and camera, before its first frame. |
Game reads all three once, when it mounts, as it reads room and quality. A later render that passes other values changes nothing, so a game that switches renderers reads its choice before Game mounts, such as from the page’s address. gl takes either form, and a game may pick one at run time: gl={useWebGPU ? factory : { stencil: true }}. A scene mounted headless, as a room’s server and spawnite simulate mount it, reads none of them: its useThree((state) => state.gl) is a stand-in that draws nothing.
Settings fixed when the renderer is made
Section titled “Settings fixed when the renderer is made”Some settings belong to the WebGL context, and the browser fixes them when it makes the canvas’s context. Pass them in gl:
| Setting | Use it for |
|---|---|
stencil |
A stencil buffer, which a portal or a mirror masks the world with. |
logarithmicDepthBuffer |
Depth precision across a planet’s scale, where near and far are kilometres apart. |
reversedDepthBuffer |
The same precision on a GPU that supports it, at less cost than a logarithmic buffer. |
alpha |
false clears the canvas opaque. Fiber’s default, true, clears it clear, so the page shows where nothing draws. The HUD draws in the page over the canvas either way. |
preserveDrawingBuffer |
A canvas that keeps its last frame, for a page that reads it with toDataURL. It can cost a copy of the frame each frame on some GPUs, and the engine’s captures do not need it. |
powerPreference |
Fiber asks for "high-performance". Pass "low-power" for a game that should spare a laptop’s battery. |
antialias |
Fiber turns it on. The look’s post-processing stack draws off the canvas and takes its samples from the player’s quality level instead. |
The following game asks for a stencil buffer and blurred shadow edges:
import { Game, Scene, ShadowType, Landscape, defineGame,} from "@spawnite/engine";import { noon } from "@spawnite/engine/looks/noon";
// A game whose portals mask the world behind them with the stencil buffer,// and whose shadows blur at the edge.function Lobby() { return <Landscape map="grid" look={noon} />;}
export function App() { return ( <Game game={defineGame({ name: "portals", start: "lobby" })} gl={{ stencil: true }} shadows={ShadowType.Variance} > <Scene name="lobby" component={Lobby} /> </Game> );}shadows takes ShadowType.Basic, ShadowType.Percentage, ShadowType.Tent or ShadowType.Variance, or the same words as strings. Fiber’s true and "soft" are not offered: they ask for three’s soft filter, which three r186 removed, so three would warn and fall back to the percentage filter. A variance map blurs each light’s shadow by its shadow.radius and shadow.blurSamples, at the cost of one blur pass per light; the sun of a look sets its own radius. Fiber puts the shadow type back each time the canvas renders again, as a resize and the devtools’ pause each make it, so set the type with this prop rather than on the renderer.
The sun’s shadow
Section titled “The sun’s shadow”The sun of a look is three’s SunLight, which casts two cascaded shadow maps, as the directional shadows of Unity, Unreal and Godot do. The near cascade covers the view from the camera to a split, sharp; the far one covers the rest out to a distance, coarser. Each fits its slice of the view every frame and snaps to its texels, so the shadow follows the camera without a box to place and without shimmering as the camera moves.
Three values set it, on Landscape and on Lighting:
shadowDistance: how far along the view the shadow reaches, in metres, never past the camera’sfar. The last fifth fades to unshadowed sun. On a Landscape the quality level sets it, 30 m at Low, and 60 m at Medium and High;Lightingreads no level and takes 60 m.shadowSplit: where the near cascade ends, 14 m by default, kept under half the distance. A first-person view lowers it; a strategy camera far above its ground raises it past the camera’s height.shadowMapSize: the side of each cascade’s map, up to 4096, and 0 casts none. The two share one map twice as wide, so 2048 holds two 2048 squares. On a Landscape the quality level sets it, 1024 at Medium and 2048 at High.
Low casts no live cascades by default, so at Low the sun’s shadow comes from the far shadow map alone, as at Minimum, unless the game turns on the cached shadow there. To cast the cascades at Low, set the level’s map size on Game’s quality prop, such as quality={{ levels: { [QualityLevel.Low]: { shadowMapSize: 512 } } }}: two 512 cascades out to Low’s 30 m. Changing what a level means shows the prop.
Unity’s quality levels name the first shadowDistance and the second a cascade split, as a share of the distance; here the split is in metres because it follows the camera, not the device. Unity and Godot offer up to four cascades and Unreal more; three draws two.
A model that does not cast, such as a far detail level with castShadow: false, leaves the far cascade unshaded where it stands. A material of a game’s own that copies three’s light loop from before r186 reads directionalLights and takes no sun at all: copy three’s lights_fragment_begin chunk, which holds the sun’s loop, or include it. The engine adds the sun’s loop to MToon’s own.
The offset that keeps a surface from shading itself
Section titled “The offset that keeps a surface from shading itself”Each cascade’s lookup moves off the surface along its normal, so a lit face does not shade itself in fine stripes, the artefact called shadow acne. The sun’s normalBias, 0.02 m, is the move at the near cascade on a face toward the sun. The far cascade’s texels are wider, so its move grows by the ratio of its texel to the near one’s. A face the sun meets near edge-on moves further, by one over the cosine between its normal and the sun, up to five times: a leaf card the sun meets edge-on shaded itself at 0.02 m and cleared at 0.1 m.
Unity URP and HDRP and Godot 4 give a directional light’s normal bias in texels, so it scales with each cascade, and URP and Godot scale it by the slope too. three’s normalBias is one move in metres for every cascade and slope. The cached shadow and the far shadow map keep their own offsets, of a texel or more of their own maps.
A smooth edge: the tent
Section titled “A smooth edge: the tent”ShadowType.Tent reads the sun’s cascades and its far shadow map through a fixed 5 by 5 texel kernel in nine of the hardware’s bilinear compares, as Ignacio Castaño’s filter for The Witness does. The edge is smooth and holds still. Percentage takes three’s five taps, turned by a noise for each pixel, which shows as grain on a smooth stylised surface, since nothing resolves the noise over frames. Other lights filter as Percentage does.
Unity URP’s default soft shadows, at Medium, and HDRP’s Low also filter 5 by 5 texels in nine fixed taps. Godot and three turn their taps by a noise that a temporal pass resolves, and Roblox sets one ShadowSoftness, 0.2 by default. The edge’s width is the kernel’s, so a smaller map softens it and loses thin shadows, such as a fence post’s. Measured on 2026-10-16 from Juice Island’s core camera at Medium on the bench’s RTX 3070, the scene pass took 2.41 ms with the tent and 2.51 ms with Percentage, within the noise of a run.
What stands still near the camera: the cached shadow
Section titled “What stands still near the camera: the cached shadow”The cached shadow draws everything that stands still once from the sun, into one depth map round the camera, and keeps it. While it holds, the cascades draw only what moves, such as the characters, moving bodies and the grass, and the ground at its drawn detail.
cachedShadowMapSize sets it, on Landscape, on Lighting and in each quality level: the map’s side in texels, up to 4096, and 0 for none, so the cascades draw everything. Its square reaches 1.5 times shadowDistance round the camera, so its texel is 3 times shadowDistance over its size: 4.4 cm at 4096 over the engine’s 60 m.
Where shadowMapSize is 0, the cached shadow is the sun’s shadow near the camera, so the pools of sun under trees hold on a laptop with no live cascade. The character casts no sun shadow there. Last Light’s Low sets shadowMapSize: 0 and cachedShadowMapSize: 2048 on Game’s quality prop, as Changing what a level means shows.
The map draws again a share of the static set each frame into a second region of its texture, and swaps in once whole, as the far map does. Before it draws, it lists what the static set holds within its square, 32,000 copies a frame, so a redraw costs no frame a spike: on Last Light’s forest edge a redraw landed in 15 to 17 frames. Each list keeps the copies a turn of the light could bring in, up to the turn that moves the square’s corner by 16 texels. A redraw the sun’s turn alone starts within that turn, with the camera where the list was made, draws the same list again and lists nothing. It draws again when:
- the sun turns enough to move a 20 m caster’s shadow by two of its texels, a smaller step as the sun sinks;
- the camera travels an eighth of
shadowDistancefrom where the map was drawn; - something in the static set is added, removed, moved, hidden or dressed anew.
While the camera stands within a quarter of shadowDistance of the map’s centre, and the map was drawn from this sun, the static set stays out of the cascades. Past that, after a teleport, a quality change, the sun’s hand-over to the moon, or a move or a hide the map has not drawn yet, the static set casts into the cascades again until a new map lands. With no cascade, the last map shades through a turn or the hand-over until the new light’s map lands, a few frames, so no frame leaves the static set unshaded. A new piece casts live until a map holds it. A removed piece’s shadow stays in the map until the redraw its removal starts lands, and the rest stays out of the cascades, so an InstancedModel that places its copies anew costs the cascades only its own copies.
Each copy of an InstancedModel draws at the level the view draws it at, measured from the map’s centre, and none where that level casts none. The map draws everything at rest, with no wind. A swaying crown reads it at its rest place, so its own shade moves with it, but the pools of sun on the floor stand still where the cascades would drift them by a few centimetres. A tree whose shadow must sway takes static={false} and casts in the cascades. HDRP’s mixed cached shadows and Unreal’s virtual shadow map cache keep what stands still the same way, and Unreal’s Rigid cache also keeps a swaying primitive’s shadow still.
Past the cascades: the far shadow map
Section titled “Past the cascades: the far shadow map”Past shadowDistance, the sun keeps a cached shadow of everything that stands still: one depth render of the static set from the sun, which the last cascade’s fade blends into. A far valley’s trees then shade one another, and a slope shades its foot. Where shadowMapSize and cachedShadowMapSize are both 0, as at the Minimum and Low quality levels, the far map is the sun’s only shadow. Unreal’s far shadow cascades and HDRP’s cached shadow maps keep the same cache of what does not move.
The map draws again when the sun has turned about a degree, a quarter of a degree when it stands low, and when the camera has travelled a quarter of the map’s reach or the static set changes. It draws a share of the set each frame into a second map, and swaps the two once the new one is whole, so no frame pays for the whole draw. At the sun’s hand-over to the moon, the sun’s map shades until the moon’s lands. The moon is the same light, so the night keeps its far shadow.
Two values set it, on Landscape and on Lighting, beside the cascades’ own:
farShadowMapSize: the map’s side in texels, up to 4096; 0 draws none. A Landscape takes the quality level’s, 1024 at Minimum and Low and 2048 at Medium and High;Lightingtakes 2048.farShadowDistance: the metres round the camera the map covers, 300 by default. Its texel is twice this over the size: 30 cm at 300 m and 2048.
What stands still
Section titled “What stands still”The far map draws the static set, and nothing that moves:
- The Landscape’s ground, which casts into the cascades too, so a hill’s shade holds from near to far.
- The map’s scatter trees and rocks, and its props. A prop’s
static: falsein the map leaves it out. - Every
InstancedModel, at its coarsest detail level that casts a shadow, and in the cached shadow at each level that casts.static={false}leaves out copies the game moves, and a model that casts no shadow stays out either way. Copies with anidleMotioncast into the far map alone. Copies past theircullDistancefrom the camera draw nothing, so they cast nothing, to within the map’s 32 m cells. - Every
Entitywhose body is fixed, a collider with nobodyamong them.staticon the Entity says otherwise either way. The far map draws an Entity’s model; aviewof the game’s own draws only within the cascades.
Characters, moving bodies, grass, particles and skinned models stay out: they cast only within the cascades. Wind sway does not move the map’s copy of a tree. A member of the static set that moves after load draws the map again each time, and a development build warns once, naming it.
To make the renderer yourself, pass a factory. Game hands it a RendererDefaults: the canvas, powerPreference, antialias and alpha. RendererDefaults, RendererFactory, RendererOptions, GameRenderer and ShadowType are exports of @spawnite/engine. The canvas mounts nothing until the factory’s promise resolves, and the loading screen stays up until the first frame draws. A factory that throws or rejects stops the game: a built game shows the engine’s error screen, and the dev server shows its overlay.
PlayCanvas takes the same context settings in createGraphicsDevice(canvas, options), and Unity and Godot hold the depth, stencil and shadow choices in a project’s settings. Here they belong to the game’s Game, read once, so one page can mount two games with two renderers.
Settings you change while the game runs
Section titled “Settings you change while the game runs”The renderer’s other settings, such as toneMapping, localClippingEnabled or outputColorSpace, can change at any time. Read the renderer with useThree((state) => state.gl) in any component under a scene, and set them in an effect. It is the same renderer gl made, and onCreated hands it over once at the start.
Two settings belong to the engine while its parts run:
- The daylight rig writes
toneMappingExposureevery frame from the hour of day, under either renderer. To scale it and keep the rig, set the look’sexposure, which multiplies the rig’s:look={{ base: noon, exposure: 0.6 }}.lighting={false}on the Landscape hands you the exposure, but it also takes away the look’s sun, sky, fill, fog and stack, so you light the scene yourself. The look’s stack tone maps with the same exposure, so the value reaches the screen either way. - The player’s quality level sets the pixel ratio. Change a level’s ratio with Game’s
qualityprop, as Graphics quality shows.
The frame’s order
Section titled “The frame’s order”Each frame runs fiber’s useFrame callbacks in order of their priority, lowest first:
| Priority | What runs |
|---|---|
| Below 0 | Your callbacks that must run before the world steps. |
| 0 | The engine’s step, which mounts first, then your callbacks at the default priority, which read the world as this frame’s step left it. |
| 1 and above | Whatever renders the frame. The look’s post-processing stack renders at 1. Any callback at 1 or above tells fiber not to render the scene itself. |
To draw the frame yourself, such as a second camera’s view or a pipeline of your own, render in a useFrame at priority 1 or above, and pass postProcessing: false to the look so its stack stands aside. The look’s stack also steps aside on its own when a scene mounts its own <PostProcessing>, as Post-processing says.
Captures from a camera of your own
Section titled “Captures from a camera of your own”Two hooks under Game’s canvas draw the scene from a camera of your own. useCaptureImage gives an image for your HTML, such as a card, a thumbnail or a cover. useCaptureTexture gives a texture for the world, such as a monitor, a security feed or a portrait on a sign. Both draw the same scene the same way, and differ in what finishes the picture and when it draws.
What both share:
- The camera: a bookmarked view,
{ eye, target, fov }with anorthoheight for an orthographic camera, or a threePerspectiveCameraorOrthographicCameraof your own. A camera gives its world transform, so one you parent under a turret carries the capture with it. It also gives its field of view or height, near and far planes, zoom, layers, and the focussetCameraFocusnames for it. The capture’s own aspect replaces the camera’s. A projection matrix of your own, a film offset or a view offset does not carry. - What stays out: gizmos and screen overlays. The scene’s fog stays in.
- Your own views: the views that stand where the player’s camera looks stand where a capture’s camera looks for its draw, then go back: the sea’s waves, its foam and shallows round what a
WaterContactFoamholds, and the look’s sky.useBeforeCapture(callback)runs your own code before every capture’s draw, the devtools’ shots too, to stand a view of your own the same way, such as a sky dome that stands on the player’s camera.callbacktakes the capture’s camera, changes what it must, and returns what changes it back, which runs right after the draw. A view only a capture should show stays hidden, andcallbackshows it. - When: at the start of a frame, before your other frame callbacks, so a camera you move in a callback shows in the next capture. A capture waits while a headset presents.
An image for your HTML
Section titled “An image for your HTML”useCaptureImage() returns a call, capture(camera, { width, height }), that resolves with a JPEG data URL width by height CSS pixels at the page’s device pixel ratio. A three camera is read as it stands when you call, so one camera moved between calls gives each call its own image.
- What: the engine’s draw of the scene through the look’s post-processing stack, the bytes the canvas would hold from that camera. It draws at the canvas’s aspect with a wider lens and crops the image, so the stack keeps its size. A very wide image on a narrow canvas widens the lens far: a 3:1 image on an upright phone takes a 60° camera past 150°. A frame your game draws its own way, such as a second scene drawn in a callback above priority 0 or a render pipeline of your own, is not in the image. Under WebGPU no stack is mounted, and it draws straight.
- When: one image a frame. Each waits a whole frame after you ask, and after the page last had a file loading or a model still to draw, so what you mounted for it is in the image. A frame whose canvas draw a held page leaves out still takes one.
- Where: in a target that stands in for the screen, never on the canvas, so the player never sees it. Each material draws with the program it draws onto the canvas with, and the stack ends on the target. Under WebGPU the target is the renderer’s output target.
- Refusals: a call rejects when the component that holds the hook unmounts, the canvas loses its WebGL context, or the GPU read fails, before the image is ready. A call waits while the tab is hidden.
- Cost: the target is the canvas’s size with its samples, shared by every hook on the canvas, and freed a second after the last one unmounts. Each image reads its crop back without stalling the frame: 6.2 MB for a 1440 by 1080 crop at a 1080p canvas, whose pixels take 2.2 ms on the main thread, and 0.2 ms for a phone’s card. The foam draws twice more for an image that frames water its contact capture does not hold, about 0.6 ms on an M2 Pro. With the nine cards of Juice Island’s Find islands sheet at Low, frames stayed within the canvas route’s spread (measured 2026-10-20, headless Chromium).
- Limits: a renderer you make with three’s HDR
outputBufferType, and itssetEffectschain, draws the image without that chain. A line wider than one pixel draws one pixel wide in the image, as every browser on ANGLE draws it on the canvas. - Keeping it: the hook keeps nothing, so keep each image with what it shows and take it again when that changes.
Juice Island’s Find islands sheet takes each island’s card this way, from a miniature of the island it mounts out of the camera’s sight, with cloud banks over the card’s horizon that only its images show.
A texture in the world
Section titled “A texture in the world”useCaptureTexture(camera, { width, height, update }) returns { texture, capture }. The texture is the same object for as long as the component is mounted, so bind it to a material once. A three camera is read live, each frame.
The engine’s tests run this file, a monitor on a wall that shows the yard from a camera sweeping on a post:
import { useFrame } from "@react-three/fiber";import { useState } from "react";import { PerspectiveCamera } from "three";import { CaptureUpdate, useCaptureTexture } from "@spawnite/engine";
// A monitor on a wall showing the yard from a camera on a post, which// sweeps from side to side, so its feed draws every frame. The monitor's// material keeps its tone mapping: the texture holds the scene's light,// and the look finishes the screen as it finishes the wall.export function SecurityMonitor() { const [camera] = useState(() => new PerspectiveCamera(60, 1, 0.1, 80)); const { texture } = useCaptureTexture(camera, { width: 512, height: 288, update: CaptureUpdate.EveryFrame, }); useFrame(({ clock }) => { camera.rotation.y = Math.sin(clock.elapsedTime * 0.3) * 0.8; }); return ( <> <primitive object={camera} position={[0, 4, 10]} /> <mesh position={[-6, 2, 0]} rotation-y={Math.PI / 2}> <planeGeometry args={[1.6, 0.9]} /> <meshBasicMaterial map={texture} /> </mesh> </> );}- What it holds: the scene drawn straight from that camera, in linear light, at
widthbyheighttexels. No post-processing runs and nothing is tone mapped, so the look’s stack finishes the monitor once, as it finishes the wall beside it. Leave the material’s tone mapping on. The stack’s own passes, such as ambient occlusion, height fog, depth of field and the hidden-character silhouette, are not in the feed. This is Unreal’s default capture source,SceneColor (HDR). - When:
CaptureUpdate.OnChange, the default, draws a whole frame after it mounts, once nothing on the page is loading. It draws again in the frame its camera, the focus you set for that camera, or its size changes, and once the page settles after eachcapture(). Callcapture()when the scene changes in front of a camera that stays still.CaptureUpdate.EveryFramedraws in every frame the canvas draws. - Before and between draws: the texture is opaque black until its first draw. A monitor in its own feed shows the draw before, because the scene draws into a second target that the feed then copies. A draw waits in a frame a held page leaves out, and while the canvas has lost its context, and draws again once the context is back.
- Cost: each draw is one more draw of the scene at the texture’s size, with its shadow maps for that camera, plus a copy and the texture’s mips. An
EveryFramefeed pays it every frame, and so does each feed you mount. The feed’s two targets hold half floats at the canvas’s samples. A 512 by 288 feed at four samples takes about 10 MB, counted from the targets’ formats. - Refusals: a width or height that is not a whole number from 1 to the GPU’s texture limit throws a
RangeErrorin development, naming the size and the limit. A production page logs it once and draws nothing. - Fairness: a feed that shows where characters are is a cue, so it draws at every quality level: never switch a feed off by quality.
Under WebGPU the feed draws the same way.
TSL node materials, a preview
Section titled “TSL node materials, a preview”three r186 can draw a TSL node material under WebGLRenderer: call setNodesHandler(new WebGLNodesHandler()) on the renderer from three/examples/jsm/tsl/WebGLNodesHandler.js, in an effect, before the first node material draws. Lights, percentage shadows, fog, instancing and skinning draw on the first build. Treat it as a preview, not a shader language to build a game on:
- At most 12 distinct node materials draw. Each takes 2 of the GPU’s 24 uniform buffer slots, and the 13th draws black. 24 is WebGL2’s minimum, so phones sit at or near it.
- A program that builds again, such as when the sun’s shadow turns on or off with a quality change, uses slots it never frees, and every node material then draws black.
- The handler draws no variance shadow, so leave
shadowsat"percentage"beside it. - It loads
three/webgpu, about 193 KiB gzipped more for a page that uses it.
These numbers were measured on three r186 in Chromium on a desktop GPU. For a shader that works today, use Shader. To run on WebGPU itself, see WebGPU (experimental).