[Solved]Why does this code not working properly? [mesh baking and replacing]

Hi!

I am trying to create a script which replace a skinned mesh with its baked skinned mesh.
I need to make this work to modify a humanoid’s bone local orientation without affecting the model’s vertices.
The small white sphere dots indicate baked skinned mesh’s vertices and these are the mesh I would like to apply to my character.


Here is the script I wrote, which made the snapshot mesh of the image above.

void MakeNewBidnPose(Transform bone, Quaternion rot)
    {
        for (int i = 0; i < meshInfo.Count; i++)
        {
            //Check bone index to see if the skm has target bone
            int targetIndex = -1;
            for (int boneNum = 0; boneNum < meshInfo[i].bones.Length; boneNum++)
            {
                if (meshInfo[i].bones[boneNum].name == bone.name)
                {
                    targetIndex = boneNum;
                    continue;
                }
            }
            //If the skm does not include target bone then skip to next skm
            if (targetIndex == -1)
                continue;
            //Rotate bone opposit direction
            bone.rotation = bone.rotation * Quaternion.Inverse(rot);

            //Get snapshot of mesh with rotation offset
            Mesh newBindMesh = new Mesh();
            meshInfo[i].skm.BakeMesh(newBindMesh);
           
            //Rotate back the bone to its original orientation
            bone.rotation = bone.rotation * rot;
           
            //Replace original mesh with the snapshot
            meshInfo[i].mesh = newBindMesh;
           
            //Display snapshot's vertices for debug purpose
            DisplayVertices(meshInfo[i].mesh);           
        }
    }

The dots are drawn by a small method of “DisplayVertices()” at the bottom of the script.
I want the mesh fits to the white dots but indeed it doesn’t and I don’t understand why.
It seems like my script fails to update the original mesh with the snapshot but at the same time the dots are drawn correctly based on the update mesh vertices.
Someone please advise me how I can work this right? I should be overlooking something really simple.

I think line 19 is not the inverse of line 26. I think you want to store the original Bone and then invert it, then restore it. That might or might not be the problem, but I think it is at least A problem.

1 Like

@Kurt-Dekker
Thank you so much for the advice. Could you please explain that point a bit more? I am new to quaternion math and as you imagine I use them with a lot of guess work.
I wrote a small piece of code and according to the code the line 19 and 26 may work fine but I have no mathematical theory behind it and you should be right.

            if (Quaternion.identity == rot * Quaternion.Inverse(rot))
            {
                Debug.Log("Matches");
            } else
            {
                Debug.Log("Don't Match");
            }

This results in “Matches.”

Ah, I got it working now.
By reviewing my script after taking a good sleep a couple of days I found that I forgot to make the mesh relevant to the skinned mesh when I set the variable.
By adding a single line of the code below, everything start working as I suppose.

skinnedMeshRendere.SharedMesh = mesh;