I am drawing objects via the Graphics.DrawProcedural (from the void OnRenderObject() function) and I can see them on the screen, so far so good.
However, this drawing function seems to bypass Unity’s normal drawing behaviour, and I can’t see any changes made to the Camera’s internal depth buffer (only game objects drawn by Unity are visible in this depth buffer).
As a result I simply cannot use Post-processing effects such as SSAO or Depth-of-field right out of the box and without any work-around, which is annoying.
Did you guys ever face this issue before, or do you know a nice way to force Unity to render into the camera depth buffer ?
I haven’t used DrawProcedural personally, but reading through the documentation, I may have a few hunches.
First, what was the last thing the engine rendered? The documentation says that the DrawProcedural function will render to the current renderTarget, using the current shader pass. If the shader being used by your “OnRenderObject” object has multiple passes, it may not render the geometry the way you would expect. Try using Material.SetPass(0) to specify the diffuse pass manually. In my brief experiments, I found that meshes drawn with Graphics.DrawMeshNow came out pure white, unless I set the material pass.
My second (Far less likely and probably wrong) theory is that the wrong render-target is bound. The DrawProcedural function is executed immediately, meaning that it will bypass the majority of the rendering process. Unity does a lot of fancy multi-target rendering (especially when in deferred mode) which would no longer work when geometry is immediately drawn. Try calling Graphics.SetRenderTarget(), and supplying RenderTexture.colorBuffer, to write to the color buffer, and then make a second call to DrawProcedural with the RenderTexture.depthBuffer bound. This is a very messy solution, but if this is indeed the problem, it may be worth a shot.
Hey Andrew, I do not have any issues so far with the rendering, I set the right pass before drawing and everything is drawn to the correct render target, however Unity seems to be handling the camera depth buffer differently I have issues accessing that buffer specifically. I cannot bind it to a shader as 2DTexture sampler for instance, nor can’t it set this camera depth buffer as a render target.