- Home /
Vertex Coordinates to Global Vector3
I'm attempting to build a terrain from a series of individual, 4-vertex planes, similar to Wurm online or a chess board but with random heights to simulate elevation. Naturally, the edges of one tile, must match the tiles adjacent to prevent gaps.
What I'm having problems with is making the appropriate vertices share the same Global location. I'm pretty sure I need to use Transform.TransformPoint, but the Script Reference is somewhat confusing on the subject. Can anyone help explain it or use a simple 1-2 line script to show how it works? Thank you.
Answer by VesuvianPrime · Oct 29, 2013 at 12:18 PM
The way I've always done it is to multiply the verts with the SkinnedMeshRenderer:
// We return the verts for the skinner mesh renderer
private List<Vector3> __skinnedMeshRendererVerts()
{
SkinnedMeshRenderer skinnedMesh = this.gameObject.GetComponent<SkinnedMeshRenderer>();
Mesh mesh = skinnedMesh.sharedMesh;
Vector3[] verts = mesh.vertices;
List<Vector3> vertsList = new List<Vector3>();
// At this stage the verts are in localspace of the actor. We need to transform them
// into world space so we can transform the actor in the camera.
foreach (Vector3 vert in verts)
vertsList.Add(skinnedMesh.localToWorldMatrix.MultiplyPoint(vert));
return vertsList;
}
Answer by robertbu · Oct 29, 2013 at 02:27 PM
You just pass a local vertex from mesh.vertices to the function and you get back a world position. It takes into account rotation and scaling.
var vertices = mesh.vertices;
var worldPos = transform.TransformPoint(vertices[0]);
It seems to me for you to solve this problem, you are going to have to deform the mesh. For that you will need to modify the vertices based on world position. For that you can use Transform.InverseTransformPoint();