Search Unity

【Shader】How To Convert a worldspace Position to ScreenPos(like Camera.WorldToViewportPoint )

Discussion in 'Shaders' started by luosiri, Sep 28, 2018.

  1. luosiri

    luosiri

    Joined:
    Dec 19, 2013
    Posts:
    11
    I'm trying to write SSR shader use GBuffer, and use 3D raymarching by world normal and depth,but I dont know how to convert a world pos to ScreenPos , somebody help me
     
  2. Pakillottk

    Pakillottk

    Joined:
    Dec 30, 2012
    Posts:
    11
    Try this in your vertex program:
    Code (csharp):
    1. o.pos = mul(UNITY_MATRIX_MVP, v.vertex); //get the transformed vertex position
    2. o.screenPos= ComputeScreenPos (o.pos);
     
    yyylny likes this.
  3. luosiri

    luosiri

    Joined:
    Dec 19, 2013
    Posts:
    11
    thank you ,but not this , its image effect shader, and got a world pos ,Want to know where it is on screen
     
  4. Pakillottk

    Pakillottk

    Joined:
    Dec 30, 2012
    Posts:
    11
    I see, maybe you can do something similar to this:


    In the video, you can see that he maps world positions into the screen. He does it by storing the worldPos of the four corners of the screen and then he gets in each fragment an interpolated world position for it. I guess you could do something like that and use the corner positions to get UV coords of the screen.
     
    Last edited: Sep 28, 2018
  5. luosiri

    luosiri

    Joined:
    Dec 19, 2013
    Posts:
    11
    Yes , I see , but this might not work in VR mode , the reason why I try to create mine SSR is wants it work correctly in VR mode. anyway ,thank you : )
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    For the answer to the question of how to convert a world position to a screen position, @Pakillottk ’s answer is close. That’s converting from a particular mesh’s object space to screen space, but that first function is transforming from object to world, then world to view, and finally view to clip space. You just need to do the same thing, minus the first transform. All of the relevant transform variables are listed here:
    https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html

    However, if that’s the question you’re stuck on already, you’ve got a long road ahead of you to get SSR going, VR or otherwise. Also, the method used in the above shader should work perfectly fine in VR.
     
    Deleted User and neoshaman like this.
  7. luosiri

    luosiri

    Joined:
    Dec 19, 2013
    Posts:
    11
    current result of : depthToWorldPos > WorldPosToScreenPos .. but it actually is dirction
     

    Attached Files:

  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    That looks like normalized clip space, not screen space. You'll need to post your code if you want any more specific help.
     
  9. luosiri

    luosiri

    Joined:
    Dec 19, 2013
    Posts:
    11
    My code changed , I'm still work on , maybe later, thank you very much ,but I have a question about my vector projection: Is this correctly?
     

    Attached Files:

  10. luosiri

    luosiri

    Joined:
    Dec 19, 2013
    Posts:
    11
    I solved this problem !!! so smart !
    Code (CSharp):
    1.             fixed2 WorldToScreenPos(fixed3 pos){
    2.                 pos = normalize(pos - _WorldSpaceCameraPos)*(_ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y))+_WorldSpaceCameraPos;
    3.                 fixed2 uv =0;
    4.                 fixed3 toCam = mul(unity_WorldToCamera, pos);
    5.                 fixed camPosZ = toCam.z;
    6.                 fixed height = 2 * camPosZ / unity_CameraProjection._m11;
    7.                 fixed width = _ScreenParams.x / _ScreenParams.y * height;
    8.                 uv.x = (toCam.x + width / 2)/width;
    9.                 uv.y = (toCam.y + height / 2)/height;
    10.                 return uv;
    11.             }
     
  11. songsongsongsong

    songsongsongsong

    Joined:
    Mar 6, 2019
    Posts:
    15
    @luosiri
    brilliant and thanks you.
    could you explain the snippet you post in detail?
     
    Stinger2121 and orangetech like this.
  12. KiraSnow

    KiraSnow

    Joined:
    Feb 22, 2021
    Posts:
    21
    i found the right way to use it is to divide it's result by it's w