Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Depth of Field and Transparency: a bug or just not possible yet?

Discussion in 'General Graphics' started by ripridehi, Jun 17, 2015.

  1. ripridehi

    ripridehi

    Joined:
    Mar 27, 2015
    Posts:
    15
    There are plenty of Answers and Forum posts about problems with Depth of Field and Transparency. Generally, the problem is that when a shader with transparency is used, it blurs based on the depth of the next opaque object behind it. So if it is in front of a very distant background, it will be blurred %100. If it is in front of an object at the focal point, it won't be blurred at all.

    I'm not a rendering expert, so I would like to know if this issue is viewed as a bug, or if it is a problem for which there simply isn't a good realtime solution available? In other words, is this a general graphics problem, or is it possible and there is just a problem with the Unity implementation?

    Has anyone had any success with transparency and depth-of-field in Unity 5?
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
    It's a general graphics problem. A pixel only stores one set of depth information, but you need more than one (because the depth determines how wide the blur is, and you want different widths for the distant opaque object as for the nearer transparent object, so you need different depths).

    Solutions do exist - e.g. by rendering the transparent object separately, blurring it separately, and then compositing it - but they're not good for performance and not easy to set up for the general case.
     
    theANMATOR2b likes this.
  3. MXAdd

    MXAdd

    Joined:
    Apr 23, 2015
    Posts:
    74
    I have problem with DOF to, maybe there is fix for this one:

    I render all my opaque geometry
    then I render all my particles ("Queue"="Transparent" to "Queue"="Transparent+10")
    then I render all my glass & windows shaders ("Queue"="Transparent+11" "RenderType"="Opaque")
    they should update '_CameraDepthTexture' right ?
    they do ... but they aparently ignore depth buffer while rendering the glass planes :/
    so I end up with '_CameraDepthTexture' that have distant glass planes rendered on top of the near ones
    the solution for this would be to actually USE ZBuffer while executing "Hidden/Camera-DepthNormalTexture"
    any way to do this ? (unity 5.1, PC, deferred rendering)
     
  4. MXAdd

    MXAdd

    Joined:
    Apr 23, 2015
    Posts:
    74
    BTW lurking into RenderDoc capture reveals that depth test & write is ON, but the actual depth buffer is not bound - this is probably not what unity authors intended :] (renderer == dx11)
     
  5. ripridehi

    ripridehi

    Joined:
    Mar 27, 2015
    Posts:
    15
    Thanks for clarifying that. So I guess for now, you can only use Depth of Field straight of the box in Unity only if you have no transparency in your scenes.
     
  6. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
    Or if you're willing to put up with DoF behaving incorrect for transparent surfaces - or if you're willing to do compositing or multi-camera tricks, yes. (The last of those is often possible - e.g. render only distant objects, blur them, then render only the near objects nice and sharply. It's a cheap PS2-style hack but it often works OK).
     
    forestrf likes this.