Преглед изворни кода

Fix conversion quaternion to vector3 (use function in babylon instead of 3ds Max API)

noalak пре 8 година
родитељ
комит
a1210fd430

+ 40 - 1
Exporters/3ds Max/BabylonExport.Entities/BabylonQuaternion.cs

@@ -1,4 +1,6 @@
-namespace BabylonExport.Entities
+using System;
+
+namespace BabylonExport.Entities
 {
     public class BabylonQuaternion
     {
@@ -11,5 +13,42 @@
         {
             return new [] {X, Y, Z, W};
         }
+
+        /**
+         * Copy / pasted from babylon 
+         */
+        public BabylonVector3 toEulerAngles()
+        {
+            var result = new BabylonVector3();
+
+            var qz = this.Z;
+            var qx = this.X;
+            var qy = this.Y;
+            var qw = this.W;
+
+            var sqw = qw * qw;
+            var sqz = qz * qz;
+            var sqx = qx * qx;
+            var sqy = qy * qy;
+
+            var zAxisY = qy * qz - qx * qw;
+            var limit = .4999999;
+
+            if (zAxisY< -limit) {
+                result.Y = (float) (2 * Math.Atan2(qy, qw));
+                result.X = (float) Math.PI / 2;
+                result.Z = 0;
+            } else if (zAxisY > limit) {
+                result.Y = (float) (2 * Math.Atan2(qy, qw));
+                result.X = (float) -Math.PI / 2;
+                result.Z = 0;
+            } else {
+                result.Z = (float)Math.Atan2(2.0 * (qx* qy + qz* qw), (-sqz - sqx + sqy + sqw));
+                result.X = (float)Math.Asin(-2.0 * (qz* qy - qx* qw));
+                result.Y = (float)Math.Atan2(2.0 * (qz* qx + qy* qw), (sqz - sqx - sqy + sqw));
+            }
+
+            return result;
+        }
     }
 }

+ 4 - 4
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Camera.cs

@@ -77,17 +77,17 @@ namespace Max2Babylon
 
             var position = localTM.Translation;
             var rotation = localTM.Rotation;
-            var exportQuaternions = Loader.Core.RootNode.GetBoolProperty("babylonjs_exportquaternions");
 
             babylonCamera.position = new[] { position.X, position.Y, position.Z };
 
-            if (exportQuaternions)
+            var rotationQuaternion = new BabylonQuaternion { X = rotation.X, Y = rotation.Y, Z = rotation.Z, W = -rotation.W };
+            if (ExportQuaternionsInsteadOfEulers)
             {
-                babylonCamera.rotationQuaternion = new[] { rotation.X, rotation.Y, rotation.Z, -rotation.W };
+                babylonCamera.rotationQuaternion = rotationQuaternion.ToArray();
             }
             else
             {
-                babylonCamera.rotation = QuaternionToEulerAngles(rotation);
+                babylonCamera.rotation = rotationQuaternion.toEulerAngles().ToArray();
             }
 
             // Target

+ 4 - 14
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Mesh.cs

@@ -509,16 +509,6 @@ namespace Max2Babylon
 
         }
 
-        private float[] QuaternionToEulerAngles(IQuat rotation)
-        {
-            float rotx = 0, roty = 0, rotz = 0;
-            unsafe
-            {
-                rotation.GetEuler(new IntPtr(&rotx), new IntPtr(&roty), new IntPtr(&rotz));
-            }
-            return new[] { rotx, roty, rotz };
-        }
-
         int CreateGlobalVertex(IIGameMesh mesh, IFaceEx face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2, bool hasColor, bool hasAlpha, List<GlobalVertex>[] verticesAlreadyExported, IIGameSkin skin, List<int> boneIds)
         {
             var vertexIndex = (int)face.Vert[facePart];
@@ -714,17 +704,17 @@ namespace Max2Babylon
             var meshTrans = localTM.Translation;
             var meshRotation = localTM.Rotation;
             var meshScale = localTM.Scaling;
-            var exportQuaternions = Loader.Core.RootNode.GetBoolProperty("babylonjs_exportquaternions");
             
             babylonAbstractMesh.position = new[] { meshTrans.X, meshTrans.Y, meshTrans.Z };
 
-            if (exportQuaternions)
+            var rotationQuaternion = new BabylonQuaternion { X = meshRotation.X, Y = meshRotation.Y, Z = meshRotation.Z, W = -meshRotation.W };
+            if (ExportQuaternionsInsteadOfEulers)
             {
-                babylonAbstractMesh.rotationQuaternion = new[] { meshRotation.X, meshRotation.Y, meshRotation.Z, -meshRotation.W };
+                babylonAbstractMesh.rotationQuaternion = rotationQuaternion.ToArray();
             }
             else
             {
-                babylonAbstractMesh.rotation = QuaternionToEulerAngles(meshRotation);
+                babylonAbstractMesh.rotation = rotationQuaternion.toEulerAngles().ToArray();
             }
 
             babylonAbstractMesh.scaling = new[] { meshScale.X, meshScale.Y, meshScale.Z };