Unity API Parsing (6)--matrix4x4 class

Source: Internet
Author: User

Typically used in non-standard projection transformations such as cameras

Multiplypoint Method--Projection matrix transformation

Projection matrix transformation on point V

It is mainly used for the projection transformation of camera, multiplypoint3x4 method for matrix transformation of general object, no projection transformation, and faster computation speed.

multiplypoint3x4--Matrix Transformation

Multiplyvector Method--matrix transformation

Matrix transformation of Direction vector v

using unityengine;using System.collections;public class Multiplyvector_ts:monobehaviour    {public Transform tr;    matrix4x4 mv0 = matrix4x4.identity;    matrix4x4 mv1 = matrix4x4.identity; void Start () {//sets the transform matrix Mv0 and mv1 the position transform and the angle transform are not 0 mv0 respectively.        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 to locate the transformed vector tr.position = mv1.    Multiplyvector (tr.position); } void Ongui () {if (GUI. button (new Rect (10.0f, 10.0f, 120.0f, 45.0f), "Rotate direction 30 degrees")) {Vector3 v = mv0.            Multiplyvector (New Vector3 (10.0f, 0.0f, 0.0f));            After printing the rotated vector, its direction has been rotated Debug.Log ("transformed vector:" +v);        Prints the length of the rotated vector,//Although the position transform of the mv0 is not 0, the length of the transformed vector should be the same as before the transformation Debug.Log ("transformed vector modulus length:" + v.magnitude); }    }}

Settrs Method--Resetting the matrx4x4 transformation matrix

POS is the position vector, q is the rotation angle, and the parameter s is the scaling vector

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 adds 5 units along the Y axis, rotates 45 degrees around the y axis, and scales twice times        M1. Settrs (Vector3.up * 5, Quaternion.euler (Vector3.up * 45.0f), Vector3.one * 2.0f);        You can also use the following static method to set the M1 transform        //m1 = Matrix4x4.trs (Vector3.up * 5, Quaternion.euler (Vector3.up * 45.0f), Vector3.one * 2.0f); C10/>V2 = M1. multiplypoint3x4 (v1);        Debug.Log ("v1 value o" + v1);        Debug.Log ("V2 value O" + v2);    }    void Fixedupdate ()    {        debug.drawline (Vector3.zero, v1, color.green);        Debug.drawline (Vector3.zero, V2, color.red);}    }

ortho--creating an orthographic projection matrix

perspective--creating a Perspective projection matrix

Using unityengine;using system.collections;public class orthoandperspective_ts:monobehaviour{Matrix4x4 Perspective = matrix4x4.identity;//Perspective projection variable matrix4x4 ortho = matrix4x4.identity;//orthogonal projection variable//declaration variable, used to record the value of the orthogonal viewport left, right, bottom, float l, R,    B, t;        void Start () {//Set perspective projection Matrix perspective = matrix4x4.perspective (65.0f, 1.5f, 0.1f, 500.0f);        t = 10.0f;        b =-T;        To prevent the view from morphing you need to multiply Camera.main.aspect by L = b * Camera.main.aspect;        r = T * Camera.main.aspect;    Set the orthogonal projection matrix ortho = Matrix4x4.ortho (L, R, B, T, 0.1f, 100.0f); } void Ongui () {//using the default orthographic projection 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; }//Use the custom orthographic projection 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; }//Use a custom perspective to project if (GUI.            button (new Rect (10.0f, 68.0f, 150.0f, 20.0f), "use Perspective")) {Camera.main.orthographic = false;        Camera.main.projectionMatrix = perspective; }//Restore system default Perspective projection if (GUI.             button (new Rect (10.0f, 98.0f, 150.0f, 20.0f), "Reset Perspective")) {Camera.main.orthographic = false;        Camera.main.ResetProjectionMatrix (); }    }}

Unity API Parsing (6)--matrix4x4 class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.