C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MVPTransform : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Matrix4x4 RM =new Matrix4x4();
RM[0,0] =Mathf.Cos (Time.realtimeSinceStartup);
RM[0,2] =Mathf.Sin(Time.realtimeSinceStartup);
RM[1,1] = 1;
RM[2,0] =-Mathf.Sin (Time.realtimeSinceStartup);
RM[2,2] =Mathf.Cos(Time.realtimeSinceStartup);
RM[3,3] = 1;
Matrix4x4 mvp = Camera.main.projectionMatrix* Camera.main.worldToCameraMatrix *transform.localToWorldMatrix*RM ;
GetComponent<Renderer>().material.SetMatrix("mvp",mvp);
}
}
shader
Shader "Unlit/V2f"
{
SubShader{
pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4x4 mvp;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos:POSITION;
};
v2f vert(a2v v)
{
v2f o;
//o.pos = UnityObjectToClipPos(v.vertex); //核心: 矩阵变换 当前模型*视图*投影矩阵
o.pos= mul(mvp,v.vertex);
return o;
}
fixed frag():SV_TARGET
{
return fixed4 (1,1,1,1);
}
ENDCG
}
}
}
网友评论