Vertex location

Is there a way to get the world space location and direction of a vertex in skinned mesh? After bone and blendshape deformation. I suppose baking might work but that feels suboptimal.

Heres something that calculates the vert positions, getting the normal direction is probably more difficult, you’d have to calculate the normals of all adjacent faces, and average them, and by that point maybe you’re better off baking the whole thing

    void Update () {
        Mesh m = gameObject.GetComponent<SkinnedMeshRenderer>().sharedMesh;
        Matrix4x4[] bindPoses = m.bindposes;
        BoneWeight[] weights = m.boneWeights;
        Transform[] bones = gameObject.GetComponent<SkinnedMeshRenderer>().bones;
        for ( int i = 0; i < m.vertexCount; i++ ) {
            Vector3 resultPos =
                bones[weights[i].boneIndex0].localToWorldMatrix.MultiplyPoint3x4( bindPoses[weights[i].boneIndex0].MultiplyPoint3x4( m.vertices[i] ) ) * weights[i].weight0 +
                bones[weights[i].boneIndex1].localToWorldMatrix.MultiplyPoint3x4( bindPoses[weights[i].boneIndex1].MultiplyPoint3x4( m.vertices[i] ) ) * weights[i].weight1 +
                bones[weights[i].boneIndex2].localToWorldMatrix.MultiplyPoint3x4( bindPoses[weights[i].boneIndex2].MultiplyPoint3x4( m.vertices[i] ) ) * weights[i].weight2 +
                bones[weights[i].boneIndex3].localToWorldMatrix.MultiplyPoint3x4( bindPoses[weights[i].boneIndex3].MultiplyPoint3x4( m.vertices[i] ) ) * weights[i].weight3;

            Debug.DrawRay( resultPos, m.normals[i] );
        }
    }
}
1 Like