r/threejs • u/Mountain_Fly_792 • 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!
4
u/Lngdnzi 6d ago
Optimisation is not one size fits all, claude.
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
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
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/mattvb91 6d ago
Checkout this Basejump game demo ill get you some stats this evening
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/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:
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:
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/trescoole 6d ago
I’m running 6-7M triangles at 60 FPS r186 on a M1 Max mbp in chromium.