Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Converting a World Space Sphere to a Screen Space Circle

Discussion in 'General Graphics' started by Watsuby, Nov 27, 2023.

  1. Watsuby

    Watsuby

    Joined:
    Jun 16, 2015
    Posts:
    9
    I have a sphere in the scene with position P_world and radius R_world.
    I want to be able to create a circle with position P_screen and radius R_screen.
    This is for a gameplay purpose like creating a 2D circle collider that matches the sphere.

    What I did so far:
    I know that I can convert P_world to screen space via Camera.WorldToScreenPoint.
    I don't know the math to convert a "World Space Length" into a "Screen Space Length".
    My best guess here is to do something where I find how far the sphere is away from the camera, use it as some ratio to scale down the radius?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,291
    You're describing a circle along a round cone, where the cone's point is at the camera A, and the cone touches the sphere of radius RS. This circle has a calculated radius RC and its distance from the camera is calculated as H.

    I found the thread below with the formulas you'd need to calculate RC and distance H.

    Once you have RC at distance H representing a sphere RS at distance D, you can calculate any other radius at any other distance, as they scale proportionally.

    https://stackoverflow.com/questions...-create-the-tangent-cone-from-point-to-sphere

     
  3. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    623
    A projected sphere is an ellipse, not a disk.
    If you are fine with conservative bounds, you can use its 2d polyhedral bounds as described in this paper by Michael Mara and Morgan McGuire:
    http://jcgt.org/published/0002/02/05/paper.pdf
     
  4. Watsuby

    Watsuby

    Joined:
    Jun 16, 2015
    Posts:
    9
    I didn't even think about that.

    So currently I am using a Perspective camera which would be A in the diagram. It creates a view fulcrum and the near plane of that view fulcrum is a plane somewhere along V. If I bring h to the distance between A and the near plane, I should be able to find rc which would be the radius of my circle.

    The only thing I'm not too clear about and maybe this is because I wasn't very clear in my question is... once I get rc wouldn't that be all in world units? I want to do some screen space manipulations with it and also have it interact with GUI elements. This would mean I would need them to be calculated in pixels? Is there some way to convert this into pixels?
     
  5. Watsuby

    Watsuby

    Joined:
    Jun 16, 2015
    Posts:
    9
    That makes sense and an exaggerated FOV makes it very obvious.

    Thanks for the paper, I'll check it out and see if the conservative bounds fit my needs.