码迷,mamicode.com
首页 > 其他好文 > 详细

Frustum Testing -- Bounds/Point/Occlusion

时间:2016-07-13 19:18:25      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

测试一个特定的渲染物体是否在一个特定的相机视锥中:

1  //returns true if renderer is within frustum
2     public static bool IsRedererInFrustum(Renderer renderer,Camera cam)
3     {
4         Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
5         return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
6     }

GeometryUtility.CalculateFrustumPlanes 得到的是相机视锥的六个平面(上下、左右以及前后) 我将其各自的法线画出来了:

技术分享

不难理解这里这六个平面  

经过测试 渲染的物体 无论是部分还是全部在视锥中,该结果均为true

 

测试某一特定的点是否在相机视锥中:

 1 //returns true is the point in the frustum
 2     public static bool IsPointInFrustum(Vector3 point,Camera cam,out Vector3 viewPortLocation)
 3     {
 4         Bounds B = new Bounds();
 5         B.center = point;
 6         B.size = Vector3.zero;
 7 
 8         //construct frustum planes from camera
 9         Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
10 
11         bool isVisible = GeometryUtility.TestPlanesAABB(planes, B);
12 
13         viewPortLocation = Vector3.zero;
14 
15         if (isVisible)
16         {
17             viewPortLocation = cam.WorldToViewportPoint(point);
18         }
19         return isVisible;
20     }

可以考虑点为上一种渲染物体的特殊情况,其边界大小为0 

 

考虑物体在相机之中,并且测试没有occlusion时 :

 1  public static bool IsVisible(Renderer renderer,Camera cam)
 2     {
 3 
 4         //whether is in  camera frustum 
 5         if (IsRedererInFrustum(renderer, cam))
 6             return !Physics.Linecast(renderer.transform.position, cam.transform.position);      //whether there‘s no objects between the two objs..
 7 
 8 
 9         return false;
10     }

判断条件除了物体在相机视锥中,并且与相机之间有无其他物体挡住

通过二者之间的物理射线检测来实现

 

//适用: 第三人称角色扮演   检测控制的角色是否到被场景遮挡的地方  

 

//--另行补充--

 

Frustum Testing -- Bounds/Point/Occlusion

标签:

原文地址:http://www.cnblogs.com/coolsiyi/p/5667081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!