zoukankan      html  css  js  c++  java
  • unity获取相机视窗口大小


    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; } }

      转载自:https://www.xuanyusong.com/archives/3036

  • 相关阅读:
    阿里巴巴JAVA开发手册
    2018开源中国最受欢迎的中国软件
    Java并发编程(多线程)中的相关概念
    关于HashMap自定义key重写hashCode和equals的问题
    MySQL视图 索引 存储过程 触发器 函数
    MySql/Oracle树形结构查询
    码农也来关注下经济问题<美元加息>对我们的影响
    不懂技术却能做到月入20万美元,差距在哪里
    solidity智能合约如何判断mapping值为空
    微信很好用却很少人知道的浮窗功能
  • 原文地址:https://www.cnblogs.com/mttnor/p/9647850.html
Copyright © 2011-2022 走看看