Example #1
0
 public void SerializeOut_mat4x4(UnityEngine.Matrix4x4 v)
 {
     SerializeOut_vector4(v.GetRow(0));
     SerializeOut_vector4(v.GetRow(1));
     SerializeOut_vector4(v.GetRow(2));
     SerializeOut_vector4(v.GetRow(3));
 }
 public static Matrix4x4 DrawRows( Matrix4x4 matrix )
 {
     matrix.SetRow( 0, EditorGUI.Vector4Field( GetRekt(), "", matrix.GetRow( 0 ) ) );
     matrix.SetRow( 1, EditorGUI.Vector4Field( GetRekt(), "", matrix.GetRow( 1 ) ) );
     matrix.SetRow( 2, EditorGUI.Vector4Field( GetRekt(), "", matrix.GetRow( 2 ) ) );
     matrix.SetRow( 3, EditorGUI.Vector4Field( GetRekt(), "", matrix.GetRow( 3 ) ) );
     return matrix;
 }
Example #3
0
 static void Matrix4x4Field(string label, ref Matrix4x4 mat)
 {
     GUILayout.Label(label);
     mat.SetRow(0, EditorGUILayout.Vector4Field("0", mat.GetRow(0)));
     mat.SetRow(1, EditorGUILayout.Vector4Field("1", mat.GetRow(1)));
     mat.SetRow(2, EditorGUILayout.Vector4Field("2", mat.GetRow(2)));
     mat.SetRow(3, EditorGUILayout.Vector4Field("3", mat.GetRow(3)));
 }
 Vector4 Mul(Matrix4x4 matrix, Vector4 vec4)
 {
     var res = new Vector4();
     res.x = Vector4.Dot(matrix.GetRow(0), vec4);
     res.y = Vector4.Dot(matrix.GetRow(1), vec4);
     res.z = Vector4.Dot(matrix.GetRow(2), vec4);
     res.w = Vector4.Dot(matrix.GetRow(3), vec4);
     return res;
 }
 Vector4 Mul4(Matrix4x4 m, Vector4 v)
 {
     var res = new Vector4();
     res.x = Vector4.Dot(m.GetRow(0), v);
     res.y = Vector4.Dot(m.GetRow(1), v);
     res.z = Vector4.Dot(m.GetRow(2), v);
     res.w = Vector4.Dot(m.GetRow(3), v);
     return res;
 }
    private void ApplyTransformation(out UnityEngine.Matrix4x4 Transformation)
    {
        //Calculating Centroids from both coordinate system

        Accord.Math.Vector3 centroidA = CalculateCentroid(UnitytoAccord(ReadFrameLowerLeft), UnitytoAccord(ReadFrameLowerRight), UnitytoAccord(ReadFrameUpperLeft), UnitytoAccord(ReadFrameUpperRight));
        Accord.Math.Vector3 centroidB = CalculateCentroid(UnitytoAccord(FrameLowerLeft), UnitytoAccord(FrameLowerRight), UnitytoAccord(FrameUpperLeft), UnitytoAccord(FrameUpperRight));

        Matrix3x3 H = CovarianceMatrixStep(UnitytoAccord(ReadFrameLowerLeft) - centroidA, UnitytoAccord(FrameLowerLeft) - centroidB)
                      + CovarianceMatrixStep(UnitytoAccord(ReadFrameLowerRight) - centroidA, UnitytoAccord(FrameLowerRight) - centroidB)
                      + CovarianceMatrixStep(UnitytoAccord(ReadFrameUpperLeft) - centroidA, UnitytoAccord(FrameUpperLeft) - centroidB)
                      + CovarianceMatrixStep(UnitytoAccord(ReadFrameUpperRight) - centroidA, UnitytoAccord(FrameUpperRight) - centroidB);
        Matrix3x3 U;

        Accord.Math.Vector3 E;
        Matrix3x3           V;
        Matrix3x3           R;

        H.SVD(out U, out E, out V);

        R = V * U.Transpose();
        Debug.Log("Row  " + R.GetRow(0));
        Debug.Log("Row  " + R.GetRow(1));
        Debug.Log("Row  " + R.GetRow(2));


        if (R.Determinant < 0)
        {
            V.V02 = (-V.V02);
            V.V12 = (-V.V12);
            V.V22 = (-V.V22);
            R     = V * U.Transpose();
            Debug.LogWarning("Reflection case");
        }
        Accord.Math.Vector3 Translation;
        Translation = (NegativeMatrix(R) * centroidA + centroidB);
        Debug.Log("Translation is" + Translation);
        Transformation = UnityEngine.Matrix4x4.identity;
        Transformation = AccordToUnityMatrix(Transformation, R, Translation);

        Debug.Log("Trans" + Transformation.GetRow(0));
        Debug.Log("Trans" + Transformation.GetRow(1));
        Debug.Log("Trans" + Transformation.GetRow(2));
        Debug.Log("Trans" + Transformation.GetRow(3));

        Transformation.SetTRS(AccordtoUnity(Translation), Quaternion.LookRotation(Transformation.GetColumn(1),
                                                                                  Transformation.GetColumn(2)), UnityEngine.Vector3.one);
    }
 static public int GetRow(IntPtr l)
 {
     try{
         UnityEngine.Matrix4x4 self = (UnityEngine.Matrix4x4)checkSelf(l);
         System.Int32          a1;
         checkType(l, 2, out a1);
         UnityEngine.Vector4 ret = self.GetRow(a1);
         pushValue(l, ret);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
Example #8
0
 static int GetRow(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         UnityEngine.Matrix4x4 obj = (UnityEngine.Matrix4x4)ToLua.CheckObject(L, 1, typeof(UnityEngine.Matrix4x4));
         int arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
         UnityEngine.Vector4 o = obj.GetRow(arg0);
         ToLua.Push(L, o);
         ToLua.SetBack(L, 1, obj);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Example #9
0
    public static Matrix4x4 constructProjectionMatrix3x4(float[] projection, float width, float height, float near, float far)
    {

        float r = width;
        float l = 0.0f;
        float t = height;
        float b = 0.0f;

        Matrix4x4 m = new Matrix4x4();

        m.m00 = projection[0];
        m.m01 = projection[1];
        m.m02 = projection[2];
        m.m03 = projection[3];

        m.m10 = projection[4];
        m.m11 = projection[5];
        m.m12 = projection[6];
        m.m13 = projection[7];

        m.m20 = projection[8];
        m.m21 = projection[9];
        m.m22 = projection[10];
        m.m23 = projection[11];

        Vector4 row2 = m.GetRow(2);
        m.SetRow(3, row2);


        float norm = Mathf.Sqrt(m.m20 * m.m20 + m.m21 * m.m21 + m.m22 * m.m22);

        float add = far * near * norm;




        row2 = row2 * (-far - near);
        m.SetRow(2, row2);
        m.m23 += add;




        Matrix4x4 ortho = new Matrix4x4();
        ortho.m00 = 2.0f / (r - l);
        ortho.m01 = 0.0f;
        ortho.m02 = 0.0f;
        ortho.m03 = (r + l) / (l - r);
        ortho.m10 = 0.0f;
        ortho.m11 = 2.0f / (t - b);
        ortho.m12 = 0.0f;
        ortho.m13 = (t + b) / (b - t);
        ortho.m20 = 0.0f;
        ortho.m21 = 0.0f;
        ortho.m22 = 2.0f / (near - far);
        ortho.m23 = (far + near) / (near - far);
        ortho.m30 = 0.0f;
        ortho.m31 = 0.0f;
        ortho.m32 = 0.0f;
        ortho.m33 = 1.0f;

        Matrix4x4 res = new Matrix4x4();
        res = ortho * m;

        return res;
    }
Example #10
0
    //Convert Matrix4x4 to double16
    public static double[] ConvertMatrix4x4ToDouble16(Matrix4x4 m)
    {
        double[] double16 = new double[16];
        int factor = 0;

        for (int i = 0; i < 4; i++)
        {

            double16[factor+i] = (double)m.GetRow(i).x;
            double16[factor+i+1] = (double)m.GetRow(i).y;
            double16[factor+i+2] = (double)m.GetRow(i).z;
            double16[factor+i+3] = (double)m.GetRow(i).w;

            factor += 3;
        }

        return double16;
    }
Example #11
0
	float Determinant(Matrix4x4 m)
	{
		Vector3 r0 = m.GetRow(0);
		Vector3 r1 = m.GetRow(1);
		Vector3 r2 = m.GetRow(2);

		return -(r0.z * r1.y * r2.x) + (r0.y * r1.z * r2.x) + (r0.z * r1.x * r2.y) - (r0.x * r1.z * r2.y) - (r0.y * r1.x * r2.z) + (r0.x * r1.y * r2.z);
	}
    static bool Matrix4x4_GetRow__Int32(JSVCall vc, int argc)
    {
        int len = argc;

        if (len == 1)
        {
            System.Int32          arg0    = (System.Int32)JSApi.getInt32((int)JSApi.GetType.Arg);
            UnityEngine.Matrix4x4 argThis = (UnityEngine.Matrix4x4)vc.csObj;                JSMgr.datax.setObject((int)JSApi.SetType.Rval, argThis.GetRow(arg0));
            JSMgr.changeJSObj(vc.jsObjID, argThis);
        }

        return(true);
    }
Example #13
0
 Vector4 multiply(Matrix4x4 mat, Vector4 point)
 {
     // Multiplies a 4x4 matrix by a vector4
     Vector4 returnVal = new Vector4();
     Vector4 row0 = mat.GetRow(0); Vector4 row1 = mat.GetRow(1); Vector4 row2 = mat.GetRow(2); Vector4 row3 = mat.GetRow(3);
     returnVal.x = row0.x*point.x + row0.y*point.y + row0.z*point.z + row0.w*point.w;
     returnVal.y = row1.x*point.x + row1.y*point.y + row1.z*point.z + row1.w*point.w;
     returnVal.z = row2.x*point.x + row2.y*point.y + row2.z*point.z + row2.w*point.w;
     returnVal.w = row3.x*point.x + row3.y*point.y + row3.z*point.z + row3.w*point.w;
     return returnVal;
 }
Example #14
0
        /// <summary>
        /// Calculates the max point of the frustum bound.
        /// </summary>
        private static Vector3 GetFrustumMax(Matrix4x4 frustumCorners)
        {
            Vector3 frustumMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            for (int i = 0; i < 4; ++i)
            {
                frustumMax = Vector3.Max(frustumMax, frustumCorners.GetRow(i));
            }

            return frustumMax;
        }
    // gets matrix values as csv line
    public static string GetMatrixAsCsv(ref Matrix4x4 mat)
    {
        // create the output string
        StringBuilder sbBuf = new StringBuilder();
        const char delimiter = ',';

        sbBuf.Append("km").Append(delimiter);

        for(int i = 0; i < 4; i++)
        {
            Vector4 vRow = mat.GetRow(i);

            sbBuf.AppendFormat("{0:F3}", vRow.x).Append(delimiter);
            sbBuf.AppendFormat("{0:F3}", vRow.y).Append(delimiter);
            sbBuf.AppendFormat("{0:F3}", vRow.z).Append(delimiter);
            sbBuf.AppendFormat("{0:F3}", vRow.w).Append(delimiter);
        }

        // remove the last delimiter
        if(sbBuf.Length > 0 && sbBuf[sbBuf.Length - 1] == delimiter)
        {
            sbBuf.Remove(sbBuf.Length - 1, 1);
        }

        return sbBuf.ToString();
    }