0
\$\begingroup\$

I wanted to do Occlusion Culling on my Scene but I just get a bunch of Errors like "Couldn't load geometry..." etc. I tried to do like another way but the same thing, the Occlusion Culling just disables the Objects the Camera dont see. I tried it with OnTriggerEnter and Exit.

I have 2 Areas one is on the left and one on the right, when the Player is walking in the first Area the Box Collider checks it and disables the Objects in Area2 but when Player is walking into Area2, Objects in Area1 disables and Different. Got the Script on both Areas that it disables a list of arrays as GameObjects.

My Question is, does this save performance ?


public GameObject[] deactivatingModels;
bool playerEntered = false;

void OnTriggerEnter (Collider other) 
{

    foreach(GameObject envis in deactivatingModels)
    {
        if(other.gameObject.tag == "OcclusionCuller")
        {
            envis.SetActive(false);
            Debug.Log ("Player has entered Occuling Zone");
        }
    }
}

void OnTriggerExit(Collider other)
{
    foreach(GameObject envis in deactivatingModels)
    {
        if(other.gameObject.tag == "OcclusionCuller")
        {
            envis.SetActive(true);
            Debug.Log ("Player has left Occuling Zone");
        }
    }
}
\$\endgroup\$
3
\$\begingroup\$

An inactive object isn't not just not rendered anymore. All other components on the game object are deactivated as well. Inactive objects aren't rendered, don't get checked for collisions and aren't updated anymore. So this should save even more performance than "normal" occlusion culling.

But you can even go a step further: Destroy the game objects in the inactive areas of your game world and re-Instantiate them at their initial positions when the player comes back. That way they don't just stop using CPU and GPU resources, but stop consuming memory as well. The drawback is that you lose the state of left areas. When world persistence is important for your game, you might want to write any information worth persisting to a file.

Keep in mind that destroying and instantiating objects are expensive operations. So you should avoid doing it every frame. You might want to place the trigger areas which cause some areas to unload and other areas to load in passageways where not much action is going to happen. If you still have visible stutter from it, you might want to do the deleting and instancing in packets in a coroutine.

\$\endgroup\$
  • 1
    \$\begingroup\$ +1 on going a step further. Even if you hold the occluded object transforms in RAM, it's still a big win on memory! It could bring a huge map back under the resource budget and you can generate the occlusion volumes with a script. Just make sure you make the occlusion conservative enough to have time to load from disk before the user sees empty space. \$\endgroup\$ – MickLH Mar 8 '17 at 22:31
  • \$\begingroup\$ I would not be so sure about this, oclusion culling is made using very efficient data structures and works internally with renderer I guess. Though here you would add collision check for every object and actually destroying the object as well as instantiating it are very if not the most costly operations - that is why there is an object pooling pattern. \$\endgroup\$ – Candid Moon _Max_ Mar 9 '17 at 0:10
  • 1
    \$\begingroup\$ Note that destroy / instantiate technique may imply to work on a loading strategy to avoid frame rate loss when you will need to reinstantiate hundreds of gamobjects \$\endgroup\$ – lvictorino Mar 9 '17 at 16:36
  • \$\begingroup\$ @CandidMoon I added a paragraph about the cost of destroying and instancing. Object pooling is a CPU->RAM tradeoff, so if having too many objects in the game is a concern, it doesn't make much sense. \$\endgroup\$ – Philipp Mar 9 '17 at 16:47
  • \$\begingroup\$ @Philipp I agree that is it much more performant to unload chunks of areas that are not being used when you have open-world or your entire game is in 1 scene and it is big. For example, game "Inside" - they have this type of unloading. But I haven't really tried implementing this, so I cannot judge about it much. Thus +1 because the answer is right. \$\endgroup\$ – Candid Moon _Max_ Mar 9 '17 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.