未验证 提交 c3d5c00a 编写于 作者: M Mr.doob 提交者: GitHub

Merge pull request #17546 from shrekshao/basis-alpha-support

BasisTextureLoader: ASTC and PVRTC alpha support update
......@@ -36,7 +36,7 @@ basisLoader.load( 'diffuse.basis', function ( texture ) {
```
For further documentation about the Basis compressor and transcoder, refer to
the [Basis GitHub repository](https://github.com/BinomialLLC/basis_universal). The JavaScript wrapper requires one modification from the version provided in the Basis repository – the declaration on the first line is changed from `var Module` to `Module`, to accomodate lazy initialization in a Web Worker ([details](https://github.com/mrdoob/three.js/issues/16524)).
the [Basis GitHub repository](https://github.com/BinomialLLC/basis_universal).
## License
......
......@@ -4,8 +4,6 @@
* @author Shrek Shao / https://github.com/shrekshao
*/
/* global Module, createBasisModule */
/**
* Loader for Basis Universal GPU Texture Codec.
*
......@@ -32,6 +30,7 @@ THREE.BasisTextureLoader = function ( manager ) {
this.workerSourceURL = '';
this.workerConfig = {
format: null,
astcSupported: false,
etcSupported: false,
dxtSupported: false,
pvrtcSupported: false,
......@@ -63,22 +62,27 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
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 = THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1;
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4;
} else if ( config.dxtSupported ) {
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1;
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3;
} else if ( config.pvrtcSupported ) {
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_OPAQUE_ONLY;
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA;
} else if ( config.etcSupported ) {
config.format = THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1;
} else {
......@@ -136,25 +140,30 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var config = this.workerConfig;
var { width, height, mipmaps } = message;
var { width, height, hasAlpha, mipmaps, format } = message;
var texture;
if ( config.etcSupported ) {
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_ETC1_Format );
} else if ( config.dxtSupported ) {
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.BasisTextureLoader.DXT_FORMAT_MAP[ config.format ], THREE.UnsignedByteType );
} else if ( config.pvrtcSupported ) {
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_PVRTC_4BPPV1_Format );
} else {
throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );
switch ( format ) {
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFASTC_4x4:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGBA_ASTC_4x4_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC1:
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFBC3:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.BasisTextureLoader.DXT_FORMAT_MAP[ config.format ], THREE.UnsignedByteType );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFETC1:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_ETC1_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_PVRTC_4BPPV1_Format );
break;
case THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA:
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGBA_PVRTC_4BPPV1_Format );
break;
default:
throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );
}
......@@ -214,12 +223,7 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
var body = [
'/* basis_transcoder.js */',
'var Module;',
'function createBasisModule () {',
' ' + jsContent,
' return Module;',
'}',
'',
jsContent,
'/* worker */',
fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
].join( '\n' );
......@@ -312,13 +316,22 @@ THREE.BasisTextureLoader.prototype = Object.assign( Object.create( THREE.Loader.
THREE.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:
......@@ -359,7 +372,7 @@ THREE.BasisTextureLoader.BasisWorker = function () {
try {
var { width, height, mipmaps } = transcode( message.buffer );
var { width, height, hasAlpha, mipmaps, format } = transcode( message.buffer );
var buffers = [];
......@@ -369,7 +382,7 @@ THREE.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 ) {
......@@ -388,19 +401,15 @@ THREE.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;
......@@ -408,8 +417,6 @@ THREE.BasisTextureLoader.BasisWorker = function () {
} );
createBasisModule();
}
function transcode( buffer ) {
......@@ -419,6 +426,7 @@ THREE.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() {
......@@ -427,6 +435,20 @@ THREE.BasisTextureLoader.BasisWorker = function () {
}
if ( ! hasAlpha ) {
switch ( config.format ) {
case 9: // Hardcoded: THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGBA
config.format = 8; // Hardcoded: THREE.BasisTextureLoader.BASIS_FORMAT.cTFPVRTC1_4_RGB;
break;
default:
break;
}
}
if ( ! width || ! height || ! levels ) {
cleanup();
......@@ -441,12 +463,6 @@ THREE.BasisTextureLoader.BasisWorker = function () {
}
if ( basisFile.getHasAlpha() ) {
console.warn( 'THREE.BasisTextureLoader: Alpha not yet implemented.' );
}
var mipmaps = [];
for ( var mip = 0; mip < levels; mip ++ ) {
......@@ -460,7 +476,7 @@ THREE.BasisTextureLoader.BasisWorker = function () {
0,
mip,
config.format,
config.etcSupported ? 0 : ( config.dxtSupported ? 1 : 0 ),
hasAlpha,
0
);
......@@ -477,7 +493,7 @@ THREE.BasisTextureLoader.BasisWorker = function () {
cleanup();
return { width, height, mipmaps };
return { width, height, hasAlpha, mipmaps, format: config.format };
}
......
......@@ -14,6 +14,7 @@ export class BasisTextureLoader extends Loader {
workerConfig: {
format: number;
astcSupported: boolean;
etcSupported: boolean;
dxtSupported: boolean;
pvrtcSupported: boolean;
......
......@@ -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 };
}
......
......@@ -51,7 +51,6 @@
var loader = new BasisTextureLoader();
loader.setTranscoderPath( 'js/libs/basis/' );
loader.detectSupport( renderer );
loader.load( 'textures/compressed/PavingStones.basis', function ( texture ) {
texture.encoding = THREE.sRGBEncoding;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册