using a C# class?

Ok, so as obvious as this is going to be… I am a C# and unity noob… learning as I go.

I need to do some matrix opperations on a vertex, and I found this class.
http://www.unifycommunity.com/wiki/index.php?title=Matrix#C.23_-_Matrix.cs

This does everything I need to do!! I just cant figure out how to use it lol
I coppied the code and put it in my project here…
/Assets/Plugins/Matrix.cs

using UnityEngine;

 

using System.Collections;

 

public class Matrix : MonoBehaviour

 {

     public float[] m;

 

    public Matrix()

     {   

         LoadIdentity();

     }   

 

    // Loads this matrix with an identity matrix

 

    public void LoadIdentity()

     {

         m = new float[ 16 ];

 

        for( int x = 0 ; x < 16 ; ++x )

         {

             m[ x ] = 0;

         }   

 

        m [ 0 ] = 1;

         m [ 5 ] = 1;

         m [ 10 ] = 1;

         m [ 15 ] = 1;

     }

 

    // Returns a translation matrix along the XYZ axes

 

    public static Matrix Translate( float _X, float _Y, float _Z )

     {

         Matrix wk = new Matrix();

 

        wk.m [ 12 ] = _X;

         wk.m [ 13 ] = _Y;

         wk.m [ 14 ] = _Z;

 

        return wk;

     }

 

    // Returns a rotation matrix around the X axis

 

    public static Matrix RotateX( float _Degree )

     {

         Matrix wk = new Matrix();

 

        if( _Degree == 0 )

         {

             return wk;

         }

 

        float C = Mathf.Cos( _Degree * Mathf.Deg2Rad );

         float S = Mathf.Sin( _Degree * Mathf.Deg2Rad );

 

        wk.m [ 5 ] = C;

         wk.m [ 6 ] = S;

         wk.m [ 9 ] = -S;

         wk.m [ 10 ] = C;

 

        return wk;

     }

 

    // Returns a rotation matrix around the Y axis

 

    public static Matrix RotateY( float _Degree )

     {

         Matrix wk = new Matrix();

 

        if( _Degree == 0 )

         {

             return wk;

         }

 

        float C = Mathf.Cos( _Degree * Mathf.Deg2Rad );

         float S = Mathf.Sin( _Degree * Mathf.Deg2Rad );

 

        wk.m [ 0 ] = C;

         wk.m [ 2 ] = -S;

         wk.m [ 8 ] = S;

         wk.m [ 10 ] = C;

 

        return wk;

     }

 

    // Returns a rotation matrix around the Z axis

 

    public static Matrix RotateZ( float _Degree )

     {

         Matrix wk = new Matrix();

 

        if( _Degree == 0 )

         {

             return wk;

         }

 

        float C = Mathf.Cos( _Degree * Mathf.Deg2Rad );

         float S = Mathf.Sin( _Degree * Mathf.Deg2Rad );

 

        wk.m [ 0 ] = C;

         wk.m [ 1 ] = S;

         wk.m [ 4 ] = -S;

         wk.m [ 5 ] = C;

 

        return wk;

     }

 

    // Returns a scale matrix uniformly scaled on the XYZ axes

 

    public static Matrix Scale( float _In )

     {

         return Matrix.Scale3D( _In, _In, _In );

     }

 

    // Returns a scale matrix scaled on the XYZ axes

 

    public static Matrix Scale3D( float _X, float _Y, float _Z )

     {

         Matrix wk = new Matrix();

 

        wk.m [ 0 ] = _X;

         wk.m [ 5 ] = _Y;

         wk.m [ 10 ] = _Z;

 

        return wk;

     }

 

    // Transforms a vector with this matrix

 

    public Vector3 TransformVector( Vector3 _V )

     {

         Vector3 vtx = new Vector3(0,0,0);

 

        vtx.x = ( _V.x * m [ 0 ] ) + ( _V.y * m [ 4 ] ) + ( _V.z * m [ 8 ] ) + m [ 12 ];

         vtx.y = ( _V.x * m [ 1 ] ) + ( _V.y * m [ 5 ] ) + ( _V.z * m [ 9 ] ) + m [ 13 ];

         vtx.z = ( _V.x * m [ 2 ] ) + ( _V.y * m [ 6 ] ) + ( _V.z * m [ 10 ] ) + m [ 14 ];

 

        return vtx;

     }

 

    // Overloaded operators

 

    public static Matrix operator *( Matrix _A, Matrix _B )

     {

         Matrix wk = new Matrix();

 

        wk.m [ 0 ] = _A.m [ 0 ] * _B.m [ 0 ] + _A.m [ 4 ] * _B.m [ 1 ] + _A.m [ 8 ] * _B.m [ 2 ] + _A.m [ 12 ] * _B.m [ 3 ];

         wk.m [ 4 ] = _A.m [ 0 ] * _B.m [ 4 ] + _A.m [ 4 ] * _B.m [ 5 ] + _A.m [ 8 ] * _B.m [ 6 ] + _A.m [ 12 ] * _B.m [ 7 ];

         wk.m [ 8 ] = _A.m [ 0 ] * _B.m [ 8 ] + _A.m [ 4 ] * _B.m [ 9 ] + _A.m [ 8 ] * _B.m [ 10 ] + _A.m [ 12 ] * _B.m [ 11 ];

         wk.m [ 12 ] = _A.m [ 0 ] * _B.m [ 12 ] + _A.m [ 4 ] * _B.m [ 13 ] + _A.m [ 8 ] * _B.m [ 14 ] + _A.m [ 12 ] * _B.m [ 15 ];

 

        wk.m [ 1 ] = _A.m [ 1 ] * _B.m [ 0 ] + _A.m [ 5 ] * _B.m [ 1 ] + _A.m [ 9 ] * _B.m [ 2 ] + _A.m [ 13 ] * _B.m [ 3 ];

         wk.m [ 5 ] = _A.m [ 1 ] * _B.m [ 4 ] + _A.m [ 5 ] * _B.m [ 5 ] + _A.m [ 9 ] * _B.m [ 6 ] + _A.m [ 13 ] * _B.m [ 7 ];

         wk.m [ 9 ] = _A.m [ 1 ] * _B.m [ 8 ] + _A.m [ 5 ] * _B.m [ 9 ] + _A.m [ 9 ] * _B.m [ 10 ] + _A.m [ 13 ] * _B.m [ 11 ];

         wk.m [ 13 ] = _A.m [ 1 ] * _B.m [ 12 ] + _A.m [ 5 ] * _B.m [ 13 ] + _A.m [ 9 ] * _B.m [ 14 ] + _A.m [ 13 ] * _B.m [ 15 ];

 

        wk.m [ 2 ] = _A.m [ 2 ] * _B.m [ 0 ] + _A.m [ 6 ] * _B.m [ 1 ] + _A.m [ 10 ] * _B.m [ 2 ] + _A.m [ 14 ] * _B.m [ 3 ];

         wk.m [ 6 ] = _A.m [ 2 ] * _B.m [ 4 ] + _A.m [ 6 ] * _B.m [ 5 ] + _A.m [ 10 ] * _B.m [ 6 ] + _A.m [ 14 ] * _B.m [ 7 ];

         wk.m [ 10 ] = _A.m [ 2 ] * _B.m [ 8 ] + _A.m [ 6 ] * _B.m [ 9 ] + _A.m [ 10 ] * _B.m [ 10 ] + _A.m [ 14 ] * _B.m [ 11 ];

         wk.m [ 14 ] = _A.m [ 2 ] * _B.m [ 12 ] + _A.m [ 6 ] * _B.m [ 13 ] + _A.m [ 10 ] * _B.m [ 14 ] + _A.m [ 14 ] * _B.m [ 15 ];

 

        wk.m [ 3 ] = _A.m [ 3 ] * _B.m [ 0 ] + _A.m [ 7 ] * _B.m [ 1 ] + _A.m [ 11 ] * _B.m [ 2 ] + _A.m [ 15 ] * _B.m [ 3 ];

         wk.m [ 7 ] = _A.m [ 3 ] * _B.m [ 4 ] + _A.m [ 7 ] * _B.m [ 5 ] + _A.m [ 11 ] * _B.m [ 6 ] + _A.m [ 15 ] * _B.m [ 7 ];

         wk.m [ 11 ] = _A.m [ 3 ] * _B.m [ 8 ] + _A.m [ 7 ] * _B.m [ 9 ] + _A.m [ 11 ] * _B.m [ 10 ] + _A.m [ 15 ] * _B.m [ 11 ];

         wk.m [ 15 ] = _A.m [ 3 ] * _B.m [ 12 ] + _A.m [ 7 ] * _B.m [ 13 ] + _A.m [ 11 ] * _B.m [ 14 ] + _A.m [ 15 ] * _B.m [ 15 ];

 

        return wk;

     }

 }

I then make a script and attach it to my object testMatrix.cs

using UnityEngine;
using System.Collections;

public class testMatrix : MonoBehaviour {

	Matrix mat = new Matrix();

	void Start () {
	
	}

	void Update () {

	}
}

the error I get is this

I think I am just not putting the Matrix.cs in the right place?
Any help would be appreciated… thanks!

That’s very odd, why would they make a math object out of a Unity Object.

Either way, the whole thing need not be a Monobehaviour.

using UnityEngine;

using System.Collections;

public class Matrix
{
    public float[] m;

    public Matrix()
    {   
        LoadIdentity();
    }   

    // Loads this matrix with an identity matrix

    public void LoadIdentity()
    {
        m = new float[ 16 ];

        for( int x = 0 ; x < 16 ; ++x )
        {
            m[ x ] = 0;
        }   

        m [ 0 ] = 1;
        m [ 5 ] = 1;
        m [ 10 ] = 1;
        m [ 15 ] = 1;
    }

    // Returns a translation matrix along the XYZ axes

    public static Matrix Translate( float _X, float _Y, float _Z )
    {
        Matrix wk = new Matrix();

        wk.m [ 12 ] = _X;
        wk.m [ 13 ] = _Y;
        wk.m [ 14 ] = _Z;

        return wk;
    }

    // Returns a rotation matrix around the X axis

    public static Matrix RotateX( float _Degree )
    {
        Matrix wk = new Matrix();

        if( _Degree == 0 )
        {
            return wk;
        }

        float C = Mathf.Cos( _Degree * Mathf.Deg2Rad );
        float S = Mathf.Sin( _Degree * Mathf.Deg2Rad );

        wk.m [ 5 ] = C;
        wk.m [ 6 ] = S;
        wk.m [ 9 ] = -S;
        wk.m [ 10 ] = C;

        return wk;
    }

    // Returns a rotation matrix around the Y axis

    public static Matrix RotateY( float _Degree )
    {
        Matrix wk = new Matrix();

        if( _Degree == 0 )
        {
            return wk;
        }

        float C = Mathf.Cos( _Degree * Mathf.Deg2Rad );
        float S = Mathf.Sin( _Degree * Mathf.Deg2Rad );

        wk.m [ 0 ] = C;
        wk.m [ 2 ] = -S;
        wk.m [ 8 ] = S;
        wk.m [ 10 ] = C;

        return wk;
    }

    // Returns a rotation matrix around the Z axis

    public static Matrix RotateZ( float _Degree )
    {
        Matrix wk = new Matrix();

        if( _Degree == 0 )
        {
            return wk;
        }

        float C = Mathf.Cos( _Degree * Mathf.Deg2Rad );
        float S = Mathf.Sin( _Degree * Mathf.Deg2Rad );

        wk.m [ 0 ] = C;
        wk.m [ 1 ] = S;
        wk.m [ 4 ] = -S;
        wk.m [ 5 ] = C;

        return wk;
    }

    // Returns a scale matrix uniformly scaled on the XYZ axes

    public static Matrix Scale( float _In )
    {
        return Matrix.Scale3D( _In, _In, _In );
    }

    // Returns a scale matrix scaled on the XYZ axes

    public static Matrix Scale3D( float _X, float _Y, float _Z )
    {
        Matrix wk = new Matrix();

        wk.m [ 0 ] = _X;
        wk.m [ 5 ] = _Y;
        wk.m [ 10 ] = _Z;

        return wk;
    }

    // Transforms a vector with this matrix

    public Vector3 TransformVector( Vector3 _V )
    {
        Vector3 vtx = new Vector3(0,0,0);

        vtx.x = ( _V.x * m [ 0 ] ) + ( _V.y * m [ 4 ] ) + ( _V.z * m [ 8 ] ) + m [ 12 ];
        vtx.y = ( _V.x * m [ 1 ] ) + ( _V.y * m [ 5 ] ) + ( _V.z * m [ 9 ] ) + m [ 13 ];
        vtx.z = ( _V.x * m [ 2 ] ) + ( _V.y * m [ 6 ] ) + ( _V.z * m [ 10 ] ) + m [ 14 ];

        return vtx;
    }

    // Overloaded operators

    public static Matrix operator *( Matrix _A, Matrix _B )
    {
        Matrix wk = new Matrix();

        wk.m [ 0 ] = _A.m [ 0 ] * _B.m [ 0 ] + _A.m [ 4 ] * _B.m [ 1 ] + _A.m [ 8 ] * _B.m [ 2 ] + _A.m [ 12 ] * _B.m [ 3 ];
        wk.m [ 4 ] = _A.m [ 0 ] * _B.m [ 4 ] + _A.m [ 4 ] * _B.m [ 5 ] + _A.m [ 8 ] * _B.m [ 6 ] + _A.m [ 12 ] * _B.m [ 7 ];
        wk.m [ 8 ] = _A.m [ 0 ] * _B.m [ 8 ] + _A.m [ 4 ] * _B.m [ 9 ] + _A.m [ 8 ] * _B.m [ 10 ] + _A.m [ 12 ] * _B.m [ 11 ];
        wk.m [ 12 ] = _A.m [ 0 ] * _B.m [ 12 ] + _A.m [ 4 ] * _B.m [ 13 ] + _A.m [ 8 ] * _B.m [ 14 ] + _A.m [ 12 ] * _B.m [ 15 ];

        wk.m [ 1 ] = _A.m [ 1 ] * _B.m [ 0 ] + _A.m [ 5 ] * _B.m [ 1 ] + _A.m [ 9 ] * _B.m [ 2 ] + _A.m [ 13 ] * _B.m [ 3 ];
        wk.m [ 5 ] = _A.m [ 1 ] * _B.m [ 4 ] + _A.m [ 5 ] * _B.m [ 5 ] + _A.m [ 9 ] * _B.m [ 6 ] + _A.m [ 13 ] * _B.m [ 7 ];
        wk.m [ 9 ] = _A.m [ 1 ] * _B.m [ 8 ] + _A.m [ 5 ] * _B.m [ 9 ] + _A.m [ 9 ] * _B.m [ 10 ] + _A.m [ 13 ] * _B.m [ 11 ];
        wk.m [ 13 ] = _A.m [ 1 ] * _B.m [ 12 ] + _A.m [ 5 ] * _B.m [ 13 ] + _A.m [ 9 ] * _B.m [ 14 ] + _A.m [ 13 ] * _B.m [ 15 ];

        wk.m [ 2 ] = _A.m [ 2 ] * _B.m [ 0 ] + _A.m [ 6 ] * _B.m [ 1 ] + _A.m [ 10 ] * _B.m [ 2 ] + _A.m [ 14 ] * _B.m [ 3 ];
        wk.m [ 6 ] = _A.m [ 2 ] * _B.m [ 4 ] + _A.m [ 6 ] * _B.m [ 5 ] + _A.m [ 10 ] * _B.m [ 6 ] + _A.m [ 14 ] * _B.m [ 7 ];
        wk.m [ 10 ] = _A.m [ 2 ] * _B.m [ 8 ] + _A.m [ 6 ] * _B.m [ 9 ] + _A.m [ 10 ] * _B.m [ 10 ] + _A.m [ 14 ] * _B.m [ 11 ];
        wk.m [ 14 ] = _A.m [ 2 ] * _B.m [ 12 ] + _A.m [ 6 ] * _B.m [ 13 ] + _A.m [ 10 ] * _B.m [ 14 ] + _A.m [ 14 ] * _B.m [ 15 ];

        wk.m [ 3 ] = _A.m [ 3 ] * _B.m [ 0 ] + _A.m [ 7 ] * _B.m [ 1 ] + _A.m [ 11 ] * _B.m [ 2 ] + _A.m [ 15 ] * _B.m [ 3 ];
        wk.m [ 7 ] = _A.m [ 3 ] * _B.m [ 4 ] + _A.m [ 7 ] * _B.m [ 5 ] + _A.m [ 11 ] * _B.m [ 6 ] + _A.m [ 15 ] * _B.m [ 7 ];
        wk.m [ 11 ] = _A.m [ 3 ] * _B.m [ 8 ] + _A.m [ 7 ] * _B.m [ 9 ] + _A.m [ 11 ] * _B.m [ 10 ] + _A.m [ 15 ] * _B.m [ 11 ];
        wk.m [ 15 ] = _A.m [ 3 ] * _B.m [ 12 ] + _A.m [ 7 ] * _B.m [ 13 ] + _A.m [ 11 ] * _B.m [ 14 ] + _A.m [ 15 ] * _B.m [ 15 ];

        return wk;
    }
}

And that works just fine lol
There is part of the problem of being a noob with this stuff… you grab stuff from the net thinking that it already works and if it doesnt you assume you did something wrong :wink:

Sadly, I only took the “: MonoBehaviour” off of the entire script… lol

use Matrix4x4. You shouldn’t even use class or arrays for data types ( and certainly not a mono behaviour lol ).

Pretty much everything in that class is already available. and whats not is available in Transform.

I do think the Matrix4x4 needs more stuff like a Rotation static but you can use the array operator to do everything they are doing, though it depends on what coordinate system their using ( it would be odd if they werent using the same as unity to begin with )

If you need just a rotation matrix you can use TRS:

public static class Matrix4x4Util {
    public static Matrix4x4 Rotation(Quaternion quat) {
        return Matrix4x4.TRS(Vector3.zero, quat, Vector3.one); 
    }
    public static Matrix4x4 Rotation(Vector3 euler) { return Rotation( Quaternion.Euler( euler ) ); }
    public static Matrix4x4 Rotation(float x, float y, float z ) { return Rotation( new Vector3( x,y,z)); }
    public static Matrix4x4 RotationX(float degrees) { return Rotation(degrees,0,0); }
    public static Matrix4x4 RotationY(float degrees) { return Rotation(0,degrees,0); }
    public static Matrix4x4 RotationZ(float degrees) { return Rotation(0,0,degrees); }
    public static Matrix4x4 Translation(Vector3 vec) { return Matrix4x4.TRS( vec, Quaternion.identity, Vector3.one ); }
    public static Matrix4x4 Translation( float x, float y, float z ) { return Translation( new Vector3( x, y, z ) ); }
    public static Matrix4x4 RotateRHS( this Matrix4x4 mat, Vector3 euler ) { return mat * Rotation(euler); }
    public static Matrix4x4 RotateRHS( this Matrix4x4 mat, Quaternion quat ) { return mat * Rotation(quat); }
    public static Matrix4x4 RotateLHS( this Matrix4x4 mat, Vector3 euler ) { return Rotation(euler) * mat; }
    public static Matrix4x4 RotateLHS( this Matrix4x4 mat, Quaternion quat ) { return Rotation(quat) * mat; }
    public static Matrix4x4 TranslateRHS( this Matrix4x4 mat, Vector3 translate ) { return mat * Translation( translate ); }
    public static Matrix4x4 TranslateLHS( this Matrix4x4 mat, Vector3 translate ) { return Translation( translate ) * mat; } }
}

i didnt go through adding everything, but it should be straight forward on how you would implement wnat you need with TRS. I do think that TRS is overkill though and there should be stuff like Rotation and Translation like there is a Matrix4x4.Scale but whatever. You can count on it optimizing stuff like the overload calling, but not the actual TRS call itself.