# "Baking" Object scale into mesh

I have a hierarchy of Unity objects, and each object in the hierarchy has a (non-uniform) scale and rotation.

For example, my hierarchy may be A->B->C.

I’m trying to write a script to “bake” the scale of these objects into the meshes in the hierarchy, so from the point of view of the rendering engine, all objects will have a scale of (1,1,1). This will allow batching to work more reliably, and more importantly, should avoid huge spikes I’m getting due to lots of calls to BakeScaledMeshCollisionData. The object hierarchies in question will be used as runtime-prefabs. I’ve verified this solution works for my needs using simple un-parented objects.

What I need help with, is figuring out how to do this in a hierarchy of objects. In other words - given the hierarchy above, how do I determine for each object what scale to apply?

Basically - how do I recreate the effects of the transforms of all the objects in the hierarchy into a single matrix, that I can then use to transform the verts of the mesh, while setting the localscale of each object in the hierachy to 1.0, to get the same effect?

I would suggest to simply move the topmost parent to 0,0,0 (world origin) and simply use transform.TransformPoint() on each vertex. The resulting vertices are transformed as you wish. Since you removed the root translation you can simply use the vertices as local coordinates of an object sitting at the world origin with eulerangles 0,0,0 and a scale of 1,1,1. Once the new object is created, just re-add the translation to move it to it’s original position.

edit
The solution is actually way simpler than imagined ``````Vector3 Bake(Transform aOld, Transform aNew, Vector3 aPos)
{
var P = aOld.TransformPoint(aPos);
return aNew.InverseTransformPoint(P);
}
``````

So just create a new object at the same world position / rotation as the nested original object and execute Bake for each vertex ``````Transform oldObj;

Transform newObj = (new GameObject(oldObj.name + "_Baked")).transform;
newObj.position = oldObj.position;
newObj.rotation = oldObj.rotation;
newObj.localScale = Vector3.one;

for(int i = 0; i < verts.Length,i++)
verts _= Bake(oldObj, newObj, verts*);*_
``````