Unity 利用Quaternion.LookRotation API 将旋转矩阵转换成四元数

15 篇文章 0 订阅

Quaternion.LookRotation

 

  1. private static Quaternion QuaternionLookRotation(Vector3 forward, Vector3 up)
  2. {
  3.     forward.Normalize();
  4.     Vector3 vector = Vector3.Normalize(forward);
  5.     Vector3 vector2 = Vector3.Normalize(Vector3.Cross(up, vector));
  6.     Vector3 vector3 = Vector3.Cross(vector, vector2);
  7.     var m00 = vector2.x;
  8.     var m01 = vector2.y;
  9.     var m02 = vector2.z;
  10.     var m10 = vector3.x;
  11.     var m11 = vector3.y;
  12.     var m12 = vector3.z;
  13.     var m20 = vector.x;
  14.     var m21 = vector.y;
  15.     var m22 = vector.z;
  16.     float num8 = (m00 + m11) + m22;
  17.     var quaternion = new Quaternion();
  18.     if (num8 > 0f)
  19.     {
  20.         var num = (float)Math.Sqrt(num8 + 1f);
  21.         quaternion.w = num * 0.5f;
  22.         num = 0.5f / num;
  23.         quaternion.x = (m12 - m21) * num;
  24.         quaternion.y = (m20 - m02) * num;
  25.         quaternion.z = (m01 - m10) * num;
  26.         return quaternion;
  27.     }
  28.     if ((m00 >= m11) && (m00 >= m22))
  29.     {
  30.         var num7 = (float)Math.Sqrt(((1f + m00) - m11) - m22);
  31.         var num4 = 0.5f / num7;
  32.         quaternion.x = 0.5f * num7;
  33.         quaternion.y = (m01 + m10) * num4;
  34.         quaternion.z = (m02 + m20) * num4;
  35.         quaternion.w = (m12 - m21) * num4;
  36.         return quaternion;
  37.     }
  38.     if (m11 > m22)
  39.     {
  40.         var num6 = (float)Math.Sqrt(((1f + m11) - m00) - m22);
  41.         var num3 = 0.5f / num6;
  42.         quaternion.x = (m10+ m01) * num3;
  43.         quaternion.y = 0.5f * num6;
  44.         quaternion.z = (m21 + m12) * num3;
  45.         quaternion.w = (m20 - m02) * num3;
  46.         return quaternion;
  47.     }
  48.     var num5 = (float)Math.Sqrt(((1f + m22) - m00) - m11);
  49.     var num2 = 0.5f / num5;
  50.     quaternion.x = (m20 + m02) * num2;
  51.     quaternion.y = (m21 + m12) * num2;
  52.     quaternion.z = 0.5f * num5;
  53.     quaternion.w = (m01 - m10) * num2;
  54.     return quaternion;
  55. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值