How to calculate a vertex position of SkinnedMesh?

Hi there, I’m trying to calculate the position of the vertices in given skinned mesh. Well, the result is mostly right, but in many models there is always some visible “offset”.

the formula I’m using:

pos = (bones’s weight) * (bone’s local2world matrix) * (SMR’s RotationMatrix) * (bindPoseMatrix) * object-space-pos

the final pos is weight-sumed.

``````Transform smrTr = smr.transform;
BoneWeight[] weights = mesh.boneWeights;
Transform[] bones = smr.bones;
Matrix4x4[] bindposes = mesh.bindposes;
Vector3 localPos = mesh.vertices[vidx];

Matrix4x4 localMat = Matrix4x4.TRS(Vector3.zero, smrTr.localRotation, Vector3.one);
if (bw.weight0 != 0)
{
Matrix4x4 mat = bones[bw.boneIndex0].localToWorldMatrix * localMat * bindposes[bw.boneIndex0];
pos += mat.MultiplyPoint(localPos) * bw.weight0;
}
if (bw.weight1 != 0)
{
Matrix4x4 mat = bones[bw.boneIndex1].localToWorldMatrix * localMat * bindposes[bw.boneIndex1];
pos += mat.MultiplyPoint(localPos) * bw.weight1;
}
if (bw.weight2 != 0)
{
Matrix4x4 mat = bones[bw.boneIndex2].localToWorldMatrix * localMat * bindposes[bw.boneIndex2];
pos += mat.MultiplyPoint(localPos) * bw.weight2;
}
if (bw.weight3 != 0)
{
Matrix4x4 mat = bones[bw.boneIndex3].localToWorldMatrix * localMat * bindposes[bw.boneIndex3];
pos += mat.MultiplyPoint(localPos) * bw.weight3;
}
``````

You can see in the pic that, the red rect(the point I calculated) drifts away from the tips of the mesh.

I cannot figure out where this offset comes from, any help is appreciated.

Okay, I know where I’m doing wrong.

The formula should be:
pos = (bones’s weight) * (bone’s local2world matrix) * (bindPoseMatrix) * object-space-pos

There’s no need to multiply the SMR’s matrix, I don’t understand how that comes to my head at first.