Search Unity

How to draw edge intersections between two meshes with shader graph?

Discussion in 'Shaders' started by sam_hastings, Sep 17, 2021.

  1. sam_hastings

    sam_hastings

    Joined:
    Oct 6, 2019
    Posts:
    1
    I'm new to making shaders and I've been searching for quite a while, but I can't seem to find what I need. There are many tutorials for intersections with objects, and I used those to get to where I am now. upload_2021-9-17_17-23-27.png
    upload_2021-9-17_17-24-39.png
    Now my problem is, that the edge is getting drawn on the invisible object when I want it drawn on the visible one.
    In my game, though, the invisible object will be a child object of the player (so they will be inside of it), and it needs to stay invisible while also getting detected by the shader on the walls and stuff that draws the intersection. It doesn't work anymore though when I make it a transparent type.

    Here is my shader graph:
    upload_2021-9-17_17-30-20.png
     
    Last edited: Sep 18, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    There's no way to do what you want with the technique you're trying to use.

    The "edge intersection" shader isn't really detecting intersections. It's comparing the current surface's depth (sort of the distance from the camera) to a depth value in the camera depth texture (what the Scene Depth node using to get a depth value). The camera depth texture is created by rendering only the depth to a texture for all opaque objects, and only the closest opaque surface per pixel. That means you can only have transparent objects detecting if they're close to the closest opaque object it in that one pixel. Opaque objects can't use the camera depth texture because the closest surface is itself, and it can't see how close it is to transparent objects in the scene because they're not in the camera depth texture at all.

    This technique literally only works if the "glow" is only on the transparent object.

    The only way to do what you want would require you to render your own custom depth map of just the invisible object, and set that as a texture for the shader to sample. To do that requires writing a custom render pass in c#. That or have 3D SDFs of your invisible shapes that you test against.