Search Unity

How to make image effect shader work on transparent objects

Discussion in 'Shaders' started by Deleted User, Feb 15, 2018.

  1. Deleted User

    Deleted User

    Guest

    I made an edge detection shader and attached it to a camera with a script, but it's not drawing edges on transparent objects, I know it's not supposed to work by default.

    Is there any way to make my image effect work on transparent objects ?
     
  2. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    It should work, unless youre using depth for edge detection. Transparent objects dont write to depth buffer
     
  3. Deleted User

    Deleted User

    Guest

    I'm using depth and normal, one of them should work but it's not the case
     
  4. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Transparent objects dont write to normals as well, the same reason they dont write to depth.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Transparent objects don't write to the depth texture.

    The short version is because Unity only renders objects with an opaque queue (2500 or less) to the depth texture or depth normals texture. Transparent objects thus aren't accounted for in image effects that make use of those textures.

    The longer version is the depth and depth normals textures can only hold one set of values per pixel, so only the closest surface will be stored. If you have a semi transparent object rendering to the depth they would prevent objects behind them from having their values stored in the texture. That means if you have an object that's 1% visible, it would have an outline, but nothing "behind" it would.

    If you want outlines on transparent objects you'll need to use an alpha tested material, or handle outlines manually.
     
  6. Deleted User

    Deleted User

    Guest

    Thanks for the answer !

    edit: is it possible to hack transparent objects rendering queue while keeping them transparent ? Like tricking unity into thinking that they are opaque ?
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Yes, you can just change the queue on the material or shader, though the shader needs a shadowcaster pass as that's what's used to render the depth. Most unlit shaders do not have this pass as the assumption is it does not need to cast shadows or be in the depth texture.

    But I suspect this won't give you the results you want even if you do get it working.
     
  8. theghostronaut

    theghostronaut

    Joined:
    Jul 27, 2018
    Posts:
    41
    Could you or anyone explain how to do this in URP?