Yesterday I discovered that you can actually detect this through shaders - because dynamic batching changes the _Object2World matrix that is accessible in the shader. But as far as I know, this matrix is not accessible from scripts, and I want to figure out if an object is dynamically batched from scripts.
This would be very useful to see where your dynamic batching is failing at a glance.
Are you the guy discussing this in the IRC channel yesterday?
It’s a cool discovery, and though I don’t have a stone-sure answer for you, I felt like adding a quick question. Maybe it’ll be relevant:
The _Object2World matrix is the current model matrix, according to the documentation. But isn’t that the same as renderer.localToWorldMatrix?
It also says on that page, specifically, that we should use that particular matrix in shaders (as opposed to the similar one on the Transform). So the question becomes:
Is renderer.localToWorldMatrix affected by dynamic batching, and if it is, wouldn’t that be a way to detect whether an object is batched in a script? Conversily, if it is not affected by dynamic batching, then you should be safe to transfer this one to the shader with Material.SetMatrix and use that instead of the built-in _Object2World.
I already checked, and renderer.localToWorldMatrix does not change, while _Object2World does. So I guess there’s a layer underneath what is exposed to the script that batches the objects and replaces the _Object2World matrix.
My concern is not really in fixing the transformation issue anymore - my concern is in building a system for quickly detecting non-batching objects. This would be of considerable help to all developers I believe! If I could tell if a mesh is nog batching, I could automatically add a Gizmo to it that warns the user that it is not, so people can identify which meshes are failing to batch.
I could write a custom shader that checks whether there is a difference between localToWorldMatrix (set through setMatrix) and _Object2World, and render a special color in a second pass or something, but that would require me to use this shader everywhere, and would not be very convenient.