Bläddra i källkod

Merge pull request #2535 from bghgary/loader-updates

glTF loader updates
David Catuhe 8 år sedan
förälder
incheckning
7248d2ed0e

+ 34 - 5
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++) {
@@ -997,7 +1026,7 @@ module BABYLON.GLTF2 {
 
             var sampler = (texture.sampler === undefined ? <IGLTFSampler>{} : this._gltf.samplers[texture.sampler]);
             var noMipMaps = (sampler.minFilter === ETextureMinFilter.NEAREST || sampler.minFilter === ETextureMinFilter.LINEAR);
-            var samplingMode = GLTFUtils.GetTextureFilterMode(sampler.minFilter);
+            var samplingMode = GLTFUtils.GetTextureSamplingMode(sampler.magFilter, sampler.minFilter);
 
             this.addPendingData(texture);
             var babylonTexture = new Texture(url, this._babylonScene, noMipMaps, false, samplingMode, () => {
@@ -1008,8 +1037,8 @@ module BABYLON.GLTF2 {
             });
 
             babylonTexture.coordinatesIndex = texCoord;
-            babylonTexture.wrapU = GLTFUtils.GetWrapMode(sampler.wrapS);
-            babylonTexture.wrapV = GLTFUtils.GetWrapMode(sampler.wrapT);
+            babylonTexture.wrapU = GLTFUtils.GetTextureWrapMode(sampler.wrapS);
+            babylonTexture.wrapV = GLTFUtils.GetTextureWrapMode(sampler.wrapT);
             babylonTexture.name = texture.name || "texture" + textureInfo.index;
 
             // Cache the texture

+ 36 - 20
loaders/src/glTF/2.0/babylon.glTFLoaderUtils.ts

@@ -29,22 +29,20 @@ module BABYLON.GLTF2 {
             return bufferView.buffer;
         }
 
-        public static ForEach(view: Uint16Array | Uint32Array | Float32Array, func: (nvalue: number, index:number) => void) : void {
+        public static ForEach(view: Uint16Array | Uint32Array | Float32Array, func: (nvalue: number, index: number) => void) : void {
             for (var index = 0; index < view.length; index++) {
                 func(view[index], index);
             }
         }
 
-        /**
-        * Returns the wrap mode of the texture
-        * @param mode: the mode value
-        */
-        public static GetWrapMode(mode: number): number {
+        public static GetTextureWrapMode(mode: ETextureWrapMode): number {
             switch (mode) {
-                case ETextureWrapMode.CLAMP_TO_EDGE: return Texture.CLAMP_ADDRESSMODE;
+                case ETextureWrapMode.CLAMP_TO_EDGE: Texture.CLAMP_ADDRESSMODE;
                 case ETextureWrapMode.MIRRORED_REPEAT: return Texture.MIRROR_ADDRESSMODE;
                 case ETextureWrapMode.REPEAT: return Texture.WRAP_ADDRESSMODE;
-                default: return Texture.WRAP_ADDRESSMODE;
+                default:
+                    Tools.Warn("Invalid texture wrap mode (" + mode + ")");
+                    return Texture.WRAP_ADDRESSMODE;
             }
         }
 
@@ -67,18 +65,36 @@ module BABYLON.GLTF2 {
             }
         }
 
-        /**
-         * Returns the texture filter mode giving a mode value
-         * @param mode: the filter mode value
-         */
-        public static GetTextureFilterMode(mode: number): ETextureMinFilter {
-            switch (mode) {
-                case ETextureMinFilter.LINEAR:
-                case ETextureMinFilter.LINEAR_MIPMAP_NEAREST:
-                case ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return Texture.TRILINEAR_SAMPLINGMODE;
-                case ETextureMinFilter.NEAREST:
-                case ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return Texture.NEAREST_SAMPLINGMODE;
-                default: return Texture.BILINEAR_SAMPLINGMODE;
+        public static GetTextureSamplingMode(magFilter: ETextureMagFilter, minFilter: ETextureMinFilter): number {
+            if (magFilter === ETextureMagFilter.LINEAR) {
+                switch (minFilter) {
+                    case ETextureMinFilter.NEAREST: return Texture.LINEAR_NEAREST;
+                    case ETextureMinFilter.LINEAR: return Texture.LINEAR_LINEAR;
+                    case ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return Texture.LINEAR_NEAREST_MIPNEAREST;
+                    case ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return Texture.LINEAR_LINEAR_MIPNEAREST;
+                    case ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return Texture.LINEAR_NEAREST_MIPLINEAR;
+                    case ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return Texture.LINEAR_LINEAR_MIPLINEAR;
+                    default:
+                        Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
+                        return Texture.LINEAR_LINEAR_MIPLINEAR;
+                }
+            }
+            else {
+                if (magFilter !== ETextureMagFilter.NEAREST) {
+                    Tools.Warn("Invalid texture magnification filter (" + magFilter + ")");
+                }
+
+                switch (minFilter) {
+                    case ETextureMinFilter.NEAREST: return Texture.NEAREST_NEAREST;
+                    case ETextureMinFilter.LINEAR: return Texture.NEAREST_LINEAR;
+                    case ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return Texture.NEAREST_NEAREST_MIPNEAREST;
+                    case ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return Texture.NEAREST_LINEAR_MIPNEAREST;
+                    case ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return Texture.NEAREST_NEAREST_MIPLINEAR;
+                    case ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return Texture.NEAREST_LINEAR_MIPLINEAR;
+                    default:
+                        Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
+                        return Texture.NEAREST_NEAREST_MIPNEAREST;
+                }
             }
         }
 

+ 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;