Browse Source

Merge pull request #5545 from TrevorDev/useInvertVScaleForKtx

Invert vScale of compressed textures when invertY is set and invertY …
sebavan 6 years ago
parent
commit
eacc0d7606

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

@@ -72,6 +72,7 @@
 - Added support for linking a bone to a transform node ([bghgary](https://github.com/bghgary))
 - Added support for linking a bone to a transform node ([bghgary](https://github.com/bghgary))
 - Factored out `setDirection` function from `lookAt` for transform node ([bghgary](https://github.com/bghgary))
 - Factored out `setDirection` function from `lookAt` for transform node ([bghgary](https://github.com/bghgary))
 - Add support for setting renderingGroupId and creating instances to `AxesViewer` ([bghgary](https://github.com/bghgary))
 - Add support for setting renderingGroupId and creating instances to `AxesViewer` ([bghgary](https://github.com/bghgary))
+- Invert vScale of compressed ktx textures as they are inverted in the file and UNPACK_FLIP_Y_WEBGL is not supported by ktx ([TrevorDev](https://github.com/TrevorDev))
 
 
 ### glTF Loader
 ### glTF Loader
 
 

+ 5 - 5
src/Engine/babylon.engine.ts

@@ -4234,7 +4234,7 @@ module BABYLON {
          * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
          * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
          * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
          * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
          * @param noMipmap defines a boolean indicating that no mipmaps shall be generated.  Ignored for compressed textures.  They must be in the file
          * @param noMipmap defines a boolean indicating that no mipmaps shall be generated.  Ignored for compressed textures.  They must be in the file
-         * @param invertY when true, image is flipped when loaded.  You probably want true. Ignored for compressed textures.  Must be flipped in the file
+         * @param invertY when true, image is flipped when loaded.  You probably want true. Certain compressed textures may invert this if their default is inverted (eg. ktx)
          * @param scene needed for loading to the correct scene
          * @param scene needed for loading to the correct scene
          * @param samplingMode mode with should be used sample / access the texture (Default: BABYLON.Texture.TRILINEAR_SAMPLINGMODE)
          * @param samplingMode mode with should be used sample / access the texture (Default: BABYLON.Texture.TRILINEAR_SAMPLINGMODE)
          * @param onLoad optional callback to be called upon successful completion
          * @param onLoad optional callback to be called upon successful completion
@@ -4306,7 +4306,7 @@ module BABYLON {
                         customFallback = true;
                         customFallback = true;
                         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, texture.invertY, scene, samplingMode, null, onError, buffer, texture, undefined, undefined, excludeLoaders);
                         return;
                         return;
                     }
                     }
                 }
                 }
@@ -4316,7 +4316,7 @@ module BABYLON {
                         texture.onLoadedObservable.remove(onLoadObserver);
                         texture.onLoadedObservable.remove(onLoadObserver);
                     }
                     }
                     if (Tools.UseFallbackTexture) {
                     if (Tools.UseFallbackTexture) {
-                        this.createTexture(Tools.fallbackTexture, noMipmap, invertY, scene, samplingMode, null, onError, buffer, texture);
+                        this.createTexture(Tools.fallbackTexture, noMipmap, texture.invertY, scene, samplingMode, null, onError, buffer, texture);
                         return;
                         return;
                     }
                     }
                 }
                 }
@@ -4333,7 +4333,7 @@ module BABYLON {
                         if (loadFailed) {
                         if (loadFailed) {
                             onInternalError("TextureLoader failed to load data");
                             onInternalError("TextureLoader failed to load data");
                         } else {
                         } else {
-                            this._prepareWebGLTexture(texture, scene, width, height, invertY, !loadMipmap, isCompressed, () => {
+                            this._prepareWebGLTexture(texture, scene, width, height, texture.invertY, !loadMipmap, isCompressed, () => {
                                 done();
                                 done();
                                 return false;
                                 return false;
                             }, samplingMode);
                             }, samplingMode);
@@ -4356,7 +4356,7 @@ module BABYLON {
                         texture._buffer = img;
                         texture._buffer = img;
                     }
                     }
 
 
-                    this._prepareWebGLTexture(texture, scene, img.width, img.height, invertY, noMipmap, false, (potWidth, potHeight, continuationCallback) => {
+                    this._prepareWebGLTexture(texture, scene, img.width, img.height, texture.invertY, noMipmap, false, (potWidth, potHeight, continuationCallback) => {
                         let gl = this._gl;
                         let gl = this._gl;
                         var isPot = (img.width === potWidth && img.height === potHeight);
                         var isPot = (img.width === potWidth && img.height === potHeight);
                         let internalFormat = format ? this._getInternalFormat(format) : ((extension === ".jpg") ? gl.RGB : gl.RGBA);
                         let internalFormat = format ? this._getInternalFormat(format) : ((extension === ".jpg") ? gl.RGB : gl.RGBA);

+ 8 - 0
src/Materials/Textures/Loaders/babylon.ktxTextureLoader.ts

@@ -32,6 +32,10 @@ module BABYLON {
          */
          */
         public transformUrl(rootUrl: string, textureFormatInUse: Nullable<string>): string {
         public transformUrl(rootUrl: string, textureFormatInUse: Nullable<string>): string {
             var lastDot = rootUrl.lastIndexOf('.');
             var lastDot = rootUrl.lastIndexOf('.');
+            if (lastDot != -1 && rootUrl.substring(lastDot + 1) == "ktx") {
+                // Already transformed
+                return rootUrl;
+            }
             return (lastDot > -1 ? rootUrl.substring(0, lastDot) : rootUrl) + textureFormatInUse;
             return (lastDot > -1 ? rootUrl.substring(0, lastDot) : rootUrl) + textureFormatInUse;
         }
         }
 
 
@@ -60,6 +64,8 @@ module BABYLON {
                 return;
                 return;
             }
             }
 
 
+            // Need to invert vScale as invertY via UNPACK_FLIP_Y_WEBGL is not supported by compressed texture
+            texture._invertVScale = !texture.invertY;
             var engine = texture.getEngine();
             var engine = texture.getEngine();
             var ktx = new KhronosTextureContainer(data, 6);
             var ktx = new KhronosTextureContainer(data, 6);
 
 
@@ -84,6 +90,8 @@ module BABYLON {
          */
          */
         public loadData(data: ArrayBuffer, texture: InternalTexture,
         public loadData(data: ArrayBuffer, texture: InternalTexture,
             callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void, loadFailed: boolean) => void): void {
             callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void, loadFailed: boolean) => void): void {
+            // Need to invert vScale as invertY via UNPACK_FLIP_Y_WEBGL is not supported by compressed texture
+            texture._invertVScale = !texture.invertY;
             var ktx = new KhronosTextureContainer(data, 1);
             var ktx = new KhronosTextureContainer(data, 1);
 
 
             callback(ktx.pixelWidth, ktx.pixelHeight, false, true, () => {
             callback(ktx.pixelWidth, ktx.pixelHeight, false, true, () => {

+ 2 - 0
src/Materials/Textures/babylon.internalTexture.ts

@@ -138,6 +138,8 @@ module BABYLON {
 
 
         // Private
         // Private
         /** @hidden */
         /** @hidden */
+        public _invertVScale = false;
+        /** @hidden */
         public _initialSlot = -1;
         public _initialSlot = -1;
         /** @hidden */
         /** @hidden */
         public _designatedSlot = -1;
         public _designatedSlot = -1;

+ 3 - 0
src/Materials/Textures/babylon.texture.ts

@@ -247,6 +247,9 @@ module BABYLON {
             scene.getEngine().onBeforeTextureInitObservable.notifyObservers(this);
             scene.getEngine().onBeforeTextureInitObservable.notifyObservers(this);
 
 
             let load = () => {
             let load = () => {
+                if (this._texture && this._texture._invertVScale) {
+                    this.vScale *= -1;
+                }
                 if (this.onLoadObservable.hasObservers()) {
                 if (this.onLoadObservable.hasObservers()) {
                     this.onLoadObservable.notifyObservers(this);
                     this.onLoadObservable.notifyObservers(this);
                 }
                 }