记录一下特效裁剪Shader的几句关键代码,以防之后忘记。
Shader "Indra/Lowpoly Additive Clip"{
Properties{
_TintColor("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_MainTex("Particle Texture", 2D) = "white" {}
_Wrap("明暗对比", range(-1, 1)) = 0
//-------------------add----------------------
_MinX("Min X", Float) = -1
_MaxX("Max X", Float) = 1
_MinY("Min Y", Float) = -1
_MaxY("Max Y", Float) = 1
}
Category{
Tags { "Queue" = "Transparent+10" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" }
Blend SrcAlpha One
ColorMask RGB
Cull Off Lighting Off ZWrite Off
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile_instancing
#include "UnityCG.cginc"
sampler2D _MainTex;
//-------------------add----------------------
float _MinX;
float _MaxX;
float _MinY;
float _MaxY;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float3 normal : NORMAL;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
UNITY_FOG_COORDS(1)
UNITY_VERTEX_OUTPUT_STEREO
float3 worldNormal : TEXCOORD2;
float4 worldPos : TEXCOORD3;
//-------------------add----------------------
float3 vpos : TEXCOORD4;
};
float4 _MainTex_ST;
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(fixed4, _TintColor)
UNITY_DEFINE_INSTANCED_PROP(fixed, _Wrap)
UNITY_INSTANCING_BUFFER_END(Props)
v2f vert(appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color * UNITY_ACCESS_INSTANCED_PROP(Props, _TintColor);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
o.vpos = ComputeScreenPos(o.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos.xyz = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldPos.w = UNITY_ACCESS_INSTANCED_PROP(Props, _Wrap);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed NDotL = saturate(dot(worldNormal, worldLightDir) * 0.5 + 0.5);
fixed4 col = 2.0f * i.color * tex2D(_MainTex, i.texcoord);
col.rgb = col.rgb * lerp(i.worldPos.w, 1, NDotL);
UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0));
//-------------------add----------------------
if (i.vpos.x < _MinX || i.vpos.x > _MaxX || i.vpos.y < _MinY || i.vpos.y > _MaxY)
col.a = 0;
return col;
}
ENDCG
}
}
}
}
其中----add-----注释下的为关键代码。