how to prevent billboarded sprite from clipping into geometry?

im billboarding a sprite and due to the rotation it is clipping into a mesh and occluding part of the sprite

is it normal for this to occur or is there a standard method to prevent this from happening?

here is the issue:

Hello,

It’s quite common for billboarded sprites to clip into geometry, especially when they’re close to other objects and the camera angle changes. However, there are several methods you can try to prevent this from happening:

Adjust the Sprite’s Pivot: Instead of placing the sprite’s pivot on the ground, position it at the center of the space where the sprite should be standing. This can help prevent the sprite from clipping into geometry it’s in front of.

Sorting Order: You can adjust the sorting order of the sprite based on its z-coordinate. This can help ensure that the sprite renders in the correct order relative to other objects.

Shader Adjustments: Modify the shader to output the z-value as if the sprite is a vertically aligned sprite, while maintaining the appearance of a screen space sprite. This advanced shader-only option can help with the clipping issue.

Depth Buffer Bias: Apply a small bias to the value written to the depth buffer using the SV_DEPTH semantic in your pixel shader output. This can help prevent clipping, but it’s more of a workaround than a proper solution.

Here’s a snippet of shader code that might help with the depth buffer bias approach:

fixed4 frag(v2f i) : SV_Target {
    // sample the texture
    fixed4 col = tex2D(_MainTex, i.uv);
    // apply fog
    UNITY_APPLY_FOG(i.fogCoord, col);
    // apply depth bias
    float depthBias = 0.00001; // Adjust this value as needed
    i.pos.z += depthBias;
    return col;
}

I hope the information may helps you.

any ideas?

any tips?

FPS did something with the weapons to prevent them clipping into the level walls. They actually render weapons into a separate layer and then use 2 cameras with different masks and flags each one, so the last camera renders only the weapons to keep them at the front of everything else.

This tutorial I saw years ago covers a similar approach:

yeah but I need the billboarded sprites to render behind the mesh if i am passing behind a mountain for example, it still needs to sort correctly