Search Unity

World position from depth

Discussion in 'Shaders' started by mgeorgoulopoulos, Sep 17, 2012.

  1. mgeorgoulopoulos

    mgeorgoulopoulos

    Joined:
    Aug 24, 2009
    Posts:
    66
    Hello,

    I am trying to get the world position of a pixel by reconstructing it from depth. I have tried different approaches, but the latest one that I found is the following (which doesn't seem to work):

    Code (csharp):
    1. [vert shader]
    2. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    3. o.uv = v.texcoord;
    4.  
    5. o.projPos = ComputeScreenPos(o.pos);
    6. o.ray = mul(UNITY_MATRIX_MV, v.vertex).xyz * float3(-1,-1,1);
    7. o.ray = lerp(o.ray, v.texcoord, v.texcoord.z != 0);
    8.  
    9. [frag shader]
    10. i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
    11. float2 uv = i.projPos.xy / i.projPos.w;
    12.  
    13. float depth = tex2D(_CameraDepthTexture, uv).r;
    14. depth = Linear01Depth(depth);
    15. float4 vpos = float4(i.ray * depth, 1);
    16. float3 wpos = mul(_CameraToWorld, vpos).xyz;
    has anybody done it before?

    Thanks a lot :)
     
    ModLunar likes this.
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Search the forums first, its been answered several times.
     
  3. mgeorgoulopoulos

    mgeorgoulopoulos

    Joined:
    Aug 24, 2009
    Posts:
    66
    I've searched, that is how I got the above solution.

    Unfortunately it seems to return view-space position, or simply the _CameraToWorld does not work as expected.
     
  4. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,761
    If you get it figured out please do post the solution, I tried a year or so back to get it working and failed, and everyone who posted in response to my pleas for help couldn't get it to work either, we need Aras to step in and show us the light on this one.
     
    ModLunar likes this.
  5. mgeorgoulopoulos

    mgeorgoulopoulos

    Joined:
    Aug 24, 2009
    Posts:
    66
    The funny thing is that I did the exact thing for my deferred renderer some months ago. I never experienced such an issue, which is why I cannot solve the problem, the math seem correct :(

    Spooky, I also read your threads about it. Let's hope we find a solution :)
     
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    _ViewProjectInverse calculated in a script:
    If you have very far camera distance, you will have precision problems.

    EDIT: For opengl, you must invert the uvCoord.y
     
  7. mgeorgoulopoulos

    mgeorgoulopoulos

    Joined:
    Aug 24, 2009
    Posts:
    66
    Thanks aubergine,

    however I do not get the same result as with getting the world position from the vertex shader. The colours seem correct, however they move around with the camera (I do not see the world-space "color cross" that I get with the vertex shader approach).

    Maybe it is my uvs? I am using ComputeScreenPos(o.pos) to get the sampling texcoords.
     
    Last edited: Sep 18, 2012
  8. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    You are using this for an image effect right? If so, uvcoords is just the v.texcoord.xy of a screen quad.
     
  9. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
  10. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    109
    forestrf and KenAtGlu like this.
  11. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    wow this is amazing!
    I'm looking for this!
    how did you make this?
    did you use simple projector?
     
    Last edited: Apr 9, 2019
  12. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    109
    Thanks)

    I used a camera. It renders the depth when I call for it. Also it sets it's projection matrix and some other parameters as global parameters. Here is a link to that script:

    https://github.com/quizcanners/Tool...inter/Texture Scripts/DepthProjectorCamera.cs

    And here is the test shader:

    https://github.com/quizcanners/Tool...Shaders/PlaytimePainter_Test_Projector.shader

    It was a bit less confusing before I started to optimize. I moved parts of the code to .cginclude file to share between a multiple shaders, which makes it harder to follow.

    Not sure if everything is 100% correct though, made it with lots of trial and error. Also it didn't work for orthogonal projection as I remember.
     
  13. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,932
    @quizcanners all your links are now dead. Did you delete them? It's a shame you didn't copy/paste them into the forum, instead of linking off site.
     
  14. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    985
  15. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Last edited: Oct 3, 2019
    AlejUb likes this.
  16. AlejUb

    AlejUb

    Joined:
    Mar 11, 2019
    Posts:
    25
    That Keijiro sample needs a hard sticky or something.
    The way he calculates the view Ray is great, all in the shader, perspective and orthographic alike.
    Thanks a lot for the share.
     
  17. Chen_Can_PW

    Chen_Can_PW

    Joined:
    Apr 28, 2021
    Posts:
    1
    wrong
     
  18. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    109
    Hi. I changed the folder structure at some point. The link to repository is:
    https://github.com/quizcanners/Playtime-Painter

    Link to the camera (currently):
    https://github.com/quizcanners/Playtime-Painter/tree/master/Painter Core/Components

    The Shader code you need is in:
    https://github.com/quizcanners/Playtime-Painter/blob/master/Shaders/PlaytimePainter_cg.cginc

    Can't promise that I will not change the structure again, but repository will not be deleted.

    By the way, I do feel like there are simpler and more correct ways to do this. I can't remamber the exact problem, but for some reason I couldn't get the right result with recommended functions (GetEyeDepth or something like that). So consider other posts before consider mine.
     
    a436t4ataf likes this.