Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reconstructing world space position from depth buffer (Orthographic camera)

Discussion in 'Image Effects' started by Kronnect, Mar 16, 2016.

  1. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,874
    Hi!

    I've been struggling with this with no luck and need some help.

    Has anyone managed to reproject a screen space coord in world space?

    The setup is only for orthographic camera. I'm trying to make some image effects work that use _CameraDepthTexture with orthographic projection.

    Any suggestions?

    Many thanks.
     
  2. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,874
    None? @Aras has this already been discussed? Perhaps you could point me towards some blog/thread? Thanks in advance.
     
  3. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,180
    Hey. I'm on vacation and I don't know off the top of my head. I don't think we provide API for this out of the box though. I'll get back asap.
     
    Passeridae likes this.
  4. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,874
    Well, I guess a tiny piece of API like that could open mighty doors to many.
     
  5. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    If your able to get the depth buffer values it shouldn't be that hard. This post will seem pretty long, but in code it'l be rather small ! :D
    You need:
    The cameras position, (world space)
    The cameras Width and height, (in world space).
    The cameras near and far plane.
    The cameras matrix.
    And obviously the depth buffer.
    With this as it's orthographic what you need to do is figure out where on the screen you are, (the projection matrix if a normal mesh, the uv- if a fullscreen effect), and you are guaranteed to have this anyways.
    Then you need to sample the depth buffer to get the distance to the cameras near plane. Convert it to linear space if it isn't already. Multiply by the difference between camera near and far. This gives you the distance from the camera. If your axis aligned this is enough. If not:
    Now you have a distance to the cameras near plane, and with the screen space co-ordinate you can get the x,y position, and multiply these by the width and height of the camera. So you have the position relative to the camera ("camera space").
    The simple solution here:
    Multiply by the inverse of the view matrix, (might be some other variation of the view matrix, such as transposed inverse, or the original), and you have your world space co-ordinates. (This will also handle scaling)
    Or (assuming no scaling on the camera)
    Offset the position with the cameras world space position.

    That should in theory re-create the world space position.
     
  6. Kronnect

    Kronnect

    Joined:
    Nov 16, 2014
    Posts:
    2,874
    Thanks for the reply.

    What it would be really cool is some sort of standard function that returns the world-space position of any screen-space pixel, being orthographic or in perspective mode! :)
     
  7. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Well, it would be great, but generally some tricks are used for perspective, making it trickier to re-create world space.