Search Unity

Projection Mapping Matrix4x4 to replace camera projection matrix. Just replace cam.projMatrix?

Discussion in 'General Graphics' started by bontempos, Jun 23, 2019.

  1. bontempos

    bontempos

    Joined:
    Nov 28, 2012
    Posts:
    4
    Hello. I have a Linear regression matrix built from correspondences between 3D world points and the position they can be found in the Screen coord system.

    I can multiply a 3D point to this matrix and I can confirm it outputs x,y values where it would be represented on the Screen coord system.

    Now, I am not sure how to apply this matrix to the pipeline correctly.
    Following some examples like this, I am just trying to apply this custom matrix to the camera.projectionMatrix

    cam.projectionMatrix =  getViewProjectionMatrix ();


    This is not working, and I can see, on Scene window, the camera frustum is messed up, plus not displaying world correctly.

    I am not sure if I should multiply something else to this custom matrix or its just applying it to the camera.

    THANKS
     
    Last edited: Jun 24, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The camera's projection matrix needs to be just the projection matrix and the view matrix comes from the camera's transform. Part of the reason for this is the projection matrix set on the camera is expected to be an OpenGL projection matrix. If you're using anything but OpenGL for rendering (currently only Android and Linux, though those both will likely soon be Vulkan by default) then the projection matrix needs to be transformed from from OpenGL to Direct3D or a similar form. If the view matrix is pre-applied, then I suspect that transformation doesn't work properly. If you can, construct the projection matrix and the view matrix separately, and apply the projection matrix to the camera and the view matrix to the camera's transform as a position and rotation.

    You could construct your projection matrices as a Direct3D in c#, but you still can't set it on the camera since it will always try to convert the projection matrix. The other option would be to pass in your matrix as a custom global property and use custom shaders to use your custom matrix instead of the built in ones. You may also need to use command buffers to render objects, as objects placed in the scene will be frustum culled if they're not in the view of the existing camera (based on the projection and view matrices it uses by default).
     
  3. bontempos

    bontempos

    Joined:
    Nov 28, 2012
    Posts:
    4
    Thanks for the reply!
    I might be wrong, but I've been convinced decomposing a view-projection matrix is a bit complicated, no? (multi-solution)

    I am trying to use the same logic I used in a sketch from Processing long ago, where, in the end, I use push and pop matrix to encapsulate the code where I apply this custom matrix to the current model matrix. Pretty much as it follows, I was in the hope something like the code below could be of any help:

    if (useProjection == true) {
    GL.PushMatrix();
    GL.LoadPixelMatrix ();
    GL.MultMatrix ( getViewProjectionMatrix () );

    //the line below draws correctly:
    GL.Color(Color.yellow);
    GL.Begin(GL.LINES);
    GL.Vertex3(5, -5, -5);
    GL.Vertex3(5, 5, -5);

    //is there a way I can render everything from scene here, under this matrix transform?

    GL.PopMatrix();
    }

    I am using this attached to the camera, OnPostRender(), just to evaluate the matrix itself. So far I know I might have to put this piece of code inside another method in case it could be ever useful. I need a little more material to study when you refer to a custom global property or command buffers if some one could indicate me.
     
    Last edited: Jun 25, 2019
  4. bontempos

    bontempos

    Joined:
    Nov 28, 2012
    Posts:
    4
    Hi, if anybody there can still help.

    I am still stuck to this problem. This is how I am obtaining my Camera Matrix (which is the View and Projection product).

    is it possible to decompose this specific matrix (the way it is obtained) to View and Projection separately to be using in Unity?