Unity---判断某个点是否在摄像机的视景范围内

 1 using UnityEngine;
 2 
 3 [RequireComponent(typeof(Camera))]
 4 public class VisualDetectionCamera : MonoBehaviour
 5 {
 6     Camera m_Camera;
 7 
 8     Transform m_Trans;
 9     public Transform Trans
10     {
11         get
12         {
13             if (null == m_Trans)
14                 m_Trans = transform;
15             return m_Trans;
16         }
17     }
18 
19     void Awake()
20     {
21         m_Camera = GetComponent<Camera>();
22         InitializeCamera();
23     }
24 
25     void InitializeCamera()
26     {
27         if (null != m_Camera)
28         {
29 
30         }
31     }
32 
33     //计算摄像机的视景并返回它的六个面
34     Plane[] GetFrustumPlanes()
35     {
36         return GeometryUtility.CalculateFrustumPlanes(m_Camera);
37     }
38 
39     public bool IsPointInFrustum(Vector3 point)
40     {
41         Plane[] planes = GetFrustumPlanes();
42 
43         for (int i = 0, iMax = planes.Length; i < iMax; ++i)
44         {
45             //判断一个点是否在平面的正方向上
46             if (!planes[i].GetSide(point))
47                 return false;
48         }
49 
50         return true;
51     }
52 }

测试工程:

链接:https://pan.baidu.com/s/13LDXG2Uk-nI80sXKguQBBw
提取码:l9oa

posted @ 2019-03-31 17:23  小·糊涂仙  阅读(2191)  评论(0编辑  收藏  举报