From 57c1ed35faaa82829e18690bfca1a8e09567d66b Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Wed, 15 Jul 2020 20:21:36 -0700 Subject: [PATCH] KTX2Loader: Fix transcode target choices for UASTC on iOS. Add BC7 option. --- examples/jsm/loaders/KTX2Loader.js | 42 +++++++++++++++++++------ examples/webgl_loader_texture_ktx2.html | 2 ++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/examples/jsm/loaders/KTX2Loader.js b/examples/jsm/loaders/KTX2Loader.js index 0ddc6786c47256..a39789f801efc1 100644 --- a/examples/jsm/loaders/KTX2Loader.js +++ b/examples/jsm/loaders/KTX2Loader.js @@ -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. @@ -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; } @@ -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.' ); diff --git a/examples/webgl_loader_texture_ktx2.html b/examples/webgl_loader_texture_ktx2.html index 1fd45059e43d4c..549a9b6cd06af9 100644 --- a/examples/webgl_loader_texture_ktx2.html +++ b/examples/webgl_loader_texture_ktx2.html @@ -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", @@ -70,6 +71,7 @@ console.info( `transcoded to ${formatStrings[ texture.format ]}` ); material.map = texture; + material.transparent = true; material.needsUpdate = true;