Editor update

Planning and Development

I’ve been working on terrain editing tonight and it’s been a little slow but I’m making progress.  Now when you select a terrain object you can hit ‘e’ to enter edit mode.  This changes the emissive color of the object so you can tell you are in edit mode.  Then, when you right click anywhere on the terrain it picks the vertex closest to where you clicked and places a little yellow sphere there.  The sphere is, of course, just a placeholder for future functionality.  Eventually I will be able to move the selected vertex up and down along the y axis.  This is actually a pretty small step to code, I just don’t feel like it tonight.

This isn’t anything groundbreaking but I thought somebody might be interested in how to select the closest vertex on a mesh.

First, you need to get an intersection point.  There are plenty of resources out there on how to do this, just search for raycaster, and specifically the .setFromCamera() method.  Anyway, once you have your intersection object it tells you some interesting things.  First of all it includes the intersection point, which is obviously necessary.  It also has the intersection face.  The face is comprised of three elements; a, b, and c.  These are the indices of the three vertices of the triangle intersected.  So to get the actual Vector3s for these three points you can do something like this:

var a = terrain.geometry.vertices[intersections[0].face.a];
var b = terrain.geometry.vertices[intersections[0].face.b];
var c = terrain.geometry.vertices[intersections[0].face.c];

Now you have the intersection point as a Vector3 and the three points of the intersected triangle, also as Vector3s.  Vector3 has a convenient method called .distanceTo() that returns, you guess it, the distance from one Vector3 to another.  So all you need to do is find out which of a, b, and c is closest to your intersection point.  I used Math.min() but there are plenty of ways to do this.  Once you know the closest one, you can do whatever you want with it.  As I said previously, I placed a little yellow sphere on the closest vertex for now, but the eventual goal is to move the vertex up and down to shape the terrain.

Maybe this will be mildly useful to someone down the road.  If you have any questions about this leave a comment and I’ll do my best to respond.

Leave a Reply

Your email address will not be published. Required fields are marked *