- Home /
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 [i]];
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;
}
Answer by daterre · Jan 08, 2013 at 10:24 AM
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.
I had the same problem, simply add a multiply to the vector.
var pos:Vector3= Vector3(Parent.transform.position.x-vertices[i].x*$$anonymous$$eshScale,Parent.transform.position.y-vertices[i].y*$$anonymous$$eshScale,Parent.transform.position.z-vertices[i].z*$$anonymous$$eshScale);
Same goes for normals, sorry I only know this in javascript.
Answer by ZFarago · Jun 19, 2013 at 02:09 PM
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.
Your answer
Follow this Question
Related Questions
Skinned Mesh Renderer Importing as Mesh Renderer 2 Answers
3D Platformer Tutorial getting Lerpz's SkinnedMeshRenderer using c# 0 Answers
Best way to send real-time blendshape data over a networkView? 0 Answers
Why does adding cloth component to skinned meshrenderer move the bounds away from the mesh? 2 Answers
Help! Skinned mesh render not visible!! 3 Answers