行矩阵列矩阵D3D&GL&U3D

    void Start () {
        //矩阵函数原型:Matrix4x4(Vector4 colum0, Vector4 colum1, Vector4 colum2, Vector4 colum3),这说明U3D使用列矩阵,同opengl
        //因此矩阵是左乘的,DX是行矩阵,矩阵是右乘的。
        //在DX中WVP矩阵组合为 W*V*P, 而GL中MVP矩阵组合为 P*V*M
        Matrix4x4 tm = new Matrix4x4(new Vector4(1, 0, 0, 3), new Vector4(0,1,0,0),new Vector4(0,0,1,0),new Vector4(0,0,0,1));
        Vector4 pt4 = new Vector4(0, 0, 0, 1);
        Vector3 pt3 = new Vector3(0, 0, 0);

        //var pt4t = pt4*tm; //编译出错,没有定义这种乘法,不支持右乘!
        var pt4t = tm*pt4; 
        var pt4tt = tm.transpose*pt4; //矩阵转置-变为行矩阵
        var pt3t = tm*pt3;
        var pt3tt = tm.transpose*pt3; //实际是 tm.transpose * (0,0,0,0),U3D将vector3扩展为w为0,而不是像DX那样将w扩展为1

        //总结:在U3D中,
        //1,矩阵是列矩阵,必须左乘,U3D未定义矩阵右乘向量这种操作
        //2,vector3(x,y,z)与矩阵相乘时,vector3被扩展为vector4(x,y,z,0)
        Debug.Log(pt4t.x + "," + pt4t.y+","+ pt4t.z); //0,0,0
        Debug.Log(pt4tt.x + "," + pt4tt.y+","+ pt4tt.z); //3,0,0
        Debug.Log(pt3t.x + "," + pt3t.y+","+ pt3t.z); //0,0,0
        Debug.Log(pt3tt.x + "," + pt3tt.y + "," + pt3tt.z);//0,0,0

        //3,鉴于2的情况,U3D中操作顶点变换最好使用函数,如下
        var tx = tm.transpose.MultiplyPoint(pt3t);
        Debug.Log(tx.x + "," + tx.y + "," + tx.z); //3,0,0

    }

 

posted @ 2018-08-13 07:50  时空观察者9号  阅读(274)  评论(0编辑  收藏  举报