How does the game Rust place Millions of Grass, Rocks, and Trees

1.7k views Asked by At

Rust has millions and millions of grass, rocks, and trees instances across its maps, which get as big as 8km.

Is that grass placed dynamically around the player at runtime? If so, is that done on the GPU somehow or using a shader?

In my game, we use vertex colors and raycasting to place vegetation, and we store transform data which gets initialized with indirect GPU instancing at runtime.

However, I can't imagine this would scale well with something like trees. Are there really thousands of mesh colliders active in the scene at all times? I thought perhaps they might store all those meshcolliders in the scene, and the gameobject could be tagged, and if you hit it with a tool, it adds a "Tree" component to it.

Am I headed in the right direction with a "spawn everything before hand, instance it at runtime" approach?

I've tested this and it actually worked, spawning 24 million instances (took 20 minutes to raycast), and then initializing the GPU with the instances.

This is cool and all, even though it lead my Unity editor to crash after a little while (memory leak?).

Maybe you store the instances before runtime, and then when you start the decicated server you do all the raycasting and place all the trees, rocks, and other interactive objects.

But I am worried that if I tried to store even 10000 gameobjects (for interaction, stuff like choppable trees, mineable rocks) that performance would tank.

1

There are 1 answers

1
i773 On

You can actually see this for yourself in the code: https://github.com/Facepunch/Rust.World/blob/master/Assets/Scripts/WorldExample.cs

The game loads the instances in memory or can stream the instances as well, but you wouldn't ever have the prefabs all instanced at the same time in GO's as culling / chunks are needed to keep performance (and not crashing your program).

There's many tricks, lowering your draw calls, billboarding, culling, object pooling, chunking, just to name a few.

A suggestion if you're interested in systems like this is to look into ECS for unity as it offers a lighter way of instancing data: https://docs.unity3d.com/Packages/[email protected]/manual/index.html

That way you can have thousands of instanced objects for a fraction of what Game Objects would offer in size and performance.

I also don't want to get too deep into generating objects as that's not the main question, but to generate a map using raycasts is extremely slow as you now know, you should look into noise generation to create biomes and quickly spawn your instance, also maps really need a set place for objects you're just adding data that a user needs to load in. Another option is to place objects that serve as visual objects arbitrarily around a viewing distance so they only exist in memory, if someone runs past a generic grass pile, they will never care if it's moved the next time the run past it.