Search Unity

View matrix explanation

Discussion in 'General Graphics' started by MikeyJY, Nov 15, 2021.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    What is the difference between view matrix and camera transformation matrix?
    The view matrix is referred:


    However this is also a formula for view matrix I have seen:


    First thing I notice is that the top 4x3 part is identical. The difference is that the bottom row has some vector computation in the second image, where in the first it has the camera's position. And the direction vectors of the top part of the matrix are altered by the parameters eye, at, and up.

    I think the fist matrix is for a particular camera matrix where the camera isn't rotated, just positioned. The second matrix is the matrix we need to rotate the view of the camera, is that correct?

    For example the lookat function that all API have use the parameters lookat(eye, at, up) by the formulas in the second image to create a view matrix that rotates the view and the camera. The first matrix image is the camera view that hasn't been rotated, it is like I would use the lookat with this parameters: lookat(new Vector3(0, 0, -2), Vector3.Zero, Vector3.Up) where the target is the center of the world so no camera rotation involved. So even if the lookat use the calculation in the second image, the fact that I don't use a rotation makes the result matrix to be similar to the first image where the bottom row is filled with the camera position and the dot products aren't doing anything because I set the at parameter to Vector3.zero. And for the direction vectors the same thing. Because the at is (0, 0, 0) the calcuation at the top of the second image aren't doing anything because of the target being Vector3.Zero the camera isn't rotated and the direction vectors of the camera are the global direction vectors: right(1, 0, 0); forward(0, 0, 1); up(0, 1, 0);

    The second thing I want to verify if I understand correctly is that the second image of the view matrix I attached is the inverse of the product of a translation matrix applied to a camera and a roation matrix applied to a camera:
    Quaternion rotation = Quaternion.LookRotation(at-eye);
    Matrix4x4.Translate(0, 0, -2) * Matrix4x4.Rotate(rotation)
    is the inverse of the product equal to the view matrix generated by the second image I attached?
     
  2. Tartiflette

    Tartiflette

    Joined:
    Apr 10, 2015
    Posts:
    84
    So what people call the view matrix can be either the camera transform matrix, or its inverse. Or maybe it's just me being extremely inconsistent, which is why I usually prefer giving them more explicit names such as CameraToWorld or WorldToCamera.
    The first formula you have describes what your transform should look like, and it's not only for unrotated cameras. Basically the first 3 vectors tell you which way the camera is pointing (i.e. Right is always pointing to the right of the camera) which gives you the rotation, and the final vector tells you where in space. What this formula doesn't tell you is how to build it. Position is trivial, but those 3 axes are more complicated and you usually have to combine a few rotation matrices together.
    The second formula is simply one way of building a matrix if you have a position and a target. However, I think the result isn't a simple matrix with a clear 3x3 rotation and position vector. I believe that rather than being a single transform, it's two transforms combined: first a rotation transform given by xaxis, yaxis, and zaxis, but then also a further translation backwards on the camera's local z axis (those dot products are actually a sneaky matrix*vector to find how much to move along the local axis).

    My math knowledge is a bit hazy so it's all to be taking with a pinch of salt :)
     
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    So the first one tells the position and the rotation by specifying the camera directions and the second one does this by rotation along a target vector so
    by using the first matrix and specifying that the right vector is (0, 1, 0) and the up vector is (0, 0, -1) that means that the camera's right is pointing upwards and camera's up is poiting backwards so the forward must be pointing leftwards. It is like setting the at parameter to the (-1, 0, 0) and the up parameter to (0, 0, -1), it would generate the same matrix.


    The WorldToCamera is the actual view matrix? I mean the MVP matrix is
    Model Matrix * View Matrix * Projection Matrix or localToWorld * worldToCamera * cameraToClip
    It wouldn't make sense to multiply localToWorld * cameraToWorld * cameraToClip
    The 2 matrices I attached are both worldToCamera, correct? Then how does cameraToWorld look?
    And if I understand correctly
    worldToCamera is the view matrix, the one that takes the vertex's global location and converting it to be relative to camera's positon
    cameraToWorld is the camera transformation matrix which is worldToCamera^(-1) which is sometime refered also as view matrix. And what is cameraToWorld for. I don't see any reason to convert a vertex position from camera space back to world space. It should be serving to position the camera, but why do I need a separate matrix to position the camera if the actual view matrix or worldToCamera actually position and rotates the camera for me?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442