Search Unity

Question Invisible Opaque Shader

Discussion in 'Shader Graph' started by Auregc, Mar 27, 2024.

  1. Auregc

    Auregc

    Joined:
    Apr 6, 2019
    Posts:
    3
    Hello everyone,

    I am planning on creating something similar to a Magnetic Field Sheet. Where the player walks around with a sheet and if it gets close to a magnetic object, it displays a sort of pattern at a certain location.
    At first, I was thinking of having a transparent shader be the sheet and using a depth texture to see things it intersects And have the "magnetic pattern" just be an invisible object.

    Since transparent objects don't write to the depth texture, is there a way to have an invisible opaque shader? Something invisible through the camera, but still writes depth?

    Or how else could I approach this?

    Thank you all for your time in advance.
     

    Attached Files:

  2. dskeie3

    dskeie3

    Joined:
    Oct 4, 2022
    Posts:
    1
    Im no expert but a few different ideas come to mind.

    1. You could make an opaque dithering shader to make a fake representation of transparency, while still rendering to the depth.
    2. In HDRP you can enable a pre-pass check on transparent shaders, making it so the material writes to a new separate depth buffer for transparent objects. This causes the transparent material to appear as you would expect, but it hides objects behind it like opaque materials do.
    3. Im not 100% sure on what your intended visual outcome is, but I wanted to simply mention that even if you use a transparent shader that wont write its material to the depth buffer (if youre using URP or Built-in) you can still detect where your material is intersecting with the depth buffers opaque materials.

    Hope some of this is helpful to you!
     
  3. Auregc

    Auregc

    Joined:
    Apr 6, 2019
    Posts:
    3
    Thank you Dskeie3,
    I tried both 1 and 2 but I didn't have any luck. I don't know how to do the 3rd one. I'm new to shaders.
    This is what have for the magnetic shader.

    And my approach was to use this intersection to simulate the magnetic fields using transparent objects.
    Screenshot 2024-03-27 131611.png
    And the intended look is like this Screenshot 2024-03-27 132000.png
     

    Attached Files:

  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    I don't know how this green magnetic sheet works, but it looks like it has some more complicated logic in it, so you would need to render objects in some special way to separate texture (different pass) and use that texture (sample) in material you want (the one with green sheet). This is fairy complex and requires you to have different models most likely, or at least specialized textures.

    The other way is to simulate those magnetic patterns using depth texture, however it's not exactly possible to reproduce the same look, you could try to make it look kind of similar using some edge detection on depth, maybe with some blur or something, but for sure it will not look exactly like you want.
     
  5. Auregc

    Auregc

    Joined:
    Apr 6, 2019
    Posts:
    3
    Thank you Qriva,
    That is what I was trying to accomplish, in the second part.
    I am new to shaders so from what I understood in the tutorials I have been seeing is that in a transparent material, I access the depth map and then use that to create a LERP of colors through the transparent material.

    It looks great when opaque objects intersect it. I was thinking of creating a ripple-looking plane. Make that object transparent and then the player walks around with the "Magnetic Sheet" and it should reveal that magnetic pattern (ripple-like plane).
     
  6. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    Nothing stops you to make shader that writes only to depth, however take into account that other object might be occluded by them because of that. You could try to fix it with queue order.

    Well it all depends on what are your needs, in my opinion either you fake it with depth and depth normals, or you make it with additional texture coming from separate pass where you render other objects separately, or the same objects, but with different shader.