BabylonVector3.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 float[] ToArray()
  10. {
  11. return new [] {X, Y, Z};
  12. }
  13. public float Length()
  14. {
  15. return (float)Math.Sqrt(X * X + Y * Y + Z * Z);
  16. }
  17. public static BabylonVector3 operator +(BabylonVector3 a, BabylonVector3 b)
  18. {
  19. return new BabylonVector3 {X = a.X + b.X, Y = a.Y + b.Y, Z = a.Z + b.Z};
  20. }
  21. public static BabylonVector3 operator -(BabylonVector3 a, BabylonVector3 b)
  22. {
  23. return new BabylonVector3 { X = a.X - b.X, Y = a.Y - b.Y, Z = a.Z - b.Z };
  24. }
  25. public static BabylonVector3 operator /(BabylonVector3 a, float b)
  26. {
  27. return new BabylonVector3 { X = a.X / b, Y = a.Y / b, Z = a.Z / b };
  28. }
  29. public static BabylonVector3 operator *(BabylonVector3 a, float b)
  30. {
  31. return new BabylonVector3 { X = a.X * b, Y = a.Y * b, Z = a.Z * b };
  32. }
  33. public BabylonQuaternion toQuaternion()
  34. {
  35. return RotationYawPitchRollToRefBabylon(Y, X, Z);
  36. }
  37. /**
  38. * (Copy pasted from babylon)
  39. * Sets the passed quaternion "result" from the passed float Euler angles (y, x, z).
  40. */
  41. private BabylonQuaternion RotationYawPitchRollToRefBabylon(float yaw, float pitch, float roll)
  42. {
  43. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  44. var halfRoll = roll * 0.5;
  45. var halfPitch = pitch * 0.5;
  46. var halfYaw = yaw * 0.5;
  47. var sinRoll = Math.Sin(halfRoll);
  48. var cosRoll = Math.Cos(halfRoll);
  49. var sinPitch = Math.Sin(halfPitch);
  50. var cosPitch = Math.Cos(halfPitch);
  51. var sinYaw = Math.Sin(halfYaw);
  52. var cosYaw = Math.Cos(halfYaw);
  53. var result = new BabylonQuaternion();
  54. result.X = (float)((cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll));
  55. result.Y = (float)((sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll));
  56. result.Z = (float)((cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll));
  57. result.W = (float)((cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll));
  58. return result;
  59. }
  60. public override string ToString()
  61. {
  62. return "{ X=" + X + ", Y=" + Y + ", Z=" + Z + " }";
  63. }
  64. }
  65. }