ForceFrustum

found in: https://answers.unity.com/questions/36446/disable-frustum-culling.html
mail@pastecode.io avatar
unknown
csharp
2 years ago
1.2 kB
0
Indexable
Never
using UnityEngine;

//PUT THIS BEHAVIOUR LATE IN YOUR SCRIPT EXECUTION ORDER

public class FrustumCullingHack : MonoBehaviour
{
    public MeshFilter meshFilter;

    void Update()
    {
        // boundsTarget is the center of the camera's frustum, in world coordinates:
        Transform camTransform = Camera.main.transform;
        Vector3 camPosition = camTransform.position;
        Vector3 normCamForward = Vector3.Normalize(camTransform.forward);
        float boundsDistance = (Camera.main.farClipPlane - Camera.main.nearClipPlane) / 2 + Camera.main.nearClipPlane;
        Vector3 boundsTarget = camPosition + (normCamForward * boundsDistance);

        // The game object's transform will be applied to the mesh's bounds for frustum culling checking.
        // We need to "undo" this transform by making the boundsTarget relative to the game object's transform:
        Vector3 realtiveBoundsTarget = this.transform.InverseTransformPoint(boundsTarget);

        // Set the bounds of the mesh to be a 1x1x1 cube (actually doesn't matter what the size is)
        meshFilter.mesh.bounds = new Bounds(realtiveBoundsTarget, Vector3.one);
    }
}