关闭深度写入的透明度混合
关闭深度写入,这样可以把透明物体后面的物体画出来。但是如果自身就有半透明物体缠绕,因为关闭了深度写,所以会出现顺序错乱的问题
Shader "Unlit/AlphaBlend"
{
Properties
{
_Color("Color Tint",Color)=(1,1,1,1)
_MainTex("Main Tex",2D) = "white"{}
_AlphaScale("Alpha Scale",Range(0,1))=1
}
SubShader
{
Tags{"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass
{
Tags{"LightModel"="ForwardBase"}
ZWrite Off //关闭深度写入,这样可以把透明物体后面的物体画出来
Blend SrcAlpha OneMinusSrcAlpha //将透明物体的颜色和和颜色缓冲中的颜色进行混合
CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert
#pragma fragment frag
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _AlphaScale;
struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:SV_POSITION;
float3 worldPos:TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float2 uv:TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
float3 wordNormal = normalize(i.worldNormal);
float3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
fixed4 texColor = tex2D(_MainTex,i.uv);
fixed3 albedo = texColor.rgb*_Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(wordNormal,worldLightDir));
return fixed4(ambient+diffuse,texColor.a*_AlphaScale);
}
ENDCG
}
}
Fallback "Transparent/VertexLit"
}
关闭了深度写入会产生错误的排序情况,就是当自身物体是一个缠绕的半透明物体时,就会有错误的效果,可以通过开启深度写入的方式。