1.原始的shader
Shader "Custom/MVP"
{
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float4x4 mvp;
v2f vert (appdata v)
{
v2f o;
//传入片段着色器的坐标值
o.vertex = mul(mvp,v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(1,0,0,1);
}
ENDCG
}
}
}
效果:
1.自定义的mvp矩阵,从脚本中传入
shader代码
Shader "Custom/MVP"
{
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float4x4 mvp;
v2f vert (appdata v)
{
v2f o;
//传入片段着色器的坐标值
o.vertex = mul(mvp,v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(1,0,0,1);
}
ENDCG
}
}
}
没传值
用C#代码传值进去,就能正常显示了,但是不推荐这样写。测试矩阵罢了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mvp : MonoBehaviour
{
private MeshRenderer rend;
private Material mat;
void Start()
{
rend = GetComponent<MeshRenderer>();
mat = rend.material;
}
// Update is called once per frame
void Update()
{
Matrix4x4 m4 = Camera.main.projectionMatrix * Camera.main.worldToCameraMatrix * transform.localToWorldMatrix;
mat.SetMatrix("mvp",m4);
}
}