Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice

How to make image effect shader work on transparent objects

Discussion in 'Shaders' started by imadddddd, Feb 15, 2018 at 1:04 AM.

  1. imadddddd

    imadddddd

    Joined:
    Dec 13, 2013
    Posts:
    12
    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:
    93
    It should work, unless youre using depth for edge detection. Transparent objects dont write to depth buffer
     
  3. imadddddd

    imadddddd

    Joined:
    Dec 13, 2013
    Posts:
    12
    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:
    93
    Transparent objects dont write to normals as well, the same reason they dont write to depth.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    3,895
    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. imadddddd

    imadddddd

    Joined:
    Dec 13, 2013
    Posts:
    12
    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:
    3,895
    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.