码迷,mamicode.com
首页 > 编程语言 > 详细

Unity API 解析(6)—— Matrix4x4 类

时间:2014-11-09 11:14:39      阅读:825      评论:0      收藏:0      [点我收藏+]

标签:style   io   color   ar   os   使用   sp   for   div   

通常用在如摄像机的非标准投影变换等

 

MultiplyPoint 方法 —— 投影矩阵变换

对点v进行投影矩阵变换

主要用于Camera的投影变换,对于一般物体的矩阵变换用MultiplyPoint3x4方法,不涉及投影变换,计算速度更快

MultiplyPoint3x4 —— 矩阵变换

MultiplyVector 方法 —— 矩阵变换

对方向向量v进行矩阵变换

using UnityEngine;
using System.Collections;

public class MultiplyVector_ts : MonoBehaviour
{
    public Transform tr;
    Matrix4x4 mv0 = Matrix4x4.identity;
    Matrix4x4 mv1 = Matrix4x4.identity;

    void Start()
    {
        //分别设置变换矩阵mv0和mv1的位置变换和角度变换都不为0
        mv0.SetTRS(Vector3.one * 10.0f, Quaternion.Euler(new Vector3(0.0f, 30.0f, 0.0f)), Vector3.one);
        mv1.SetTRS(Vector3.one * 10.0f, Quaternion.Euler(new Vector3(0.0f, 0.6f, 0.0f)), Vector3.one);
    }

    void Update()
    {
        //用tr来定位变换后的向量
        tr.position = mv1.MultiplyVector(tr.position);
    }
    void OnGUI()
    {
        if (GUI.Button(new Rect(10.0f, 10.0f, 120.0f, 45.0f), "方向旋转30度"))
        {
            Vector3 v = mv0.MultiplyVector(new Vector3(10.0f, 0.0f, 0.0f));
            //打印旋转后的向量,其方向发生了旋转
            Debug.Log("变换后向量:"+v);
            //打印旋转后向量的长度,
            //尽管mv0的位置变换不为0,但变换后向量的长度应与变换前相同
            Debug.Log("变换后向量模长:" + v.magnitude);
        }
    }
}

SetTRS 方法 —— 重设Matrx4x4变换矩阵

pos 为位置向量,q为旋转角度,参数s为缩放向量

 

 

using UnityEngine;
using System.Collections;

public class SetTRS_ts : MonoBehaviour
{
    Vector3 v1 = Vector3.one;
    Vector3 v2 = Vector3.zero;

    void Start()
    {
        Matrix4x4 m1 = Matrix4x4.identity;
        //Position 沿y轴增加5个单位,绕y轴旋转45度,缩放2倍
        m1.SetTRS(Vector3.up * 5, Quaternion.Euler(Vector3.up * 45.0f), Vector3.one * 2.0f);
        // 也可以使用如下静态方法设置m1变换
        //m1 = Matrix4x4.TRS(Vector3.up * 5, Quaternion.Euler(Vector3.up * 45.0f), Vector3.one * 2.0f);
        v2 = m1.MultiplyPoint3x4(v1);
        Debug.Log("v1的值o" + v1);
        Debug.Log("v2的值o" + v2);
    }

    void FixedUpdate()
    {
        Debug.DrawLine(Vector3.zero, v1, Color.green);
        Debug.DrawLine(Vector3.zero, v2, Color.red);
    }
}

Ortho —— 创建正交投影矩阵

Perspective —— 创建透视投影矩阵

using UnityEngine;
using System.Collections;

public class OrthoAndPerspective_ts : MonoBehaviour
{
    Matrix4x4 Perspective = Matrix4x4.identity;//透视投影变量
    Matrix4x4 ortho = Matrix4x4.identity;//正交投影变量
    //声明变量,用于记录正交视口的左,右,下,上的值
    float l, r, b, t;
    void Start()
    {
        //设置透视投影矩阵
        Perspective = Matrix4x4.Perspective(65.0f, 1.5f, 0.1f, 500.0f);
        t = 10.0f;
        b = -t;
        //为防止视图变形需要与 Camera.main.aspect 相乘
        l = b * Camera.main.aspect;
        r = t * Camera.main.aspect;
        //设置正交投影矩阵
        ortho = Matrix4x4.Ortho(l, r, b, t, 0.1f, 100.0f);
    }

    void OnGUI()
    {
        // 使用默认正交投影
        if (GUI.Button(new Rect(10.0f, 8.0f, 150.0f, 20.0f), "Reset Ortho"))
        {
            Camera.main.orthographic = true;
            Camera.main.ResetProjectionMatrix();
            Camera.main.orthographicSize = 5.1f;
        }
        // 使用自定义正交投影
        if (GUI.Button(new Rect(10.0f, 38.0f, 150.0f, 20.0f), "use Ortho"))
        {
            ortho = Matrix4x4.Ortho(l, r, b, t, 0.1f, 100.0f);
            Camera.main.orthographic = true;
            Camera.main.ResetProjectionMatrix();
            Camera.main.projectionMatrix = ortho;
            Camera.main.orthographicSize = 5.1f;
        }
        // 使用自定义透视投影
        if (GUI.Button(new Rect(10.0f, 68.0f, 150.0f, 20.0f), "use Perspective"))
        {
            Camera.main.orthographic = false;
            Camera.main.projectionMatrix = Perspective;
        }
        // 恢复系统默认透视投影
        if (GUI.Button(new Rect(10.0f, 98.0f, 150.0f, 20.0f), "Reset  Perspective"))
        {
            Camera.main.orthographic = false;
            Camera.main.ResetProjectionMatrix();
        }
    }
}

Unity API 解析(6)—— Matrix4x4 类

标签:style   io   color   ar   os   使用   sp   for   div   

原文地址:http://www.cnblogs.com/sprint1989/p/4084662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!