title: Gradient shader Z fighting in Scene view only
body: <p>I have created the following gradient that takes an Image components source image and apply a two colour gradient to it. Using a toggle it can be switched to using the Source image's alpha for the gradient alpha, or set the alpha per gradient colour.</p>
  
  <pre><code> Properties
  {
      [PerRendererData] _MainTex ("Texture", 2D) = "white" {}
      [Header(Colours)]
      _Color1("Color 1", Color) = (0,0,0,1)
      _Color2("Color 2", Color) = (1,1,1,1)
  
      [Toggle]_UseImageAlpha("Use Image alpha", float) = 0
      [Header(Cull mode)]
      [Enum(UnityEngine.Rendering.CullMode)] _CullMode("Cull mode", float) = 2
      [Header(ZTest)]
      [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", float) = 4
      [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 1
  
  }
  SubShader
  {
      Tags {"Queue" = "Transparent" "RenderType"="Transparent"}
      LOD 100
      Blend SrcAlpha OneMinusSrcAlpha
      ZTest [_ZTest]
      Cull [_CullMode]
      Pass
      {
          CGPROGRAM
          #pragma vertex vert
          #pragma fragment frag
          #pragma multi_compile_local _ UNITY_UI_ALPHACLIP
          #include "UnityCG.cginc"
          struct appdata
          {
              float4 vertex : POSITION;
              float2 uv : TEXCOORD0;
              fixed4 col : COLOR;
          };
          struct v2f
          {
              float2 uv : TEXCOORD0;
              float4 vertex : SV_POSITION;
              fixed4 col : COLOR;
          };
          sampler2D _MainTex;
          float4 _MainTex_ST;
          fixed4 _Color1;
          fixed4 _Color2;
          bool _UseImageAlpha;
          v2f vert (appdata v)
          {
              v2f o;
              o.vertex = UnityObjectToClipPos(v.vertex);
              o.uv = TRANSFORM_TEX(v.uv, _MainTex);
              o.col = v.col;
              return o;
          }
          fixed4 frag (v2f i) : SV_Target
          {
              if (_UseImageAlpha) {
                  _Color1.a = i.col.a;
                  _Color2.a = i.col.a;
              }
  
              fixed4 col = tex2D(_MainTex, i.uv);
              col *= lerp(_Color1, _Color2, i.uv.y);
              col.a = clamp(col.a, 0, 1);
  
  #ifdef UNITY_UI_ALPHACLIP
              clip(col.a - .001);
  #endif
  
              return col;
          }
          ENDCG
      }
  }
  </code></pre>
  
  <p>This shader works fine and shows the gradient as expected, however once I start adding multiple layers of Images (in example a blue square behind it, and a green quare in front of it) it starts having issues with Z fighting <strong>in the scene view only</strong> based on the angle of the scene camera with the object that comes next in the hierachy (in this example the green square). In the Game view and on builds the Z fighting doesn't occur. <a href="https://i.stack.imgur.com/ppZAf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ppZAf.png" alt="Gradient circle z fighting with the green square in front of it"></a></p>
  
  <p>I am using the default <code>LessEqual</code> ZTest option, with back culling and render queue set to 3000 (which is the same as the render queue for the image in front and behind of it). As per <a href="https://docs.unity3d.com/Manual/SL-CullAndDepth.html" rel="nofollow noreferrer">Unity's documentation</a> having it set to <code>LessEqual</code> should make it so Objects in front get drawn on top, and objects behind get hidden:</p>
  
  <blockquote>
    <p>How should depth testing be performed. Default is LEqual (draw objects in from or at the distance as existing objects; hide objects behind them).</p>
  </blockquote>
  
  <p>Setting the ZTest to any of the other options (off, always, greaterEqual etc) doens't yield a better result.</p>
  
  <p>If I set the Render queue higher (3001) it will always draw the gradient on top in the Scene view (no changes in the Game view) whereas setting it to 2999 will still make it z fight with the object in front of it (green square), while making the blue square behind it transparent. <a href="https://i.stack.imgur.com/kjBFf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kjBFf.png" alt="Gradient circle z fighting with the green square in front, hiding the blue square behind with lower render queue"></a></p>
  
  <p>When I only have the green square in front of the gradient it will z fight in some places, while cutting out the green square in other places where the source image doesn't have any pixels. <a href="https://i.stack.imgur.com/LC0Yv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LC0Yv.png" alt="Gradient circle z fighting with the green square in front with equal render queue"></a></p>
  
  <p>Using the alpha of the source image, or using the alpha of the two individual colours does not make a difference either.</p>
  
  <p><a href="https://gyazo.com/7651798922d533c19d2e86018339a890" rel="nofollow noreferrer">(gyazo) Example gif</a> of the fighting changing depending on the camera angle.</p>
  
  <p>What is causing this z fighting, and why does it only occur in the scene view?</p>
  
  <p>Using Unity 2019.3.13f1, same results in 2019.2, 2019.1m 2018.4 LTS, 2017 LTS on Windows. </p>