unity摄像机投影

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

Unity3D研究院之获取摄像机的视口区域 - 雨松MOMO程序研究院 - 1

 

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

using UnityEngine;
using System.Collections;

public class Demo : MonoBehaviour
{


    private Camera theCamera;

    //距离摄像机8.5米 用黄色表示
    public float upperDistance = 1.0f;
    //距离摄像机30米 用红色表示
    public float lowerDistance = 20.0f;

    private Transform tx;

    private bool isFirst = true;
    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);

        //if (isFirst == false)
        //    return;
        Vector3 cornersFirst = (corners[0] + corners[1])/2;
        Vector3 cornersSecord = (corners[2] + corners[3])/2;
        Vector3 finalCorners = (cornersFirst + cornersSecord) / 2;
        Debug.LogWarning("finalCorners.x = " + finalCorners.x + ",finalCorners.y =" + finalCorners.y + ",finalCorners.z = " + finalCorners.z);

        GameObject obj = GameObject.FindWithTag("BattleBg");
        obj.transform.position = finalCorners;

        GameObject cameraParent = GameObject.Find("SmartCamera");
        if (null == cameraParent)
            return;


        Debug.LogWarning("world , UpperLeft, corners[0].x = " + corners[0].x + ",[0].y = " + corners[0].y + ",[0].z = " + corners[0].z);
        Debug.LogWarning("world , UpperRight, corners[1].x = " + corners[1].x + ",[1].y = " + corners[1].y + ",[1].z = " + corners[1].z);
        Debug.LogWarning("world,  LowerLeft,  corners[2].x = " + corners[2].x + ",[2].y = " + corners[2].y + ",[2].z = " + corners[2].z);
        Debug.LogWarning("world , LowerRight,corners[3].x = " + corners[3].x + ",[3].y = " + corners[3].y + ",[3].z = " + corners[3].z);

        Vector3 screenPoint1 = Camera.main.WorldToScreenPoint(corners[0]);
        Vector3 screenPoint2 = Camera.main.WorldToScreenPoint(corners[1]);
        Vector3 screenPoint3 = Camera.main.WorldToScreenPoint(corners[2]);
        Vector3 screenPoint4 = Camera.main.WorldToScreenPoint(corners[3]);

        Debug.LogWarning("screen , UpperLeft,  screenPoint1.x = " + screenPoint1.x + ",y = " + screenPoint1.y + ",z = " + screenPoint1.z);
        Debug.LogWarning("screen , UpperRight, screenPoint2.x = " + screenPoint2.x + ",y = " + screenPoint2.y + ",z = " + screenPoint2.z);
        Debug.LogWarning("screen,  LowerLeft,  screenPoint3.x = " + screenPoint3.x + ",y = " + screenPoint3.y + ",z = " + screenPoint3.z);
        Debug.LogWarning("screen , LowerRight, screenPoint4.x = " + screenPoint4.x + ",y = " + screenPoint4.y + ",z = " + screenPoint4.z);

        Vector3 meshVec = obj.GetComponent<MeshFilter>().mesh.bounds.size;
        Vector3 meshRender = obj.GetComponent<MeshRenderer>().bounds.size;

        //Old
        Quaternion quaternion = cameraParent.transform.rotation;
        Vector3 euler = quaternion.eulerAngles;
        Vector3 finalVec = new Vector3(90 - euler.x, euler.y - 180, euler.z);
        quaternion = Quaternion.Euler(finalVec);
        obj.transform.rotation = quaternion;

        //New error
        //obj.transform.LookAt(cameraParent.transform);
        //Quaternion quaternion = obj.transform.rotation;
        //Vector3 euler = quaternion.eulerAngles;
        //Vector3 finalVec = new Vector3(euler.x , euler.y , euler.z);
        //quaternion = Quaternion.Euler(finalVec);
        //obj.transform.rotation = quaternion;

        //TODO
        //obj.transform.localScale = new Vector3(2.81f, 1f, 1.27f);
        float finalHeight = 1.27f;
        float finalWidth = theCamera.aspect * finalHeight;
        
        obj.transform.localScale = new Vector3(finalWidth, 1f, finalHeight);
        //isFirst =false;
    }


    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的坐标点,有了坐标信息那么想干什么都好干了,呵呵。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值