• RuntimesUnity
  • Why are spine characters rendered overlaying transparent objects?


The spine character's Z value is less than that of the transparent object, but it is rendered overlaying it.

Why does it behave like this?
How can the character be rendered behind the transparent object?

Here is the transparent obj shader codes

Shader "Custom/TransparentTex"
{
  Properties
  {
    _MainTex("Base Texture", 2D) = "white" {}
    _Color("Color", Color) = (1, 1, 1, 1)
    _TexLod("Texure LOD", Float) = 0.0

    [Enum(UnityEngine.Rendering.BlendMode)]
    _SrcBlend("Source blend Factor", Int) = 1
    [Enum(UnityEngine.Rendering.BlendMode)]
    _DstBlend("Destination blend Factor", Int) = 1
  }
  SubShader
  {
    Tags
    {
      "RenderType" = "Transparent"
      "Queue" = "Transparent"
      "RenderPipeline" = "UniversalPipeline"
    }

    pass
    {
      Blend [_SrcBlend] [_DstBlend]
      ZWrite Off

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

      sampler2D _MainTex;
      CBUFFER_START(UnityPerMaterial)
        float4 _Color;
        float4 _MainTex_ST;
        float _TexLod;
      CBUFFER_END

      struct appdata
      {
        float4 positionOS: POSITION;
        float2 uv: TEXCOORD0;
      };

      struct v2f
      {
        float4 positionCS: SV_POSITION;
        float2 uv: TEXCOORD0;
      };

      v2f vert(appdata inputData)
      {
        v2f outData;
        outData.positionCS = TransformObjectToHClip(inputData.positionOS);
        outData.uv = TRANSFORM_TEX(inputData.uv, _MainTex);
        return outData;
      }

      float4 frag(v2f inputData) : SV_TARGET
      {
        float4 sampleUVs = float4(inputData.uv, 0.0f, _TexLod);
        float4 textureSample = tex2Dlod(_MainTex, sampleUVs);
        return textureSample * _Color;
      }

      ENDHLSL
    }
  }
  FallBack "Diffuse"
}
Related Discussions
...

The issue you're encountering with Spine characters overlaying transparent objects in Unity, despite their Z value suggesting they should be rendered behind, is likely due to how Unity handles transparency and sorting of objects. The Spine characters and the transparent object are probably not correctly depth-sorted due to the way transparency is managed in Unity's rendering pipeline.

Unity's rendering of transparent objects does not rely solely on the Z value for sorting. Instead, it uses several factors to determine render order, especially for transparent materials where ZWrite is turned off, as in your shader code. This means that even if an object is further away in terms of Z value, it might still be rendered on top if its sorting settings place it higher in the render queue.

To ensure that the Spine character is rendered behind the transparent object, you can try the following approaches:

  1. Adjust the Render Queue: Manually adjust the render queue of the materials used by the Spine character and the transparent object. You can set the Spine character's material to a lower priority in the render queue than the transparent object's material.
  2. Use Sorting Layers and Orders: Utilize Unity's Sorting Layers and Sorting Order within those layers to control the render order of your objects. Ensure the Spine character is in a layer behind the transparent object or has a lower sorting order if they are in the same layer.
  3. Depth and Camera Settings: Ensure that the camera settings are appropriate for the type of sorting you need (e.g., perspective vs. orthographic, transparencySortMode).

Remember, when dealing with transparency in Unity, the Z value is not the only factor determining render order. The material's settings, including its render queue and the use of sorting layers, play a significant role.

  • Harald が「いいね」しました。

If turn off ZWrite

      // ZWrite Off

The character is hidden

Oh... I solved it. The issue was caused by setting the "Order in Layer" to 1.

@MXXXXXS In addition to what Spinebot already mentioned, what shader are you using at the Spine skeleton, what are the material settings?

Please make sure you're using shaders from the Spine URP Shaders UPM package (or other URP compatible shaders) with your skeleton when using URP in your project. Also be sure not to use 2D URP shaders when using a URP 3D Renderer, and vice versa.

See details here:
https://esotericsoftware.com/spine-unity#URP-Shaders---Extension-UPM-Package