Search Unity

intersection shader that doesn't pass through objects

Discussion in 'General Graphics' started by nuttolum, Jun 20, 2021.

  1. nuttolum

    nuttolum

    Joined:
    Apr 17, 2020
    Posts:
    4
    I need an intersection shader that only shows intersections that are actually visible to the camera, not ones behind other objects. every intersection shader ive tried either shows intersections through objects or shows on screen overlaps instead of actual geometry intersections. I've never written a shader before so I honestly have no clue where to even start
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    All generalized "intersection" shaders aren't actually showing intersection at all. They're only showing the screen space depth between a transparent surface and the single opaque surface visible behind it in the same pixel. It ends up working well enough as an approximation for showing "intersection" by people inverting the depth difference so that the shader gets brighter / more opaque as the depth difference tends toward zero, but it's dependant on the shape of the surface using the shader and the visible opaque surface.

    Some more complex setups might be able to test several points in the depth texture around each pixel position and see if the current triangle's plane is visible at all checked positions. This would be more akin to what some post process outline shaders do, and may do what you need, but if you need to highlight intersections with a line wider than a single pixel or two, this probably isn't the solution you want. These kinds of setups still won't give you 100% what you want as there's no way to know with perfect accuracy if an "edge" is from an actual intersection, or if there's just an opaque surface closer to the camera than the test position.

    And this is ultimately the limitation any shader based approaches will have. The camera depth texture only has a single depth value per pixel, and you cannot differentiate between an intersection and an overlap.

    Really, if you want true intersections, and only true intersections, you'd need to calculate those in c# or a compute shader by testing each triangle in your "intersection shape" and some other set of geometry, and generate a mesh line of some kind to draw along the intersection you want to highlight. To limit to visible intersections you would either need to also check for occlusion when building the mesh, or use a custom shader on the line mesh that lets it check the depth texture at the actual intersection point to see if it's occluded or not.
     
    mbrownie825 likes this.