In my game, I need to load terrain assets from Resources and Instantiate them in my scene to save memory. I’m loading them asynchronously this way :
IEnumerator AsyncLoad(name)
{
var request = Resources.LoadAsync(name);
yield return request;
// Terrain GameObject creation code, not the source of the overhead
}
It works, but in the profiler I get a ~300ms synchronous spike from TerrainData.AwakeFromLoad in a single frame. This call contains calls to TerrainData.GenerateBaseMap, TerrainData.GenerateBaseMapSpecularOrMetallic and other lighter methods. No matter what I try to execute over several frames, I can’t get rid of that particular spike.
I guess I could divide terrains even more, but it would take some time and I would get more spikes, but smaller. Is there another way to get rid of it, or at least reduce it?
Hi …
I’m not sure how to get rid of it, but what are GenerateBaseMap and such doing? Those are functions you wrote i assume? And about what terrain size do we speak here?
Those are not functions I wrote, they are internal calls in TerrainData.AwakeFromLoad. Here’s a screenshot from the profiler.
Each terrain has a 250X250 size, with 2048 of Heightmap Resolution, Detail Resolution, Control Texture Resolution and Base Texture Resolution.
Ok, i misunderstood that.
Without beeing an expert on unity terrains, i would say there’s not much you could do. At least i had similar problems creating terrains of that size from float arrays. Simply assigning the heightmap, detail data and such directly to the terrain and detail textures was pretty slow with 2048 resolution. I tried a while to work around that, but never found a faster approach, threading didn’t really work and so on - so i decided i would probably go with 512x512 maps.
If you ever find a solution to this, be sure to let me know.
Thanks for your input, I tried reducing the resolutions of the different terrain map resolutions and it accelerated the process, but it destroys my terrain’s heightmap and splatmap and it still takes too much time to run. I would really like to have some way to make this process run before the terrains are loaded without having to keep the terrains in memory, or to have it run in the background.
1 Like
bump
This is still the case with InstantiateAsync whereby the gameObject contains a terrain with terrainData. When intergrated with the mainthread this terrainData.AwakeFromLoad is still the bottleneck.