Comments and answers for "What is the source code of Quaternion.LookRotation?"
http://answers.unity.com/questions/467614/what-is-the-source-code-of-quaternionlookrotation.html
The latest comments and answers for the question "What is the source code of Quaternion.LookRotation?"Comment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1531340/view.html
We don't know the exact implementation of Unity's LookRotation method as [it's defined in native code][1].
<br>
I have not confirmed the code here is right, but it looks like he's using pretty much the same approach [mentioned here at the end][2].
[1]: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/$$anonymous$$ath.bindings.cs#L99
[2]: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/Thu, 19 Jul 2018 13:41:42 GMTBunny83Comment by gkillz on gkillz's answer
http://answers.unity.com/comments/1531232/view.html
is this the actual implementation in unity or u have come up with this ?Thu, 19 Jul 2018 08:27:26 GMTgkillzComment by aeroson on aeroson's answer
http://answers.unity.com/comments/1068918/view.html
It seems to me that vector3 is orthogonalized up vector.Sat, 19 Sep 2015 09:56:15 GMTaerosonAnswer by vladibo
http://answers.unity.com/answers/467979/view.html
This is what I was able to come out with. Seems to work. Just note that forward and up vector must be orthogonal, the function doesn't have orthogonization of up vector.
private static Quaternion QuaternionLookRotation(Vector3 forward, Vector3 up)
{
forward.Normalize();
Vector3 vector = Vector3.Normalize(forward);
Vector3 vector2 = Vector3.Normalize(Vector3.Cross(up, vector));
Vector3 vector3 = Vector3.Cross(vector, vector2);
var m00 = vector2.x;
var m01 = vector2.y;
var m02 = vector2.z;
var m10 = vector3.x;
var m11 = vector3.y;
var m12 = vector3.z;
var m20 = vector.x;
var m21 = vector.y;
var m22 = vector.z;
float num8 = (m00 + m11) + m22;
var quaternion = new Quaternion();
if (num8 > 0f)
{
var num = (float)Math.Sqrt(num8 + 1f);
quaternion.w = num * 0.5f;
num = 0.5f / num;
quaternion.x = (m12 - m21) * num;
quaternion.y = (m20 - m02) * num;
quaternion.z = (m01 - m10) * num;
return quaternion;
}
if ((m00 >= m11) && (m00 >= m22))
{
var num7 = (float)Math.Sqrt(((1f + m00) - m11) - m22);
var num4 = 0.5f / num7;
quaternion.x = 0.5f * num7;
quaternion.y = (m01 + m10) * num4;
quaternion.z = (m02 + m20) * num4;
quaternion.w = (m12 - m21) * num4;
return quaternion;
}
if (m11 > m22)
{
var num6 = (float)Math.Sqrt(((1f + m11) - m00) - m22);
var num3 = 0.5f / num6;
quaternion.x = (m10+ m01) * num3;
quaternion.y = 0.5f * num6;
quaternion.z = (m21 + m12) * num3;
quaternion.w = (m20 - m02) * num3;
return quaternion;
}
var num5 = (float)Math.Sqrt(((1f + m22) - m00) - m11);
var num2 = 0.5f / num5;
quaternion.x = (m20 + m02) * num2;
quaternion.y = (m21 + m12) * num2;
quaternion.z = 0.5f * num5;
quaternion.w = (m01 - m10) * num2;
return quaternion;
}Mon, 03 Jun 2013 11:17:47 GMTvladiboAnswer by Graham-Dunnett
http://answers.unity.com/answers/467633/view.html
Dunno what the source code is exactly, but normalize the vectors, take a cross product, then you have 3 vectors that will form a rotation matrix. (So, _(0,0,1)_ multiplied by `M` gives forward, _(0,1,0)_ multiplied by `M` gives upward, and _(1,0,0)_ multiplied by `M` gives right.) Now you have a rotation matrix, so google and get:
http://renderfeather.googlecode.com/hg-history/034a1900d6e8b6c92440382658d2b01fc732c5de/Doc/optimized%20Matrix%20quaternion%20conversion.pdf
which tells you how to get from a matrix to a quaternion.
(You can, of course, license the source code to Unity if you really need to know exactly what's happening under the hood.)Sun, 02 Jun 2013 20:35:25 GMTGraham-Dunnett