BabylonVector3.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. namespace BabylonExport.Entities
  3. {
  4. public class BabylonVector3
  5. {
  6. public float X { get; set; }
  7. public float Y { get; set; }
  8. public float Z { get; set; }
  9. public BabylonVector3() { }
  10. /**
  11. * Creates a new Vector3 object from the passed x, y, z (floats) coordinates.
  12. * A Vector3 is the main object used in 3D geometry.
  13. * It can represent etiher the coordinates of a point the space, either a direction.
  14. */
  15. public BabylonVector3(float x, float y, float z)
  16. {
  17. this.X = x;
  18. this.Y = y;
  19. this.Z = z;
  20. }
  21. public float[] ToArray()
  22. {
  23. return new [] {X, Y, Z};
  24. }
  25. public float Length()
  26. {
  27. return (float)Math.Sqrt(X * X + Y * Y + Z * Z);
  28. }
  29. public static BabylonVector3 operator +(BabylonVector3 a, BabylonVector3 b)
  30. {
  31. return new BabylonVector3 {X = a.X + b.X, Y = a.Y + b.Y, Z = a.Z + b.Z};
  32. }
  33. public static BabylonVector3 operator -(BabylonVector3 a, BabylonVector3 b)
  34. {
  35. return new BabylonVector3 { X = a.X - b.X, Y = a.Y - b.Y, Z = a.Z - b.Z };
  36. }
  37. public static BabylonVector3 operator /(BabylonVector3 a, float b)
  38. {
  39. return new BabylonVector3 { X = a.X / b, Y = a.Y / b, Z = a.Z / b };
  40. }
  41. public static BabylonVector3 operator *(BabylonVector3 a, float b)
  42. {
  43. return new BabylonVector3 { X = a.X * b, Y = a.Y * b, Z = a.Z * b };
  44. }
  45. public BabylonQuaternion toQuaternion()
  46. {
  47. return RotationYawPitchRollToRefBabylon(Y, X, Z);
  48. }
  49. /**
  50. * (Copy pasted from babylon)
  51. * Sets the passed quaternion "result" from the passed float Euler angles (y, x, z).
  52. */
  53. private BabylonQuaternion RotationYawPitchRollToRefBabylon(float yaw, float pitch, float roll)
  54. {
  55. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  56. var halfRoll = roll * 0.5;
  57. var halfPitch = pitch * 0.5;
  58. var halfYaw = yaw * 0.5;
  59. var sinRoll = Math.Sin(halfRoll);
  60. var cosRoll = Math.Cos(halfRoll);
  61. var sinPitch = Math.Sin(halfPitch);
  62. var cosPitch = Math.Cos(halfPitch);
  63. var sinYaw = Math.Sin(halfYaw);
  64. var cosYaw = Math.Cos(halfYaw);
  65. var result = new BabylonQuaternion();
  66. result.X = (float)((cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll));
  67. result.Y = (float)((sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll));
  68. result.Z = (float)((cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll));
  69. result.W = (float)((cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll));
  70. return result;
  71. }
  72. /**
  73. * Returns a new Vector3 set from the index "offset" of the passed array.
  74. */
  75. public static BabylonVector3 FromArray(float[] array, int offset = 0)
  76. {
  77. return new BabylonVector3(array[offset], array[offset + 1], array[offset + 2]);
  78. }
  79. public override string ToString()
  80. {
  81. return "{ X=" + X + ", Y=" + Y + ", Z=" + Z + " }";
  82. }
  83. }
  84. }