I have a shader containing 3 passes that must be executed in an exact order. In my example, pass #0 sets the color to red, pass #1 sets it to green, pass #3 sets it to blue. If they run in the right order, the object is blue. (My actual shader uses the stencil buffer which is why the render order matters).
Depending on the camera angle and other objects in the scene, the order that the 3 passes are rendered changes. I can verify this via the Frame Debug tool which shows it alternates between rendering pass #0, then 1, then 2 (blue - correct) or rendering pass #0, then 3, then 2 (green - broken). The result is that the object flickers between blue/green as the camera moves. It’s affected by adding or removing other objects behind the main one, and if I clear everything else from the scene the object is always blue (correct).
Is this a bug in Unity 5.5.2f1?
Is there a way to force shader passes to always run in the order they are written in the .shader file?
Shader "WallThrough/WallThroughEverything" {
SubShader{
// PASS #0 RED
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct VertexIn
{
float4 vertex : POSITION;
};
v2f vert(VertexIn v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR
{
return float4(1,0,0,1);
}
ENDCG
}
// PASS #1 GREEN
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct VertexIn
{
float4 vertex : POSITION;
};
v2f vert(VertexIn v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR
{
return float4(0,1,0,1);
}
ENDCG
}
// PASS #2 BLUE
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct VertexIn
{
float4 vertex : POSITION;
};
v2f vert(VertexIn v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR
{
return float4(0,0,1,1);
}
ENDCG
}
}
}