- Home /
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.
The problem is when I try to make more than one connection, I get the image below.
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[i].vertices;
verts[0] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));
verts[1] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));
verts[2] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));
verts[3] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));
verts[8] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));
verts[9] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));
verts[13] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));
verts[14] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));
verts[16] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[7]));
verts[17] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[11]));
verts[22] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[10]));
verts[23] = markers[i].transform.InverseTransformVector(markers[i+1].transform.TransformPoint(meshes[i + 1].vertices[6]));
int[] tris = meshes[i].triangles;
meshes[i].Clear();
meshes[i].vertices = verts;
meshes[i].triangles = tris;
meshes[i].RecalculateNormals();
meshes[i].RecalculateBounds();
meshes[i].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[i] = markers[i].GetComponent<MeshFilter>().mesh;
}
}
Any clues to where I've gone wrong?
Answer by Bunny83 · Sep 07, 2018 at 04:46 PM
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[i].vertices;
Vector3[] verts2 = meshes[i + 1].vertices;
Transform t = markers[i].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[i].vertices = verts;
meshes[i].RecalculateNormals();
meshes[i].RecalculateBounds();
meshes[i].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.
Thank you very much for this answer. The mistake of mixing up InverseTransformPoint and InverseTransformVertex is an example of reliance on intellisense and copy+paste. Also, I appreciate the tips to let me take this project farther. $$anonymous$$uch to consider with this post, much respect from me. Thank you.
Your answer
Follow this Question
Related Questions
Converting 2d sprite to mesh - looking better algorithm 1 Answer
Why does this script put Unity into a not responding state? 0 Answers
Prevent seams on a generated mesh with split edges. 0 Answers
if I rotate an object, do they vertices of its mesh change? 1 Answer
Normal maps not applying to certain faces of a mesh 2 Answers