# Distance between camera and nearest poly

## Recommended Posts

Hello community,

Is there a way to know the distance between the camera and the nearest poly in front of the camera ? (not the pivot point of the first mesh hit, its surface...)

More clearly, see attached graphic, how to determine :

1. distance between camera and the nearest poly in the central axis of the camera  (red distance)

2. distance between camera and the nearest poly in the whole viewport  (green distance)

3. (more difficult I presume) "orthographic distance" between camera and the nearest poly (I don't know if it's the right name, it would be the distance between camera and the plane parallel to camera passing throught the nearest poly)  (blue distance)

Far above my math knowledge...

##### Share on other sites

That's quite a question :-)

As far as I know the only way to solve this is to use nearest neighbour algorithms. Sorting your points in a kd tree ( http://en.m.wikipedia.org/wiki/K-d_tree) or an octree will probably give you faster results, but the preparation takes time. With a simple mesh like a cube inspecting that won't take too long. With a complex mesh expect a performance reduction, unless you use a web worker. Then expect late results :-)

Getting the nearest poly and not the vertex is possible using ray tracing. Maybe first run nearest neighbour search, and then use ray tracing to all polys that they are a part of. But this is just a quick solution I thought if now, never tested it.

##### Share on other sites

For the red part and green, I would run a ray from the camera in the 'Z' direction (for the red) and 'Z' . camera.fov direction (green). These ray would return the first picked mesh, and thus distance.

I have no idea for the blue part.

##### Share on other sites

how much important that problem i have solution  with shader that is need new scene and some mathematical progress but it is faster than sorting and raycast.

##### Share on other sites

Can you know the angle between red and green rays ?

If yes, a simple  green_distance * cosinus(angle) will give you the blue distance, I guess

##### Share on other sites

@RaanaW: that's sounds a really heavy cost for performance, and since result should be real time I can't use webworkers...

@Temechon: that was my first idea, but I thought that ray would give me the distance to the pivot point of the first mesh hit, not the surface. Will I get the surface distance ? That would be great indeed.

For the green one, I don't understand what you mean by 'Z' camera.fov direction. The camera.fov is a float, not a vector... I don't see how to get a direction   (and if by this method you mean getting the distance at the limit of the field of view - not sure if this was your idea, since I don't completly get it -, my drawing is just an example and the corner of the box seams to be at the limit of the frustum but it's not the case, the idea is to manage to get the distance for any kind of mesh, anywhere in the view, as long as it is the closest to camera)

@NasimiAsl: so you're creating real time a clone of your scene, assigning material with proper shader to compute those kind of distance ? (and that is more performant than raycasting ?)

##### Share on other sites

@jerome: No I don't know any angle at all, since the box in the drawing is just an example, in real case that would be any mesh in a scene, with any possible shape, and those meshes are moving, and the camera also...

And to make it even simpler, the scene content is not known in advance :lol:

But if I'm able to send the green ray to determine its distance, I'll be able to know the angle and calculate the blue one (well, I presume... )

##### Share on other sites

Vousk-prod.

yes

performant than live raycasting not single ray cast  .

shader is not slower than standard material (standard material is a managed shader ).

for mesh id just set a color (256*256*256) and for distance just u need set alpha;

// d is range of see

// color_id converted from id

shader fragment  = vec4(color_id , distance(camera, pos)*d);

so you have it just need  a many camera ( 3 or 4 ) and long viewport like (10px x 150px)   for  see all around and just find laower than alpha and read rgb to get id

##### Share on other sites

Will I get the surface distance ? That would be great indeed.

Yes, ray.distance gives the distance between the ray origin and ray.pickedPoint.

I don't understand what you mean by 'Z' camera.fov direction

Z dot camera.fov : camera.fov is an angle (the max angle you can see in front of the camera), so you can get the vector result of the Z vector (0,0,1) multiply by this angle. I don't know if I'm clear

##### Share on other sites

@Vousk-prod. can you fix it ?

##### Share on other sites

@Vousk-prod. can you fix it ?

Not for the moment, I'm pretty busy on some other (urgent) things, this distance calculation stuff is for an idea I'll eventually do later, when I have time

I've never written a shader so I don't totally understand what your solution is, but it seams pretty smart

Trying to understand your idea made me think of an other way: I may use the BJS depth renderer and analyse produced depth map to determine the closest point to the camera, and then send a ray to that point to get the distance (or maybe the depth map can indicate me direclty the exact distance ?).

Do you think this would cost much in terms of performance ?

##### Share on other sites

it is use more ram and make 2 scene but you can manage  it . if you can run all process in gpu (shader) you have performance .

i ask you how match important this task. if it is necessary i can write sample shader for do this.

##### Share on other sites

The purpose is to enhance the Babylon lens effect post process, so the task is not "important", but that would be great.

However, I'm not sure if the need of creating a second scene would not be too much restrictive for the generic usage of BabylonJS.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.