RayCast against geometry (not involving colliders)?

I’m looking for a way to raycast against current scene geometry, without involving colliders. Eventually, I want to get the distance to the closest triangle from some point in space. Is there any way to do that?

My current approximate solution is to have a scene with somewhat evenly distributed vertices and then find the closest vertex to the point. But still, it introduces huge error when the point is located close enough to the geometry.

The other typical way of doing “raycasts” is to use the Scene Picker which lets you determine which mesh instance has been selected. This project has a good example of the two methods.

However, I don’t think entity picking willing give you the absolute depth from the camera. It will just tell you which meshinstance was clicked on. I’m not sure if you can pull out the distance from the depth buffer though.

Thanks, Dave. After your reply I’ve actually gon an interesting idea. I could render low-res cubemap in required point and then find the closest pixel.

@underlight - Did you get anywhere with this? I am trying to raycast onto a car so I can place a .obj where ever the user clicks. But it seems the only built in PC solution is to use a cube or sphere collider (pc.BoundingBox) which just will not be accurate enough. Perhaps I can use multiple bounding boxes somehow but that seems really wrong …

@dirkus If you are willing to use the PlayCanvas supported physics engine ammo.js (or one of the other ones on GitHub), you can use a mesh collider using a low poly mesh of the car.

@yauster Do you have any code examples to help with how to raycast against geometry?

I have tried doing this in lots of different ways but not able to get it working.
-Latest simple test here- raycast.js
https://playcanvas.com/project/491331/overview/raycast

The latest way I am trying to do this is to surround the car with boxes and use pc.BoundingBox on each box to capture the click event and place the object. But the “intersectsRay” depending on the angle will sometimes go through both boxes and instead place the object onto the second box behind the car so I cannot do that.

I would much rather be able to just use the car mesh to capture the click events and let the user place an object on the car section that they have clicked. I did take a look at the ammo.js docs but there was nothing in there that I could use and I really would prefer to find a Playcanvas solution to this as I am sure there must be one.

Thanks again @will for the solution that seems to work really nicely … info here - [SOLVED] Calculate bounding box of a model

Using that code against the car entity as seen here https://playcanvas.com/project/491331/overview/raycast - seems to work really well for what I need it for anyway :smiley:

I don’t, but I believe @Mr_F does from one of his projects.

Nah, if you mean After the Flood collision, it’s just snapping to a closest triangle point. However, having a mesh raycast routine would be indeed useful.

The bounding box solution I mentioned in my last comment that @will provided works but unfortunately it creates a cube shaped object that the raycast hits. So it’s not very accurate for most cars when you try to select them from a side view. So I have just manually created a work around which is a pity as the demo I did for my company is not nearly as accurate as I am sure it could be. It sure would be nice to have an outline of the object that was a little more accurate. PS “After the flood” is quite amazing.

Just read this. In PlayCanvas, you can use a FBX as a mesh collider to raycast against/collide against.

Really quick example:
Editor: https://playcanvas.com/editor/scene/536756
Published: https://playcanv.as/p/firSvBCd/

@yaustar That example project looks like it’s working perfectly! But I cannot get into the project code to see how it’s done. I think the Editor link might be wrong? Or project set to private?

Fixed. I’m sure it was public when I created the project but looks like it wasn’t. It is now public.

Extra note: Although I used the model mesh here for the collider, I recommend creating a low poly version for the collider for performance reasons.