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 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. 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