瀏覽代碼

support ktx in gltf

Trevor Baron 6 年之前
父節點
當前提交
4e6d1aa974
共有 3 個文件被更改,包括 38 次插入19 次删除
  1. 1 0
      dist/preview release/what's new.md
  2. 35 19
      loaders/src/glTF/2.0/babylon.glTFLoader.ts
  3. 2 0
      src/Engine/babylon.engine.ts

+ 1 - 0
dist/preview release/what's new.md

@@ -64,6 +64,7 @@
 - `Layer` are now supported in `RenderTargetTexture` ([Sebavan](https://github.com/Sebavan))
 - `Layer` are now supported in `RenderTargetTexture` ([Sebavan](https://github.com/Sebavan))
 - Make onscreen joystick's canvas public ([TrevorDev](https://github.com/TrevorDev))
 - Make onscreen joystick's canvas public ([TrevorDev](https://github.com/TrevorDev))
 - Added `Tools.CustomRequestHeaders`, `Tools.UseCustomRequestHeaders`, `Tools.InjectCustomRequestHeaders` to send Custom Request Headers alongside XMLHttpRequest's i.e. when loading files (Tools.Loadfile) from resources requiring special headers like 'Authorization' ([susares](https://github.com/susares))
 - Added `Tools.CustomRequestHeaders`, `Tools.UseCustomRequestHeaders`, `Tools.InjectCustomRequestHeaders` to send Custom Request Headers alongside XMLHttpRequest's i.e. when loading files (Tools.Loadfile) from resources requiring special headers like 'Authorization' ([susares](https://github.com/susares))
+- Load KTX textures in the gltf2 loader when textureFormat is set on engine ([TrevorDev](https://github.com/TrevorDev))
 
 
 ### glTF Loader
 ### glTF Loader
 
 

+ 35 - 19
loaders/src/glTF/2.0/babylon.glTFLoader.ts

@@ -1713,28 +1713,44 @@ module BABYLON.GLTF2 {
             const sampler = (texture.sampler == undefined ? GLTFLoader._DefaultSampler : ArrayItem.Get(`${context}/sampler`, this.gltf.samplers, texture.sampler));
             const sampler = (texture.sampler == undefined ? GLTFLoader._DefaultSampler : ArrayItem.Get(`${context}/sampler`, this.gltf.samplers, texture.sampler));
             const samplerData = this._loadSampler(`/samplers/${sampler.index}`, sampler);
             const samplerData = this._loadSampler(`/samplers/${sampler.index}`, sampler);
 
 
-            const deferred = new Deferred<void>();
-            const babylonTexture = new Texture(null, this.babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, () => {
-                if (!this._disposed) {
-                    deferred.resolve();
-                }
-            }, (message, exception) => {
-                if (!this._disposed) {
-                    deferred.reject(new Error(`${context}: ${(exception && exception.message) ? exception.message : message || "Failed to load texture"}`));
-                }
-            });
-            promises.push(deferred.promise);
+            const image = ArrayItem.Get(`${context}/source`, this.gltf.images, texture.source);
+            let babylonTexture: Texture;
+            if (image.uri && this.babylonScene.getEngine().textureFormatInUse) {
+                // If an image uri and a texture format is set like (eg. KTX) load from url instead of blob to support texture format and fallback
+                const deferred = new Deferred<void>();
+                babylonTexture = new Texture(`${this._uniqueRootUrl}` + image.uri, this.babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, () => {
+                    if (!this._disposed) {
+                        deferred.resolve();
+                    }
+                }, (message, exception) => {
+                    if (!this._disposed) {
+                        deferred.reject(new Error(`${context}: ${(exception && exception.message) ? exception.message : message || "Failed to load texture"}`));
+                    }
+                });
+                promises.push(deferred.promise);
+            }else {
+                const deferred = new Deferred<void>();
+                babylonTexture = new Texture(null, this.babylonScene, samplerData.noMipMaps, false, samplerData.samplingMode, () => {
+                    if (!this._disposed) {
+                        deferred.resolve();
+                    }
+                }, (message, exception) => {
+                    if (!this._disposed) {
+                        deferred.reject(new Error(`${context}: ${(exception && exception.message) ? exception.message : message || "Failed to load texture"}`));
+                    }
+                });
+                promises.push(deferred.promise);
 
 
-            babylonTexture.wrapU = samplerData.wrapU;
-            babylonTexture.wrapV = samplerData.wrapV;
+                promises.push(this.loadImageAsync(`/images/${image.index}`, image).then((data) => {
+                    const name = image.uri || `${this._fileName}#image${image.index}`;
+                    const dataUrl = `data:${this._uniqueRootUrl}${name}`;
+                    babylonTexture.updateURL(dataUrl, new Blob([data], { type: image.mimeType }));
+                }));
 
 
-            const image = ArrayItem.Get(`${context}/source`, this.gltf.images, texture.source);
-            promises.push(this.loadImageAsync(`/images/${image.index}`, image).then((data) => {
-                const name = image.uri || `${this._fileName}#image${image.index}`;
-                const dataUrl = `data:${this._uniqueRootUrl}${name}`;
-                babylonTexture.updateURL(dataUrl, new Blob([data], { type: image.mimeType }));
-            }));
+            }
 
 
+            babylonTexture.wrapU = samplerData.wrapU;
+            babylonTexture.wrapV = samplerData.wrapV;
             assign(babylonTexture);
             assign(babylonTexture);
 
 
             this.logClose();
             this.logClose();

+ 2 - 0
src/Engine/babylon.engine.ts

@@ -4307,6 +4307,7 @@ module BABYLON {
                         excludeLoaders.push(loader);
                         excludeLoaders.push(loader);
                         Tools.Warn((loader.constructor as any).name + " failed when trying to load " + texture.url + ", falling back to the next supported loader");
                         Tools.Warn((loader.constructor as any).name + " failed when trying to load " + texture.url + ", falling back to the next supported loader");
                         this.createTexture(urlArg, noMipmap, invertY, scene, samplingMode, null, onError, buffer, texture, undefined, undefined, excludeLoaders);
                         this.createTexture(urlArg, noMipmap, invertY, scene, samplingMode, null, onError, buffer, texture, undefined, undefined, excludeLoaders);
+                        return;
                     }
                     }
                 }
                 }
 
 
@@ -5716,6 +5717,7 @@ module BABYLON {
                     if (fallbackUrl) {
                     if (fallbackUrl) {
                         excludeLoaders.push(loader);
                         excludeLoaders.push(loader);
                         this.createCubeTexture(fallbackUrl, scene, files, noMipmap, onLoad, onError, format, extension, createPolynomials, lodScale, lodOffset, texture, excludeLoaders);
                         this.createCubeTexture(fallbackUrl, scene, files, noMipmap, onLoad, onError, format, extension, createPolynomials, lodScale, lodOffset, texture, excludeLoaders);
+                        return;
                     }
                     }
                 }
                 }