Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Shader object space error on 2D animation object

Discussion in 'General Graphics' started by LianZH_, Sep 8, 2022.

  1. LianZH_

    LianZH_

    Joined:
    Sep 16, 2017
    Posts:
    12
    hi all, I am experiencing different shader behaviors depending on the number of objects on the screen. I suspect the reason is related to the rendering batch.

    The shader is a simple debug shader that shows the object space position as the RGB color. The test objects are rigged objects using the 2D animation package. Each individual body part is a separate game object with the same material/shader.

    This is the expected behavior, each individual body part has its own object space.
    normal.png
    This is the bugged behavior, depending on what the camera sees, the object space changes.
    combined.png

    Can somebody explain how this could happen?
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    651
    Can you post the shader, please?
     
  3. LianZH_

    LianZH_

    Joined:
    Sep 16, 2017
    Posts:
    12
    Hi, it is a very simple debug shader.
    DebugShader.png
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    651
    TBH, I was hoping for a regular shader lol. It's difficult to debug those shader graphs (generated code for this simple shader is 6500 lines). It looks fine, though.

    Maybe you can see what's going on in the frame debugger or in RenderDoc. Sorry, wish I had a better answer.
     
  5. LianZH_

    LianZH_

    Joined:
    Sep 16, 2017
    Posts:
    12
    A
    Thanks for replying. The shader itself is really simple. I don't think the problem is caused by the shader. In fact, I've tested regular objects such as quad/cube. They are fine. I think the problem is related to the 2D animation rigging pipeline.

    Code (CSharp):
    1. Shader "Unlit/Test2"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalRenderPipeline" }
    10.  
    11.         Pass
    12.         {
    13.             HLSLPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.  
    17.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    18.  
    19.             struct appdata
    20.             {
    21.                 float4 vertex : POSITION;
    22.                 float2 uv : TEXCOORD0;
    23.             };
    24.  
    25.             struct v2f
    26.             {
    27.                 float4 vertex : SV_POSITION;
    28.                 float4 objectPosition : TEXCOORD0;
    29.             };
    30.  
    31.             sampler2D _MainTex;
    32.             float4 _MainTex_ST;
    33.  
    34.             v2f vert (appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.objectPosition = v.vertex;
    38.                 o.vertex = TransformObjectToHClip(v.vertex.xyz);
    39.                 return o;
    40.             }
    41.  
    42.             half4 frag (v2f i) : SV_Target
    43.             {
    44.                 // sample the texture
    45.                 half4 col = half4(i.objectPosition.xyz, 1.0);
    46.                 return col;
    47.             }
    48.             ENDHLSL
    49.         }
    50.     }
    51. }
    52.  
     
  6. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    2D animation pack is an extension of sprites - batching for sure and easily can repro. Sprites constructor is capable of batching them from same material... and it can also break one off just as oddly
    batching.gif

    even moving this quad affects one of em
    batchingEvenWithQuad.gif
     
    Last edited: Sep 8, 2022
  7. LianZH_

    LianZH_

    Joined:
    Sep 16, 2017
    Posts:
    12
    Is there a way to control the batching? In my example, sometimes one character's arm can be in the same batch as the other character's leg while all other body parts are not batched at all. The outcome is so random, that a slight movement of the object completely changes the batching.
     
  8. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    651
  9. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    \
    yes it is dynamics - I also went hunting for all that last night in "Player settings" and the SRP->URP asset >
    advanced

    im in 2021.2 and nothing showed up

    the only thing I could think to hack it is set everything to like -1 X (mirror); maybe that will break everything?
     
  10. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    651
    Hm, weird.

    In 2020.3 it was either in the Project Settings (for built-in) ...
    upload_2022-9-8_20-21-23.png

    ...or in the URP asset.
    upload_2022-9-8_20-27-5.png

    in 2022.1, the setting is nowhere to be seen
    upload_2022-9-8_20-28-21.png

    but you can still enable/disable it in the debug view:
    upload_2022-9-8_20-29-1.png

    They've probably hid it because it's kind of deprecated AFAIK. It really only helps performance on some mobile platforms.
    It seems to be off by default, which makes me wonder if your problem is caused by something else.
     
    Last edited: Sep 9, 2022
  11. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    bizarre : the only thing i found that caused something was Depth priming mode
    disabled it caused 1 sprite to stop batching.
     
  12. LianZH_

    LianZH_

    Joined:
    Sep 16, 2017
    Posts:
    12
    I have tried the dynamics batching thingy before, does not seems to make any difference. The only solution I have found so far is to dynamically create new material instances at runtime, but this is kinda expensive.
     
  13. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    are you creating + assigning new materials? i have heard that is slow

    or a RNG script that plops a variable -> material exposed something inside the shader
    might work quicker to make dynamic instances (im not sure)
     
  14. LianZH_

    LianZH_

    Joined:
    Sep 16, 2017
    Posts:
    12
    Basically, a c# script that changes the material to a copy instance every time I enter play mode.