- Home /
How to write to Camera's Depth Buffer with a basic vertex and fragment shader?
Create the most basic, unlit shader: Project->Create->Shader->Unlit Shader. Create a material and set the shader you just created. Drag in a texture. Add a cube or plane to your scene, drag that material onto it. Everything is good so far, you should see a textured cube in your scene.
However, if you do:
float depth = -LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, input.texCoords));
that object is not in the camera's depth buffer.
There is no ZWrite line in that default shader, but docs say it on by default. Add ZWrite On anyway, no difference.
Nothing fancy with transparency here, the object is fully opaque. Add it to the Geometry queue (I think it is by default anyway):
Tags {"Queue" = "Geometry" "RenderType"="Opaque" }
Image effects like Depth-of-Field use that depth buffer. But for any vertex/fragment shaders I write, even with ZWrite On, are not getting included.
Does anyone know how to get a vert/frag program to write into the zbuffer in such a way that is included when sampling the camera depth texture?
Alternatively, is there some other depth buffer that has the depth of these vert/frag shaded objects? They're drawing in the correct order, depth is stored somewhere.
Thanks.
Anyone? How can I find the depth of pixels of the objects I just rendered with a vert/frag shader?
Adding Fallback "Diffuse" causes it to work. So from that I assume there is some pass being done that is not covered in the default code produced by Project->Create->Shader->Unlit Shader. Can anyone explain why adding Fallback "Diffuse" causes the depth to show up in SA$$anonymous$$PLE_DEPTH_TEXTURE(_CameraDepthTexture,.. ?
Hi, I think it's due to the fact that the Z pass uses a shader pass with the tag "ShadowCaster" (info here: http://docs.unity3d.com/$$anonymous$$anual/SL-RenderPipeline.html). Your shader does not have this pass/tag but the fallback one has it. To confirm, you could ask this question on the forum in the graphics/shader section, there may be more expert there. You may also be able to see that in the "frame debugger".
Thanks @dns, that helps. I'm starting to get my head around the Unity rendering pipeline now. Also thanks for the advice about checking out the forums.
I've gone further down the spiral now. Learned plenty about the rendering pipelines in the last 24 hours for sure. Now I've refined the question to be: How do I get the depth of objects rendered with Graphics.DrawProcedural in OnObjectRender()? The ShadowCaster pass does answer the specific question posed by this thread, so I've started a : more specific thread on the forums
Not really an answer, but I found this link that has a nice multi-part tutorial about the depth buffer. I haven;t had a chance to get through it yet but it seems very promising:
http://williamchyr.com/2013/11/unity-shaders-depth-and-normal-textures/
Thanks @rageingnonsense . I have seen that tutorial. It was indeed helpful trying to understand how to get and use the camera's depth texture, but not so much for shedding light on how that depth texture is generated in the first place. Was that an unintended pun? ... depth is created by the Unity shadowcaster pass ... never$$anonymous$$d. That tutorial is by the guy making $$anonymous$$anifold Garden btw.
Your answer
Follow this Question
Related Questions
Modifying the depth buffer of an image effect? 0 Answers
Intersection Highlight Shader -1 Answers
Setting depth buffer per-fragment 2 Answers
Find out if an object is in front of a target in fragment shader? 0 Answers
2D Water top surface profile 1 Answer