Search Unity

Hiding faces you can't see

Discussion in 'Scripting' started by Vexer, Jul 13, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys im trying to create a voxel procedural generator but i ran into a problem and that is that if i have too many cubes in my scene my game starts lagging and my fps drops down to 3fps so my question is is there any good way to only render the faces of the cube that the player can see? i heard that should help ALOT!! thx :)
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    What you're describing is Occlusion Culling:

    https://docs.unity3d.com/Manual/OcclusionCulling.html

    Are you sure the lag is due to rendering, and not something else? You should be running the profiler to determine the specific cause of the slow down. For all you know, you have a script on every one of your cubes that's running an Update method every frame.

    Also, you'll want to make sure you have batching set up properly. The materials should have instancing enabled.
     
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Unity does not deal with a massive amount of gameobjects well.For me 120k empties will crash it same as 120k cubes .If the amount of cubes is the case you need to employ a system of only loading the visible cubes at any given time.I.
     
  4. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Yes it is the rendering that makes me have 15 fps my question is tho how can i make the faces that the player can't see transparent i don't think occlusion culling is going to help
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Faces that are angled away from the camera will not be rendered. Occlusion Culling will, as stated by dgoyette exclude objects that are obscured by nearer objects. What are these other faces you have that need removing?

    Just to mention, trying to create something like a Minecraft world by building cubes is not really going to scale well. Here is a video showing why.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Occlusion Culling is usually the most efficient for static objects. It does also support dynamic culling but this one does only make sense if a consistent (static) environment exists.

    Additionally, occlusion culling does benefit alot from a level layouts that are designed to hide alot. A world and/or a camera perspective that allows to see most of the objects at any time does not help a lot.

    I'm also not sure whether an efficient runtime solution does exist, so baking does only help when the world is known at build-time.

    There are other techniques that enable you to gain massive improvements though. Using tree strucures (custom containers) for spatial subdivision in order to deal with a chunk of the environment that is currently of interest (based on player and/or camera position).
    For smaller world, you could keep the world in memory, and only apply the information for the chunks of interest.
    For bigger worlds, you can load/unload those information on demand from/to a persistent storage.

    On the other hand, there is also GPU instancing, which allows to efficiently draw great amounts of objects that share the same mesh and the same material. This might help if you approach a world similar to minecraft.

    You could also try do the heavy work on the GPU directly. The environment could be generated asynchronously and when calculations are done, you could update it on Unity's main thread.

    There are a lot of possibilities, I'm not sure how well they would combine in a complex scenario (some are excluding others by the nature of their implementation).
     
    Doug_B likes this.