0
\$\begingroup\$

I'm using the standard render pipeline and the unity_ObjectToWorld variable for some calculations in my shader. After I enabled dynamic batching these calculations got broken. It seems that unity_ObjectToWorld is set to default when the objects are being batched. Is it by design? I didn't find anything in the documentation.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This is documented in the manual where it describes dynamic and static batching:

Dynamic batching works by transforming all GameObject vertices into world space on the CPU

Internally, static batching works by transforming the static GameObjects into world space and building one shared vertex and index buffer for them

And here:

Some shaders (mostly ones that do object-space vertex deformations) do not work when Draw Call Batching is used – that’s because batching transforms all geometry into world space, so “object space” is lost.

Naturally, since all the vertices in the batch have been pre-transformed into world space on the CPU side prior to rendering, unity_ObjectToWorld is simply the identity matrix. There's no other work for it to do in the GPU shader, and because the objects in the batch could have come from multiple different object spaces, there's no way this one variable can preserve the information about all of them.

We have some prior Q&A on this topic here, with some hints about how we can work around this for some shaders that need object space data, without giving up batching completely.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .