(转载)Unity3D研究院之获取摄像机的视口区域
来源:互联网 发布:水溶性颜料淘宝 编辑:程序博客网 时间:2024/05/17 03:00
转载http://www.xuanyusong.com/archives/3036
摄像机分为两种,一种是正交摄像机还有一种是透视摄像机。正交摄像机无论远近它的视口范围永远是固定的,但是透视摄像机是由原点向外扩散性发射,也就是距离越远它的视口区域也就越大。那么我们如何获取距离摄像机任意距离的视口区域呢?如下图所示,分别用红色和黄色两种颜色将计算出来的视口区域标记了出来。
下面上代码,把如下脚本挂在摄像机出直接运行游戏即可看到。
usingUnityEngine;usingSystem.Collections; publicclassCameraView:MonoBehaviour{privateCameratheCamera; //距离摄像机8.5米 用黄色表示publicfloatupperDistance=8.5f;//距离摄像机12米 用红色表示publicfloatlowerDistance=12.0f;privateTransformtx;void Start(){if(!theCamera){theCamera=Camera.main;}tx=theCamera.transform;}void Update(){FindUpperCorners();FindLowerCorners();}void FindUpperCorners(){Vector3[]corners=GetCorners(upperDistance);// for debuggingDebug.DrawLine(corners[0],corners[1],Color.yellow);// UpperLeft -> UpperRightDebug.DrawLine(corners[1],corners[3],Color.yellow);// UpperRight -> LowerRightDebug.DrawLine(corners[3],corners[2],Color.yellow);// LowerRight -> LowerLeftDebug.DrawLine(corners[2],corners[0],Color.yellow);// LowerLeft -> UpperLeft}void FindLowerCorners(){Vector3[]corners=GetCorners(lowerDistance);// for debuggingDebug.DrawLine(corners[0],corners[1],Color.red);Debug.DrawLine(corners[1],corners[3],Color.red);Debug.DrawLine(corners[3],corners[2],Color.red);Debug.DrawLine(corners[2],corners[0],Color.red);}Vector3[]GetCorners( floatdistance ){Vector3[]corners=newVector3[4];floathalfFOV=(theCamera.fieldOfView *0.5f)*Mathf.Deg2Rad;floataspect=theCamera.aspect;floatheight=distance *Mathf.Tan(halfFOV);floatwidth=height *aspect;// UpperLeftcorners[0]=tx.position-(tx.right *width);corners[0]+=tx.up *height;corners[0]+=tx.forward *distance;// UpperRightcorners[1]=tx.position+(tx.right *width);corners[1]+=tx.up *height;corners[1]+=tx.forward *distance;// LowerLeftcorners[2]=tx.position-(tx.right *width);corners[2]-=tx.up *height;corners[2]+=tx.forward *distance;// LowerRightcorners[3]=tx.position+(tx.right *width);corners[3]-=tx.up *height;corners[3]+=tx.forward *distance;returncorners;}}
我们可以根据文章里的算法计算出视口3D的坐标点,有了坐标信息那么想干什么都好干了,呵呵。
0 0
- (转载)Unity3D研究院之获取摄像机的视口区域
- Unity3D研究院之获取摄像机的视口区域
- Unity3D研究院之获取摄像机的视口区域
- (转载)Unity3D研究院之处理摄像机与角色之间被挡时的局部透明效果
- Unity获取摄像机的视口区域
- Unity 获取摄像机的视口区域
- Unity3D研究院之处理摄像机与角色之间被挡时的局部透明效果
- Unity3D for VR 学习(4): 自绘摄像机的视口区域锥体
- Unity3D for VR 学习(4): 自绘摄像机的视口区域锥体
- Unity3D for VR 学习(4): 自绘摄像机的视口区域锥体
- Unity3D研究院之获取某个方法执行的时间
- Camera---获得摄像机的视口区域(转载大神雨松MOMO)
- unity3D之摄像机的调换
- 获取透视摄像机的可视区域
- Unity3D研究院之Assetbundle的原理
- Unity3D研究院之Assetbundle的实战
- Unity3D研究院之Assetbundle的实战
- Unity3D研究院之Assetbundle的使用
- 白盒测试
- iOS音频的后台播放总结(后台网络请求歌曲,Remote控制,锁屏封面,各种打断)
- ssl例4
- Linux驱动之设备模型(9)-platform
- 第12周项目1 一个定义完整的类
- (转载)Unity3D研究院之获取摄像机的视口区域
- Failed to obtain address information for hostname mynode1: Name or service not known
- 如何防止SQLserver服务器被黑?
- memset函数
- 使用log4jdbc记录SQL信息
- LOJ 1422 Halloween Costumes【区间DP】
- swift网络数据请求方法
- ElasticSearch(九)-分布式搜索
- Windows下安装node及基本操作