|
I'm attempting to convert a Matrix4x4 which is being currently being applied to a camera using camera.worldToCameraMatrix and works fine. I'm able to get the position out with:
But at the moment can't get the quaternion to come out. I've pulled some code from a matrix and quaternion FAQ I stumbled across and have partially implemented, but at the moment its not behaving.
If I turn off the camera.worldToCameraMatrix, and rely on the Vector and Quaternion I get out (ignoring cases where it gets to the 'more to calculate here') the position of the camera looks fine, but its rotation is way off.
(comments are locked)
|
|
For getting a quaternion from a Matrix4x4, this function works:
For getting the position, GetColumn(3) works fine. Thanks! It should be noted that if you're attempting to convert a valid worldToCameraMatrix to a quaternion rotation you'll need to adjust for the reversed z on camera and call something like: Camera.main.transform.rotation = Quaternion.QuaternionFromMatrix(matrixToSet.inverse * Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1,1,-1)));
Feb 14 '10 at 11:29 PM
Leigh
such a great solution, thank you Rune!
Jan 30 '11 at 06:12 PM
3Duaun
What about the inverse operation? Obtaining a 4x4 out of a quaternion. Is that possible?
Sep 15 '11 at 07:27 AM
roamcel
(comments are locked)
|
|
Here's a faster and easier solution: public static Quaternion QuaternionFromMatrix(Matrix4x4 m) { return Quaternion.LookRotation(m.GetColumn(2), m.GetColumn(1)); }
(comments are locked)
|
|
Actually I can confirm that the
Doesn't work, and the
Works. I'm making a voxel engine that performs all sorts of transforms and the first method produces: While the second función produces And the control test uses Graphics.DrawMesh() with the matrix transform as a parameter
(comments are locked)
|


