- Home /
How to get Unity's DOF effect with custom shaders?
Hi,
I'm using a custom shader for rendering certain types of objects, and I'd like to use Unity's built-in Depth of Field effect (scatter, i.e. the new one) in combination with it. The DOF effect works fine if I use the standard diffuse shader for my objects, but not with my custom shader. That is, every object seems to receive the same amount of blurring, regardless of the focal distance.
If I toggle the "visualize" option for the DOF effect, every object appears white with my shader. My fragment shader does update the depth buffer, though:
struct fragment_out {
float4 Color : COLOR0;
float depth : DEPTH;
};
fragment_out my_f_shader (v2p i) {
fragment_out OUT;
[…]
OUT.Color = color;
OUT.depth = depth;
return OUT;
}
It's a pretty complicated raycasting shader and I'm not the one who wrote it, nor do I fully understand it, but as far as I can tell from the rendering, this depth value seems to be correct. I wanted to get a sense of what these depth values where, and since I couldn't print them, I decided to color my objects with float4(depth, depth, depth, 1). I don't know if there's a simpler way to do that, but this is what I came up with.
Anyway, they all appeared white, which seemed strange since it implied a constant depth of 1, but when I raised the depth value to the power of 1000 before creating the color, I got a nice spectrum from almost black to almost white, with objects closest to the camera being darkest, and vice versa.
So at least the depth value is consistent. But why does the DOF effect fail to take it under account? Do I need to somehow make sure the depth is updated to somewhere else in Unity?
This is with OpenGL on MacOS, if that's relevant.
Any clue as to what might be going wrong would be much appreciated.
I'm surprised that no one has answered this yet. I figured this case would be pretty common and that I was probably missing something simple, or even obvious.
Anyway, if anyone was wondering, I'm still looking for a solution.
Sounds like you've already found the problem. The DOF post process is heavily dependent on the depth pass... $$anonymous$$akes sense right? So if you're shader is altering the depth pass, that's screwing up the DOF shader.
What is the custom shader doing exactly?
Does it need to be editing the depth value?
Can you replace the depth value with another value for output?
The shader is the problem, not the DOF effect. So that's what you need to understand in order to know how to fix it.
Hope this helps!
The shader in question does volumetric raycasting for objects defined analytically (by equations). Without editing the depth value, it can't seem to deter$$anonymous$$e the correct order in which objects are to be rendered.
That said, when the depth value is edited, everything is rendered in the right order, including other objects rendered with Unity's built-in shader. To me, this suggests that the depth value is correct, but I could be missing something. Since the DOF doesn't work, I probably am.
I'll try to find a way to compare the depth values written by the custom shader with those produced by the standard diffuse one to confirm this.
Thanks for your help!
Your answer
Follow this Question
Related Questions
Problems with depth values in _CameraDepthTexture 4 Answers
Dynamically alter shader parameters? 1 Answer
The meaning of the depth map value 1 Answer
Better shader effects on terrain - Bumpmapping or Specular? 2 Answers
Mesh z-fighting to itself 1 Answer