Using a ray to get coordinates on a mesh surface

Hi there, Im looking into rays and wanted to know if you can use a ray to get the coordinates on the surface of a mesh, rather then getting the coordinates of the scene or picking an entire mesh.

For example, if there were a plane mesh in the scene, is there a way of checking what part of that plane had been picked, ie the top-right corner, even if the plane mesh were to move within the scene.

I notice on this page theres a brief mention about using the Ray class to get local coordinates but it seems the documentation is unfinished (its a small section thats duplicated twice on the page and doesn’t have much context)

(under the multi-pick section)

I’m suspect the mesh may have to be subdivided to get this effect, if it’s possible at all, but any suggestions welcome :slight_smile:

Hi!

PickingInfo.bu and bv are your friends here :blush:

1 Like

A small example PG:
https://playground.babylonjs.com/#L78MU4

2 Likes

This is perfect, thanks a lot :slight_smile:

1 Like

ok, soo after some playing around and slightly confusing results ive stumbled into another issue of what I want to do.

It seems like the Barycentric coordinates given by bv/bu are relative to the triangles of the mesh (the simplex…?), and so im getting these readings (from the cube in the above playground):


So crossing a plane or face of a box on one axis, the value rises until it hits the middle diagonal crossing, then stays static.

What I would like to do is treat a plane more an x/y graph where I could get readings like this:

Any idea how this could be achieved? is there a way to get the particular triangle thats being picked and map the data somehow to give a continuous value?

1 Like

Just a quick PG using quads instead of triangles in the cube. This should solve the problem. Or isn’t it? :smiley:
https://playground.babylonjs.com/#L78MU4#1

Or you can try pickingInfo.getTextureCoordinates().
I am just guessing here.

1 Like

I think the quads just get triangulated automatically, so its the same result :triangular_ruler:

However the texture coordinates thing i shall try, thanks for the suggestion :slight_smile:

1 Like

Damned triangles :smiley: I was clicking on the cube and seemed to return good values :smiley:
Let me know whether the second one helped.

1 Like

Yes, this works! :smile_cat:

Caveat is that a single face wont map from 0-1 (or maybe would if the uvs were changed, im not sure) but it doesn’t crossover in the middle and gives a linear value I can use (in the playground above its like 0.4-0.6 or so on the front face, continuing wrapping around the cube).

Here it is for posterity using a plane (which does map from 0 to1). Might be good for graph-type things. Or 3D darts :dart:
https://playground.babylonjs.com/#L78MU4#2

Thanks again!

2 Likes

You are welcome! We both have learned something new. :vulcan_salute:

1 Like