Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by temptest123 · Nov 25, 2013 at 07:24 PM · animationcameraorthographicperspectiveprojection-matrix

fluent animation from orthographic to perspective projection

Unity Editor has this neat little feature to switch between orthographic and perspective projection with a fluent, natural animation (by clicking on the axis of the viewport cube).

How can I implement this effect in a custom application?

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

2 Replies

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

Answer by Roland1234 · Nov 25, 2013 at 09:01 PM

Even though the question has been asked before and given an answer, I thought I'd post a solution anyways (apologies if that's against guidelines, I saw nothing against it in the FAQ).

 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class CameraProjectionChange : MonoBehaviour
 {
     public float ProjectionChangeTime = 0.5f;
     public bool ChangeProjection = false;
 
     private bool _changing = false;
     private float _currentT = 0.0f;
 
     private void Update()
     {
         if(_changing)
         {
             ChangeProjection = false;
         }
         else if(ChangeProjection)
         {
             _changing = true;
             _currentT = 0.0f;
         }
     }
 
     private void LateUpdate()
     {
         if(!_changing)
         {
             return;
         }
 
         var currentlyOrthographic = camera.orthographic;
         Matrix4x4 orthoMat, persMat;
         if(currentlyOrthographic)
         {
             orthoMat = camera.projectionMatrix;
 
             camera.orthographic = false;
             camera.ResetProjectionMatrix();
             persMat = camera.projectionMatrix;
         }
         else
         {
             persMat = camera.projectionMatrix;
 
             camera.orthographic = true;
             camera.ResetProjectionMatrix();
             orthoMat = camera.projectionMatrix;
         }
         camera.orthographic = currentlyOrthographic;
 
         _currentT += (Time.deltaTime / ProjectionChangeTime);
         if(_currentT < 1.0f)
         {
             if(currentlyOrthographic)
             {
                 camera.projectionMatrix = MatrixLerp(orthoMat, persMat, _currentT * _currentT);
             }
             else
             {
                 camera.projectionMatrix = MatrixLerp(persMat, orthoMat, Mathf.Sqrt(_currentT));
             }
         }
         else
         {
             _changing = false;
             camera.orthographic = !currentlyOrthographic;
             camera.ResetProjectionMatrix();
         }
     }
 
     private Matrix4x4 MatrixLerp(Matrix4x4 from, Matrix4x4 to, float t)
     {
         t = Mathf.Clamp(t, 0.0f, 1.0f);
         var newMatrix = new Matrix4x4();
         newMatrix.SetRow(0, Vector4.Lerp(from.GetRow(0), to.GetRow(0), t));
         newMatrix.SetRow(1, Vector4.Lerp(from.GetRow(1), to.GetRow(1), t));
         newMatrix.SetRow(2, Vector4.Lerp(from.GetRow(2), to.GetRow(2), t));
         newMatrix.SetRow(3, Vector4.Lerp(from.GetRow(3), to.GetRow(3), t));
         return newMatrix;
     }
 }

Surprisingly, linear interpolation between matrices seems to do the trick.

Comment
Add comment · Show 3 · 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 temptest123 · Nov 28, 2013 at 09:16 AM 0
Share

I see that you square the t parameter when you transit to perspective, and you squareroot it when you transit to orthographic, but this does not seem to be the full truth as it doesn't yield a smooth, linear transition to me (even though better than t is used directly) - can you mathematically reason about this? Also, how to properly change/animate the position of the camera? Since the position does not matter for orthographic projection, but matters for perspective projection, the current code gives only a nice transition if the camera distance to the object is in a certain relation to the orthographicSize of the orthographic camera

avatar image Roland1234 · Nov 30, 2013 at 10:48 PM 0
Share

can you mathematically reason about this?

$$anonymous$$athematically, not quite - but geometrically I believe the effect is due to things that are further away from the camera beco$$anonymous$$g more greatly distorted as the view frustum goes from pyramid shape to box than things that are closer.

After spending some time analyzing the editor camera I had to conclude that their approach is fundamentally different - you are absolutely correct in noticing that the camera's position is changing. The editor camera doesn't interpolate the projection matrices, it fakes an orthogonal projection by using a very small FOV from a great distance, and transitions the change by increasing the FOV while decreasing the distance about a target position (which is the origin by default and set to an object's position when you double-click it in the hierarchy pane).

With that in $$anonymous$$d I've attached an implementation which looks to effectively mimic the editor functionality. Just be sure to change the file extension to .cs.

Setting a focus point is optional; the script will default to the origin if none is set. Let me know if this is what you were looking for!

cameraprojectionchangedeux.txt (4.0 kB)
avatar image _eternal · Jul 18, 2016 at 07:19 PM 0
Share

Thanks for posting this solution! Sorry to necro, but how does the ProjectionChangeTime float work? It doesn't seem to correspond directly to seconds. For example, this is what I get when I set ProjectionChangeTime to 2, yet it looks like the transition takes maybe 0.5 seconds: https://gfycat.com/HospitableDisastrousAardwolf

avatar image
0

Answer by iwaldrop · Nov 25, 2013 at 07:56 PM

This question has already been answered, here:

http://answers.unity3d.com/questions/58306/transition-a-camera-from-ortographic-to-perspectiv.html

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

overlay perspective on orthographic camera 2 Answers

Click and Drag Camera only works in Orthographic camera 1 Answer

the default camera makes everything look like its moving towards the center? 2 Answers

Orthographic objects follow in perspective 2 Answers

Convert 2D Camera to 3D for my Gameplay UI Setup 0 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