Jump to content

Distance between camera and nearest poly


Vousk-prod.
 Share

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... :wacko:

 

post-6528-0-90710400-1429211226.jpg

Link to comment
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.

Link to comment
Share on other sites

Thanks for your answers.

 

@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 :wacko:  (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 ?)

Link to comment
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... :D

And to make it even simpler, the scene content is not known in advance :lol: :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... :))

Link to comment
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

 

 

Link to comment
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 :)

Link to comment
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 :P 

 

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 ?

Link to comment
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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...