Shaders breaking in URP

So I wrote two shaders in builtin pipeline and they were working fine but recently I ported them over to URP and they started to act weird.
Lets call these shaders cube and sphere because that’s what I’m using to debug.
What the shaders do is basically if the sphere is in front of the cube, it will render normally but if its behind the cube, the pixels behind the cube will be discarded.
I’m using the stencil buffer to do this.
Now in URP it works properly for a certain camera distance but if the camera moves away or closer, pixels of sphere in front of the cube are also getting discarded. I’ll provide the shader code and a screenshots of the issue below.


These 2 images are from same scene, only the distance of camera from the objects is changed

Shader "Custom/SphereStencilURP"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geometry" }
        Pass
        {
            // Enable stencil buffer
            Stencil
            {
                Ref 1
                Comp NotEqual
            }

            ZWrite On

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
            };

            float4 _Color;

            Varyings vert (Attributes v)
            {
                Varyings o;
                o.positionHCS = TransformObjectToHClip(v.positionOS);
                return o;
            }

            half4 frag (Varyings i) : SV_Target
            {
                return half4(_Color.rgb, _Color.a);
            }
            ENDHLSL
        }
    }
}
Shader "Custom/CubeStencilURP"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Geometry" }
        Pass
        {
            // Enable stencil buffer
            Stencil
            {
                Ref 1
                Comp Always
                Pass Replace
            }

            // Disable color writes
            ColorMask 0

            // Disable depth writes
            ZWrite Off

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
            };

            Varyings vert (Attributes v)
            {
                Varyings o;
                o.positionHCS = TransformObjectToHClip(v.positionOS);
                return o;
            }

            half4 frag (Varyings i) : SV_Target
            {
                // Output fully transparent color
                return half4(0, 0, 0, 0);
            }
            ENDHLSL
        }
    }
}