Trouble assigning vertices in a mesh to connect to other meshes vertices.

So what I’m trying to do is to take an array of meshes and connect them by changing the vertices to be at the location of the mesh in front of it in the array.

Right now, my algorithm only works for the first pair in the array. Images below.
[124109-combined-connect.png*_|124109]

The problem is when I try to make more than one connection, I get the image below.

[124110-connect-3.png*_|124110]

Here is the algorithm I use, I found the vertices to connect by a lot of testing, using the default unity cubes. Assigning the locations of the front face and all connecting points, to be at the locations of the back face of the mesh in front of it. I don’t understand why it works fine for the first connection but after that it is so off

   void ConnectMeshes()
    {


        for (int i = 0; i < meshes.Length-1; i++)
        {


            Vector3[] verts = meshes*.vertices;*

verts[0] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));*
verts[1] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));*
verts[2] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));*
verts[3] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));*
verts[8] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));*
verts[9] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));*
verts[13] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));*
verts[14] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));*
verts[16] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));*
verts[17] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));*
verts[22] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));*
verts[23] = markers*.transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));*

int[] tris = meshes*.triangles;*

meshes*.Clear();*
meshes*.vertices = verts;*
meshes*.triangles = tris;*
meshes*.RecalculateNormals();*
meshes*.RecalculateBounds();*
meshes*.RecalculateTangents();*

}
}
The name of the gameObjects of associated meshes are called Markers in the code. I know the meshes are in the same order because of this code.

void Start()
{
meshes = new Mesh[markers.Length];

for (int i = 0; i < markers.Length; i++)
{
meshes = markers*.GetComponent().mesh;*
}
}
Any clues to where I’ve gone wrong?
*
*

You correctly use “TransformPoint” but you incorrectly use “InverseTransformVector” which should be InverseTransformPoint. “InverseTransformVector” expects a Vector4 with homogeneous coordinates. Since you pass in a Vector3 it gets implicitly converted to Vector4 but the w component will be 0 instead of 1. Therefore the translation is not applied, only rotation and scale. This does work correctly:

    for (int i = 0; i < meshes.Length - 1; i++)
    {
        Vector3[] verts = meshes*.vertices;*

Vector3[] verts2 = meshes[i + 1].vertices;
Transform t = markers*.transform;*
Transform t2 = markers[i + 1].transform;
verts[0] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));
verts[1] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
verts[2] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
verts[3] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));
verts[8] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
verts[9] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));
verts[13] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));
verts[14] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
verts[16] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
verts[17] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));
verts[22] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
verts[23] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));
meshes*.vertices = verts;*
meshes*.RecalculateNormals();*
meshes*.RecalculateBounds();*
meshes*.RecalculateTangents();*
}
Note that you really should cache the “verts2” like i did. Every time you access mesh.vertices Unity will create a copy of the whole vertices array. Since you do this 12 times each iteration you create a lot of unnecessary garbage.
You could improve this further by removing duplicate positions. A Cube has “8” logical but 24 actual vertices since they need to be split for each face. However that means there are always 3 vertices with the same position. So you can calculate that position only once and assign it to the corresponding vertices. Just like that:
verts[0] = verts[13] = verts[23] = t.InverseTransformPoint(t2.TransformPoint(verts2[6]));
verts[1] = verts[14] = verts[16] = t.InverseTransformPoint(t2.TransformPoint(verts2[7]));
verts[2] = verts[8] = verts[22] = t.InverseTransformPoint(t2.TransformPoint(verts2[10]));
verts[3] = verts[9] = verts[17] = t.InverseTransformPoint(t2.TransformPoint(verts2[11]));
I would have suggested creating a Matrix4x4 by combining the two transformations into one, that way each point only need to be transformed once, directly from source to target. However since we now only have 4 points this is quite irrelevant. However you could have done this as well:
Matrix4x4 m = t.worldToLocalMatrix * t2.localToWorldMatrix;
verts[0] = verts[13] = verts[23] = m.MultiplyPoint3x4(verts2[6]);
verts[14] = verts[1] = verts[16] = m.MultiplyPoint3x4(verts2[7]);
verts[2] = verts[22] = verts[8] = m.MultiplyPoint3x4(verts2[10]);
verts[17] = verts[3] = verts[9] = m.MultiplyPoint3x4(verts2[11]);
Also note there’s no reason to call Mesh.Clear. This should only be used when you actually change the number of vertices or when you create different triangles than there were before. Since you just modify the existing mesh, just update the vertices. The triangles will stay the same.