Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nimwe · Apr 10, 2018 at 06:04 PM · camerazoomfield of view

Mathematical relation between zoom and camera position

Greetings!

I am currently working on a project which requires me to use an immobile camera to zoom in on an object based on a formula which tells me at what distance that object looks fine on my camera. The problem arises the moment I want to modelize this concept, as I can only work with the camera FoV as a parameter, but i only have specifics on the optimal distance. Thus I ask whether there is a mathematical relation between FoV (zoom) and the "hypothetical" camera position you would need to have to obtain the same zooming effect without touching the FoV

Sorry if I wasn't clear enough, ask any question if you need to

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Apr 10, 2018 at 07:02 PM

the results won't be exactly the same due to the difference in perspective however this should get you some reasonable results:

 private Camera camera;
 
 private float originalFov;
 
 private float actualDistance;
 
 public float MimickDistance;
     
 void Start()
 {
     camera = this.GetComponent<Camera>();
     originalFov = camera.fieldOfView;
     actualDistance = 10; //this probably needs to be set to the distance of the front face of your object from the camera
 }
 
 void Update()
 {
     camera.fieldOfView = calculateFieldOfView(originalFov, actualDistance, MimickDistance);
 }
 
 private float calculateFieldOfView(float originalFov, float actualDistance, float mimickDistance)
 {
     float theta = originalFov * Mathf.Deg2Rad;
     float y = 2 * mimickDistance * Mathf.Tan(theta / 2);
     float fov = 2 * Mathf.Atan2(y, 2 * actualDistance);
     return fov * Mathf.Rad2Deg;
 }

You can see here in the first screenshot is the actual image, of a camera at a distance 5 from a cube, and in the second image is the camera at a distance 10 with a mimicked value of 5.

alt text


Why does this work?

Apparently I can't have more than two screenshots on the answer otherwise I would have added this there!

It comes down to some reasonably straight forward trigonometry but it is applied a little confusingly. The best thing we can try to achieve is 'change the field of view such that, object at a distance x are shown at the same size as moving the original camera to a distance y'.

So let's first look at how the field of view triangle would look if we could move the camera to where we wanted (the number '1' in the image below). We have our original field of view value a and we say that at distance x the 'size' of the view is y. This gives us a target of the shape of the new triangle we are going to create, that is we want at our new distance u, we also want the 'size' of the view to be y so y is the same in both the 1st and 2nd images.

We can then write two equations, one for each of the 2 triangles, the 3rd image gives some context to how these calculations come about:

  • y = 2*x*tan(a/2)

  • y = 2*u*tan(b/2)

Now we have values for a and x so we can find the value of y straight away, but our second equation needs rearranging so that we are solving for b.

  • y/(2*u) = tan(b/2)

  • atan(y/(2*u)) = b/2

  • 2*atan(y/(2*u)) = b

we have a value for y and u at this point, so substituting those values in gets an answer for b. The only change I made at this point is to use Unity's useful Atan2 method instead of Atan which means I split up the argument y/(2*u) into two separate arguments, so that Unity handles dividing by zero for us.

alt text


trianglemaths.png (58.0 kB)
test.png (201.4 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nimwe · Apr 10, 2018 at 10:35 PM 1
Share

Thank you for your exhaustive answer!

Would you care to explain the geometrical reasoning behind your calculateFieldOfView function? I've tried to come up with something similiar but failed miserably.

avatar image Scribe Nimwe · Apr 11, 2018 at 05:53 PM 0
Share

Sorry for the delay, but I've edited my answer with the 'why?'

avatar image Nimwe Scribe · Apr 14, 2018 at 03:52 PM 1
Share

Thank you very much for your clear explanation!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to know the relationship between zoom ratio and field of view 1 Answer

Camera zooming in and out 1 Answer

IPhone is zoomed in/out depending on which model 0 Answers

Hide gameObjects which are blocking camera's view 1 Answer

Camera Zoom problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges