Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.9KB

  1. #if UNITY_MATHEMATICS
  2. using Svelto.ECS.Components;
  3. using Unity.Mathematics;
  4. public static partial class ExtensionMethods
  5. {
  6. public static float3 ToFloat3(in this ECSVector3 vector)
  7. {
  8. return new float3(vector.x, vector.y, vector.z);
  9. }
  10. public static quaternion ToQuaternion(in this ECSVector4 vector)
  11. {
  12. return new quaternion(vector.x, vector.y, vector.z, vector.w);
  13. }
  14. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  15. public static void Mul(ref this float3 vector1, float value)
  16. {
  17. vector1.x *= value;
  18. vector1.y *= value;
  19. vector1.z *= value;
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. public static void Div(ref this float3 vector1, float value)
  23. {
  24. vector1.x /= value;
  25. vector1.y /= value;
  26. vector1.z /= value;
  27. }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public static void ProjectOnPlane(ref this float3 vector, in float3 planeNormal)
  30. {
  31. var num1 = math.dot(planeNormal,planeNormal);
  32. if ((double) num1 < (double) Mathf.Epsilon)
  33. return;
  34. var num2 = math.dot(vector,planeNormal) / num1;
  35. vector.x -= planeNormal.x * num2;
  36. vector.y -= planeNormal.y * num2;
  37. vector.z -= planeNormal.z * num2;
  38. }
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public static void Normalize(ref this float3 x)
  41. {
  42. x.Mul(math.rsqrt(math.dot(x,x)));
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public static void NormalizeSafe(ref this float3 x)
  46. {
  47. var len = math.dot(x,x);
  48. x = len > math.FLT_MIN_NORMAL ? x * math.rsqrt(len) : float3.zero;
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public static void Add(ref this float3 a, in float3 b)
  52. {
  53. a.x += b.x;
  54. a.y += b.y;
  55. a.z += b.z;
  56. }
  57. }
  58. #endif