获取相机不同距离的视口矩形

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],Color.red );
        Debug.DrawLine( corners[1],Color.red );
        Debug.DrawLine( corners[3],Color.red );
        Debug.DrawLine( corners[2],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;
    }
}

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...