Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is it possible to affect the Camera depth buffer via Graphics.DrawProcedural ?

Discussion in 'General Graphics' started by matmuze, Jan 7, 2015.

  1. matmuze

    matmuze

    Joined:
    Jan 14, 2014
    Posts:
    27
    Hi there,

    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 ?

    Best regards,
    Mat.
     
  2. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    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.

    Hope that helps,
    -Andrew
     
  3. matmuze

    matmuze

    Joined:
    Jan 14, 2014
    Posts:
    27
    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.
     
  4. maxwelldoggums

    maxwelldoggums

    Joined:
    Sep 8, 2008
    Posts:
    157
    Hmm... I'm not really sure then!
    What shader are you using for your material? Does it perform a depth-write?
     
  5. paddy9614

    paddy9614

    Joined:
    Nov 25, 2014
    Posts:
    5
    Do you have found a solution?