فهرست منبع

Add better handling of coordinate systems for glTF loader

Gary Hsu 8 سال پیش
والد
کامیت
1236974d64
2فایلهای تغییر یافته به همراه44 افزوده شده و 2 حذف شده
  1. 31 2
      loaders/src/glTF/2.0/babylon.glTFLoader.ts
  2. 13 0
      loaders/src/glTF/babylon.glTFFileLoader.ts

+ 31 - 2
loaders/src/glTF/2.0/babylon.glTFLoader.ts

@@ -89,8 +89,6 @@ module BABYLON.GLTF2 {
         }
 
         private _loadAsync(nodeNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onError: () => void): void {
-            scene.useRightHandedSystem = true;
-
             this._clear();
 
             this._loadData(data);
@@ -107,6 +105,23 @@ module BABYLON.GLTF2 {
         }
 
         private _onRenderReady(): void {
+            switch (this._parent.coordinateSystemMode) {
+                case GLTFLoaderCoordinateSystemMode.AUTO:
+                    if (!this._babylonScene.useRightHandedSystem) {
+                        this._addRightHandToLeftHandRootTransform();
+                    }
+                    break;
+                case GLTFLoaderCoordinateSystemMode.PASS_THROUGH:
+                    // do nothing
+                    break;
+                case GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED:
+                    this._babylonScene.useRightHandedSystem = true;
+                    break;
+                default:
+                    Tools.Error("Invalid coordinate system mode (" + this._parent.coordinateSystemMode + ")");
+                    break;
+            }
+
             this._succeeded = (this._errors.length === 0);
             if (this._succeeded) {
                 this._showMeshes();
@@ -155,6 +170,20 @@ module BABYLON.GLTF2 {
             }
         }
 
+        private _addRightHandToLeftHandRootTransform(): void {
+            var rootMesh = new Mesh("root", this._babylonScene);
+            rootMesh.scaling = new Vector3(1, 1, -1);
+            rootMesh.rotationQuaternion = new Quaternion(0, 1, 0, 0);
+
+            var nodes = this._gltf.nodes;
+            for (var i = 0; i < nodes.length; i++) {
+                var mesh = nodes[i].babylonMesh;
+                if (mesh && !mesh.parent) {
+                    mesh.parent = rootMesh;
+                }
+            }
+        }
+
         private _showMeshes(): void {
             var nodes = this._gltf.nodes;
             for (var i = 0; i < nodes.length; i++) {

+ 13 - 0
loaders/src/glTF/babylon.glTFFileLoader.ts

@@ -1,6 +1,18 @@
 /// <reference path="../../../dist/preview release/babylon.d.ts"/>
 
 module BABYLON {
+    export enum GLTFLoaderCoordinateSystemMode {
+        // Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene (scene.useRightHandedSystem).
+        // NOTE: When scene.useRightHandedSystem is false, an additional transform will be added to the root to transform the data from right-handed to left-handed.
+        AUTO,
+
+        // The glTF right-handed data is not transformed in any form and is loaded directly.
+        PASS_THROUGH,
+
+        // Sets the useRightHandedSystem flag on the scene.
+        FORCE_RIGHT_HANDED,
+    }
+
     export interface IGLTFLoaderData {
         json: Object;
         bin: ArrayBufferView;
@@ -20,6 +32,7 @@ module BABYLON {
         public static IncrementalLoading: boolean = true;
 
         // V2 options
+        public coordinateSystemMode: GLTFLoaderCoordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
         public onTextureLoaded: (texture: BaseTexture) => void;
         public onMaterialLoaded: (material: Material) => void;
         public onComplete: () => void;