Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question how to prevent billboarded sprite from clipping into geometry?

Discussion in '2D' started by pertholdth, May 22, 2024.

  1. pertholdth

    pertholdth

    Joined:
    Feb 28, 2024
    Posts:
    227
    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:

    Untitled.png
     
  2. Dennisleon

    Dennisleon

    Joined:
    May 22, 2024
    Posts:
    1
    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:

    Code (CSharp):
    1. fixed4 frag(v2f i) : SV_Target {
    2.     // sample the texture
    3.     fixed4 col = tex2D(_MainTex, i.uv);
    4.     // apply fog
    5.     UNITY_APPLY_FOG(i.fogCoord, col);
    6.     // apply depth bias
    7.     float depthBias = 0.00001; // Adjust this value as needed
    8.     i.pos.z += depthBias;
    9.     return col;
    10. }
    I hope the information may helps you.
     
  3. pertholdth

    pertholdth

    Joined:
    Feb 28, 2024
    Posts:
    227
    any ideas?

    Untitled.png
     
  4. pertholdth

    pertholdth

    Joined:
    Feb 28, 2024
    Posts:
    227
    any tips?
     
  5. pioj

    pioj

    Joined:
    Nov 5, 2012
    Posts:
    36
    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:
     
  6. pertholdth

    pertholdth

    Joined:
    Feb 28, 2024
    Posts:
    227
    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