unity3d 获取相机视口四个角的坐标
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity3d 获取相机视口四个角的坐标相关的知识,希望对你有一定的参考价值。
功能:如标题所示,主要考虑用来做3d Plane的自适应屏幕
/// <summary> /// 获取指定距离下相机视口四个角的坐标 /// </summary> /// <param name="cam"></param> /// <param name="distance">相对于相机的距离</param> /// <returns></returns> public static Vector3[] GetCameraFovPositionByDistance(this Camera cam, float distance) { Vector3[] corners = new Vector3[4]; float halfFOV = (cam.fieldOfView * 0.5f) * Mathf.Deg2Rad; float aspect = cam.aspect; float height = distance * Mathf.Tan(halfFOV); float width = height * aspect; Transform tx = cam.transform; // 左上角 corners[0] = tx.position - (tx.right * width); corners[0] += tx.up * height; corners[0] += tx.forward * distance; // 右上角 corners[1] = tx.position + (tx.right * width); corners[1] += tx.up * height; corners[1] += tx.forward * distance; // 左下角 corners[2] = tx.position - (tx.right * width); corners[2] -= tx.up * height; corners[2] += tx.forward * distance; // 右下角 corners[3] = tx.position + (tx.right * width); corners[3] -= tx.up * height; corners[3] += tx.forward * distance; return corners; }
以上是关于unity3d 获取相机视口四个角的坐标的主要内容,如果未能解决你的问题,请参考以下文章