I am making a multiplayer game, where I want to generate terrain at runtime as it´s discovered(Minecraft has a similar approach). I dont want any loading screens etc. I want to do it at runtime.
So my first thoughts were about assetbundles. But the problem is that those only support static terrains, but I want to generate them at runtime.
The second approach would be to multithread the generation. The random generation itself(returns a float[ ][ ] of heights) would be possible, but since Unity doesnt support multithreading access, I cant assign the generated values to a TerrainData.
Generate your float array in another thread, them apply it to terrain in the main thread
Unity isn’t thread safe so I’m afraid it’s the only way to do it
Are you actually creating a blocky, Minecraft style terrain that you can tunnel into, or a traditional terrain, that is based on a heightmap? Sounds like you are going for the traditional terrain, but you just want to generate it on the fly.
You should be able to generate the terrain data (the floats) in a separate thread. Then, create the terrain from that in your main thread.
In order to cut down on the lag, you’ll need to create small terrains. But you’ll have to experiment with the size of each small terrain, how many trees, how much grass, and textures. Obviously…the more variety you have and the more dense your vegetation and such, the slower the terrain creation will be.
I haven’t actually profiled it…but I think trees are the main cause of lag. Create a terrain about 2048x2048, with about 300k trees, and watch your computer crawl over to the corner and pee all over itself. Remove them and watch it fly. Which is kind of surprising, the vast majority of them are billboarded, so I don’t know why it takes such a hit. Maybe all of the tree colliders…hmmm now I’m wondering why I haven’t tested that.
There are whispers of an improved terrain engine at some point in the future, though I haven’t heard any mention of what features it will have.
Yeah. I’d be curious to see how smooth that terrain gen is with a decent variety and amount of trees, grass, and textures. It’s a neat demo, but I only see one texture. Terrain generates pretty quickly there
ok so i tried it out. it loads in the data from a file asynchronously.
my problem is a good compromise between RAM and Lag.
when i have 32x32 tiles, there´s a lag in updating the terrain collider.
those small tiles take up a lot of RAM because the overhead seems to be really big.
any idea how i can split the updating of the terrain collider into a few frames?
Hmmm. I’ve split my main terrain up, into smaller ones that are 128x128. But, the terrain itself is scaled up, so it’s not so small. I try to keep less than 2000 trees per terrain, and it can still produce a slight hitch when a couple terrains load. Not a big deal, but it’s there. I keep wondering how hard it would be for the community to just roll its own.
I didnt even do trees yet!
as alex already said, building the terrain collider slows everything.
i am switching to another engine, because this is not acceptable for my game…