Search Unity

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

Resolved Ajust FOV to fit objects bounds?

Discussion in 'Scripting' started by bisewski, Nov 1, 2023.

  1. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    274
    Hi!..
    I have a camera orbit that zoom I do from FOV and works really good.

    I have a button in scene called "back" to back to an anterior state.

    Is possible to calculare the FOV to fit the objects bounds in scene?

    I already calculate the boundaries to center camera. But I also want to try to fit the zoom.

    I saw some examples online where they adjust distance, transform position that isn't my case.

    Thank you!
     
    Last edited: Nov 1, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,072
    Probably but I think Cinemachine has something like that built in or at least has the scripting Api for checking target bounds.
     
    bisewski likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,847
    Well, it depends on how precise you want that. Getting a precise zoom so the object really just touches the borders of the camera can be very tricky, especially when the objects have unusual shapes. For a precise frame you would need to determine the angle from the center to each vertex of the object and take the largest one. This has to be done for both, the horizontal and vertical axis. Then, taking the aspect ratio into account, determine which one is the larger angle and use that as the fov. There are other ways by only considering the AABB (aka Bounds) of the object. However that is generally larger than the actual object, so the object will appear smaller. Depending on the shape and orientation of the object this could make a huge difference.

    A middle ground would be to define a "sphere" that should be focused. So you could temporarily add a sphere as a child, position and scale it so it represents that object nicely in most situations and then just remember that sphere center and radius. With those you could calculate the necessary fov to frame the sphere from the current camera position. That would simplify the situation heavily. The math is extremely easy.
     
    Last edited: Nov 1, 2023
    adamgolden, Nad_B and bisewski like this.
  4. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    274

    Math is easy hum? hahaha

    I got the b value but now can you help me to implement all the others functions in a script?

    Thanks
     

    Attached Files:

  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,847
    That value is all you actually need. I put all irrelevant stuff in that folder that is just there to visualize the fov with the two lines and display the target circle.

    Code (CSharp):
    1. public float objectRadius = 3f; // set in inspector
    2. public Transform obj; // set in inspector
    3.  
    4. // [ ... ]
    5. float dist = Vector3.Distance(obj.position, Camera.main.transform.position);
    6. float fov = Mathf.Asin( objectRadius / dist )*Mathf.Rad2Deg*2f;
    7. Camera.main.fieldOfView = fov;
    I would have used more meaningful variable names in the desmos drawing, but variables can only be single characters, math, you know ^^.
     
    bisewski and adamgolden like this.
  6. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    316
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,295
    Honestly I've just seen games set a minimum distance that objects can be orbited and call it a day.

    EVE Online does this. There's very clearly just a defined radius at which is the minimum distance that you can orbit your camera around an object.
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,847
    Yes, I've seen games that try to be smart about the camera zoom and framing (Space engineers) but it horribly fails at unusual builds. Like a very long station (space elevator for example). The game tries to frame the whole thing which is completely useless as I'm not interested in seeing my station from 3km distance ^^. Though the camera has limits so it just sticks at the furtherst possible point and you can not zoom in at all.
     
    bisewski and spiney199 like this.
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,295
    Calling yourself out here? ;P Or did someone else work on the camera?
     
    bisewski likes this.
  10. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,847
    :D No, I was talking about Space Engineers. It's like minecraft but in space. You can actually build space crafts or stations with a voxel system and it doesn't really have a limit how large you can build. Just recently I actually drilled into the center of the moon (about 9km deep) and build an actual elevator shaft to the center :) Currently I'm about half the way out the other side. When you're docked to a station, the third person orbit camera will include the station you're docked to.
     
    bisewski likes this.
  11. bisewski

    bisewski

    Joined:
    Jan 16, 2014
    Posts:
    274
    Holly, works perfectly...That is impressive...Very simple...E forgot to implement *Mathf.Rad2Deg*2f.

    Really thank you...
     
    Bunny83 likes this.
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,295
    Oh, I thought you had mentioned in the past you worked on the game. Or am I remembering wrong there?
     
    bisewski likes this.