Retrieving information about collisions from depth testing / stencil test


I have a 3D universe full of spheres and ellipsoids. I fire a laser into this universe and, with depth testing / stencil testing, can see the laser correctly renders until it hits something.

Now I want to do something graphical with the point where the laser hits another object (an explosion for instance).

I can do some nice linear algebra to find intersections of lines in 3D with spheres and ellipsoids which is a bit complex but can be done. However, before I roll out the masses of matrix multiplications required it strikes me (sorry for the pun) that my shaders / GPU may already have the answer as they are correctly depth testing / stencil testing and not rendering the laser beyond any object it hits.

Is there a way to obtain the result of the depth test / stencil test result for my laser hitting an object ? whereby I get the object the laser hit, or the position on the object it or any relevant information about the “collision” which would enable me to identify the object hit and position it was hit ?

Sorry for the long question and hope it makes sense.


1 Like

I wonder if you could do something similar to what we’ve been talking about with recognising which object is selected by touch / click.

If you rendered your objects with unique colors to a texture, and then worked out in screen space where your laser is, and what the color of the texture is at that point?

The thread is here:

Thanks Caroline - its kind of the route I am taking - I was just curious if there was a way to directly tap the already computed depth test result which has clearly figured out that my laser hit something already without any additional computation

You can read the depth texture in the same way as for shadows. I’m not sure that would help you determine which object you hit though?