How do I adjust the vertices of a skinned mesh seamlessly?
I'm currently working on an avatar editor for a game. The Avatar Editor has sets of controls which allow me to manually tweak the vertices of a skinned "Male Base Mesh". The code all generally works properly up until the point where I adjust the actual vertices of the Skinned Mesh. Once this happens, the mesh no longer orients properly to the rig.
The code I'm using to update the mesh is as follows:
private void UpdateMesh()
{
Mesh mesh = new Mesh();//This is the new mesh we'll make changes to
SkinnedMeshRenderer sceneMeshRenderer = spawnedPrefab.transform.Find("Avatar_Male_Body_Geo").GetComponent<SkinnedMeshRenderer>();
sceneMeshRenderer.sharedMesh = selectedData.baseMesh;//Assign the base Mesh to the skinnedmeshRenderer
sceneMeshRenderer.BakeMesh(mesh);//Bake the mesh's data into our new mesh
Vector3[] vertices = mesh.vertices;
for (int ix = 0; ix < bodySliders.Count; ix++)
{//once we get here, go through all the vertices in the mesh and update their positions based on the controls
for (int i = 0; i < vertices.Length; i++)
{//This is where all the changes are made to the vertices
vertices[i] += bodySliders[ix].control.position.cur * bodySliders[ix].control.weightTable[i].weight;
}
}
mesh.vertices = vertices;//Assign the new vertex positions
sceneMeshRenderer.sharedMesh = mesh;//Reassign the mesh
}
As you can see from the following image, after this code executes, the mesh is no longer properly oriented to the rig and I don't know why. The mesh still animates properly and the changed vertices are even updated properly on the mesh. It seems to be the mesh itself is just changing orientation for some unclear reason.
I'm sure the answer is something simple, but I don't know what it is.
Answer by Endless_Aftermath · Jun 23, 2016 at 03:17 AM
If your mesh script is attached to another gameObject that is a child of another, the mesh object may be orienting itself to its parent orientation. In order to fix this (if this parenting is the case) you have to remove the mesh object, reset the transform of the rig, add the mesh object, THEN rotate your rig object to its proper orientation. I dealt with this problem with weapons and instantiating them into a hand instead of attaching them before animation and just disabling them. It's a pain, but that's just the way Unity works.
The eventual solution to my issue was a little more complicated and had to do with how the transforms were set up before I exported to Unity, but you were generally on the right track. Thanks for the help!
Awesome that you figured it out. I knew it had to be something with the transform. Best wishes to you and your project. $$anonymous$$odel looks good, btw.
Your answer
Follow this Question
Related Questions
Unity 5.6.0: Mesh only rotates if I rotate the root bone of the skinned mesh renderer 0 Answers
I want to make the DepthImage of Kinect.V2 Renderer Mesh Skinned. 0 Answers
Missing a lot of faces 0 Answers
Getting the vertex position of a skinnedmeshrenderer after blendshape? 1 Answer
Empty mesh with vertices and triangles 0 Answers