- Home /
GL LoadProjectionMatrix and GL modelview does not change anything
I want to change a camera view using projection&model view matrix. I am using the code below. However, it does not change the view at all even if I run the scene. I am new in Unity. So I think I am misunderstanding something. If you know the solution, please help me out.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class configuration : MonoBehaviour
{
//public Material mat;
void OnPostRender() {
Matrix4x4 mv = new Matrix4x4();
mv[0, 0] = -0.52185F;
mv[0, 1] = -0.852863F;
mv[0, 2] = -0.0172527F;
mv[0, 3] = 659.578F;
mv[1, 0] = -0.368024F;
mv[1, 1] = 0.206849F;
mv[1, 2] = 0.906516F;
mv[1, 3] = 806.439F;
mv[2, 0] = -0.769565F;
mv[2, 1] = 0.479415F;
mv[2, 2] = -0.421818F;
mv[2, 3] = -2953.51F;
mv[3, 0] = -0;
mv[3, 1] = 0;
mv[3, 2] = -0;
mv[3, 3] = 1;
Matrix4x4 p = new Matrix4x4();
p[0, 0] = 2.2158F;
p[0, 1] = 0;
p[0, 2] = 0;
p[0, 3] = 0;
p[1, 0] = 0;
p[1, 1] = -2.2158F;
p[1, 2] = 0;
p[1, 3] = 0;
p[2, 0] = 0;
p[2, 1] = 0;
p[2, 2] = -1.46619F;
p[2, 3] = -2874.3F;
p[3, 0] = 0;
p[3, 1] = 0;
p[3, 2] = -1;
p[3, 3] = 0;
GL.PushMatrix();
GL.LoadProjectionMatrix(p);
GL.modelview =mv;
GL.PopMatrix();
}
void LateUpdate()
{
Camera cam = Camera.main;
OnPostRender ();
}
}
Answer by Bunny83 · Jan 04, 2018 at 11:20 AM
The GL class is for immediate manual rendering. All rendering parameters for "normal" rendering are managed by the Camera.
OnPostRender is a callback that is called by Unity. You should never manually call a callback that is driven by Unity. OnPostRender runs after the camera rendering has been completed.
Finally even if you actually want to render something manually you would need to render it here:
GL.PushMatrix();
GL.LoadProjectionMatrix(p);
GL.modelview =mv;
// render things here
GL.PopMatrix();
After calling PopMatrix you actually revert all changes to the matrix stack(s) and you restore the state that you pushed onto the stack previously.
It's not really clear what you want to do. If you want to change the projection matrix you may want to change the projectionMatrix of your camera. The modelview is calculated on the fly by Unity based on the transform of the objects and the transform of the camera.
Your answer
Follow this Question
Related Questions
Can you serialize custom camera projection matrices? 1 Answer
WorldToViewportPoint and ViewportToWorldPoint math 2 Answers
Possible to use an oblique frustum camera and still be able to change the fov? 1 Answer
Custom camera projection matrix of orthographic camera 1 Answer
Generating rays from cameras with custom projection matrices 1 Answer