Unity中的矩阵Matrix

Transform Matrix4x4

(1) 如下代码块是计算RectTransform组件的四条边的坐标实例:

void Start()
{
    //获取RectTransform的四条边的坐标
    RectTransform tran = GetComponent<RectTransform>();
    //使用父节点的坐标矩阵
    Transform parent = tran.parent;
    Matrix4x4 matrix = parent.worldToLocalMatrix;
    //获得四个顶点的世界坐标
    Vector3[] corners = new Vector3[4];
    tran.GetWorldCorners(corners);
    //世界坐标转本地坐标
    for(int i = 0; i < corners.Length; ++i)
    {
        corners[i] = matrix.MultiplyPoint3x4(corners[i]);
    }
    //四条边本地坐标
    float bottom = corners[0].y;
    float top = corners[2].y;
    float left = corners[0].x;
    float right = corners[2].x;
    Debug.LogError("bottom=" + bottom + ",top=" + top + ",left=" + left + ",right=" + right);
}

(2) 如下代码块是计算Transform组件的Position的实例:

void Start()
{
    //计算世界坐标
    Transform tran = GetComponent<Transform>();
    //使用父节点的坐标矩阵
    Transform parent = tran.parent;
    Matrix4x4 matrix = parent.localToWorldMatrix;
    //计算
    Vector3 pos = matrix.MultiplyPoint3x4(tran.localPosition);
    Debug.LogError("caculate pos=" + pos + ",real pos=" + tran.position);
}

Camera Matrix

Camera.worldToCameraMatrix
Camera.cameraToWorldMatrix
Camera.ResetWorldToCameraMatrix
Camera.projectionMatrix
Camera.ResetProjectionMatrix

GL Matrix

public class ExampleClass : MonoBehaviour
{
    void OnPostRender()
    {
        // Set your materials
        GL.PushMatrix();
        // yourMaterial.SetPass( );
        // Draw your stuff
        GL.PopMatrix();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值