pass已经是循环,不用再写for
Shader "ShaderLab学习/Shader04"
{
Properties
{
_Color("颜色",color) = (1.0,1.0,1.0,1.0)
_Scale("缩放值",Range(0,10)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
fixed4 _Color;
half _Scale;
struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
};
struct v2f
{
float4 pos:SV_POSITION;
fixed3 color:COLOR0;
};
v2f vert (a2v v)
{
v2f o; //实例化一个结构体
float4 vp; //定义一个float
if(v.normal.z > 0.5) //如果顶点法线大于0.5
{
vp = v.vertex + float4(0,1.0,0,0.0)*_Scale; //顶点Y值向上移动
}else
{
vp = v.vertex + float4(v.normal,1.0)*_Scale; //顶点三维都移动,体现为整体变大
}
o.pos = UnityObjectToClipPos(vp); //将vp转为投影坐标
o.color = v.normal * 0.5 + fixed3(0.5,0.5,0.5); //将各个法线顶点数值赋值给颜色,体现为颜色都不一样
return o;
}
//指定的片元着色器
//SV_Target 表示最后输出的颜色
fixed4 frag (v2f i) : SV_Target
{
fixed3 c = i.color * _Color.rgb; //各个顶点的颜色乘你在外部手动调节的颜色
return fixed4(c,1.0); //输出最后的颜色
}
ENDCG
}
}
}
最后效果