Search Unity

Issue when trying to convert position by matrix

Discussion in 'Scripting' started by AnKOu, Jun 18, 2019.

  1. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    Hello,

    Here is an image of what I am trying to do :
    C is like a camera with a frustum projected onto the wall.

    My goal is to find the matrix which transform the world space point A in C local space by applying the projection.

    My code looks like this :

    Code (CSharp):
    1.  
    2. Matrix4x4 l_Proj = Matrix4x4.Perspective(40f, 1f, m_Distance, m_MaxDistance);
    3. Matrix4x4 l_GLProj = GL.GetGPUProjectionMatrix(l_Proj,false);
    4. Matrix4x4 l_WorldToFrustum = l_GLProj * C.worldToLocalMatrix;
    5.  
    6. Vector3 B = l_WorldToFrustum.MultiplyPoint(A);
    7. B.z = 0;
    8.  
    I must find the matrix because I use it in a shader later.

    I was expecting to retreive local position roughly the size of my violet square but my computed local points, such as B, are way too far from the point C compared to where A is located.

    My question is : where do I mess up ? I'm not sure of the matrix order when multiplying them, I tried invert the order without success...
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Why don't you just use transfrom.worldToLocalMatrix?
     
  3. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    Because it does not take into account the projection.
    If I use only this matrix on my four world space points, my local points will form a trapeze. I want that my four world space points form a local square.

    As you see, my four blue dot related by dotted lines form a square. But not of the good size.
     
  4. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123

    Here is an exemple when I only use C.worldToLocalMatrix

    Code (CSharp):
    1. Matrix4x4 l_WorldToFrustum =  C.worldToLocalMatrix;
    2. Vector3 B = l_WorldToFrustum.MultiplyPoint(A);
    3. B.z = 0;
    I have a trapeze but i want a square...
     
  5. AnKOu

    AnKOu

    Joined:
    Aug 30, 2013
    Posts:
    123
    Well... turns out this was in fact correct since my coord for each corner are ~ (1,1), (-1,-1), (-1,1), (1,-1).
    Ahah... silly me...