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
3
Question by Chris Ellingsworth · Dec 06, 2010 at 09:32 PM · cameraviewport

Is it possible to shift the camera viewport?

Hey,

What I'm wanting to know is if it's possible to shift the camera viewport so I can have GUI elements on the left side and the center of the camera is rendering right-of-center on the screen?

Thanks,

Chris

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

3 Replies

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

Answer by Eric5h5 · Dec 06, 2010 at 10:20 PM

Use the following script :

 function SetVanishingPoint (cam : Camera, perspectiveOffset : Vector2) {
     var m = cam.projectionMatrix;
     var w = 2*cam.nearClipPlane/m.m00;
     var h = 2*cam.nearClipPlane/m.m11;
  
     var left = -w/2 - perspectiveOffset.x;
     var right = left+w;
     var bottom = -h/2 - perspectiveOffset.y;
     var top = bottom+h;
  
     cam.projectionMatrix = PerspectiveOffCenter(left, right, bottom, top, cam.nearClipPlane, cam.farClipPlane);
 }
  
 static function PerspectiveOffCenter (
     left : float, right : float,
     bottom : float, top : float,
     near : float, far : float ) : Matrix4x4
 {
     var x =  (2.0 * near)        / (right - left);
     var y =  (2.0 * near)        / (top - bottom);
     var a =  (right + left)        / (right - left);
     var b =  (top + bottom)        / (top - bottom);
     var c = -(far + near)        / (far - near);
     var d = -(2.0 * far * near) / (far - near);
     var e = -1.0;
  
     var m : Matrix4x4;
     m[0,0] =   x;  m[0,1] = 0.0;  m[0,2] = a;   m[0,3] = 0.0;
     m[1,0] = 0.0;  m[1,1] =   y;  m[1,2] = b;   m[1,3] = 0.0;
     m[2,0] = 0.0;  m[2,1] = 0.0;  m[2,2] = c;   m[2,3] =   d;
     m[3,0] = 0.0;  m[3,1] = 0.0;  m[3,2] = e;   m[3,3] = 0.0;
     return m;
 }

Call OffsetVanishingPoint by passing a camera and a Vector2 containing the x and y amounts that you want shifted compared to the default vanishing point. The Vector2 is an absolute offset, not a relative offset. A sample usage snippet:

 // Do a pan from left to right
 var panSpeed = .15;
 var panLimit = .1;
  
 function Start () {
     var t = 0.0;
     while (t < 1.0) {
         t += Time.deltaTime * panSpeed;
         var x = Mathf.Lerp(panLimit, -panLimit, t);
         SetVanishingPoint(camera.main, Vector2(x, 0.0));
         yield;
     }
 }

Be careful, as the docs say, using projectionMatrix will make the camera no longer update its rendering based on its fieldOfView, so using this function will make the camera stick at whatever FOV it had before you used it. If you want to change the FOV after using this function, you have to call ResetProjectionMatrix first.

Link : http://wiki.unity3d.com/index.php?title=OffsetVanishingPoint

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 Chris Ellingsworth · Dec 06, 2010 at 10:52 PM 0
Share

Awesome, that worked splendidly. I ended up doing this to get it to offset a targeted object to the right side of the viewport.

SetVanishingPoint(camera.main, Vector2(0.1, 0.0));

avatar image sstulov · Jun 19, 2013 at 05:04 PM 0
Share

What are the measurement units of this perspective offset? Are they pixels of the screen? Are they somewhere in between [0, 1]. Or are they in some other intermediate coordinates?

avatar image sumeetkhobare sstulov · Sep 14, 2013 at 03:06 PM 0
Share

I guess its [0,1] with it being float value.

avatar image SteelRaven7 sstulov · Mar 01, 2019 at 01:58 PM 0
Share

I stumbled upon this really old thread trying to offset the vanishing point of a full-screen camera by a specific number of pixels. After some analysis I came up with the following transformation from pixels to the perspectiveOffset vector:

 Vector2 pixelOffset; // Your pixel offset lives here
 Camera camera; // This is your camera
 
 float scale = (camera.nearClipPlane*2 * $$anonymous$$athf.Tan((camera.fieldOfView/2) * $$anonymous$$athf.Deg2Rad)) / Screen.height;
 Vector2 perspectiveOffset = pixelOffset*scale;
 
 SetVanishingPoint(camera, perspectiveOffset);

I hope this helps someone even though the thread is old!

avatar image
0
Best Answer

Answer by Chris Ellingsworth · Jan 25, 2011 at 04:54 PM

After messing around with the previous script, there was a problem in the web player when the aspect ratio changed where the camera wouldn't adjust its perspective properly. Objects would be squashed or too thin.

I ended up implementing this and now it works as intended: http://unity3d.com/support/documentation/ScriptReference/Camera-worldToCameraMatrix.html

I wasn't clear on that before and wanted to post this answer in case it helps someone in the future.

So now my camera can shift its viewport, even animate the shift when certain gui elements are on the screen. It also properly displays the perspective when the web player is resized.

Thanks!

Chris

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
avatar image
1

Answer by Statement · Dec 06, 2010 at 09:43 PM

You can use the camera normalized view port rect on the camera to set a portion of the screen to render there. I don't know how you can shift the center without avoiding gaps, but if it is acceptable to render something else in the other rect with another camera then it's a possible way to go.

Comment
Add comment · Show 2 · 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 Statement · Dec 06, 2010 at 09:44 PM 0
Share

$$anonymous$$aybe you can do some funny trick as offsetting the camera with a parent node that allows you to "angle in" on the intended view.

avatar image Chris Ellingsworth · Dec 06, 2010 at 10:21 PM 0
Share

Thanks for the input, I tried that and am getting it close. I can't seem to get rid of what amounts to be a pixel or two difference between the viewports. I'm wondering if there's another way or if I need to keep experimenting?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

possible to set rotation of camera viewport? 1 Answer

camera or viewport 2 Answers

Attach 3d object to screen 1 Answer

Changing camera aspect ratio with non-fullscreen viewport 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