Transform problem with vertex positions from SkinnedMeshRenderer.BakeMesh

I’m trying to retrieve a mesh triangle’s vertex coordinates from the current animation using the new Unity 4 method SkinnedMeshRenderer.BakeMesh. I’m having a weird problem where the coordinates returned are exponentially larger than the actual mesh. When the animated object’s scale is 1, the returned data is the same size, but for scale 2, the vertex data is scaled up by roughly 3, for scale 5 scaled by about 20, etc. At first I thought this was a simple transformation bug on my part, but when I swap the BakeMesh output for the SkinnedMeshRenderer.sharedMesh, I get correct data. Is this a Unity 4 bug or am I missing something?

Thanks for any help.

public Vector3[] GetTriangleVertices (int triangle)
{
	SkinnedMeshRenderer skin = this.GetComponent<SkinnedMeshRenderer> ();
		
	Mesh baked = new Mesh();
	skin.BakeMesh(baked);
	
	var normals = new Vector3[baked.normals.Length]; baked.normals.CopyTo(normals, 0);
	var triangles = new int[baked.triangles.Length]; baked.triangles.CopyTo(triangles, 0);
	var vertices = new Vector3[baked.vertices.Length]; baked.vertices.CopyTo(vertices, 0);
		
	int i = triangle * 3;

	Vector3[] verts = new Vector3[3];
	verts[0] = vertices [triangles *];*
  • verts[1] = vertices [triangles [i + 1]];*

  • verts[2] = vertices [triangles [i + 2]];*

  • if (this.transform != null)*

  •  for (int v = 0; v <3; v++)*
    
  •  	verts [v] = this.transform.TransformPoint (verts [v]);*
    
  • return vertices;*
    }

Turns out the vertex coords I was getting were using the scale of the model from the model import settings without the transform applied. To get the right coords I left the GameObject’s transform scale at 1 and changed only the scaling in the model import settings.

Far from ideal, probably a bug, FYI.

Just before call the bakemesh function, make sure the skinned mesh object has localScale of (1,1,1). This should not affect the skinned mesh renderer’s appearance.