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

Question Extracting mesh rotation from unity_ObjectToWorld

Discussion in 'Shaders' started by magebuzzcutt, Feb 16, 2023.

  1. magebuzzcutt

    magebuzzcutt

    Joined:
    Jun 20, 2021
    Posts:
    16
    Hi,

    I am trying to get the rotation angle of the mesh while in the fragment shader. Currently, I've got the following code to try and get these values using the unity_ObjectToWorld which I've got redefined as UNITY_MATRIX_M.

    Code (CSharp):
    1. float objectRotation_y = degrees(asin(UNITY_MATRIX_M._m03));
    2. float objectRotation_z = degrees(asin(UNITY_MATRIX_M._m01))/(-1*cos(radians(objectRotation_y)));
    3. float objectRotation_x = degrees(-1*asin(UNITY_MATRIX_M._m12))/cos(radians(objectRotation_y));
    4.  
    Currently it the these variables are returning nothing at all which I figured out is something to do with the asin function where it only takes values between -PI/2 and PI/2. However when I go
    Code (CSharp):
    1. float objectRotation_y = degrees(asin(UNITY_MATRIX_M._m03 % PI/2));
    it still returns nothing unless I go above UNITY_MATRIX_M._m03 % PI/2.1.

    Am I going in the right direction to get the mesh rotation? I know I could easily get this information by either using a compute buffer or setting a material property via script. But I'm not sure which would be the most efficient. Also, is there some documentation about the value ranges of the elements in the unity_ObjectToWorld matrix?

    TIA
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Yep, those won’t get you anything remotely close to the values you want. Search Google for “matrix to Euler” and look for something that uses
    atan2()
    .
     
    magebuzzcutt likes this.
  3. magebuzzcutt

    magebuzzcutt

    Joined:
    Jun 20, 2021
    Posts:
    16
    thanks, I got it working now