DEV Community

petgem
petgem

Posted on Originally published at lullwoodgame.com Fully Autonomous

Lullwood: How Many Trees Can three.js Render? 5,200, With Chunked InstancedMesh

Lullwood, our free browser horror game, draws a 5,200-tree forest by splitting it into 64 chunks, giving each chunk its own trio of three.js InstancedMesh objects, and building only the chunks near the player: 25 at spawn, 36 at most while walking. The heaviest whole frame we measured was 101,996 triangles. Drawn as one pool, the trees alone would be 270,400. Here is how it works and what we could not measure.

One pool, never culled

The forest is 5,200 trees on a 480-unit map. A tree is three meshes: a six-sided CylinderGeometry trunk and two seven-sided ConeGeometry foliage cones. The first version drew every tree from one InstancedMesh pool covering the whole map. A mesh that spans the map is always partly in view, so frustumCulled was off and all 5,200 trees went to the GPU every frame.

Chunks, so the frustum test has something to skip

The fix is a grid. Chunks are 60 units wide, so the map is 8 by 8 = 64 chunks, and each tree is filed by its x and z. Every chunk gets its own trunk and two cone meshes, each sized to that chunk's exact tree count. frustumCulled stays at the three.js default of true, and the engine calls computeBoundingSphere() on each mesh once, since the layout is static.

That sphere is built from the instance matrices, so it wraps the chunk's trees rather than one tree's geometry. A chunk behind the camera is skipped with no new per-frame code. The test is coarse: a 60-unit cell has a diagonal of about 85 units, and a chunk that grazes the view draws all of its trees.

Then most of it stops existing

A later change streams the chunks. Only those within two chunks of the player are built, a 5 by 5 square of 300 by 300 units, and a chunk is disposed only once it is more than three chunks away, so crossing a boundary back and forth does not rebuild it. The ring is 25 chunks, but the unload band keeps the chunks you just left, so a walking player can have up to 36 of the 64 as meshes: six per axis, two ahead and three behind (L1286-1291). We derived 36 from that rule and then measured it. The map is still 5,200 trees in data, and building a chunk makes no random-number call, so every seed gives the same forest.

The numbers

We measured the live game: headless Chrome, 1280 by 720, our pinned QA seed, five positions and four headings each.

  • At spawn, 25 chunks are live and 2,088 of the 5,200 trees exist as instances. In a map corner the ring is clamped to 9 chunks and 760 trees.
  • Across all 20 views, one whole frame (trees, cover, predators, effects) cost 35 to 190 draw calls and 11,640 to 82,740 triangles.
  • Turning on the spot matters. From one position the frame was 75,928 triangles facing one way and 19,582 facing the other, which is consistent with the frustum test, though the heading also changes which cover props and predators are in view.
  • Walking changes the chunk count. We stepped the player one chunk at a time, teleporting to each chunk centre because the unload rule reads only the chunk index, along a staircase path across the map. Live chunks climbed to 35, and a path that doubled back across a boundary reached 36 chunks and 2,983 trees. The heaviest whole frame in those walks was 101,996 triangles and 219 draw calls.

The 270,400 for the old pool is arithmetic, not a measurement: 52 triangles a tree (24 + 14 + 14, from the geometries as three.js 0.185.1 builds them) times 5,200. It is three draw calls, every frame.

The part we do not have

There is no recorded before. When chunking landed, its own commit message reported the same reading before and after: 1 draw call and 2 triangles. renderer.info.render only holds the last render call of a frame, and ours ends on the full-screen post-processing blit, so the probe was counting a quad. It was fixed two days later, and nobody has run the single-pool version through the fixed probe.

Chunking also trades triangles for draw calls: a full ring can submit 75 tree draw calls at spawn and up to 108 (36 chunks times 3) while walking, where the old pool submitted 3. We ran no frame-time benchmark, so there is no speed claim here. And the ring reaches at least 120 units while a comment in our source puts the fog's limit at 40 to 60, so most of it is fogged out. We have not tried a smaller radius.

The full version, with a line number for every claim, is the Lullwood devlog post this one is cross-posted from. You can play Lullwood free in your browser and judge the forest from inside it.

Disclosure: we are the studio behind Lullwood. This post was drafted by an AI agent, checked by a second agent against our written brand rules, and published by the studio.

Top comments (0)