Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity custom camera view matrix in shader?

Discussion in 'Editor & General Support' started by valleeey, Apr 3, 2014.

  1. valleeey

    valleeey

    Joined:
    Oct 11, 2013
    Posts:
    27
    Hello community,

    I want to create a custom view matrix so that I can use it in my shader. My question is: how does unity set up the matrix?

    Playing around with Camera.worldToCameraMatrix makes me think that the matrix is set up like this:

    Code (csharp):
    1.  
    2.  
    3. float4x4 customViewMatrix2 ={   right.x,        right.y,        right.z,        -CamPos.x,
    4.                                 up.x,           up.y,           up.z,           -CamPos.y,
    5.                                 -forward.x,     -forward.y,     -forward.z,     CamPos.z,
    6.                                 0,              0,              0,              1};
    7.  
    8.  
    But the right column (CamPos) seems to be not completely right when you add orientation to the camera!

    Looking forward for help!! :D

    Thanks!
     
    Last edited: Apr 3, 2014
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  3. valleeey

    valleeey

    Joined:
    Oct 11, 2013
    Posts:
    27
    Hey Graham,

    thanks for your quick response.

    Sorry, I did'nt make it clear - I definitely need to create a custom view matrix.

    I need to render from different positions in one shader pass and I just have the camera positions stored in GPU memory. So that's the only (fast) solution, I guess.

    So please, when somebody knows how especially the last column of the matrix gets calculated, please tell me! :)
     
  4. valleeey

    valleeey

    Joined:
    Oct 11, 2013
    Posts:
    27
    noone? please, I need this for my bachelor thesis and can't figure it out myself! :/

    I need the last column of the matrix. When you have no rotation, the [-camPos.x,-camPos.y,camPos.z] is correct, but with rotation the values are changing.
     
  5. valleeey

    valleeey

    Joined:
    Oct 11, 2013
    Posts:
    27
    Finally I could figure it out myself.

    The Unity camera view matrix gets computed like this:

    Code (csharp):
    1.  
    2.  
    3. float4x4 customViewMatrix = {   right.x,        right.y,        right.z,        -CamPos.x*right.x - CamPos.y*right.y - CamPos.z*right.z,
    4.                                 up.x,           up.y,           up.z,           -CamPos.x*up.x - CamPos.y*up.y - CamPos.z*up.z,
    5.                                 -forward.x,     -forward.y,     -forward.z,     -CamPos.x*(-forward.x) - CamPos.y*(-forward.y) - CamPos.z*(-forward.z),
    6.                                 0,              0,              0,              1};
    7.  
    8.  
    The reason for this is surprisingly simple. It's because of the multiplication of two matrices. For more explanation see this link:
    http://schabby.de/view-matrix/

    Hope this'll help someone in the future.