|
@@ -10,13 +10,13 @@ import {
|
|
|
LinearFilter,
|
|
|
LinearMipmapLinearFilter,
|
|
|
Loader,
|
|
|
+ RGBA_ASTC_4x4_Format,
|
|
|
+ RGBA_PVRTC_4BPPV1_Format,
|
|
|
RGB_ETC1_Format,
|
|
|
RGB_PVRTC_4BPPV1_Format,
|
|
|
UnsignedByteType
|
|
|
} from "../../../build/three.module.js";
|
|
|
|
|
|
-/* global Module, createBasisModule */
|
|
|
-
|
|
|
/**
|
|
|
* Loader for Basis Universal GPU Texture Codec.
|
|
|
*
|
|
@@ -43,6 +43,7 @@ var BasisTextureLoader = function ( manager ) {
|
|
|
this.workerSourceURL = '';
|
|
|
this.workerConfig = {
|
|
|
format: null,
|
|
|
+ astcSupported: false,
|
|
|
etcSupported: false,
|
|
|
dxtSupported: false,
|
|
|
pvrtcSupported: false,
|
|
@@ -74,22 +75,27 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
|
|
|
|
|
|
var config = this.workerConfig;
|
|
|
|
|
|
+ config.astcSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_astc' );
|
|
|
config.etcSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_etc1' );
|
|
|
config.dxtSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_s3tc' );
|
|
|
config.pvrtcSupported = !! renderer.extensions.get( 'WEBGL_compressed_texture_pvrtc' )
|
|
|
|| !! renderer.extensions.get( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
|
|
|
|
|
|
- if ( config.etcSupported ) {
|
|
|
+ if ( config.astcSupported ) {
|
|
|
|
|
|
- config.format = BasisTextureLoader.BASIS_FORMAT.cTFETC1;
|
|
|
+ config.format = BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4;
|
|
|
|
|
|
} else if ( config.dxtSupported ) {
|
|
|
|
|
|
- config.format = BasisTextureLoader.BASIS_FORMAT.cTFBC1;
|
|
|
+ config.format = BasisTextureLoader.BASIS_FORMAT.cTFBC3;
|
|
|
|
|
|
} else if ( config.pvrtcSupported ) {
|
|
|
|
|
|
- config.format = BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_OPAQUE_ONLY;
|
|
|
+ config.format = BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA;
|
|
|
+
|
|
|
+ } else if ( config.etcSupported ) {
|
|
|
+
|
|
|
+ config.format = BasisTextureLoader.BASIS_FORMAT.cTFETC1;
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -147,25 +153,30 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
|
|
|
|
|
|
var config = this.workerConfig;
|
|
|
|
|
|
- var { width, height, mipmaps } = message;
|
|
|
+ var { width, height, hasAlpha, mipmaps, format } = message;
|
|
|
|
|
|
var texture;
|
|
|
|
|
|
- if ( config.etcSupported ) {
|
|
|
-
|
|
|
- texture = new CompressedTexture( mipmaps, width, height, RGB_ETC1_Format );
|
|
|
-
|
|
|
- } else if ( config.dxtSupported ) {
|
|
|
-
|
|
|
- texture = new CompressedTexture( mipmaps, width, height, BasisTextureLoader.DXT_FORMAT_MAP[ config.format ], UnsignedByteType );
|
|
|
-
|
|
|
- } else if ( config.pvrtcSupported ) {
|
|
|
-
|
|
|
- texture = new CompressedTexture( mipmaps, width, height, RGB_PVRTC_4BPPV1_Format );
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );
|
|
|
+ switch ( format ) {
|
|
|
+
|
|
|
+ case BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4:
|
|
|
+ texture = new CompressedTexture( mipmaps, width, height, RGBA_ASTC_4x4_Format );
|
|
|
+ break;
|
|
|
+ case BasisTextureLoader.BASIS_FORMAT.cTFBC1:
|
|
|
+ case BasisTextureLoader.BASIS_FORMAT.cTFBC3:
|
|
|
+ texture = new CompressedTexture( mipmaps, width, height, BasisTextureLoader.DXT_FORMAT_MAP[ config.format ], UnsignedByteType );
|
|
|
+ break;
|
|
|
+ case BasisTextureLoader.BASIS_FORMAT.cTFETC1:
|
|
|
+ texture = new CompressedTexture( mipmaps, width, height, RGB_ETC1_Format );
|
|
|
+ break;
|
|
|
+ case BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB:
|
|
|
+ texture = new CompressedTexture( mipmaps, width, height, RGB_PVRTC_4BPPV1_Format );
|
|
|
+ break;
|
|
|
+ case BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA:
|
|
|
+ texture = new CompressedTexture( mipmaps, width, height, RGBA_PVRTC_4BPPV1_Format );
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -225,12 +236,7 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
|
|
|
|
|
|
var body = [
|
|
|
'/* basis_transcoder.js */',
|
|
|
- 'var Module;',
|
|
|
- 'function createBasisModule () {',
|
|
|
- ' ' + jsContent,
|
|
|
- ' return Module;',
|
|
|
- '}',
|
|
|
- '',
|
|
|
+ jsContent,
|
|
|
'/* worker */',
|
|
|
fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
|
|
|
].join( '\n' );
|
|
@@ -323,13 +329,22 @@ BasisTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
|
|
|
|
|
|
BasisTextureLoader.BASIS_FORMAT = {
|
|
|
cTFETC1: 0,
|
|
|
- cTFBC1: 1,
|
|
|
- cTFBC4: 2,
|
|
|
- cTFPVRTC1_4_OPAQUE_ONLY: 3,
|
|
|
- cTFBC7_M6_OPAQUE_ONLY: 4,
|
|
|
- cTFETC2: 5,
|
|
|
- cTFBC3: 6,
|
|
|
- cTFBC5: 7,
|
|
|
+ cTFETC2: 1,
|
|
|
+ cTFBC1: 2,
|
|
|
+ cTFBC3: 3,
|
|
|
+ cTFBC4: 4,
|
|
|
+ cTFBC5: 5,
|
|
|
+ cTFBC7_M6_OPAQUE_ONLY: 6,
|
|
|
+ cTFBC7_M5: 7,
|
|
|
+ cTFPVRTC1_4_RGB: 8,
|
|
|
+ cTFPVRTC1_4_RGBA: 9,
|
|
|
+ cTFASTC_4x4: 10,
|
|
|
+ cTFATC_RGB: 11,
|
|
|
+ cTFATC_RGBA_INTERPOLATED_ALPHA: 12,
|
|
|
+ cTFRGBA32: 13,
|
|
|
+ cTFRGB565: 14,
|
|
|
+ cTFBGR565: 15,
|
|
|
+ cTFRGBA4444: 16,
|
|
|
};
|
|
|
|
|
|
// DXT formats, from:
|
|
@@ -370,7 +385,7 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
|
|
|
try {
|
|
|
|
|
|
- var { width, height, mipmaps } = transcode( message.buffer );
|
|
|
+ var { width, height, hasAlpha, mipmaps, format } = transcode( message.buffer );
|
|
|
|
|
|
var buffers = [];
|
|
|
|
|
@@ -380,7 +395,7 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
|
|
|
}
|
|
|
|
|
|
- self.postMessage( { type: 'transcode', id: message.id, width, height, mipmaps }, buffers );
|
|
|
+ self.postMessage( { type: 'transcode', id: message.id, width, height, hasAlpha, mipmaps, format }, buffers );
|
|
|
|
|
|
} catch ( error ) {
|
|
|
|
|
@@ -399,19 +414,15 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
|
|
|
function init( wasmBinary ) {
|
|
|
|
|
|
+ var BasisModule;
|
|
|
transcoderPending = new Promise( ( resolve ) => {
|
|
|
|
|
|
- // The 'Module' global is used by the Basis wrapper, which will check for
|
|
|
- // the 'wasmBinary' property before trying to load the file itself.
|
|
|
-
|
|
|
- // TODO(donmccurdy): This only works with a modified version of the
|
|
|
- // emscripten-generated wrapper. The default seems to have a bug making it
|
|
|
- // impossible to override the WASM binary.
|
|
|
- Module = { wasmBinary, onRuntimeInitialized: resolve };
|
|
|
+ BasisModule = { wasmBinary, onRuntimeInitialized: resolve };
|
|
|
+ BASIS( BasisModule );
|
|
|
|
|
|
} ).then( () => {
|
|
|
|
|
|
- var { BasisFile, initializeBasis } = Module;
|
|
|
+ var { BasisFile, initializeBasis } = BasisModule;
|
|
|
|
|
|
_BasisFile = BasisFile;
|
|
|
|
|
@@ -419,8 +430,6 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
|
|
|
} );
|
|
|
|
|
|
- createBasisModule();
|
|
|
-
|
|
|
}
|
|
|
|
|
|
function transcode( buffer ) {
|
|
@@ -430,6 +439,7 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
var width = basisFile.getImageWidth( 0, 0 );
|
|
|
var height = basisFile.getImageHeight( 0, 0 );
|
|
|
var levels = basisFile.getNumLevels( 0 );
|
|
|
+ var hasAlpha = basisFile.getHasAlpha();
|
|
|
|
|
|
function cleanup() {
|
|
|
|
|
@@ -438,6 +448,20 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ if ( ! hasAlpha ) {
|
|
|
+
|
|
|
+ switch ( config.format ) {
|
|
|
+
|
|
|
+ case 9: // Hardcoded: BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA
|
|
|
+ config.format = 8; // Hardcoded: BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
if ( ! width || ! height || ! levels ) {
|
|
|
|
|
|
cleanup();
|
|
@@ -452,12 +476,6 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( basisFile.getHasAlpha() ) {
|
|
|
-
|
|
|
- console.warn( 'THREE.BasisTextureLoader: Alpha not yet implemented.' );
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
var mipmaps = [];
|
|
|
|
|
|
for ( var mip = 0; mip < levels; mip ++ ) {
|
|
@@ -471,7 +489,7 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
0,
|
|
|
mip,
|
|
|
config.format,
|
|
|
- config.etcSupported ? 0 : ( config.dxtSupported ? 1 : 0 ),
|
|
|
+ hasAlpha,
|
|
|
0
|
|
|
);
|
|
|
|
|
@@ -488,7 +506,7 @@ BasisTextureLoader.BasisWorker = function () {
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
- return { width, height, mipmaps };
|
|
|
+ return { width, height, hasAlpha, mipmaps, format: config.format };
|
|
|
|
|
|
}
|
|
|
|