• 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 Joyetta · Sep 18, 2014 at 11:29 AM · zoom

Unity zoom to selected rectangle

I draw a rectangle on the screen alt text. Is there anybody know how to make the camera zoom to the selected area or make the selected area full of screen? Thank you so much!!!

and I tried as this: http://stackoverflow.com/questions/10545537/zoom-to-screen-space-selection-box-in-3d , but doesn't work very well.

draw rectangle.jpg (41.8 kB)
Comment
Add comment · Show 3
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 LearnUnity3d · Sep 18, 2014 at 12:09 PM 0
Share

What was the problem when you use that? Dharmesh

avatar image Slev · Sep 18, 2014 at 12:53 PM 0
Share

So there's two things here. You should be able to zoom in to fill that using some field of view and manipulation. However... the horizontal FOV changes based on the user's resolution. As such you would be wiser to get the zoom to fit the vertical length and then let the horizontal be what it will.

avatar image robertbu · Sep 18, 2014 at 12:57 PM 0
Share

I don't think a general solution for a 3D environment is possible, or if it is, it will likely involve some sort of fancy projection matrix.

Imagine the rectangle offset more than it is now, and there are three object at different distances in the exact center of that rectangle. Simply moving the camera with its current camera plane will only be able to center one of the three objects since as the camera is moved, the objects will go out of alignment. You would have to pick one object (or one distance), and allow the other object to go out of alignment. As an alternate, you could rotate the camera so that camera plane matched the normal between the different object.

But then you have problems with the zoo$$anonymous$$g. Typically zoo$$anonymous$$g is done by changing the field of view, so given objects at different distances, you will not get the same fra$$anonymous$$g as the rectangle. In addition, if you did the rotation to keep objects of different distances in the center, the rectangle projected on the new plane is no longer a rectangle.

There are lots for something that is difficult to describe, but you can duplicate the problem using a frame and your eyes in a real-world environment.

2 Replies

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

Answer by robertbu · Sep 18, 2014 at 04:08 PM

Here is a bit of code. My comments above still hold, so this is not a perfect solution. But it will rotate, translate, and zoom (by changing the FOV) to approximately the view specified by a screen rectangle. Note I'm assuming you are confining your selection rectangle to the aspect ratio of the screen. If you are not, there is more work to be done to figure out to figure out the correct zoom (since width rather than height can be a limiting factor for an arbitrary rectangle).

 // cam - camera to use
 // center - screen pixel center
 // pixelHeight - height of the rectangle in pixels
 // time - time to take zooming
 function ZoomToDisplay(cam : Camera, center : Vector3, pixelHeight : float, time : float) {
     var camTran = cam.transform;
     var ray = cam.ScreenPointToRay(center);
     var endRotation = Quaternion.LookRotation(ray.direction);
     var endPosition = ProjectPointOnPlane(camTran.forward, camTran.position, ray.origin);
     var opp = Mathf.Tan(cam.fieldOfView * 0.5 * Mathf.Deg2Rad);
     opp *= pixelHeight / Screen.height;
     var endFOV = Mathf.Atan(opp) * 2.0 * Mathf.Rad2Deg;
     
     var timer = 0.0;
     var startRotation = camTran.rotation;
     var startFOV = cam.fieldOfView;
     var startPosition = camTran.position;
     
     while (timer <= 1.0) {
         var t = Mathf.Sin(timer * Mathf.PI * 0.5);
         camTran.rotation = Quaternion.Slerp(startRotation, endRotation, t);
         camTran.position = Vector3.Lerp(startPosition, endPosition, t);
         cam.fieldOfView = Mathf.Lerp(startFOV, endFOV, t);
         timer += Time.deltaTime / time;
         yield;
     }
     
     camTran.rotation = endRotation;
     camTran.position = endPosition;
     cam.fieldOfView = endFOV;
 }
     
 
 function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
     planeNormal.Normalize();
     var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
     return point + planeNormal * distance;
 }    
Comment
Add comment · Show 6 · 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 Joyetta · Sep 19, 2014 at 02:57 AM 0
Share

It works!! Thanks buddy!!

avatar image Joyetta · Sep 19, 2014 at 10:33 AM 0
Share

I found out that objects will disappear when I look farther scene. Is there any way can zoom in/out by changing the camera position? Thanks!

avatar image robertbu · Sep 20, 2014 at 08:13 AM 0
Share

I don't see how you can make zoo$$anonymous$$g work. That is, when you move the camera, nearer objects become larger faster than objects further away. So when you zoom, the results will not be anything like the fra$$anonymous$$g. In addition, to make the math work, you have to select a distance from the camera for the rectangle, and there is no distance that makes sense for this problem. Here is a link to the math:

http://docs.unity3d.com/$$anonymous$$anual/FrustumSizeAtDistance.html

The problem you are running into are why I made my first comment. If you really need this, it might be possible to calculate some sort of custom camera projection, but that will distort the world.

avatar image Joyetta · Sep 21, 2014 at 01:01 PM 0
Share
  1. Here is how I make zoo$$anonymous$$g work. I call ZoomToDisplay in Input.Get$$anonymous$$ouseButtonUp(0) and let center be new Vector3((end.x + startPT.x) / 2f, (end.y + startPT.y) / 2f, (end.z + startPT.z) / 2f), startPT/end is the left up/bottom right point of the selected rectangle. pixelHeight/ Screen.height is replaced by $$anonymous$$athf.Abs(end.y - startPT.y)/Screen.height. I translate the code into C# because I'm not familiar with Java. Besides, I don't know the purpose between line 14 and line 25 and yield; is not allowed to use like this in C#. So I don't translate this part.

  2. I tried to zoom in by changing camera position in this way and I don't know how to calculate the dist.

    Ray ray = cam.ScreenPointToRay(center); Vector3 centerWorld = Camera.main.ScreenToWorldPoint(center); float dist = Vector3.Distance(center, ray.origin) - cam.nearClipPlane; float scale = $$anonymous$$athf.Abs(end.y - startPT.y) / Screen.height; cam.transform.position += cam.transform.TransformDirection(ray.direction dist scale);

Thanks!

avatar image robertbu · Sep 21, 2014 at 04:23 PM 0
Share

Here is an untested conversion of the above Javascript code to C#:

 // cam - camera to use
 // center - screen pixel center
 // pixelHeight - height of the rectangle in pixels
 // time - time to take zoo$$anonymous$$g
 IEnumerator ZoomToDisplay(Camera cam, Vector3 center, float pixelHeight, float time) {
     Transform camTran = cam.transform;
     Ray ray = cam.ScreenPointToRay(center);
     Quaternion endRotation = Quaternion.LookRotation(ray.direction);
     Vector3 endPosition = ProjectPointOnPlane(camTran.forward, camTran.position, ray.origin);
     float opp = $$anonymous$$athf.Tan(cam.fieldOfView * 0.5f * $$anonymous$$athf.Deg2Rad);
     opp *= pixelHeight / Screen.height;
     float endFOV = $$anonymous$$athf.Atan(opp) * 2.0f * $$anonymous$$athf.Rad2Deg;
     
     float timer = 0.0f;
     Quaternion startRotation = camTran.rotation;
     float startFOV = cam.fieldOfView;
     Vector3 startPosition = camTran.position;
     
     while (timer <= 1.0f) {
         float t = $$anonymous$$athf.Sin(timer * $$anonymous$$athf.PI * 0.5f);
         camTran.rotation = Quaternion.Slerp(startRotation, endRotation, t);
         camTran.position = Vector3.Lerp(startPosition, endPosition, t);
         cam.fieldOfView = $$anonymous$$athf.Lerp(startFOV, endFOV, t);
         timer += Time.deltaTime / time;
         yield return null;
     }
     
     camTran.rotation = endRotation;
     camTran.position = endPosition;
     cam.fieldOfView = endFOV;
 }
 
 
 Vector3 ProjectPointOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point) {
     planeNormal.Normalize();
     var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
     return point + planeNormal * distance;
 }   

You would use it by calling StartCoroutine().

I don't know the purpose between line 14 and line 25 and yield;

The purpose of this code is to do the zoo$$anonymous$$g over time, rather than immediately. As I've done in the translation, it is done using IEnumerator and calling the code using StartCoroutine().

I tried to zoom in by changing camera position in this way and I don't know how to calculate the dist.

That is the whole point in my last comment. There is no 'dist' that makes sense. Imagine you had three object in the selection rectangle: a rock 5 units in front of the camera, a tree 10 units in front, and a building at 20 units in front of the camera. You can get any one of those objects to appear as they do in the rectangle by using their distance to calculate the camera placement, but the other two will not scale to represent the rectangle. For example, imagine the tree is selected as the distance. Either the rock will not appear (since the distance calculation will place the camera behind the rock) or the rock would be huge in comparison to the way it appears in the rectangle. Because of the way a camera views a 3D world, I don't know of a way for you to get exactly what you want. That is the point of my original comment.

Show more comments
avatar image
0

Answer by bhartu · Sep 18, 2014 at 01:01 PM

http://www.theappguruz.com/sample-code/pinch-zoom-panning-unity/

hope this will help you..!!!

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

zooming in and out 1 Answer

Lens Effect 1 Answer

Changing zoom speed? 1 Answer

Zooming In To Close Loses the Object from view 1 Answer

RTS Camera - Zoom is not uniform, yet code is? 2 Answers


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