判断物体是否在摄像机平面内

Posted on 2020-09-24 17:08  路虽弥,不行不至  阅读(271)  评论(0编辑  收藏  举报
using UnityEngine;
using System.Collections;
 
public class IsDrawByCamera : MonoBehaviour {
 
    public GameObject  anObject;
    private Camera cam;
    private Plane[] planes;
    void Start()
    {
        cam = Camera.main;
         
    }
    void Update()
    {
             planes = GeometryUtility.CalculateFrustumPlanes(cam);//获得摄像机视景平面
            if (GeometryUtility.TestPlanesAABB(planes, anObject.collider.bounds))//测试平面(测试一个物体是否在摄像机视角内,无论它是否被渲染)
            {
                anObject.SetActive(true);
                Debug.Log(anObject.name + " has been detected!");
            }
            else
            {
                anObject.SetActive(false);
                Debug.Log("Nothing has been detected");
            }  
    }
 
}

 

Copyright © 2024 路虽弥,不行不至
Powered by .NET 9.0 on Kubernetes