- Home /
Strange Render Order Issue
I have two vertex/fragment shaders, both unlit, optimized for mobile. One is a transparent additive shader for particles (We'll call him "Particles"), and the other is a simple, bare texture mapping (We'll call him "Background"). "Particles" is set to a higher Queue (I've taken it as high as "Overlay+20000"), and "Background" is set to the Background queue.
The strangeness that I am seeing is that sometimes (not always), "Background" obscures "Particles", even though the geometry drawn by "Particles" is closer to the camera. I have a barebones scene that shows this perfectly - I have a background quad shaded by "Background", a sphere in front that also shaded by "Background" using a different material, and another quad in front of it all shaded by "Particles".
The sphere incorrectly obscures the foremost quad shaded by "Particles", but the background quad does not obscure the foremost quad.
Here is the code for the "Particles" shader:
Shader "Mobile/Particles/Additive (CG)" {
Properties {
_MainTex ("Particle Texture", 2D) = "white" {}
}
Category {
SubShader {
Tags { "Queue"="Overlay+20000" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha One
AlphaTest Greater .01
ColorMask RGB
Cull Off
Lighting Off
ZWrite Off
Fog { Color (0,0,0,0) }
LOD 250
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
float4 _MainTex_ST;
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;
}
fixed4 frag (v2f i) : COLOR
{
return i.color * tex2D(_MainTex, i.texcoord);
}
ENDCG
}
}
}
Fallback "Mobile/Particles/Additive"
}
And here is the "Background" shader:
Shader "Mobile/Unlit/Background (CG)" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "Queue"="Background" "RenderType"="Opaque" }
Pass {
Tags {"Queue"="Background" "RenderType"="Opaque" }
Lighting Off
Fog { Color (0,0,0,0) }
ColorMask RGB
LOD 100
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 col = tex2D(_MainTex, i.texcoord);
return col;
}
ENDCG
}
}
Fallback "Mobile/Background"
}
I feel like I'm either missing something really simple, or I've stumbled across some strange bug. Can anyone lend some guidance here?
I've also attached a package containing a sample scene that demonstrates this anomoly.
I'm still not sure what was going on, but completely re-creating the material for the sphere from scratch seems to solve the issue. I can still reproduce this at will using the attached package, though. It's as though some bad, hidden state is being carried with the offending material.