Search Unity

Terrain.CullAllTerrains causes stutter?

Discussion in 'Scripting' started by Nevermind, May 27, 2011.

  1. Nevermind

    Nevermind

    Joined:
    Jun 10, 2010
    Posts:
    66
    I've made a simple project with camera flying over some terrain. It works OK, but sometimes I get sudden FPS drops for 1 frame. Profiler identifies Terrain.CullAllTerrains() call as the culprit: it takes 100ms or more to complete. I'm generating terrains procedurally, but this doesn't seem to be connected with stutter: stutter happens even when no generation takes place, and the only thing that changes is the camera position.
    What does CullAllTerrains do that's so slow, and what can I do to make it faster?
     
  2. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    I have this issue as well. I can't find anything in the script reference, nor the manual on this.
    Is there really no way to turn off the terrain culling?
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    No there is really no way to turn it off.
    and if you did it would stutter even more than with it enabled as not culling things is 10 to 100 times worse performance wise than doing it.

    you need to setup the terrains realistically for the view range and camera above ground position, going with detailed stuff as if you were on ground with a 1000 view range and with occlusion culling in a flight game isn't gonna work no matter what you do.

    thats a thing that then requires custom occlusion you implement yourself and normal meshes
     
  4. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Alright, thanks anyway :)
     
  5. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    904
    Dredging up this old post just in case anyone else comes across this problem.

    In my case, the main culprit here was actually terrain details (grass, specifically). When moving the camera long distances, I temporarily lower the detail density via script, then raise it back up again when the camera slows down. This eliminated the stuttering for me.
     
  6. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    The stutter is actually caused by the GC which isn't explicity stated. Basically, this method generates some garbage, which after a while will cause the GC to do a collection, hence the stutter. Its always annoyed me.
     
  7. Nevermind

    Nevermind

    Joined:
    Jun 10, 2010
    Posts:
    66
    Um, no it is not. GC collections are shown in profiler as calls to GC.Collect. In this case, profiler showed Terrain.CullAllTerrains() itself as taking lots of time.
     
  8. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    From what I've seen, the GC collects can sneak into other things, and the profiler doesn't seem to be aware of it. I could be wrong, but I often see methods that generate lots of garbage (ie: gui stuff) take a suspiciously long time at regular intervals.
     
  9. Mingo

    Mingo

    Joined:
    Nov 14, 2010
    Posts:
    39
    I'm prototyping a board game that makes use of Unity terrains for the pieces, of which there will be roughly 40. Performance steadily decreases as more terrains are added to the scene, with CullAllTerrains() being guilty of eating a growing percentage of frame time with each terrain added.

    I'd like some information on what exactly this function is doing, why it is necessary, and if the performance can be improved. Reducing camera draw distance to 1 and pointing the cam up into the sky makes no difference to the call time, although disabling some of the terrains does.

    If this function is actually responsible for culling the terrains, as the name suggests, could it be made optional or simplified? It appears that calculating if the terrains should be culled is taking a lot longer than just drawing the terrains in my case. Does it really need to take this long to cull terrains? They have a known and fixed bounding box, so why does it take so long to do basic viewport culling on them compared to meshes?
     
  10. Bezzy

    Bezzy

    Joined:
    Apr 1, 2009
    Posts:
    75
    Hmm. I'm not even using Terrain and this is poking up for me.

    Is it more of a general case culling that's going on?