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.

71 lines
1.9KB

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