Shader object space error on 2D animation object

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.


This is the bugged behavior, depending on what the camera sees, the object space changes.

Can somebody explain how this could happen?

Can you post the shader, please?

Hi, it is a very simple debug shader.
8423712--1114806--DebugShader.png

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.

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.

Shader "Unlit/Test2"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalRenderPipeline" }

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

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

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 objectPosition : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.objectPosition = v.vertex;
                o.vertex = TransformObjectToHClip(v.vertex.xyz);
                return o;
            }

            half4 frag (v2f i) : SV_Target
            {
                // sample the texture
                half4 col = half4(i.objectPosition.xyz, 1.0);
                return col;
            }
            ENDHLSL
        }
    }
}

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
8423847--1114842--batching.gif

even moving this quad affects one of em
8423847--1114860--batchingEvenWithQuad.gif

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.

Maybe this is dynamic batching. You can turn it off in the options.

[quote=“c0d3_m0nk3y, post:8, topic: 893661, username:c0d3_m0nk3y”]
Maybe this is dynamic batching. You can turn it off in the options.
https://docs.unity3d.com/Manual/dynamic-batching.html#dynamic-batching-meshes
[/quote]
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?

Hm, weird.

In 2020.3 it was either in the Project Settings (for built-in) …

…or in the URP asset.

in 2022.1, the setting is nowhere to be seen

but you can still enable/disable it in the debug view:

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.

bizarre : the only thing i found that caused something was Depth priming mode
disabled it caused 1 sprite to stop batching.

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.

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)

Basically, a c# script that changes the material to a copy instance every time I enter play mode.