- Home /
Sorting semi-transparent pixels
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
}
}
}
I found another thread explaining that for the render queue in the shader to work I need to set the material render queue to -1, I tried that and managed to get the render queue in the shader to actually work. Which helps debugging.
I've successfully removed the artifacts seen in the image above by setting the second pass in the shader to Transparent+1, but when I tweak the render queue it always produces artifacts somewhere else.
And it's very inconsistent, one same mesh may have proper semi-transparent pixels on one side but no semi-transparent pixels on the other side.
Answer by Oruji · Dec 30, 2015 at 08:28 AM
I have successfully eliminated all artifacts by using custom rendering queue in the material slot. It works but I'm interested in a better/automatic solution if anyone has any.
To adjust the custom rendering queue manually:
Set the inspector to debug, scroll down to the material component and find the "custom rendering queue" variable.
For my transparent passes I'm using values between 2000 and 3000 to manually sort the pixels.