MVP矩阵顶点变换

Shader "Custom/test1" {

    SubShader {
        pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag 

            #include "unitycg.cginc"

            float4x4 mvp;

            struct v2f{
                float4 pos:POSITION;
            };


            v2f vert(appdata_base v)
            {
                v2f o;
                o.pos = mul(mvp,v.vertex);
                //o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
                return o;
            }

            fixed4 frag():COLOR
            {
                return fixed4(1,1,1,1);
            }
            ENDCG
        }
    }

}
using UnityEngine;
using System.Collections;

public class uniform : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        Matrix4x4 mvp = Camera.main.projectionMatrix*Camera.main.worldToCameraMatrix*transform.localToWorldMatrix;

        GetComponent().material.SetMatrix("mvp",mvp);
    }
}

unity有内置的mvp矩阵 也可以自己去在C#中构造 传入shader中

你可能感兴趣的:(unity3d)