Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DoF with transparent objects? #95

durnurd opened this issue Feb 13, 2017 · 3 comments


None yet
4 participants
Copy link

commented Feb 13, 2017

When showing a transparent object at a distance that should be in focus, it is still not in focus. I've attempted to write the shape to the z-buffer manually before the DoF effect is applied, but I couldn't get it to work. I know that this would not correctly blur the view behind the transparent object, but for my use case this is not a problem.

Including a custom shader and/or MonoBehavior to apply to transparent objects that should still be in focus when at the correct distance would be nice.


This comment has been minimized.

Copy link

commented Feb 14, 2017

Depth of field makes use of the depth buffer. Transparent objects can't write to depth. So there are two ways of dealing with transparent objects when it comes to depth of field.

  1. Render DoF before transparent objects
  2. Render DoF after transparent objects

In the first case, transparent objects will always be in focus no matter what their distance is as they will be essentially be drawn on top of the DoF effect output. In practice, this looks pretty bad when used with particles and even worse when dealing with things like glass panels. So, like most games do nowadays, we went with the second method: apply DoF after transparent objects have been rendered so they can at least inherit some of the blur from DoF, even though it doesn't always look correct.

I know that this would not correctly blur the view behind the transparent object, but for my use case this is not a problem.

Here's a quick shader I wrote that does just that (for some reason it only works in play mode for me). Unity uses the ShadowCaster pass to write to the _CameraDepthTexture that DoF makes use of. You should be able to extend it to add textures etc.

Here's how it looks with an exaggerated DoF (the red quad in the middle is transparent and writes to depth while being in focus):

@Chman Chman closed this Feb 14, 2017


This comment has been minimized.

Copy link

commented Jun 7, 2018

You can try this tool to make your transparent objects write to z-buffer:


This comment has been minimized.

Copy link

commented Jul 13, 2018

@Chman Should your shader still work correctly with the latest post processing package (2.0.8)? In my experiments it either renders its depth to the CameraDepthTexture or its color to the scene, but not both. I set up a simple scene with a background and two cubes. The left cube has a standard diffuse shader. For the right cube I used your shader.

This is how it looks like if the right shader is set to Transparent:

And this is how it looks like set to Geometry:

I would like the right cube (that has a Transparent shader) to write its depth into the depth buffer and be affected by the DoF effect as if it was opaque.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.