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.

ExtensionMethods.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.ECS.Components;
  4. public static partial class ExtensionMethods
  5. {
  6. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  7. public static float SqrMagnitude(in this ECSVector2 a) { return a.x * a.x + a.y * a.y; }
  8. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  9. public static float SqrMagnitude(in this ECSVector3 a) { return a.x * a.x + a.y * a.y + a.z * a.z; }
  10. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  11. public static float Magnitude(in this ECSVector2 a) { return (float) Math.Sqrt(a.SqrMagnitude()); }
  12. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  13. public static float Magnitude(in this ECSVector3 a) { return (float) Math.Sqrt(a.SqrMagnitude()); }
  14. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  15. public static void Add(ref this ECSVector3 vector1, in ECSVector3 vector2)
  16. {
  17. vector1.x += vector2.x;
  18. vector1.y += vector2.y;
  19. vector1.z += vector2.z;
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. public static void Add(ref this ECSVector3 vector1, float x, float y, float z)
  23. {
  24. vector1.x += x;
  25. vector1.y += y;
  26. vector1.z += z;
  27. }
  28. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  29. public static void Set(ref this ECSVector3 vector1, float x, float y, float z)
  30. {
  31. vector1.x = x;
  32. vector1.y = y;
  33. vector1.z = z;
  34. }
  35. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36. public static void Zero(ref this ECSVector3 vector1)
  37. {
  38. vector1.x = 0;
  39. vector1.y = 0;
  40. vector1.z = 0;
  41. }
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. public static void Set(ref this ECSQuaternion quaternion, float x, float y, float z, float w)
  44. {
  45. quaternion.x = x;
  46. quaternion.y = y;
  47. quaternion.z = z;
  48. quaternion.w = w;
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public static void Interpolate(ref this ECSVector3 vector, in ECSVector3 vectorS, in ECSVector3 vectorE, float time)
  52. {
  53. vector.x = vectorS.x * (1 - time) + vectorE.x * time;
  54. vector.y = vectorS.y * (1 - time) + vectorE.y * time;
  55. vector.z = vectorS.z * (1 - time) + vectorE.z * time;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static float Dot(ref this ECSVector3 vector1, in ECSVector3 vector2)
  59. {
  60. return vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z;
  61. }
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static ECSVector3 Cross(ref this ECSVector3 lhs, in ECSVector3 rhs)
  64. {
  65. return new ECSVector3(lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z,
  66. lhs.x * rhs.y - lhs.y * rhs.x);
  67. }
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static ECSVector3 Mul(in this ECSQuaternion rotation, in ECSVector3 point)
  70. {
  71. float x = rotation.x * 2F;
  72. float y = rotation.y * 2F;
  73. float z = rotation.z * 2F;
  74. float xx = rotation.x * x;
  75. float yy = rotation.y * y;
  76. float zz = rotation.z * z;
  77. float xy = rotation.x * y;
  78. float xz = rotation.x * z;
  79. float yz = rotation.y * z;
  80. float wx = rotation.w * x;
  81. float wy = rotation.w * y;
  82. float wz = rotation.w * z;
  83. return new ECSVector3((1F - (yy + zz)) * point.x + (xy - wz) * point.y + (xz + wy) * point.z,
  84. (xy + wz) * point.x + (1F - (xx + zz)) * point.y + (yz - wx) * point.z,
  85. (xz - wy) * point.x + (yz + wx) * point.y + (1F - (xx + yy)) * point.z);
  86. }
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static void Swap(ref this ECSVector3 vector, ref ECSVector3 vectorS)
  89. {
  90. float x = vector.x;
  91. float y = vector.y;
  92. float z = vector.z;
  93. vector.x = vectorS.x;
  94. vector.y = vectorS.y;
  95. vector.z = vectorS.z;
  96. vectorS.x = x;
  97. vectorS.y = y;
  98. vectorS.z = z;
  99. }
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. public static void Sub(ref this ECSVector3 vector1, in ECSVector3 vector2)
  102. {
  103. vector1.x -= vector2.x;
  104. vector1.y -= vector2.y;
  105. vector1.z -= vector2.z;
  106. }
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. public static ref ECSVector3 Mul(ref this ECSVector3 vector1, float value)
  109. {
  110. vector1.x *= value;
  111. vector1.y *= value;
  112. vector1.z *= value;
  113. return ref vector1;
  114. }
  115. }