Skip to content

Commit

Permalink
KTX2Loader: Fix transcode target choices for UASTC on iOS. Add BC7 op…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
donmccurdy committed Jul 16, 2020
1 parent 83917a6 commit 57c1ed3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
42 changes: 33 additions & 9 deletions examples/jsm/loaders/KTX2Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ import {
LinearEncoding,
LinearFilter,
LinearMipmapLinearFilter,
RGBAFormat,
RGBA_ASTC_4x4_Format,
RGBA_BPTC_Format,
RGBA_ETC2_EAC_Format,
RGBA_PVRTC_4BPPV1_Format,
RGBA_S3TC_DXT5_Format,
RGB_ETC1_Format,
RGB_ETC2_Format,
RGBA_ETC2_EAC_Format,
RGB_PVRTC_4BPPV1_Format,
RGB_S3TC_DXT1_Format,
RGBA_ASTC_4x4_Format,
RGBA_PVRTC_4BPPV1_Format,
RGBA_S3TC_DXT5_Format,
sRGBEncoding,
UnsignedByteType,
sRGBEncoding,
} from '../../../build/three.module.js';

// Data Format Descriptor (DFD) constants.
Expand Down Expand Up @@ -81,10 +83,21 @@ class KTX2Loader extends CompressedTextureLoader {
etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
bptcSupported: renderer.extensions.has( 'EXT_texture_compression_bptc' ),
pvrtcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' )
|| renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' )
};

alert( JSON.stringify( {
WEBGL_compressed_texture_astc: !! renderer.extensions.has( 'WEBGL_compressed_texture_astc' ),
WEBGL_compressed_texture_etc1: !! renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ),
WEBGL_compressed_texture_etc: !! renderer.extensions.has( 'WEBGL_compressed_texture_etc' ),
WEBGL_compressed_texture_s3tc: !! renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ),
EXT_texture_compression_bptc: !! renderer.extensions.has( 'EXT_texture_compression_bptc' ),
WEBGL_compressed_texture_pvrtc: !! renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' )
|| !! renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' )
}, null, 2 ) );

return this;

}
Expand Down Expand Up @@ -458,32 +471,43 @@ class KTX2Container {
targetFormat = TranscodeTarget.ASTC_4x4_RGBA;
this.transcodedFormat = RGBA_ASTC_4x4_Format;

} else if ( config.bptcSupported && texFormat === TextureFormat.UASTC4x4 ) {

targetFormat = hasAlpha ? TranscodeTarget.BC7_M5_RGBA : BC7_M6_RGB;
this.transcodedFormat = RGBA_BPTC_Format;

} else if ( config.dxtSupported ) {

targetFormat = hasAlpha ? TranscodeTarget.BC3_RGBA : TranscodeTarget.BC1_RGB;
this.transcodedFormat = hasAlpha ? RGBA_S3TC_DXT5_Format : RGB_S3TC_DXT1_Format;

} else if ( config.pvrtcSupported ) {
} else if ( config.pvrtcSupported && texFormat === TextureFormat.ETC1S ) {

targetFormat = hasAlpha ? TranscodeTarget.PVRTC1_4_RGBA : TranscodeTarget.PVRTC1_4_RGB;
this.transcodedFormat = hasAlpha ? RGBA_PVRTC_4BPPV1_Format : RGB_PVRTC_4BPPV1_Format;

} else if ( config.etc2Supported ) {
} else if ( config.etc2Supported && texFormat === TextureFormat.ETC1S ) {

targetFormat = hasAlpha ? TranscodeTarget.ETC2_RGBA : TranscodeTarget.ETC1_RGB /* subset of ETC2 */;
this.transcodedFormat = hasAlpha ? RGBA_ETC2_EAC_Format : RGB_ETC2_Format;

} else if ( config.etc1Supported ) {
} else if ( config.etc1Supported && texFormat === TextureFormat.ETC1S ) {

targetFormat = TranscodeTarget.ETC1_RGB;
this.transcodedFormat = RGB_ETC1_Format;

} else {

throw new Error( 'THREE.KTX2Loader: No suitable compressed texture format found.' );
console.warn( 'THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32.' );

targetFormat = TranscodeTarget.RGBA32;
this.transcodedFormat = RGBAFormat;

}

// DO NOT SUBMIT
alert( targetFormat.constructor.name );

if ( ! this.basisModule.isFormatSupported( targetFormat, texFormat ) ) {

throw new Error( 'THREE.KTX2Loader: Selected texture format not supported by current transcoder build.' );
Expand Down
2 changes: 2 additions & 0 deletions examples/webgl_loader_texture_ktx2.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
scene.add( mesh );

var formatStrings = {
[ THREE.RGBAFormat ]: "RGBA32",
[ THREE.RGBA_ASTC_4x4_Format ]: "RGBA_ASTC_4x4",
[ THREE.RGB_S3TC_DXT1_Format ]: "RGB_S3TC_DXT1",
[ THREE.RGBA_S3TC_DXT5_Format ]: "RGBA_S3TC_DXT5",
Expand All @@ -70,6 +71,7 @@
console.info( `transcoded to ${formatStrings[ texture.format ]}` );

material.map = texture;
material.transparent = true;

material.needsUpdate = true;

Expand Down

0 comments on commit 57c1ed3

Please sign in to comment.