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

Resolved Math Behind : ScreenToViewportPoint ?

Discussion in 'Scripting' started by tassarho, Jul 8, 2022.

  1. tassarho

    tassarho

    Joined:
    Aug 25, 2019
    Posts:
    64
    After hours of searching i can't find how is calculated :
    camera.ScreenToViewportPoint(Vector3 point);

    i tried to tweak "WorldToViewportPoint" explain in this post : https://forum.unity.com/threads/camera-worldtoviewportpoint-math.644383/#post-4319056

    but it seems the method to calculate world to viewport is far more different than screen to viewport, i'm on my knees.. i need to know the math behind in order to use it inside job (and make is burstable).

    Can someone post the calculation of "camera.ScreenToViewportPoint(Vector3 point)" ? :oops:
     
  2. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    following
     
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    is it not just dividing the screen point by the screens width and height in pixels.

    screen points range is from x: 0 to Screen.width and y: 0 to Screen.height viewport space is 0f to 1f for both axis.
     
  4. tassarho

    tassarho

    Joined:
    Aug 25, 2019
    Posts:
    64
    Yes, i read about this and try different variant but none give me the right result..

    Code (CSharp):
    1. //point = Mouse.current.position.ReadValue();
    2.             float x = point.x / playerCamera.pixelWidth;
    3.             float y = point.y / playerCamera.pixelHeight;
    4.             return normalize(new float2(x,y));
    Code (CSharp):
    1. //point = Mouse.current.position.ReadValue();
    2.             float x = point.x / Screen.Width;
    3.             float y = point.y / Screen.Height;
    4.             return normalize(new float2(x,y));
     
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    why are you normalizing, its not a directional vector it's a point.
     
  6. tassarho

    tassarho

    Joined:
    Aug 25, 2019
    Posts:
    64
    OMG! it was that dumb!? i read "normalize point" in this tutorial (https://codesaying.com/understanding-screen-point-world-point-and-viewport-point-in-unity3d/) and assume i have to normalize the result xD, thanks! it works now!:)

    for the futur reader, the final result is:

    Code (CSharp):
    1. private float2 ScreenToViewport(float2 point, int pixelWidth, int pixelHeight)
    2.         {
    3.             float x = point.x / pixelWidth; //given by camera.pixelWidth
    4.             float y = point.y / pixelHeight; //given by camera.pixelHeight
    5.             return new float2(x,y);
    6.         }
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    normalizing is really only used when your vector is representing a direction, and not a point in space. its making the vector have a length of 1, so say you normalized a float2 of (1, 1) that would give you (0.5, 0.5) so still same direction but only a length of one now.