I am creating a game with a bunch of planes forming cubes to make terrain. I am using OnBecameVisible and OnBecameInvisible to turn the renderer on and off.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cull : MonoBehaviour
{
void OnBecameInvisible()
{
GetComponent<MeshRenderer>().enabled = false;
}
void OnBecameVisible()
{
Debug.Log("Visible");
GetComponent<MeshRenderer>().enabled = true;
}
}
The planes become invisible, but when OnBecomeVisible is called, the script prints that it is visible again, but the meshrenderer does not turn on.
This forum doesn’t have markup. Use the code tags provided in the little formatting bar.
If you disable the renderer, then the system has no idea when it becomes visible again, because it’s the renderer that decides whether it’s visible or not.
Bake?? Where do i click bake? My planes are spawned in by a script like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockSpawner : MonoBehaviour
{
public GameObject block;
public int width = 0;
public int height = 0;
public int depth = 0;
void Start()
{
transform.position = new Vector3(-0.5f, -(depth/2), -0.5f);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < depth; j++)
{
transform.position = new Vector3((i / 2) + -0.5f, 0f, (j / 2) + -0.5f);
Instantiate(block, transform.position, Quaternion.identity);
}
}
}
}
well instead of inventing your own, why not use the tools available? ok so your objects arent marked as anything by default, but Unity - Scripting API: StaticOcclusionCulling will set you on some path, or you could play around in a scene and see for yourself using the occlusion culling window provided to set properties and bake and the like to see what it does.
As others have mentioned already, renderers will be culled automatically. OnBecameVisible/OnBecameInvisible is driven by these culling results, so it doesn’t make sense to use it to disable the renderer. It is meant to disable other calculations that are not needed while invisible.
You can do culling manually with the CullingGroup API, if you really want. The CullingGroup API is independent of the renderer.
However, the tricky thing is that a renderer can be seen by more than one camera - in particular the cameras that are used for shadow map rendering. So you not only have to check if the object is invisible to the main camera, you also have to check each shadow casting light (… cascade).
PS: The StaticOcclusionCulling class seems to be an editor-only helper class. Don’t think you can use that one.