Search Unity

Depth of Field bug with alpha channels

Discussion in 'Editor & General Support' started by CrowbarSka, Feb 23, 2011.

  1. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    I logged a bug with Unity over a month ago and I've heard nothing since then, despite there being an update to the DoF effect with Unity 3.2.

    Basically the problem is that any material with an alpha channel transparency is not rendered correctly when using Depth of Field. Instead of blurring appropriately for its distance from the camera it inherits the blurriness of whatever is being rendered behind it. So any particles, for example, which are against the skybox are being rendered at 100% blurriness, and then they become sharp again when they pass in front of nearby objects.

    Has anyone else experienced this or know a fix?
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    This is apparently to do with the fact that transparent objects don't write to the depth buffer (ie, whatever is behind the transparent object still has to be rendered for the transparency to be noticeable). It is unlikely that there will be a general fix for this but there may be something you can do, depending on what is happening in the game. Can you describe what is going on in the scene and what role the transparent objects play?
     
  3. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Sure. My game is viewed from a 2D side perspective using parallax layers of 2D geometry, a little bit like Braid. The transparent object make up the majority of my foreground and background scenery, composed of squares with a simple rock silhouette texture applied. There are also lots of particle effects going on in the foreground emitted constantly from characters and floating objects.
     
  4. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
  5. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
    In 2.6.1 I "fixed" it happening to me with a transparent/distorted/reflective shader, by changing the shader to opaque or something along those lines.

    I say "fixed" because while it appeared to work fine relatively ok, and would blur correctly at distances, anything seen through it wasn't effected by the depth of field, and it's highly possible there were other issues involved that I didn't spot at the time (i is a newb at messing with shaders).

    I suppose you could always try using different camera's, and only have DOF on the camera showing things in the distance. It's probably far from perfect and might cause other problems though?
     
  6. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    The first solution probably wouldn't be suitable because I need to see other objects behind the layers. If you imagine the silhouetted outline of a rocky landscape, there will be more 'layers' of silhouettes behind it giving the impression of depth.

    I will have a look into using multiple cameras but this feels like a really awkward work-around for what seems like a simple task. I assumed that using alpha channels was quite common and would be supported by DoF.

    andeeee: Why do you doubt there will be a fix?
     
  7. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
    You mentioned 2D in a previous post. Couldn't you fake the DOF for distant stuff with pre-blurred textures? Or wouldn't that work for what you're doing?
     
  8. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Good idea. It might work but I can see potential problems where I'm overlapping tiles with blurred edges. I'll have a play around at the weekend. Thanks!
     
  9. arioch82

    arioch82

    Joined:
    Dec 3, 2009
    Posts:
    253
    i'm interested in this too, please let me know if you will find any fix... i've a bit of research a while ago but I couldn't find anything.
    maybe using a particular shader instead of the normal transparent one? (I'm not a shader programmer)
     
  10. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
    Assuming you're going along the route of 2D art (like Max and the Magic Marker) rather than 2D style but 3D objects. You should be fine with overlapping objects and their transparency in the blurred parts. I guess the only problem would be if you want to have your player moving in and out of the screen (or even just the camera) as the blur would remain the same. But yeah other than that, if you've got that alpha sorting I think it is? Then they should all appear just fine.

    Plus it would work on a lot more systems than the DOF image shader, and might be perfectly acceptable on the iPhone and Android too being just blurred images and nothing more.


    If you do work something out with the 3D side of things, let us know cause I'd be very curious myself for a possible fix, lol.
     
  11. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    Thanks for the help. I had a fiddle and created a more blurred version for distant objects. This looks good and has the effect I'm looking for, but it has lead me to a slightly different problem.

    I was previously using the Transparent/Cutout/Diffuse shader to which was giving me a nicely defined edge to my silhouette backgrounds. However this shader doesn't seem to support alpha channels. So I tried Transparent/Diffuse instead, but now I get odd an 'popping' visual bug on objects with this shader applied when they are near the edges of the screen. It looks like they suddenly turn paler, then back to dark again when they come closer to the centre of the screen again.

    It looks like this solution could work for me, but not unless I fix this new problem!

    Any ideas on which shader I should be using for 2D silhouettes with alpha channels?
     
  12. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
    Hmm, I had a mess around to see if I could duplicate that popping effect but couldn't. Though did notice that Transparent/Cutout/Soft Edged Unlit does seem to give the soft edged benefits of regular transparent shaders, but also those of the cutout ones, if you move the slider to the far right. no shadows from it and it is unlit of course, so don't know if that will be any good to you.

    By the looks of it though just a regular Transparent shader should be fine. While I can't say for sure, they do seem to behave nicer than in 2.6.1, or maybe that's just Deferred helping in that case. If you've got sharp edges though, stick with the cutout shaders, except the soft edge one, the others will give you shadows, and should all display at the correct depths when lined up. The faked DOF elements you'll have to use the regular transparency shaders, no shadows, but at those distances nobody would notice (and you can always fake the shadows too if you needed to).
     
  13. CrowbarSka

    CrowbarSka

    Joined:
    Dec 15, 2009
    Posts:
    192
    I'm wondering if it's a Z-depth issue, as it mainly occurs with two overlapping planes.

    I can't really get the look I want with Transparent/Cutout/Soft Edged Unlit because it's rendering the edges differently. I lose the accuracy of the alpha transparency values and Unity doesn't seem to be calculating the edges in quite the same way. I'm basically stuck using Cutout which is really not good for finer details.
     
  14. caseym

    caseym

    Joined:
    Nov 29, 2012
    Posts:
    2
    This still seems to be a bug in Unity 4.0, anyone found a work around?
     
  15. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    I'm looking for solutions too.
     
  16. timothyjoelwrigh

    timothyjoelwrigh

    Joined:
    Feb 16, 2013
    Posts:
    3
    Last edited: Jul 31, 2014
  17. hippysniper

    hippysniper

    Joined:
    May 9, 2014
    Posts:
    5
    Old post but i think i should post an "answer" / "Solution"

    may not be what your after in your particulare project but unity 5 has semi fixed this issue in the standard cutout texture in unity 5.

    however it will impose the blur from the standard texture onto any other "non-standard" texture type, eg if you mix sprites and standard textures togeather then you will have a world of problems. However if you change all texture types to standard textures you should be able to use depth of field with no issue.
     
    vfxjex likes this.
  18. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    no it didnt, still as bad as it was
     
  19. Pawl

    Pawl

    Joined:
    Jun 23, 2013
    Posts:
    113
    Six years later, we encountered this same issue with DoF and transparent objects. We were using NGUI with a billboarded text label positioned in 3D space over our player character. We ended up converting the 3d world position to 2d viewport coordinates and render the text with our UI camera, which renders _after_ the primary DoF camera, so the transparent text appears crisp without artifacts.

    This can be accomplished using WorldToViewportPoint and ViewportToWorldPoint, or if you're using NGUI, it's wrapped in NGUIMath.cs OverlayPosition().

    2017-06-21_1434.png
     
    Last edited: Jun 21, 2017