Manipulate SkinnedMeshRenderer vertices in realtime

I am working on a tool to move the vertices of a SkinnedMeshRenderer in the editor, but I cannot seem to figure out the offset between the original vertices of the SkinnedMeshRenderer’s sharedMesh and the current vertices of the SkinnedMeshRenderer as it is being animated in the animation window.

I have some code I am working through:

// skin is a component that holds the control points I am manipulating, the control points are just an array of Vector3
// skinnedMesh is the sharedMesh for the SkinnedMeshRenderer

void OnSceneGUI() {
        if (skin != null && skinnedMeshRenderer != null && skinnedMesh != null
        && skin.controlPoints != null && skin.controlPoints.Length > 0 && skin.points != null) {
            Event e = Event.current;

            Handles.matrix = skin.transform.localToWorldMatrix;
            EditorGUI.BeginChangeCheck();
            Ray r = HandleUtility.GUIPointToWorldRay(e.mousePosition);
            Vector2 mousePos = r.origin;
            float selectDistance = HandleUtility.GetHandleSize(mousePos) * baseSelectDistance;

            #region Draw vertex handles
            Handles.color = handleColor;
            Mesh mesh = new Mesh();
            skinnedMeshRenderer.BakeMesh(mesh);
            Vector3[] vertices = new Vector3[mesh.vertexCount];
   
            for (int i = 0; i < mesh.vertexCount; i++)
            {
                vertices[i] = mesh.vertices[i];
            }

            for(int i = 0; i < skin.controlPoints.Length; i++) {
                // if (Handles.Button(skin.points.GetPoint(skin.controlPoints[i]), Quaternion.identity, selectDistance, selectDistance, Handles.CircleCap)) {
                if (Handles.Button(vertices[i], Quaternion.identity, selectDistance, selectDistance, Handles.CircleCap)) {
                    selectedIndex = i;
                }
                if (selectedIndex == i) {
                    EditorGUI.BeginChangeCheck();
                    skin.controlPoints[i].position = Handles.DoPositionHandle(skin.points.GetPoint(skin.controlPoints[i]), Quaternion.identity);
                    if (EditorGUI.EndChangeCheck()) {
                        skin.points.SetPoint(skin.controlPoints[i]);
                        Undo.RecordObject(skin, "Changed Control Point");
                        Undo.RecordObject(skin.points, "Changed Control Point");
                        EditorUtility.SetDirty(this);
                    }
                }
            }
            #endregion
        }
    }

I want the handle of the control point I am manipulating to match that of the vertex I am manipulating as it is being animated. Currently it is offset and moves in local space, so the vertex I am moving does not match the movement and placement of where I want to place it because the vertex is being manipulated by the SkinnedMeshRenderer in its bindpose matrix. Does anyone have any ideas on how to solve this or better accomplish what I am trying to do here?

2751368--198467--MoveControlPoints.gif

Anyone with any experience in writing tools with SkinnedMeshRenderer understand this a bit better to explain a better way of going about this?

You can’t read actual vertex positions from skinned meshes directly. There’s a BakeMesh function you might be able to use.

–Eric

Actually I am using that in the code above, the tricky part is having the handle move the vertex in the bindpose matrix while offsetting the original vertex position.