zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ftell
    diff
    继承
    类的组合
    拷贝构造函数
    内存管理
    Hibernate学习-Hibernate查询语言HQL
    JAVA解析JSON数据
    Android异步加载
    Android数据存储-文件操作
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/10632171.html
Copyright © 2011-2022 走看看