BabylonVector3.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /**
  34. * Returns a new Quaternion object, computed from the Vector3 coordinates.
  35. */
  36. public BabylonQuaternion toQuaternion()
  37. {
  38. var result = new BabylonQuaternion();
  39. var cosxPlusz = Math.Cos((this.X + this.Z) * 0.5);
  40. var sinxPlusz = Math.Sin((this.X + this.Z) * 0.5);
  41. var coszMinusx = Math.Cos((this.Z - this.X) * 0.5);
  42. var sinzMinusx = Math.Sin((this.Z - this.X) * 0.5);
  43. var cosy = Math.Cos(this.Y * 0.5);
  44. var siny = Math.Sin(this.Y * 0.5);
  45. result.X = (float)(coszMinusx * siny);
  46. result.Y = (float)(-sinzMinusx * siny);
  47. result.Z = (float)(sinxPlusz * cosy);
  48. result.W = (float)(cosxPlusz * cosy);
  49. return result;
  50. }
  51. ///**
  52. // * Returns a new Quaternion object, computed from the Vector3 coordinates.
  53. // */
  54. //public BabylonQuaternion toQuaternion()
  55. //{
  56. // return RotationYawPitchRollToRef(Y,X,Z);
  57. //}
  58. /**
  59. * Sets the passed quaternion "result" from the passed float Euler angles (y, x, z).
  60. */
  61. public BabylonQuaternion RotationYawPitchRollToRef(float yaw, float pitch, float roll)
  62. {
  63. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  64. var halfRoll = roll * 0.5;
  65. var halfPitch = pitch * 0.5;
  66. var halfYaw = yaw * 0.5;
  67. var sinRoll = Math.Sin(halfRoll);
  68. var cosRoll = Math.Cos(halfRoll);
  69. var sinPitch = Math.Sin(halfPitch);
  70. var cosPitch = Math.Cos(halfPitch);
  71. var sinYaw = Math.Sin(halfYaw);
  72. var cosYaw = Math.Cos(halfYaw);
  73. var result = new BabylonQuaternion();
  74. result.X = (float)((cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll));
  75. result.Y = (float)((sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll));
  76. result.Z = (float)((cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll));
  77. result.W = (float)((cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll));
  78. return result;
  79. }
  80. }
  81. }