Plain Three.js, pushed hard

No engine and no R3F: chunked instancing, a ten-slot light pool, dithered wall cut-outs, procedural lava, GPU weather and a per-biome post stack at 60 fps.

Lanternwatch harbour at night in the rain, the waypoint's beam lit among docks and foam-rimmed water
Contents
  1. A map becomes a few hundred draw calls
  2. Ten real lights, and the illusion of 150
  3. Walls that get out of the way
  4. Lava is the light
  5. Weather, sea and sky with no CPU cost
  6. One post stack, tuned per biome
  7. Scaling down without a settings screen
  8. What plain Three.js cost and bought
  9. Read next

About 11,000 lines on Three.js r186, with no engine, React Three Fiber or editor. React owns the HUD; a client-only component hands a canvas to an Engine that reads the zone map, room state and local prediction. Everything an engine would do (batching, lights, occlusion, quality scaling) is done on purpose.

A map becomes a few hundred draw calls

WorldBuilder buckets every prop by 32 × 32-tile chunk and model part:

src/game/engine/world/WorldBuilder.tsTypeScript
const add = (v: ModelVariant | undefined, x: number, y: number, rotY: number, scale: number, occluder: boolean, shadow: boolean, h = 0, shade = 1) => {
  if (!v) return
  const key = `${Math.floor(x / CHUNK)},${Math.floor(y / CHUNK)}|${vid(v)}|${occluder ? 1 : 0}|${shadow ? 1 : 0}`
  let b = buckets.get(key)
  if (!b) {
    b = { variant: v, occluder, shadow, list: [] }
    buckets.set(key, b)
  }
  const m = new THREE.Matrix4().compose(
    new THREE.Vector3(x, h, y),
    new THREE.Quaternion().setFromAxisAngle(UP, rotY),
    new THREE.Vector3(scale, scale, scale),
  )
  b.list.push({ m, shade })
}
  • One InstancedMesh per bucket per material. Chunks give tight bounds, so frustum and shadow culling skip most of the map.
  • Occluder and shadow flags in the key keep one material behaviour per mesh.
  • Per-instance shade stops identical rocks looking stamped.
  • Towns and wilds use continuous chunked terrain from terrain.ts: blended vertex colours, height from heights, fake AO on cliff edges. Dungeons use procedural brick or rock wall blocks.

F3 overlay, headless GPU browser, 1600 × 900, all zones at a steady 60 fps:

Zone Draw calls Triangles Shader programs
Hollowmere (Act I town) 249 556k 32
The River of Flame 146 84k 38
The Storm Coast 390 192k 50
The Drowned Cathedral 193 450k 42
The Frozen Forest 123 224k 34
The Shattered Isles 83 117k 37
The Mirror Halls 228 524k 44

Storm Coast has everything at once (sea, foam, rain, docks, wrecks, lanterns); towns are dense with detailed Kenney houses.

Ten real lights, and the illusion of 150

A crypt has 147 placed lights. Every forward-rendered PointLight costs every lit fragment, and changing the count recompiles every lit shader. So there are exactly ten real lights.

A diagram of a crypt with 147 map lights as dim dots and the ten nearest to the player as bright real lights, with notes on glow cards, splats and the handover
The light pool. Every light always has a cheap glow card and ground splat; only the ten that matter most get a real PointLight.

Every map light gets a glow card and an additive ground splat from one batched mesh. LightRig.reassign gives the ten slots to live effect lights first, then map lights by distance, and keeps slots with owners still wanted:

src/game/engine/lighting.tsTypeScript
private reassign(): void {
  const cand = this.scratch
  cand.length = 0
  this.dyn.forEach((d, i) => {
    if (!d.alive) return
    const dd = Math.hypot(d.x - this.focus.x, d.y - this.focus.z)
    if (dd < 40) cand.push({ i: -2 - i, d: dd - 1000 })
  })
  this.lights.forEach((m, i) => {
    const dd = Math.hypot(m.x - this.focus.x, m.y - this.focus.z) - m.radius * 0.4
    if (dd < 32) cand.push({ i, d: dd })
  })
  cand.sort((a, b) => a.d - b.d)
  // …keep slots whose owner is still wanted; hand free slots to the rest
}
  • Handover: the real light fades in as the splat fades out by the same amount. The splat matches what the real light does to the floor, so nothing pops.
  • Fixed count: unused slots sit at intensity 0, so shaders never recompile.
  • Tiers: 10, 6 or 4 slots.
The Hollowmere town square at night, lit by lamp posts and a forge glow, the hero at the centre
Hollowmere, captured for this post. 74 map lights; the lamp posts nearest the hero are real lights, the rest are cards and splats you can't tell apart.

Walls that get out of the way

Diablo cuts a hole through whatever hides the hero. Transparency needs sorting and material swaps; a dither is one discard. Occluder materials get a few lines via onBeforeCompile, reading global uniforms for the player and cursor screen positions:

src/game/engine/occlusion.tsGLSL
if (uOccParams.w > 0.5 && vOccWorld.y > 0.45) {
  float fd = distance(cameraPosition, vOccWorld);
  float a = max(occAmount(uOccScreen.xy, uOccParams.x, fd), occAmount(uOccScreen.zw, uOccParams.y, fd) * 0.8);
  // keep ~25% of the pixels at full occlusion; fade the stub in over the first metre
  a *= smoothstep(0.45, 1.1, vOccWorld.y);
  if (a > 0.0 && occBayer(gl_FragCoord.xy) < a * 0.75) discard;
}
  • Cut inside a screen circle around player or cursor, only if nearer the camera than the player
  • 4 × 4 Bayer pattern, up to 75% of pixels: a ghost, not a hole
  • The bottom ~half metre stays as a stub
  • Three.js chunks stay intact, so occluders still get shadows, fog and pooled lights; zero CPU cost

Lava is the light

A MeshBasicMaterial with its colour replaced, world-space procedural so tile seams never show:

src/game/engine/world/lava.tsGLSL
vec2 flow = vec2(t * 0.11, t * 0.045);
vec2 q = vec2(lavaF(p * 0.32 + flow), lavaF(p * 0.32 - flow.yx + 5.2));
float n = lavaF(p * 0.55 + q * 1.9 + vec2(t * 0.07, -t * 0.05));
float edge = lavaEdge(p * 0.95 + q * 0.9 + vec2(t * 0.04, t * 0.025));
float plate = smoothstep(0.4, 0.5, n + 0.1 * sin(t * 0.6 + q.x * 6.0));
float crust = plate * smoothstep(0.03, 0.14, edge);
float pulse = 0.85 + 0.15 * sin(t * 1.6 + n * 7.0);
// hottest (yellow-orange) in the open channels, cooling to deep red toward the plates
vec3 hot = mix(vec3(1.0, 0.5, 0.08), vec3(0.85, 0.1, 0.01), smoothstep(0.2, 0.45, n));
vec3 molten = hot * (1.2 + 1.2 * (1.0 - n)) * pulse;
  • Domain-warped fBm for flow, drifting Voronoi for crust plates with glowing seams
  • HDR colours above 1.0, so bloom picks them up with no tagging
  • Square tiles fixed by a one-texel-per-tile mask, linearly sampled, box-blurred and noise-thresholded: rounded banks, a cooled crust rim, a hot seam inside
  • Area glow comes from generator-placed lava lights in the same pool
A lava channel in Act III. The surface is one instanced quad per tile; the rounded banks, crust plates and glowing seams are all in the fragment shader.

Weather, sea and sky with no CPU cost

Build once per zone from the ZoneMap, animate entirely in shaders from a shared time uniform. Weather: up to 7,000 particles, one instanced draw, each drop a pure function of seed, time and wind, wrapped into a box around the player:

src/game/engine/world/weather.tsGLSL
float speed = 0.75 + aSeed.w * 0.5;
vec3 vel = uWind * speed;
vec3 p = aSeed.xyz * uBox + vel * uTime;
// …snow sways, blizzard gusts…
// wrap into a box centred on the player (the box origin tracks uCenter)
vec3 lo = uCenter - uBox * vec3(0.5, 0.0, 0.5);
p = lo + mod(p - lo, uBox);
// fade at the box edges so wrapping never pops
vec3 q = (p - uCenter) / (uBox * vec3(0.5, 1.0, 0.5));
vFade = (1.0 - smoothstep(0.7, 1.0, max(abs(q.x), abs(q.z)))) * smoothstep(0.0, 0.08, q.y) * (1.0 - smoothstep(0.8, 1.0, q.y));

Four uniforms per frame. Rain streaks along velocity; snow sways; blizzards gust.

A hero walking a snowy path through the Frozen Forest at night, snow falling, pines heavy with snow
The Frozen Forest. Snowfall is one draw call; the blue night comes from the biome preset's fog, moon and grade.
  • Sea: built along every shore, waves and foam in the shader
  • Starfield abyss: layered at depths for parallax, with generated rock undersides on every island
  • Reflections: a 128² cube map around the player, one face per frame, about a sixth of a scene render
A hero on a floating island over a starfield in the Shattered Isles, a plank bridge leading off into the dark
The Shattered Isles. The abyss has no mesh at all; the starfield layers and island undersides are generated from the map.

One post stack, tuned per biome

One EffectComposer; the scene renders to a 4× MSAA half-float target so HDR survives to tonemapping.

Five boxes in a row: scene, heat, bloom, output and grade, each with its per-biome settings
The post stack. The heat pass exists only in Act III's biomes; the other four run everywhere with preset-driven settings.
Pass What
Heat Act III only; pixels wobble above hot red sources; four taps, one displaced read
Bloom UnrealBloomPass at half res, preset strength/radius/threshold
Output tonemap to sRGB at preset exposure
Grade lift/gain, saturation, elliptical vignette, shadow-weighted grain, damage and level-up flashes

The grade turns the same models into desert dusk or a cold ice palace: frost desaturates to 72% and lifts shadows blue; Act II warms highlights and adds haze.

Scaling down without a settings screen

src/game/engine/Engine.tsTypeScript
const TIERS: Readonly<Record<QualityTier, { samples: number; shadow: number; shadowEvery: number; pool: number; particles: number; bloom: boolean; dprFloor: number }>> = {
  high: { samples: 4, shadow: 2048, shadowEvery: 1, pool: 10, particles: 1, bloom: true, dprFloor: DPR_MIN },
  medium: { samples: 0, shadow: 1024, shadowEvery: 2, pool: 6, particles: 0.7, bloom: true, dprFloor: 1 },
  low: { samples: 0, shadow: 1024, shadowEvery: 3, pool: 4, particles: 0.45, bloom: false, dprFloor: 0.8 },
}
  • Touch devices start at medium
  • Frame-time EMA > 20 ms for 1.5 s: pixel ratio −0.2; at the tier floor, drop a tier
  • < 14 ms for 5 s: pixel ratio back up to the device target
  • The moon's shadow camera follows the player, so 2,048² stays crisp; medium and low refresh it every second or third frame

What plain Three.js cost and bought

Cost: four files an engine would give you (instancing and culling, the light pool, occlusion, the quality governor), each readable in one sitting. Bought: control of the frame, a bundle that only loads on engine pages, and features that compose because each is a stock material plus a few injected lines. Lava blooms, occluders take shadows, mirror floors reflect pooled torchlight.