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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM