摄像机分为两种,一种是正交摄像机还有一种是透视摄像机。正交摄像机无论远近它的视口范围永远是固定的,但是透视摄像机是由原点向外扩散性发射,也就是距离越远它的视口区域也就越大。那么我们如何获取距离摄像机任意距离的视口区域呢?如下图所示,分别用红色和黄色两种颜色将计算出来的视口区域标记了出来。

下面上代码,把如下脚本挂在摄像机出直接运行游戏即可看到。

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using UnityEngine;
using System.Collections;
 
public class CameraView : MonoBehaviour {
 
 
private Camera theCamera;
 
        //距离摄像机8.5米 用黄色表示
public float upperDistance = 8.5f;
//距离摄像机12米 用红色表示
public float lowerDistance = 12.0f;
 
private Transform tx;
 
 
void  Start (){
if ( !theCamera )
{
theCamera = Camera.main;
}
tx = theCamera.transform;
}
 
 
void  Update (){
FindUpperCorners();
FindLowerCorners();
}
 
 
void  FindUpperCorners (){
Vector3[] corners = GetCorners( upperDistance );
 
// for debugging
Debug.DrawLine( corners[0], corners[1], Color.yellow ); // UpperLeft -> UpperRight
Debug.DrawLine( corners[1], corners[3], Color.yellow ); // UpperRight -> LowerRight
Debug.DrawLine( corners[3], corners[2], Color.yellow ); // LowerRight -> LowerLeft
Debug.DrawLine( corners[2], corners[0], Color.yellow ); // LowerLeft -> UpperLeft
}
 
 
void  FindLowerCorners (){
Vector3[] corners = GetCorners( lowerDistance );
 
// for debugging
Debug.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 (  float distance   ){
Vector3[] corners = new Vector3[ 4 ];
 
float halfFOV = ( theCamera.fieldOfView * 0.5f ) * Mathf.Deg2Rad;
float aspect = theCamera.aspect;
 
float height = distance * Mathf.Tan( halfFOV );
float width = height * aspect;
 
// UpperLeft
corners[ 0 ] = tx.position - ( tx.right * width );
corners[ 0 ] += tx.up * height;
corners[ 0 ] += tx.forward * distance;
 
// UpperRight
corners[ 1 ] = tx.position + ( tx.right * width );
corners[ 1 ] += tx.up * height;
corners[ 1 ] += tx.forward * distance;
 
// LowerLeft
corners[ 2 ] = tx.position - ( tx.right * width );
corners[ 2 ] -= tx.up * height;
corners[ 2 ] += tx.forward * distance;
 
// LowerRight
corners[ 3 ] = tx.position + ( tx.right * width );
corners[ 3 ] -= tx.up * height;
corners[ 3 ] += tx.forward * distance;
 
return corners;
}
}

这个脚本是我在逛国外网站无意间发现的,我这里翻译成了C#语言。http://answers.unity3d.com/questions/509466/scale-box-collider-to-camera-view-1.html?sort=oldest

我们可以根据文章里的算法计算出视口3D的坐标点,有了坐标信息那么想干什么都好干了,呵呵。

Unity3D研究院之获取摄像机的视口区域的更多相关文章

  1. Unity3D for VR 学习(4): 自绘摄像机的视口区域锥体

    在Unity Editor下,当选择Camera组件后,可呈现出Camera视口区域锥体,非常方便.但是当选择其他物体,如Cube后,就无法得知是否在Camera市口区内了,这里我找到了雨松MOMO的 ...

  2. Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条

    Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条 异步任务相信大家应该不会陌生,那么本章内容MOMO将带领大家学习Unity中的一些异步任务.在同步加载游戏场景的时候通常会使用方法 Ap ...

  3. (转)Unity3D研究院之Assetbundle的实战(六十三)

    上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院之Assetbundle的原理(六十一) 本篇文章我们将说说assetbundl ...

  4. Unity3D研究院之Assetbundle的实战(六十三)

    http://www.xuanyusong.com/archives/2405 上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院 ...

  5. js获取网页屏幕可视区域高度

    document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.document ...

  6. Unity3D研究院之Jenkins的使用(七十八)

    长夜漫漫无心睡眠,来一篇嘿嘿.我相信如果已经用Shell脚本完成IOS和Android打包的朋友一定需要Jenkins 怎么才能让策划打包ipa和apk?怎么才能彻底省去程序的时间,只要在同一局域网内 ...

  7. Unity3D研究院之与Android相互传递消息

    原地址:http://www.xuanyusong.com/archives/676 上一篇文章我们学习了Unity向Android发送消息,如果Android又能给Unity回馈消息那么这就玩美了. ...

  8. (转)Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景

    自:http://www.xuanyusong.com/archives/1919 导出Unity场景的所有游戏对象信息,一种是XML一种是JSON.本篇文章我们把游戏场景中游戏对象的.旋转.缩放.平 ...

  9. Unity3D研究院编辑器之脚本获取资源内存和硬盘大小

    内存 使用Profiler可以查看某个资源的内存占用情况,但是必须启动游戏,并且待查看的资源已经载入游戏中.我希望的是不启动游戏,也能看到它的内存好做统计. 硬盘 由于unity中的资源压缩格式记录在 ...

随机推荐

  1. caffe中python接口的使用

    下面是基于我自己的接口,我是用来分类一维数据的,可能不具通用性: (前提,你已经编译了caffe的python的接口) 添加 caffe塻块的搜索路径,当我们import caffe时,可以找到. 对 ...

  2. 转:[置顶] 从头到尾彻底理解KMP(2014年8月22日版)

    [置顶] 从头到尾彻底理解KMP(2014年8月22日版)

  3. stm32cube--IWDG使用

    IWDG使用的是32芯片内部的40k独立晶振,该晶振为rtc和iwdg提供时钟,即使是主时钟坏了也不影响它们. 主要用到三个寄存器, IWDG_KR    键值寄存器 IWDG_PR     预分频寄 ...

  4. 使用spark与ElasticSearch交互

    使用 elasticsearch-hadoop 包,可在 github 中搜索到该项目 项目地址 example import org.elasticsearch.spark._ import org ...

  5. 习题-第1章了解ASP.NET MVC

    一.选择题 1.ASP.NET MVC自2007年首次公布预览以来,作为(    )的替代品,普及度已明显提高,现在很多大型Web应用程序都是使用这一技术构建的. A.ASP    B.ASP.NET ...

  6. linux split (分割文件)命令

    linux split 命令 功能说明:切割文件. 语 法:split [--help][--version][-<行数>][-b <字节>][-C <字节>][- ...

  7. Minimum Path Sum [LeetCode]

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. 张艾迪(创始人):Hello.世界...

    The World No.1 Girl :Eidyzhang The World No.1 Internet Girl :Eidyzhang AOOOiA.global Founder :Eidyzh ...

  9. css\html布局及部分知识小分享~~~

    近期发现和总结的知识跟大侠们分享,请大侠们多多评论指教一二?  HTML 1.(1)body需设置页面默认字体大小 body{font-size:12px;} (2)IE6下png图片划过切换失效,建 ...

  10. linux 的 磁盘操作

    //显示目录和文件 以kb m g为单位 du -ah //总大小 du -sh /etc //查看分区 fdisk -l //对磁盘进行分区 fdisk /dev/sdb //格式化分区 mkfs ...