Line segment coordinates

When I look at the intersects array, I see a distance property and a point object. I assume that the distance refers to how far away the nearest object is from the point object and that the point object represents the location that user clicked on.

In my case, I have a bunch of line segments that are all stored in a single geometry, mesh, and material. This is done for performance reasons. When I click on the line segments what I don’t see are the coordinates of the line segments that appear in the array. Is there a way to obtain these coordinates?

The point object represents the actual intersection point in world space. The distance value represents the distance between the origin of the ray and the intersection point.

What do you mean with “coordinates”? Can you demonstrate your intersection test in a live example?

Not sure about THREE.LineSegments() with THREE.Geometry(), but, if you work with THREE.BufferGeometry(), then .intersectObject() method returns an array of objects and one of properties of those objects is .index, that represents the index of the neares vertex of the intersected line segment (if I got it correctly, though the documentation doesn’t say about it).

Thus, when you know the index of one point of a line segment, you can find the index of the other point just like that:
suppose idx is the index you found by intersection
let idxNear = idx % 2 === 0 ? idx + 1: idx - 1;

So, you know indices of vertices, you can obtain their coordinates from line.geometry.attributes.position with .fromBufferAttribute() method of THREE.Vector3().

Just a related fiddle, where you can see how to find the indices of points in a line segment:

One more example of interaction with points (an indexed THREE.BufferGeometry()):


@Mugen87, I appreciate the clarification about what the distance means and about what the point object represents. Before, I put together a live example I want to try @prisoner849 technique.

@prisoner849, thank you for the example.

Do the indices of the vertexes represent the order in which they were placed in the position array when creating the BufferGeometry? For example, a vertex with an index value of 7 means it was the seventh added?

Yes, indices of vertices in the geometry.attributes.position buffer attribute.
7th vertex has values from 18th, 19th and 20th items of geometry.attributes.position.array.
If I’m not wrong :thinking:

+1 to @prisoner849

I add that I’m actually working on line segments. When you get raycaster.intersectObjects indexes, you can retrieve vertex coordinates just passing the index to getX getY getZ methods of BufferAttribute, so:

lines.geometry.attributes.position.getX( index );
lines.geometry.attributes.position.getY( index );
lines.geometry.attributes.position.getZ( index );

Notice that returned array from raycaster.intersectobjects is ordered from closest to further intersection. [0] is the closest one.

Hi! :slight_smile:
Just out of curiousity, what revision of the framework do you use?
I can’t find any mention about .getXYZ() method neither in the documentation nor in the source code of THREE.BufferAttribute().
There are only .getX(), .getY(), .getZ() and .getW() methods.
But maybe I misunderstood something from your reply. :beers:

1 Like

I messed up, I was thinking about setXYZ :smiley:

Anyway, yes, you can access XYZ coordinates separetely. I usually combine them in an object to use it

1 Like

Thanks! :):grin:

Thank you!

1 Like

You’re welcome :slight_smile:

1 Like