- Home /
Shader `unity_ObjectToWorld` variable doesn't seem to work for dynamically batched objects
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.
Answer by Bunny83 · Jan 03, 2021 at 12:27 PM
Yes, of course this is by design ^^. That's how batching works. Instead of heaving multiple seperate objects which are rendered one by one they get combined into a single object which is rendered as one object. And yes, that means that one combined object has only one model matrix. The transform information of the individual objects get used during the batching process on the CPU when the batched object is created.
So if you have a shader that requires the object to have a seperate model matrix you can't batch it. An alternative is to write a specialized shader and "bake" the required per-object-data into vertex attributes of the mesh. For example if you need the object center / pivot in the shader you could set an unused texture coordinate of all vertices of that mesh to that position. So in the shader you can read that position from any vertex as its part of the vertex stream itself.