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 mvp = Camera.main.projectionMatrix* Camera.main.worldToCameraMatrix *transform.localToWorldMatrix ;
GetComponent<Renderer>().material.SetMatrix("mvp",mvp);
}
}
shader脚本
Shader "Unlit/V2f"
{
SubShader{
pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4x4 mvp; //加入C# 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); //传递mvp
return o;
}
fixed frag():SV_TARGET
{
return fixed4 (1,1,1,1);
}
ENDCG
}
}
}
效果对比
网友评论