Unity3D摄像机远、近切面绘制

最近在研究unity3D的Debug.DrawLine, 就利用该函数绘制了U3D上Mian Camera---Perspective模式下的近切面、远切面。思路来源:https://www.xuanyusong.com/archives/3036

接下来回顾一下unity3D camera绘制远、近切面的相关组件:

(1)首先选择摄像机的投影方式,Camera包括透视投影、正交投影两种(如下图),选择透视投影

         1.orthographic正交摄像机:投影线垂直于投影面,也叫平行投影;

         2.perspective透视摄像机:犹如我们的眼睛一样,会根据距离的远近显示 大小;

             Unity3D摄像机远、近切面绘制_第1张图片Unity3D摄像机远、近切面绘制_第2张图片

(2)Field of View   视角(只有在透视投影时才有的特性)

         即: 视角越大,能看到的视野也越大,对应的焦距也越短

(3)Clipping Planes 裁剪平面,Near和Far指定了裁剪的区域范围

       远近裁剪平面和Field Of view决定的平面一起构成一个椎体,被称为相机椎体或视椎体(如下图)

Unity3D摄像机远、近切面绘制_第3张图片

(4)接下来新建脚本Draw.cs将其挂在maincamera上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Draw : MonoBehaviour
{

    private Camera theCamera;

    public float NearDistance = 0.3f;

    public float FarDistance = 1000.0f;
    private Transform tx;

    // Start is called before the first frame update
    void Start()
    {
        if (!theCamera)
        {
            theCamera = Camera.main;
        }
        tx = theCamera.transform;
    }

    // Update is called once per frame
    void Update()
    {
        FindUpperCorners();
        FindLowerCorners();
    }
    void FindUpperCorners()
    {
        NearDistance = theCamera.GetComponent().nearClipPlane;
        Vector3[] corners = GetCorners(NearDistance);

        // 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()
    {
        FarDistance = theCamera.GetComponent().farClipPlane;
        Vector3[] corners = GetCorners(FarDistance);

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

}
(5)运行效果

Unity3D摄像机远、近切面绘制_第4张图片

你可能感兴趣的:(Unity3D)