Skip to content

Commit

Permalink
Merge pull request #5545 from TrevorDev/useInvertVScaleForKtx
Browse files Browse the repository at this point in the history
Invert vScale of compressed textures when invertY is set and invertY …
  • Loading branch information
sebavan committed Nov 27, 2018
2 parents e487117 + 1aec037 commit eacc0d7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions dist/preview release/what's new.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
- 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))
- 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

Expand Down
10 changes: 5 additions & 5 deletions src/Engine/babylon.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4234,7 +4234,7 @@ module BABYLON {
* * 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'
* @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 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
Expand Down Expand Up @@ -4306,7 +4306,7 @@ module BABYLON {
customFallback = true;
excludeLoaders.push(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;
}
}
Expand All @@ -4316,7 +4316,7 @@ module BABYLON {
texture.onLoadedObservable.remove(onLoadObserver);
}
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;
}
}
Expand All @@ -4333,7 +4333,7 @@ module BABYLON {
if (loadFailed) {
onInternalError("TextureLoader failed to load data");
} else {
this._prepareWebGLTexture(texture, scene, width, height, invertY, !loadMipmap, isCompressed, () => {
this._prepareWebGLTexture(texture, scene, width, height, texture.invertY, !loadMipmap, isCompressed, () => {
done();
return false;
}, samplingMode);
Expand All @@ -4356,7 +4356,7 @@ module BABYLON {
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;
var isPot = (img.width === potWidth && img.height === potHeight);
let internalFormat = format ? this._getInternalFormat(format) : ((extension === ".jpg") ? gl.RGB : gl.RGBA);
Expand Down
8 changes: 8 additions & 0 deletions src/Materials/Textures/Loaders/babylon.ktxTextureLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ module BABYLON {
*/
public transformUrl(rootUrl: string, textureFormatInUse: Nullable<string>): string {
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;
}

Expand Down Expand Up @@ -60,6 +64,8 @@ module BABYLON {
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 ktx = new KhronosTextureContainer(data, 6);

Expand All @@ -84,6 +90,8 @@ module BABYLON {
*/
public loadData(data: ArrayBuffer, texture: InternalTexture,
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);

callback(ktx.pixelWidth, ktx.pixelHeight, false, true, () => {
Expand Down
2 changes: 2 additions & 0 deletions src/Materials/Textures/babylon.internalTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ module BABYLON {

// Private
/** @hidden */
public _invertVScale = false;
/** @hidden */
public _initialSlot = -1;
/** @hidden */
public _designatedSlot = -1;
Expand Down
3 changes: 3 additions & 0 deletions src/Materials/Textures/babylon.texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ module BABYLON {
scene.getEngine().onBeforeTextureInitObservable.notifyObservers(this);

let load = () => {
if (this._texture && this._texture._invertVScale) {
this.vScale *= -1;
}
if (this.onLoadObservable.hasObservers()) {
this.onLoadObservable.notifyObservers(this);
}
Expand Down

0 comments on commit eacc0d7

Please sign in to comment.