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 Skaltum · Mar 11, 2019 at 11:07 AM · matrixorthographic cameraprojection-matrix

How to set ortho projection / model view matrix without a camera?

I'm trying to create the correct projection and modelview matrix for rendering without a camera.

_orthoCamera is a camera I set up in the scene view. Using the _orthoCamera projection matrix and modelview matrix works but I don't know exactly how to recreate these. My attempt is the commented code.

 //not correct
     //var projection = Matrix4x4.Ortho(-512, 512, -512, 512, 0.3f, 1000);
     //var modelView = Matrix4x4.LookAt(new Vector3(512, 512, -5), new Vector3(512, 512, 0), Vector3.up); //inverse?
 
     //correct result
     var projection = _orthoCamera.projectionMatrix;
     var modelView = _orthoCamera.worldToCameraMatrix;
 
     GL.LoadProjectionMatrix(projection);
     GL.modelview = (modelView);

This is how I set the camera up in the scene. alt text

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
0
Best Answer

Answer by Skaltum · Mar 13, 2019 at 02:25 PM

Ended up going with this. Based on this documentation.

 var orthoSize = 512f;
 var projection = Matrix4x4.Ortho(-orthoSize, orthoSize, -orthoSize, orthoSize, 0.3f, 1000);
 var offset = new Vector3(512, 512, -5);
 var modelView = Matrix4x4.TRS(new Vector3(-offset.x, -offset.y, offset.z), Quaternion.identity, new Vector3(1,1,-1));


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
0

Answer by Bunny83 · Mar 11, 2019 at 12:58 PM

Why don't you just use a disabled camera? It has no overhead and does all the calculations for you. However if you want to create it manually, have a look over here. Setting up a projection matrix works the same regardless of the used engine.

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 Skaltum · Mar 11, 2019 at 01:14 PM 0
Share

I don't want to use a camera because I would have to move it around across scenes with different settings and positions. That adds a bunch of state I don't need, muddying the process.

From my understanding, my code should produce the same results. But they don't and I don't know why.

avatar image Bunny83 Skaltum · Mar 12, 2019 at 10:29 AM 1
Share

The "state" overhead of a camera is $$anonymous$$imal. I haven't tried it but i'm pretty sure that you can even use a camera on a prefab without instantiating it, in order to change / get it's settings.


Though if you really want to setup your matrices manually here are some mistakes in your code:

First off all your orthographic projection does not take care about the aspect ratio of the screen. Next your "camera matrix" is the wrong way round. So yes, you need to use the inverse. The camera matrix transforms from worldspace into the local space of the camera.


$$anonymous$$eep in $$anonymous$$d is that different platforms or rendering APIs uses different conventions internally (left handed / right handed system, inverted z, ...). As far as i remember the GL API of Unity does take care of those conversion. Just keep in $$anonymous$$d that those matrices may look different in the actual shader than what you've set.


This should work (though i can't test since i don't have a PC with Unity around)

 float orthoSize = 512f;
 float aspect = (float)Screen.width / Screen.height;
 var projection = $$anonymous$$atrix4x4.Ortho(-orthoSize * aspect, orthoSize * aspect, -orthoSize, orthoSize, 0.3f, 1000f);
 
 var modelView = $$anonymous$$atrix4x4.LookAt(new Vector3(512, 512, -5), new Vector3(512, 512, 0), Vector3.up).inverse;

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

101 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

Related Questions

Custom camera projection matrix of orthographic camera 1 Answer

Setting the camera projection matrix does nothing 0 Answers

Replicating the Minecraft Acid Mod 1 Answer

Change projection matrix for each objects 0 Answers

Can you serialize custom camera projection matrices? 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