Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Allow DoF with transparent objects? #95
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.
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.
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.
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
@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.
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.