I know similar questions have been asked before, but I can’t find answers that are applicable to my problem.
I have an unlit cutout shader with a second pass that renders semi-transparent pixels.
It sorts the opaque pixels correctly, but the semi-transparent pixels often end up incorrect.
In this picture; everything is using the cutout shader with a semi-transparent pass.
To the right you can see the semi-transparent pixels rendered correctly.
To the left there is a semi-transparent white plane with the same shader behind the plant, as you can see the second pass (semi-transparent pixels) are rendered behind the white semi-transparent plane even though the plane is behind the plant.
Any idea how to fix this?
The shader looks like this:
Shader "Unlit/AlphaTestBlend" {
Properties{
_Color("Main Color", Color) = (1, 1, 1, 1)
_Cutoff("Base Alpha cutoff", Range(0,.9)) = .5
_MainTex("MainTex", 2D) = ""
}
SubShader{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Lighting off
ZWrite on
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Cutoff;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 _Color;
half4 frag(v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.texcoord);
clip(col.a - _Cutoff);
return col;
}
ENDCG
}
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
ZWrite off
Lighting off
// Set up alpha blending
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Cutoff;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
half4 frag(v2f i) : SV_Target
{
half4 col = tex2D(_MainTex, i.texcoord);
clip(-(col.a - _Cutoff));
return col;
}
ENDCG
}
}
}