Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 48 additions & 37 deletions examples/jsm/loaders/KTX2Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
* - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor
*
* To do:
* - [ ] Cross-platform testing
* - [ ] Specify JS/WASM transcoder path
* - [ ] High-quality demo
* - [ ] Documentation
* - [ ] TypeScript definitions
* - [ ] (Optional) Include BC5
* - [ ] (Optional) Include EAC RG on mobile (WEBGL_compressed_texture_etc)
* - [ ] (Optional) Include two-texture output mode (see: clearcoat + clearcoatRoughness)
Expand Down Expand Up @@ -136,61 +133,75 @@ class KTX2Loader extends CompressedTextureLoader {

} );

// parse() will call initModule() again, but starting the process early
// should allow the WASM to load in parallel with the texture.
this.initModule();

Promise.all( [ bufferPending, this.basisModulePending ] ).then( function ( [ buffer ] ) {
Promise.all( [ bufferPending, this.basisModulePending ] )
.then( function ( [ buffer ] ) {

scope.parse( buffer, function ( _texture ) {
scope.parse( buffer, function ( _texture ) {

texture.copy( _texture );
texture.needsUpdate = true;
texture.copy( _texture );
texture.needsUpdate = true;

if ( onLoad ) onLoad( texture );
if ( onLoad ) onLoad( texture );

}, onError );
}, onError );

} );
} )
.catch( onError );

return texture;

}

parse( buffer, onLoad, onError ) {

var BasisLzEtc1sImageTranscoder = this.basisModule.BasisLzEtc1sImageTranscoder;
var UastcImageTranscoder = this.basisModule.UastcImageTranscoder;
var TextureFormat = this.basisModule.TextureFormat;
var scope = this;

var ktx = new KTX2Container( this.basisModule, buffer );
// load() may have already called initModule(), but call it again here
// in case the user called parse() directly. Method is idempotent.
this.initModule();

// TODO(donmccurdy): Should test if texture is transcodable before attempting
// any transcoding. If supercompressionScheme is KTX_SS_BASIS_LZ and dfd
// colorModel is ETC1S (163) or if dfd colorModel is UASTCF (166)
// then texture must be transcoded.
var transcoder = ktx.getTexFormat() === TextureFormat.UASTC4x4
? new UastcImageTranscoder()
: new BasisLzEtc1sImageTranscoder();
this.basisModulePending.then( function () {

ktx.initMipmaps( transcoder, this.transcoderConfig )
.then( function () {
var BasisLzEtc1sImageTranscoder = scope.basisModule.BasisLzEtc1sImageTranscoder;
var UastcImageTranscoder = scope.basisModule.UastcImageTranscoder;
var TextureFormat = scope.basisModule.TextureFormat;

var texture = new CompressedTexture(
ktx.mipmaps,
ktx.getWidth(),
ktx.getHeight(),
ktx.transcodedFormat,
UnsignedByteType
);
var ktx = new KTX2Container( scope.basisModule, buffer );

texture.encoding = ktx.getEncoding();
texture.premultiplyAlpha = ktx.getPremultiplyAlpha();
texture.minFilter = ktx.mipmaps.length === 1 ? LinearFilter : LinearMipmapLinearFilter;
texture.magFilter = LinearFilter;
// TODO(donmccurdy): Should test if texture is transcodable before attempting
// any transcoding. If supercompressionScheme is KTX_SS_BASIS_LZ and dfd
// colorModel is ETC1S (163) or if dfd colorModel is UASTCF (166)
// then texture must be transcoded.
var transcoder = ktx.getTexFormat() === TextureFormat.UASTC4x4
? new UastcImageTranscoder()
: new BasisLzEtc1sImageTranscoder();

onLoad( texture );
ktx.initMipmaps( transcoder, scope.transcoderConfig )
.then( function () {

} )
.catch( onError );
var texture = new CompressedTexture(
ktx.mipmaps,
ktx.getWidth(),
ktx.getHeight(),
ktx.transcodedFormat,
UnsignedByteType
);

texture.encoding = ktx.getEncoding();
texture.premultiplyAlpha = ktx.getPremultiplyAlpha();
texture.minFilter = ktx.mipmaps.length === 1 ? LinearFilter : LinearMipmapLinearFilter;
texture.magFilter = LinearFilter;

onLoad( texture );

} )
.catch( onError );

} );

return this;

Expand Down