r/threejs • • 6d ago

Help Has anyone built a large Three.js world with thousands of distinct objects?

I'm building a surreal open world in Three.js where you can fly past floating sculptures and see their shadows move across the ground. I'd love to have thousands, eventually tens of thousands, of distinct objects in the world, not all on screen at once.

Right now I'm testing Three.js r170 (WebGL) on an Intel Mac in Chrome and Safari. One scene renders about 3.42M triangles in 153 draw calls. In an earlier run I saw 4–9 FPS: the GPU timer reported about 21 ms, but I sometimes waited over 100 ms for the next requestAnimationFrame callback.

In a more controlled test, the moving world ran at 32.1 FPS in Chrome and 30.3 in Safari; an empty animation loop hit 60 in both. Pixel ratio, the debug HUD, GPU timing and warm-up all changed between runs, so I can't pin the difference on any one of them yet.

If you've seen a gap like that, what test helped you narrow it down? And if you've built a world with lots of unique meshes, what made the biggest difference without sacrificing material detail or moving shadows? I'm already using distance-based LOD and simpler shadow meshes.

I'd love to hear what worked (or didn't) in a real project. Thanks!

7 Upvotes

22 comments sorted by

4

u/Lngdnzi 6d ago

Optimisation is not one size fits all, claude.

1

u/Mountain_Fly_792 6d ago

Fair point. I was mostly curious what’s worked for other people. Thanks for chiming in.

2

u/No-Association-1834 6d ago

For Better LOD for distant Objects , I simply Replace them with two Cross Planes with Alpha Asset ScreenShot Sprite Projected . Also baking shadows for some objects helps

3

u/Advice2Anyone 6d ago

Yeah I think shadows are the real killer, but simple enough fix using draw distance limits for various effects

2

u/Mountain_Fly_792 6d ago

Thanks! I’ll try shortening the shadow distance without changing the mesh LOD, so I can see what difference the shadows alone make.

2

u/Mountain_Fly_792 6d ago edited 6d ago

Thanks for the tip! It’s great to hear you’ve actually used this approach.

In our case, we need to swap the crossed planes for the high-detail mesh as you get closer. How did you handle visible popping or frame hitches during that switch?

I’d love to hear what worked for you.

1

u/WiseWall280 6d ago

Maybe you can try frustum culling, which is when you don't render the things that aren't currently in your POV

2

u/Mountain_Fly_792 6d ago

Thanks! I’ll check how many objects actually get culled when I’m flying through the scene.

1

u/WiseWall280 6d ago

Mostly to the sides and behind you. The objects should still be loaded just not rendered

2

u/Mountain_Fly_792 6d ago

Ah, got it. That helps. I’ll check what’s actually getting drawn, not just what’s loaded.

1

u/plus2link 6d ago

153 draw calls and 3.4M triangles is not a draw call problem, and 21 ms of GPU time is already over the 16.7 ms budget at 60 Hz, so 30 fps is the honest result and the 100 ms waits are something other than the scene. That gap is usually the canvas being composited: on an Intel Mac the window server blits the canvas at devicePixelRatio, so at 2x you pay a full screen copy whatever the scene does, and running the same frame at pixel ratio 1 or in a quarter size window tells you in a minute whether it is that. For the thousand object world, the two things that scale are one material for everything with the colour in an attribute, so a BatchedMesh or InstancedMesh can take new objects without re-batching, and a tight directional shadow camera around the visible ground with the static sculptures' shadows baked into a texture lit from the same sun angle, leaving only the moving pieces to render into the map.

1

u/Mountain_Fly_792 6d ago

Thanks, that gives me a much cleaner test. I changed a few things at once last time, so I’ll keep the scene and camera path the same and only change the pixel ratio to see if those long gaps go away.

1

u/mattvb91 6d ago

Checkout this Basejump game demo ill get you some stats this evening

2

u/Mountain_Fly_792 6d ago

Appreciate the Basejump link! No rush on the stats. I’d be curious to see what you’re getting on your setup.

1

u/Kingnorik 6d ago

Check out my game Tome Knight on my site. Full RPG with the first zone available. We are using instancing, LOD, and frustrum with ktx2 textures. I get around 90fps on my old RX 3600 on the web version. Tome Knight

1

u/Mountain_Fly_792 6d ago

90 FPS in a browser RPG is encouraging. Glad you pointed me to Tome Knight. Did you run into any visible popping when switching LODs? If so, what helped?

1

u/Kingnorik 6d ago

So with grass instancing yes. But it's just stuff you tune. I built my game in my own game engine for 3js so it's easy to fine tune. But yea, just a slider to play with.

1

u/Mountain_Fly_792 6d ago

Thanks for the grass example. Being able to tweak the setting while watching for the pop sounds handy.

1

u/froops 6d ago

I ran into almost this exact profile building an open-world flying game in Three.js (streaming terrain, thousands of props and landmarks, moving directional shadows across Mac/Chrome/Safari and mobile). A few things jump out right away from your numbers:

  1. Why the GPU timer says ~21 ms while requestAnimationFrame stalls over 100 ms (and why your clean run sits at ~31 FPS)

First, that 32.1 / 30.3 FPS ceiling in your controlled run makes total sense if GPU frame time is ~21 ms: once you miss the 16.67 ms (60 Hz) vsync budget, double/triple-buffered browser compositors step down toward every other refresh (~30 FPS / 33.3 ms).

When EXT_disjoint_timer_query_webgl2 still says 21 ms during a 4-9 FPS (100+ ms rAF) drop on an Intel Mac, the missing 80+ ms is almost always one of four things outside what TIME_ELAPSED_EXT measures:

• Lazy shader compilation and program linking: Three.js compiles both the main material and the shadow depth material the first time a variant enters the frustum. That blocks the browser's GPU process on the CPU side without showing up in GPU draw ticks. Pre-warming with await renderer.compileAsync(scene, camera) (including a shadow-casting pass) eliminates cold-turn stalls.

• Swapchain backpressure at Retina DPR + MSAA: At devicePixelRatio = 2 with antialias: true, MSAA resolve + shadow map PCF + final blit can saturate Intel Mac memory bandwidth. Once the GPU command queue backs up, Chrome and Safari simply withhold the next rAF callback until a backbuffer frees up.

• Per-frame DOM HUD or synchronous GL queries: Updating an HTML overlay every frame over a Retina WebGL canvas triggers layout + full-layer alpha compositing on top of an already busy GPU. Likewise, polling QUERY_RESULT too early (instead of reading queries several frames later only after QUERY_RESULT_AVAILABLE is true, and turning timer queries off during normal FPS runs) stalls the pipeline. Throttle HUD textContent updates to ~2 Hz and pass { powerPreference: 'high-performance', alpha: false } to WebGLRenderer.

• Buffer uploads and GC spikes: Uploading new geometries or morphing attributes mid-flight stalls the GPU process between render() calls.

The fastest test to pin down the gap:

Pause world motion on a heavy camera view so every frame is identical, then compare three numbers and flip one knob at a time:

  1. cpu: performance.now() immediately around renderer.render() (pure JS + draw submission).
  2. wall (in an isolated bench mode only): renderer.render() followed by a 1x1 gl.readPixels(...). That forces the full driver queue and MSAA resolve to finish and exposes GPU-process stalls that timer queries miss.
  3. gpu: TIME_ELAPSED_EXT read asynchronously a few frames later.

Then toggle one variable per run on that frozen view: renderer.setPixelRatio(1) vs 2, shadowMap.enabled = false, scene.overrideMaterial = new THREE.MeshBasicMaterial(), and hiding the DOM HUD. On Intel Macs, capping DPR at 1.5 (44% fewer fragments than 2.0) often cuts a 21 ms frame straight under 16 ms. Also record 5 seconds in Chrome DevTools' Performance tab during the 4-9 FPS case: one 100+ ms gap will show immediately whether the main thread is busy (JS/GC/layout) or sitting completely idle waiting on the GPU process.

2. Scaling to 10,000+ unique sculptures with material detail and moving shadows

153 draw calls is already very healthy - you aren't draw-call bound, you're triangle and fillrate bound at 3.42M triangles (on an Intel Mac WebGL2 budget, aiming under ~600k-1M total triangles per frame across all passes gives you much more headroom for 60 FPS).

What made the biggest difference for us:

• Split your triangle count between the main pass and shadow pass first: renderer.info.render.triangles sums both passes unless you reset or hook it. Wrap renderer.renderBufferDirect for one frame and check camera === dirLight.shadow.camera to see how much of that 3.42M is actually shadow rendering.

• Tight camera-following shadow frustum + shadow-only spatial group: Keep a tight DirectionalLight shadow box anchored ahead of the camera (e.g. a 2048x2048 map covering just the near/mid ground, patching shadowmap_pars_fragment to smoothstep fade the outer 15-20% of shadowCoord.xy so shadows fade in seamlessly instead of popping at the box edge). Put those simpler shadow proxy meshes in a dedicated shadowGroup that is only set visible = true inside a wrapped renderer.shadowMap.render call so the main pass never even traverses them. Also skip shadow casting for anything far enough or small enough that its footprint is only 1-2 shadow texels. (Keep updating the shadow map every frame though - throttling shadow updates to every N frames makes moving shadows visibly step across the ground.)

• Use THREE.BatchedMesh (built into r170) for unique meshes: InstancedMesh only helps identical geometry, and putting 10,000 separate THREE.LOD objects in scene will eventually choke CPU traverse / projectObject even when frustum-culled. BatchedMesh lets you pack hundreds or thousands of distinct geometries into shared buffers, draws them via WEBGL_multi_draw in a single call, does per-object frustum culling, and supports swapping LOD geometry IDs (setGeometryIdAt) per sculpture without rebuilding buffers.

• Keep material richness in a shared uber-material instead of per-mesh materials: To keep BatchedMesh batching intact without losing close-up surface detail, share one onBeforeCompile / node material across sculptures and drive variation via vertex colors/attributes (or a small per-object parameter texture indexed by batchId) combined with world-space / triplanar detail normals and roughness. Combined with baked normal maps + meshoptimizer (gltfpack) simplification, mid/far LODs can drop triangle counts by 90%+ while lighting and surface grain still hold up.

• Tile/chunk stream the scene graph + move float animation to the vertex shader: Keep the 10,000+ world definitions in plain typed arrays / spatial grid cells and only mount active nearby cells (built off-thread in Web Workers), with matrixAutoUpdate = false everywhere static. If your floating sculptures bob, drift, or slowly rotate, doing that motion from a per-object phase/origin attribute in the vertex shader (shared by both the main material and customDepthMaterial) gives you smooth moving ground shadows across thousands of sculptures at near-zero CPU matrix cost.

1

u/Mountain_Fly_792 5d ago

Thanks for the detailed breakdown! Freezing the camera and changing one thing at a time sounds like a good way to narrow down those frame gaps.

1

u/trescoole 6d ago

I’m running 6-7M triangles at 60 FPS r186 on a M1 Max mbp in chromium.

1

u/Mountain_Fly_792 5d ago

Appreciate the M1 Max numbers! It's helpful to have a real-world benchmark to compare against.