1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- namespace BabylonExport.Entities
- {
- public class BabylonVector3
- {
- public float X { get; set; }
- public float Y { get; set; }
- public float Z { get; set; }
- public float[] ToArray()
- {
- return new [] {X, Y, Z};
- }
- public float Length()
- {
- return (float)Math.Sqrt(X * X + Y * Y + Z * Z);
- }
- public static BabylonVector3 operator +(BabylonVector3 a, BabylonVector3 b)
- {
- return new BabylonVector3 {X = a.X + b.X, Y = a.Y + b.Y, Z = a.Z + b.Z};
- }
- public static BabylonVector3 operator -(BabylonVector3 a, BabylonVector3 b)
- {
- return new BabylonVector3 { X = a.X - b.X, Y = a.Y - b.Y, Z = a.Z - b.Z };
- }
- public static BabylonVector3 operator /(BabylonVector3 a, float b)
- {
- return new BabylonVector3 { X = a.X / b, Y = a.Y / b, Z = a.Z / b };
- }
- public static BabylonVector3 operator *(BabylonVector3 a, float b)
- {
- return new BabylonVector3 { X = a.X * b, Y = a.Y * b, Z = a.Z * b };
- }
- public BabylonQuaternion toQuaternion()
- {
- return RotationYawPitchRollToRefBabylon(Y, X, Z);
- }
- /**
- * (Copy pasted from babylon)
- * Sets the passed quaternion "result" from the passed float Euler angles (y, x, z).
- */
- private BabylonQuaternion RotationYawPitchRollToRefBabylon(float yaw, float pitch, float roll)
- {
- // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
- var halfRoll = roll * 0.5;
- var halfPitch = pitch * 0.5;
- var halfYaw = yaw * 0.5;
- var sinRoll = Math.Sin(halfRoll);
- var cosRoll = Math.Cos(halfRoll);
- var sinPitch = Math.Sin(halfPitch);
- var cosPitch = Math.Cos(halfPitch);
- var sinYaw = Math.Sin(halfYaw);
- var cosYaw = Math.Cos(halfYaw);
- var result = new BabylonQuaternion();
- result.X = (float)((cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll));
- result.Y = (float)((sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll));
- result.Z = (float)((cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll));
- result.W = (float)((cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll));
- return result;
- }
- public override string ToString()
- {
- return "{ X=" + X + ", Y=" + Y + ", Z=" + Z + " }";
- }
- }
- }
|