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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't reproduce projection or view matrix in script

Discussion in 'Shaders' started by kingofmyworld324, Mar 9, 2017.

  1. kingofmyworld324

    kingofmyworld324

    Joined:
    Sep 24, 2013
    Posts:
    10
    Hello, I'm trying to make a shadow map but I'm stuck on getting the light source's projection and view matrices.
    It makes no sense because I manually create the matrices, apply them to the camera, pass them to the shader, and then they do not match.

    I create the matrices then apply them to the camera and shader like so:

    Code (CSharp):
    1. sCam.worldToCameraMatrix = lightViewMatrix;
    2.             sCam.projectionMatrix = projectionMatrix;
    3.  
    4.             shadowMapMaterial.SetMatrix("_viewMatrix", lightViewMatrix);
    5.             shadowMapMaterial.SetMatrix("_projectionMatrix", projectionMatrix);
    But when I compare them in the shader then are not equal to their built in counterparts:

    Code (CSharp):
    1. fixed4 frag (v2f i) : SV_Target
    2.             {
    3.                 if (i.uv.x < .5)
    4.                 {
    5.                     if (_viewMatrix[0].x == UNITY_MATRIX_V[0].x)
    6.                         return fixed4(0, 1, 0, 1);
    7.                     else
    8.                         return fixed4(1, 0, 0, 1);
    9.                 }
    10.                 else
    11.                 {
    12.                     if (_projectionMatrix[0].x == UNITY_MATRIX_P[0].x)
    13.                         return fixed4(0, 1, 0, 1);
    14.                     else
    15.                         return fixed4(1, 0, 0, 1);
    16.                 }
    17.             }
    I have also tried using GL.GetGPUProjectionMatrix for the projection matrix but that doesn't work either.

    It is necessary for me to get the exact UNITY_MATRIX_V and UNITY_MATRIX_P from this camera because I need to use them later in another shader on another camera.

    Any help would be appreciated.
     
    Last edited: Mar 9, 2017
  2. kingofmyworld324

    kingofmyworld324

    Joined:
    Sep 24, 2013
    Posts:
    10
    I think I solved it. I was able to make them match when applying them to a replacement shader. I guess screen materials use different matrices.