Unity SpriteRender的 sheder中 如何解决默认渲染管线半透明渲染后重叠的问题?
目前效果
使用代码
Properties
{
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags
{ "Quene" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "TransparentCutout"
}
Pass{
Tags{ "LightMode" = "ForwardBase" }
ZWrite Off
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert_img
#pragma fragment frag
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _AlphaScale;
fixed4 frag(v2f_img i) : COLOR
{
fixed4 color = tex2D(_MainTex, i.uv);
_Color.a *= color.a;
return (_Color);
}
ENDCG
}
}
想要效果 PS的
要解决默认渲染管线半透明渲染后重叠的问题,可以使用深度测试和深度写入来解决。具体来说,可以在shader中添加以下代码:
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
Pass
{
ZWrite Off // 禁止写入深度缓冲
Blend SrcAlpha OneMinusSrcAlpha // 使用透明混合
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// 省略其他代码
ENDCG
}
这样可以保证在绘制半透明物体时,只有离摄像机更近的像素会被绘制,从而避免重叠问题。同时,由于禁止了深度写入,这样的半透明物体不会影响后面物体的深度信息,从而也避免了深度冲突问题。