Shader 'Trailing/Duplication' Visual Effect Bug

I’m working on a custom shader that acts as a window, where it hides everything behind it leaving only the skybox. I’m using the BuiltIn pipeline

Here’s the shader:

Shader "Custom/Occlusion"
{
    SubShader
    {
        Tags
        {
            "RenderType" = "Opaque"
            "Queue" = "Geometry-1"
        }

        Pass
        {
            ColorMask 0
            ZWrite on
            ZTest always


            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }

            half4 frag(v2f i) : COLOR
            {
                return half4(1,1,0,1);
            }
            ENDCG
        }
    }
}

(I’m not too well-versed on shaders, so please let me know if I’m making any mistakes or could write something better. I appreciate it!)

The issue is that it works perfectly in the scene view, but is black in the gameview. In playmode (in gameview), the shader has this weird visual effect where it seems to duplicate the view as the camera moves or when objects move in front of it. See the second and third gifs for reference


Top/Scene View is what it should look like

ezgif.com-video-to-gif-converter (1)
Visual effect happening in gameview

2024-09-2514-33-40-ezgif.com-optimize
It should be showing just the skybox

I don’t understand why it’s working in the scene view but not in the game view.

Let me know if you need any more details, I’d be happy to provide them. Thanks for your time!

the issue with the smearing is a problem with depth clearing. your shader kind of confuses me though, so i can’t tell you exactly why, i would assume it has something to do with the fact that the skybox just draws over everything instead of clearing depth. i would try messing with the queue on the skybox shader as well as the window, but if all else fails you could just write a shader that displays the skybox. they’re very simple, and if you aren’t using reflection probes, you can just read unity_SpecCube0 to get the current skybox. (though it will be static and you have to bake it when changed.)

here’s a link to a similar shader i wrote for fun.

Trying to Implement Multiple skyboxes in my Unity scene - Unity Engine - Unity Discussions

1 Like

Awesome, thanks for your insight!

I’m still very new to creating custom shaders, so this shader was mainly a combination of other ones found online and a lot of trial and error.

I did mess around with the render queues for the window and skybox, but it didn’t seem to fix it unfortunately. Still, I’ll try it again today.

Thanks for sharing that shader, it seems like it might do the job. I’ll test it out in a few hours

I found a solution!

What was happening is that the skybox was getting rendered after the window (like POOKSHANK mentioned). To fix this, I found this thread which had a few possible solutions. I went with the second camera option and it works perfectly now.

To do that,

  • I added a second camera as a child of the main one
  • lowered the camera’s depth parameter to a value lower than the main camera (-2 for second camera, -1 for main)
  • set its culling mask to Nothing
  • On the main camera, set the Clear Flags to ‘Depth Only’

Both cameras are set to ‘Forward’ rendering.

I’m sure there are better solutions, but this one works well for my case