UnityMatrixExtension.cs 979 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using UnityEngine;
  3. namespace Leap.Unity
  4. {
  5. public static class UnityMatrixExtension
  6. {
  7. public static Quaternion CalculateRotation(this LeapTransform trs)
  8. {
  9. Vector3 upwards = trs.yBasis.ToVector3();
  10. Vector3 forward = -trs.zBasis.ToVector3();
  11. return Quaternion.LookRotation(forward, upwards);
  12. }
  13. public static LeapTransform GetLeapMatrix(this Transform t)
  14. {
  15. Vector scale = new Vector(t.lossyScale.x * UnityMatrixExtension.MM_TO_M, t.lossyScale.y * UnityMatrixExtension.MM_TO_M, t.lossyScale.z * UnityMatrixExtension.MM_TO_M);
  16. LeapTransform result = new LeapTransform(t.position.ToVector(), t.rotation.ToLeapQuaternion(), scale);
  17. result.MirrorZ();
  18. return result;
  19. }
  20. public static readonly Vector LEAP_UP = new Vector(0f, 1f, 0f);
  21. public static readonly Vector LEAP_FORWARD = new Vector(0f, 0f, -1f);
  22. public static readonly Vector LEAP_ORIGIN = new Vector(0f, 0f, 0f);
  23. public static readonly float MM_TO_M = 0.001f;
  24. }
  25. }