- Home /
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.
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));
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.
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.
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;