Kaynağa Gözat

Use a native dictionary instead of a Map
Update root detection using matrix

noalak 5 yıl önce
ebeveyn
işleme
b63642bc08

+ 1 - 1
serializers/src/glTF/2.0/Extensions/KHR_lights_punctual.ts

@@ -97,7 +97,7 @@ export class KHR_lights_punctual implements IGLTFExporterExtensionV2 {
                 }
                 else {
                     const lightPosition = babylonLight.position.clone();
-                    let convertToRightHandedSystem = this._exporter._convertToRightHandedSystemMap.get(babylonNode);
+                    let convertToRightHandedSystem = this._exporter._convertToRightHandedSystemMap[babylonNode.uniqueId];
                     if (!lightPosition.equals(Vector3.Zero())) {
                         if (convertToRightHandedSystem) {
                             _GLTFUtilities._GetRightHandedPositionVector3FromRef(lightPosition);

+ 2 - 2
serializers/src/glTF/2.0/glTFAnimation.ts

@@ -228,7 +228,7 @@ export class _GLTFAnimation {
      * @param convertToRightHandedSystemMap
      * @param animationSampleRate
      */
-    public static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: { [key: number]: number }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystemMap: Map<Node, boolean>, animationSampleRate: number) {
+    public static _CreateNodeAnimationFromAnimationGroups(babylonScene: Scene, glTFAnimations: IAnimation[], nodeMap: { [key: number]: number }, nodes: INode[], binaryWriter: _BinaryWriter, bufferViews: IBufferView[], accessors: IAccessor[], convertToRightHandedSystemMap: { [nodeId: number]: boolean }, animationSampleRate: number) {
         let glTFAnimation: IAnimation;
         if (babylonScene.animationGroups) {
             let animationGroups = babylonScene.animationGroups;
@@ -246,7 +246,7 @@ export class _GLTFAnimation {
                         let animationInfo = _GLTFAnimation._DeduceAnimationInfo(targetAnimation.animation);
                         if (animationInfo) {
                             let babylonTransformNode = target instanceof TransformNode ? target as TransformNode : target[0] as TransformNode;
-                            let convertToRightHandedSystem = convertToRightHandedSystemMap.get(babylonTransformNode) as boolean;
+                            let convertToRightHandedSystem = convertToRightHandedSystemMap[babylonTransformNode.uniqueId];
                             _GLTFAnimation.AddAnimation(`${animation.name}`,
                                 glTFAnimation,
                                 babylonTransformNode,

+ 17 - 14
serializers/src/glTF/2.0/glTFExporter.ts

@@ -1,7 +1,7 @@
 import { AccessorType, IBufferView, IAccessor, INode, IScene, IMesh, IMaterial, ITexture, IImage, ISampler, IAnimation, ImageMimeType, IMeshPrimitive, IBuffer, IGLTF, MeshPrimitiveMode, AccessorComponentType, ITextureInfo } from "babylonjs-gltf2interface";
 
 import { FloatArray, Nullable, IndicesArray } from "babylonjs/types";
-import { Viewport, Color3, Vector2, Vector3, Vector4, Quaternion, Epsilon } from "babylonjs/Maths/math";
+import { Viewport, Color3, Vector2, Vector3, Vector4, Quaternion, Epsilon, Matrix } from "babylonjs/Maths/math";
 import { Tools } from "babylonjs/Misc/tools";
 import { VertexBuffer } from "babylonjs/Meshes/buffer";
 import { Node } from "babylonjs/node";
@@ -124,7 +124,7 @@ export class _Exporter {
     /**
      * Specifies if a Babylon node should be converted to right-handed on export
      */
-    public _convertToRightHandedSystemMap: Map<Node, boolean>;
+    public _convertToRightHandedSystemMap: { [nodeId: number]: boolean };
 
     /**
      * Baked animation sample rate
@@ -1310,12 +1310,15 @@ export class _Exporter {
     private isNodeConvertingToLeftHanded(node: Node): boolean {
         if (node instanceof TransformNode)
         {
-            // TRS
-            if (!node.position.equalsToFloats(0, 0, 0) ||
-                (!node.rotationQuaternion && node.rotation && (node.rotation.x != 0 || node.rotation.z != 0 || Math.abs(node.rotation.y - Math.PI) > Epsilon)) || // rotation Quaternion has priority over Vector3
-                (node.rotationQuaternion && !node.rotationQuaternion.equals(new Quaternion(0, 1, 0, 0))) ||
-                !node.scaling.equalsToFloats(1, 1, -1)) {
-                return false;
+            // Transform
+            let matrix = node.getWorldMatrix();
+            let matrixToLeftHanded = Matrix.Compose(new Vector3(-1, 1, 1), Quaternion.Identity(), Vector3.Zero());
+            let matrixProduct = matrix.multiply(matrixToLeftHanded);
+            let matrixIdentity = Matrix.Identity();
+            for (let i = 0; i < 16; i++) {
+                if (Math.abs(matrixProduct.m[i] - matrixIdentity.m[i]) > Epsilon) {
+                    return false;
+                }
             }
 
             // Geometry
@@ -1343,13 +1346,13 @@ export class _Exporter {
         const nodes: Node[] = [...babylonScene.transformNodes, ...babylonScene.meshes, ...babylonScene.lights];
         let rootNodesToLeftHanded: Node[] = [];
 
-        this._convertToRightHandedSystemMap = new Map<Node, boolean>();
+        this._convertToRightHandedSystemMap = {};
 
         // Set default values for all nodes
         babylonScene.rootNodes.forEach((rootNode) => {
-            this._convertToRightHandedSystemMap.set(rootNode, !babylonScene.useRightHandedSystem);
+            this._convertToRightHandedSystemMap[rootNode.uniqueId] = !babylonScene.useRightHandedSystem;
             rootNode.getDescendants(false).forEach((descendant) => {
-                this._convertToRightHandedSystemMap.set(descendant, !babylonScene.useRightHandedSystem);
+                this._convertToRightHandedSystemMap[descendant.uniqueId] = !babylonScene.useRightHandedSystem;
             });
         });
 
@@ -1367,7 +1370,7 @@ export class _Exporter {
 
                     // Cancel conversion to right handed system
                     rootNode.getDescendants(false).forEach((descendant) => {
-                        this._convertToRightHandedSystemMap.set(descendant, false);
+                        this._convertToRightHandedSystemMap[descendant.uniqueId] = false;
                     });
                 }
             });
@@ -1401,7 +1404,7 @@ export class _Exporter {
                                 Tools.Log("Omitting " + babylonNode.name + " from scene.");
                             }
                             else {
-                                let convertToRightHandedSystem = this._convertToRightHandedSystemMap.get(babylonNode);
+                                let convertToRightHandedSystem = this._convertToRightHandedSystemMap[babylonNode.uniqueId];
                                 if (convertToRightHandedSystem) {
                                     if (glTFNode.translation) {
                                         glTFNode.translation[2] *= -1;
@@ -1456,7 +1459,7 @@ export class _Exporter {
         for (let babylonNode of nodes) {
             if (!this._options.shouldExportNode || this._options.shouldExportNode(babylonNode)) {
                 promiseChain = promiseChain.then(() => {
-                    let convertToRightHandedSystem = this._convertToRightHandedSystemMap.get(babylonNode) as boolean;
+                    let convertToRightHandedSystem = this._convertToRightHandedSystemMap[babylonNode.uniqueId];
                     return this.createNodeAsync(babylonNode, binaryWriter, convertToRightHandedSystem).then((node) => {
                         const promise = this._extensionsPostExportNodeAsync("createNodeAsync", node, babylonNode);
                         if (promise == null) {