mirror of
https://github.com/BinomialLLC/basis_universal.git
synced 2026-11-15 20:36:13 +00:00
Merge pull request #1 from BinomialLLC/gltf-demo
glTF 3D Model demo and more
This commit is contained in:
@@ -59,11 +59,10 @@ If you are doing rate distortion comparisons vs. other similar systems, be sure
|
||||
|
||||
### WebGL test
|
||||
|
||||
The "WebGL" directory contains a very simple WebGL demo that uses the transcoder compiled to asm.js with [emscripten](https://emscripten.org/). It currently only supports transcoding to the BC1 and BC3 texture formats. The file `WebGL/basis_wrappers.cpp` contains a simple C-style API that the Javascript code calls to interface with the C++ Basis transcoder.
|
||||
The "WebGL" directory contains two very simple WebGL demos that use the transcoder compiled to wasm with [emscripten](https://emscripten.org/). See more details [here](webgl/README.md).
|
||||
|
||||
On browsers that don't support BC1 (Firefox is one), there's a low-quality fallback code path for opaque textures (but no fallback for BC3 yet). Note that the fallback path only converts to 16-bit RGB images at the moment, so the quality isn't as good as it should be.
|
||||
|
||||
Note that I was unable to disable assertions when compiling the transcoder in release (-O2), and I'm not sure why yet. Currently, basisu.h forcefully #undef's the assert() macro, which is ugly but works. Including assert()'s in release will slow transcoding down.
|
||||

|
||||

|
||||
|
||||
### Transcoder details
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# WebGL Examples
|
||||
|
||||
Requires WebAssembly and WebGL support.
|
||||
|
||||
## Texture
|
||||
|
||||
`texture/index.html` renders a single texture, using the transcoder (compiled to WASM with emscripten) to generate one of the following compressed texture formats:
|
||||
|
||||
* BC1
|
||||
* BC3
|
||||
|
||||
On browsers that don't support BC1 (Firefox is one), there's a low-quality fallback code path for opaque textures (but no fallback for BC3 yet). Note that the fallback path only converts to 16-bit RGB images at the moment, so the quality isn't as good as it should be.
|
||||
|
||||

|
||||
|
||||
## glTF 3D Model
|
||||
|
||||
`gltf/index.html` renders a glTF 3D model with `.basis` texture files, transcoded into one of the following compressed texture formats:
|
||||
|
||||
* DTX (BC1)
|
||||
* Tested in Chrome (Linux and macOS) and Firefox (macOS).
|
||||
* ETC1
|
||||
* Tested in Chrome on Android, Pixel 3 XL.
|
||||
* PVRTC (COMPRESSED_RGB_PVRTC_4BPPV1_IMG)
|
||||
* Tested in Chrome and Safari on iOS iPhone 6 Plus.
|
||||
|
||||
The glTF model in this demo uses a hypothetical `GOOGLE_texture_basis` extension. That extension is defined for the sake of example only – the glTF format will officially embed Basis files within a KTX2 wrapper, through a new
|
||||
extension that is currently in development.
|
||||
|
||||

|
||||
|
||||
## Testing locally
|
||||
|
||||
See [how to run things locally](https://threejs.org/docs/#manual/en/introduction/How-to-run-things-locally), or (with [Node.js](https://nodejs.org/en/) installed), run:
|
||||
|
||||
```
|
||||
npx serve
|
||||
```
|
||||
|
||||
The console will display a `localhost` URL for local testing, and (on supported WiFi networks and devices) may also display an IP address accessible by other devices on the same network. Note that mobile devices must support WebAssembly to run this demo. Learn more about [remote debugging your android devices](https://developers.google.com/web/tools/chrome-devtools/remote-debugging/).
|
||||
File diff suppressed because one or more lines are too long
@@ -1,228 +0,0 @@
|
||||
// basis_wrappers.cpp - Simple C-style wrappers to the C++ transcoder for WebGL use.
|
||||
#include "basisu_transcoder.h"
|
||||
|
||||
using namespace basist;
|
||||
|
||||
static basist::etc1_global_selector_codebook *g_pGlobal_codebook;
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void basis_init();
|
||||
|
||||
void *basis_open(void *src, uint src_size);
|
||||
void basis_close(void *h);
|
||||
|
||||
uint basis_get_has_alpha(void *h);
|
||||
|
||||
uint basis_get_num_images(void *h);
|
||||
uint basis_get_num_levels(void *h, uint image_index);
|
||||
|
||||
uint basis_get_image_width(void *h, uint image_index, uint level_index);
|
||||
uint basis_get_image_height(void *h, uint image_index, uint level_index);
|
||||
|
||||
uint basis_get_image_transcoded_size_in_bytes(void *h, uint image_index, uint level_index, uint format);
|
||||
|
||||
uint basis_start_transcoding(void *h);
|
||||
|
||||
uint basis_transcode_image(void *h, void *dst, uint dst_size_in_bytes,
|
||||
uint image_index, uint level_index, uint format,
|
||||
uint pvrtc_wrap_addressing, uint get_alpha_for_opaque_formats);
|
||||
}
|
||||
|
||||
void basis_init()
|
||||
{
|
||||
basisu_transcoder_init();
|
||||
|
||||
if (!g_pGlobal_codebook)
|
||||
g_pGlobal_codebook = new basist::etc1_global_selector_codebook(g_global_selector_cb_size, g_global_selector_cb);
|
||||
}
|
||||
|
||||
#define MAGIC 0xDEADBEE1
|
||||
|
||||
struct basis_file
|
||||
{
|
||||
int m_magic;
|
||||
basisu_transcoder m_transcoder;
|
||||
void *m_pFile;
|
||||
uint m_file_size;
|
||||
|
||||
basis_file() :
|
||||
m_transcoder(g_pGlobal_codebook)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
void *basis_open(void *src, uint src_size)
|
||||
{
|
||||
if ((!src) || (!src_size))
|
||||
return nullptr;
|
||||
|
||||
basis_file *f = new basis_file;
|
||||
f->m_pFile = src;
|
||||
f->m_file_size = src_size;
|
||||
|
||||
if (!f->m_transcoder.validate_header(f->m_pFile, f->m_file_size))
|
||||
{
|
||||
delete f;
|
||||
return nullptr;
|
||||
}
|
||||
f->m_magic = MAGIC;
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
void basis_close(void *h)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return;
|
||||
|
||||
delete f;
|
||||
}
|
||||
|
||||
uint basis_get_has_alpha(void *h)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
basisu_image_level_info li;
|
||||
|
||||
if (!f->m_transcoder.get_image_level_info(f->m_pFile, f->m_file_size, li, 0, 0))
|
||||
return 0;
|
||||
|
||||
return li.m_alpha_flag;
|
||||
}
|
||||
|
||||
uint basis_get_num_images(void *h)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
return f->m_transcoder.get_total_images(f->m_pFile, f->m_file_size);
|
||||
}
|
||||
|
||||
uint basis_get_num_levels(void *h, uint image_index)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
basisu_image_info ii;
|
||||
|
||||
if (!f->m_transcoder.get_image_info(f->m_pFile, f->m_file_size, ii, image_index))
|
||||
return 0;
|
||||
|
||||
return ii.m_total_levels;
|
||||
}
|
||||
|
||||
uint basis_get_image_width(void *h, uint image_index, uint level_index)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
uint orig_width, orig_height, total_blocks;
|
||||
if (!f->m_transcoder.get_image_level_desc(f->m_pFile, f->m_file_size, image_index, level_index, orig_width, orig_height, total_blocks))
|
||||
return 0;
|
||||
|
||||
return orig_width;
|
||||
}
|
||||
|
||||
uint basis_get_image_height(void *h, uint image_index, uint level_index)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
uint orig_width, orig_height, total_blocks;
|
||||
if (!f->m_transcoder.get_image_level_desc(f->m_pFile, f->m_file_size, image_index, level_index, orig_width, orig_height, total_blocks))
|
||||
return 0;
|
||||
|
||||
return orig_height;
|
||||
}
|
||||
|
||||
uint basis_get_image_transcoded_size_in_bytes(void *h, uint image_index, uint level_index, uint format)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
if (format >= cTFTotalTextureFormats)
|
||||
return 0;
|
||||
|
||||
uint bytes_per_block = basis_get_bytes_per_block((transcoder_texture_format)format);
|
||||
|
||||
uint orig_width, orig_height, total_blocks;
|
||||
if (!f->m_transcoder.get_image_level_desc(f->m_pFile, f->m_file_size, image_index, level_index, orig_width, orig_height, total_blocks))
|
||||
return 0;
|
||||
|
||||
return total_blocks * bytes_per_block;
|
||||
}
|
||||
|
||||
uint basis_start_transcoding(void *h)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
return f->m_transcoder.start_transcoding(f->m_pFile, f->m_file_size);
|
||||
}
|
||||
|
||||
uint basis_transcode_image(void *h, void *dst, uint dst_size_in_bytes,
|
||||
uint image_index, uint level_index, uint format,
|
||||
uint pvrtc_wrap_addressing, uint get_alpha_for_opaque_formats)
|
||||
{
|
||||
basis_file *f = static_cast<basis_file *>(h);
|
||||
if (!f)
|
||||
return 0;
|
||||
|
||||
assert(f->m_magic == MAGIC);
|
||||
if (f->m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
if (format >= cTFTotalTextureFormats)
|
||||
return 0;
|
||||
|
||||
uint bytes_per_block = basis_get_bytes_per_block((transcoder_texture_format)format);
|
||||
|
||||
return f->m_transcoder.transcode_image_level(f->m_pFile, f->m_file_size, image_index, level_index,
|
||||
dst, dst_size_in_bytes / bytes_per_block,
|
||||
(basist::transcoder_texture_format)format,
|
||||
(pvrtc_wrap_addressing ? basisu_transcoder::cDecodeFlagsPVRTCWrapAddressing : 0) | (get_alpha_for_opaque_formats ? basisu_transcoder::cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0));
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
@REM -O0 -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1
|
||||
@REM -O2 -s ASSERTIONS=0
|
||||
emcc -s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding']" -o basis.js ../transcoder/basisu_transcoder.cpp basis_wrappers.cpp -s TOTAL_MEMORY=60000000 -O2 -s ASSERTIONS=0 -I ../transcoder
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# rg - I haven't tested this shell script yet (I use build.bat on Windows)
|
||||
|
||||
~/emscripten_tutorial/emscripten/emcc \
|
||||
-s EXPORTED_FUNCTIONS="['allocate', '_malloc', '_free', '_basis_init','_basis_open','_basis_close','_basis_get_has_alpha','_basis_get_num_images','_basis_get_num_levels','_basis_get_image_width','_basis_get_image_height','_basis_get_image_transcoded_size_in_bytes','_basis_transcode_image','_basis_start_transcoding']" \
|
||||
-s TOTAL_MEMORY=60000000 -O2 -s ASSERTIONS=0 -I ../transcoder \
|
||||
-o basis.js \
|
||||
../transcoder/basisu_transcoder.cpp basis_wrappers.cpp && \
|
||||
chmod -R a+rX .
|
||||
@@ -0,0 +1,169 @@
|
||||
/**
|
||||
* @author Don McCurdy / https://www.donmccurdy.com
|
||||
* @author Austin Eng / https://github.com/austinEng
|
||||
* @author Shrek Shao / https://github.com/shrekshao
|
||||
*/
|
||||
|
||||
/**
|
||||
* An example three.js loader for Basis compressed textures.
|
||||
*/
|
||||
THREE.BasisTextureLoader = class BasisTextureLoader {
|
||||
|
||||
constructor () {
|
||||
|
||||
this.etcSupported = false;
|
||||
this.dxtSupported = false;
|
||||
this.pvrtcSupported = false;
|
||||
|
||||
this.format = null;
|
||||
|
||||
}
|
||||
|
||||
detectSupport ( renderer ) {
|
||||
|
||||
const context = renderer.context;
|
||||
|
||||
this.etcSupported = context.getExtension('WEBGL_compressed_texture_etc1');
|
||||
this.dxtSupported = context.getExtension('WEBGL_compressed_texture_s3tc');
|
||||
this.pvrtcSupported = context.getExtension('WEBGL_compressed_texture_pvrtc')
|
||||
|| context.getExtension('WEBKIT_WEBGL_compressed_texture_pvrtc');
|
||||
|
||||
if ( ! this.etcSupported && ! this.dxtSupported && ! this.pvrtcSupported ) {
|
||||
|
||||
throw new Error( 'THREE.BasisTextureLoader: No suitable compressed texture format found.' );
|
||||
|
||||
}
|
||||
|
||||
this.format = this.etcSupported
|
||||
? BASIS_FORMAT.cTFETC1
|
||||
: ( this.dxtSupported
|
||||
? BASIS_FORMAT.cTFBC1
|
||||
: BASIS_FORMAT.cTFPVRTC1_4_OPAQUE_ONLY );
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
load ( url, onLoad, onProgress, onError ) {
|
||||
|
||||
fetch( url )
|
||||
.then( ( res ) => res.arrayBuffer() )
|
||||
.then( (arrayBuffer) => this._createTexture( arrayBuffer ) )
|
||||
.then( onLoad )
|
||||
.catch( onError );
|
||||
|
||||
}
|
||||
|
||||
_createTexture ( arrayBuffer ) {
|
||||
|
||||
const BASIS_FORMAT = THREE.BasisTextureLoader.BASIS_FORMAT;
|
||||
const DXT_FORMAT_MAP = THREE.BasisTextureLoader.DXT_FORMAT_MAP;
|
||||
|
||||
const basisFile = new BasisFile( new Uint8Array( arrayBuffer ) );
|
||||
|
||||
const width = basisFile.getImageWidth( 0, 0 );
|
||||
const height = basisFile.getImageHeight( 0, 0 );
|
||||
const images = basisFile.getNumImages();
|
||||
const levels = basisFile.getNumLevels( 0 );
|
||||
|
||||
function cleanup () {
|
||||
|
||||
basisFile.close();
|
||||
basisFile.delete();
|
||||
|
||||
}
|
||||
|
||||
if ( ! width || ! height || ! images || ! levels ) {
|
||||
|
||||
cleanup();
|
||||
throw new Error( 'THREE.BasisTextureLoader: Invalid .basis file' );
|
||||
|
||||
}
|
||||
|
||||
if ( ! basisFile.startTranscoding() ) {
|
||||
|
||||
cleanup();
|
||||
throw new Error( 'THREE.BasisTextureLoader: .startTranscoding failed' );
|
||||
|
||||
}
|
||||
|
||||
const dst = new Uint8Array( basisFile.getImageTranscodedSizeInBytes( 0, 0, this.format ) );
|
||||
|
||||
const startTime = performance.now();
|
||||
|
||||
const status = basisFile.transcodeImage(
|
||||
dst,
|
||||
0,
|
||||
0,
|
||||
this.format,
|
||||
this.etcSupported ? 0 : ( this.dxtSupported ? 1 : 0 ),
|
||||
0
|
||||
);
|
||||
|
||||
console.log( `THREE.BasisTextureLoader: Transcode time: ${performance.now() - startTime}ms`, dst );
|
||||
|
||||
cleanup();
|
||||
|
||||
if ( ! status ) {
|
||||
|
||||
throw new Error( 'THREE.BasisTextureLoader: .transcodeImage failed.' );
|
||||
|
||||
}
|
||||
|
||||
const mipmaps = [ { data: dst, width, height } ];
|
||||
|
||||
let texture;
|
||||
|
||||
if ( this.etcSupported ) {
|
||||
|
||||
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_ETC1_Format );
|
||||
|
||||
} else if ( this.dxtSupported ) {
|
||||
|
||||
texture = new THREE.CompressedTexture( mipmaps, width, height, DXT_FORMAT_MAP[ this.format ], THREE.UnsignedByteType );
|
||||
|
||||
} else if ( this.pvrtcSupported ) {
|
||||
|
||||
texture = new THREE.CompressedTexture( mipmaps, width, height, THREE.RGB_PVRTC_4BPPV1_Format );
|
||||
|
||||
} else {
|
||||
|
||||
throw new Error( 'THREE.BasisTextureLoader: No supported format available.' );
|
||||
|
||||
}
|
||||
|
||||
texture.minFilter = THREE.LinearMipMapLinearFilter;
|
||||
texture.magFilter = THREE.LinearFilter;
|
||||
texture.encoding = THREE.sRGBEncoding;
|
||||
texture.generateMipmaps = false;
|
||||
texture.flipY = false;
|
||||
texture.needsUpdate = true;
|
||||
|
||||
return texture;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const BASIS_FORMAT = {
|
||||
cTFETC1: 0,
|
||||
cTFBC1: 1,
|
||||
cTFBC4: 2,
|
||||
cTFPVRTC1_4_OPAQUE_ONLY: 3,
|
||||
cTFBC7_M6_OPAQUE_ONLY: 4,
|
||||
cTFETC2: 5,
|
||||
cTFBC3: 6,
|
||||
cTFBC5: 7,
|
||||
};
|
||||
|
||||
// DXT formats, from:
|
||||
// http://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_s3tc/
|
||||
const DXT_FORMAT_MAP = {};
|
||||
const COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
|
||||
const COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
|
||||
const COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
|
||||
const COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
|
||||
DXT_FORMAT_MAP[ BASIS_FORMAT.cTFBC1 ] = COMPRESSED_RGB_S3TC_DXT1_EXT;
|
||||
DXT_FORMAT_MAP[ BASIS_FORMAT.cTFBC3 ] = COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
|
||||
THREE.BasisTextureLoader.BASIS_FORMAT = BASIS_FORMAT;
|
||||
THREE.BasisTextureLoader.DXT_FORMAT_MAP = DXT_FORMAT_MAP;
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
## Credits
|
||||
|
||||
* Contributors:
|
||||
* [Don McCurdy](https://www.donmccurdy.com)
|
||||
* [Austin Eng](https://github.com/austinEng)
|
||||
* [Shrek Shao](https://github.com/shrekshao)
|
||||
* Made with [three.js](https://threejs.org/).
|
||||
* Thanks to [AGI](http://agi.com/) for providing the glTF model.
|
||||
Executable
+254
@@ -0,0 +1,254 @@
|
||||
{
|
||||
"accessors": [
|
||||
{
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"type": "VEC3",
|
||||
"count": 55381,
|
||||
"min": [
|
||||
-527.3590087890625,
|
||||
-104.39354705810547,
|
||||
-495.3590087890625
|
||||
],
|
||||
"max": [
|
||||
599.3590087890625,
|
||||
-33.72303009033203,
|
||||
561.3590087890625
|
||||
],
|
||||
"bufferView": 0,
|
||||
"name": "mesh-0-0_Accessor_POSITION"
|
||||
},
|
||||
{
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"type": "VEC2",
|
||||
"count": 55381,
|
||||
"min": [
|
||||
0.0000017028407910402166,
|
||||
0.00000289904824057885
|
||||
],
|
||||
"max": [
|
||||
0.9993386268615723,
|
||||
0.9999556541442871
|
||||
],
|
||||
"bufferView": 1,
|
||||
"name": "mesh-0-0_Accessor_TEXCOORD_0"
|
||||
},
|
||||
{
|
||||
"byteOffset": 0,
|
||||
"componentType": 5123,
|
||||
"type": "SCALAR",
|
||||
"count": 151158,
|
||||
"bufferView": 4,
|
||||
"name": "mesh-0-0_Accessor_indices"
|
||||
},
|
||||
{
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"type": "VEC3",
|
||||
"count": 30806,
|
||||
"min": [
|
||||
-527.3590087890625,
|
||||
-105.48443603515625,
|
||||
-495.3590087890625
|
||||
],
|
||||
"max": [
|
||||
599.3590087890625,
|
||||
-26.287290573120117,
|
||||
561.3590087890625
|
||||
],
|
||||
"bufferView": 2,
|
||||
"name": "mesh-1-0_Accessor_POSITION"
|
||||
},
|
||||
{
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"type": "VEC2",
|
||||
"count": 30806,
|
||||
"min": [
|
||||
0.00000964943137660157,
|
||||
0.000003775880941248033
|
||||
],
|
||||
"max": [
|
||||
0.9999449253082275,
|
||||
0.9999914169311523
|
||||
],
|
||||
"bufferView": 3,
|
||||
"name": "mesh-1-0_Accessor_TEXCOORD_0"
|
||||
},
|
||||
{
|
||||
"byteOffset": 302316,
|
||||
"componentType": 5123,
|
||||
"type": "SCALAR",
|
||||
"count": 58476,
|
||||
"bufferView": 4,
|
||||
"name": "mesh-1-0_Accessor_indices"
|
||||
}
|
||||
],
|
||||
"asset": {
|
||||
"generator": "obj2gltf",
|
||||
"version": "2.0"
|
||||
},
|
||||
"buffers": [
|
||||
{
|
||||
"name": "mesh-0-0_Buffer_POSITION",
|
||||
"byteLength": 2143008,
|
||||
"uri": "mesh-0-0_Buffer_POSITION.bin"
|
||||
}
|
||||
],
|
||||
"bufferViews": [
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 664572,
|
||||
"byteOffset": 0,
|
||||
"target": 34962,
|
||||
"name": "bufferView_0",
|
||||
"byteStride": 12
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 443048,
|
||||
"byteOffset": 664572,
|
||||
"target": 34962,
|
||||
"name": "bufferView_0",
|
||||
"byteStride": 8
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 369672,
|
||||
"byteOffset": 1107620,
|
||||
"target": 34962,
|
||||
"name": "bufferView_0",
|
||||
"byteStride": 12
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 246448,
|
||||
"byteOffset": 1477292,
|
||||
"target": 34962,
|
||||
"name": "bufferView_0",
|
||||
"byteStride": 8
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteLength": 419268,
|
||||
"byteOffset": 1723740,
|
||||
"target": 34963,
|
||||
"name": "bufferView_1"
|
||||
}
|
||||
],
|
||||
"materials": [
|
||||
{
|
||||
"name": "Texture",
|
||||
"pbrMetallicRoughness": {
|
||||
"baseColorTexture": {
|
||||
"index": 0,
|
||||
"texCoord": 0
|
||||
},
|
||||
"metallicFactor": 0,
|
||||
"roughnessFactor": 1,
|
||||
"baseColorFactor": [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
]
|
||||
},
|
||||
"emissiveTexture": {
|
||||
"index": 0,
|
||||
"texCoord": 0
|
||||
},
|
||||
"alphaMode": "OPAQUE",
|
||||
"doubleSided": false,
|
||||
"emissiveFactor": [
|
||||
0,
|
||||
0,
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"meshes": [
|
||||
{
|
||||
"primitives": [
|
||||
{
|
||||
"attributes": {
|
||||
"POSITION": 0,
|
||||
"TEXCOORD_0": 1
|
||||
},
|
||||
"indices": 2,
|
||||
"material": 0,
|
||||
"mode": 4
|
||||
}
|
||||
],
|
||||
"name": "mesh-split_1"
|
||||
},
|
||||
{
|
||||
"primitives": [
|
||||
{
|
||||
"attributes": {
|
||||
"POSITION": 3,
|
||||
"TEXCOORD_0": 4
|
||||
},
|
||||
"indices": 5,
|
||||
"material": 0,
|
||||
"mode": 4
|
||||
}
|
||||
],
|
||||
"name": "mesh-split_2"
|
||||
}
|
||||
],
|
||||
"nodes": [
|
||||
{
|
||||
"children": [
|
||||
1
|
||||
],
|
||||
"name": "rootNode_0",
|
||||
"mesh": 0
|
||||
},
|
||||
{
|
||||
"mesh": 1
|
||||
}
|
||||
],
|
||||
"samplers": [
|
||||
{
|
||||
"magFilter": 9729,
|
||||
"minFilter": 9729,
|
||||
"wrapS": 33071,
|
||||
"wrapT": 33071,
|
||||
"name": "sampler_0"
|
||||
}
|
||||
],
|
||||
"scene": 0,
|
||||
"scenes": [
|
||||
{
|
||||
"nodes": [
|
||||
0
|
||||
],
|
||||
"name": "scene"
|
||||
}
|
||||
],
|
||||
"textures": [
|
||||
{
|
||||
"sampler": 0,
|
||||
"name": "textureAtlas",
|
||||
"extensions": {
|
||||
"GOOGLE_texture_basis": {
|
||||
"source": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"images": [
|
||||
{
|
||||
"name": "textureAtlasImage",
|
||||
"mimeType": "image/basis",
|
||||
"uri": "textureAtlasImage.basis"
|
||||
}
|
||||
],
|
||||
"extensionsUsed": [
|
||||
"GOOGLE_texture_basis"
|
||||
],
|
||||
"extensionsRequired": [
|
||||
"GOOGLE_texture_basis"
|
||||
]
|
||||
}
|
||||
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,117 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<head>
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@v0.104.0"></script>
|
||||
<script src="GLTFLoader.js"></script>
|
||||
<script src="BasisTextureLoader.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@v0.104.0/examples/js/controls/OrbitControls.js"></script>
|
||||
<script src="../transcoder/build/basis_transcoder.js"></script>
|
||||
<style>
|
||||
html, body { width:100%; height:100%; margin:0; padding:0; }
|
||||
canvas { display:block; }
|
||||
#panel { position: absolute; top: 10px; left: 10px; color: white; background-color:rgba(0.3, 0.3, 0.3, 0.3); padding: 0.5em; max-width: 400px;}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script>
|
||||
Module.onRuntimeInitialized = () => {
|
||||
|
||||
const { BasisFile, initializeBasis } = Module;
|
||||
|
||||
window.BasisFile = BasisFile;
|
||||
|
||||
initializeBasis();
|
||||
|
||||
// Initialize three.js scene and renderer.
|
||||
|
||||
const renderer = new THREE.WebGLRenderer( { antialias: true } );
|
||||
renderer.setSize( window.innerWidth, window.innerHeight );
|
||||
renderer.gammaOutput = true;
|
||||
renderer.gammaFactor = 2.2;
|
||||
|
||||
const scene = new THREE.Scene();
|
||||
scene.background = new THREE.Color( 0xf0f0f0 );
|
||||
|
||||
const light = new THREE.AmbientLight();
|
||||
scene.add( light );
|
||||
|
||||
const light2 = new THREE.PointLight();
|
||||
scene.add( light2 );
|
||||
|
||||
const camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
|
||||
camera.position.set( 8, 6, 5 );
|
||||
camera.lookAt( new THREE.Vector3( 0, -2, 0 ) );
|
||||
|
||||
const controls = new THREE.OrbitControls( camera, renderer.domElement );
|
||||
controls.autoRotate = true;
|
||||
|
||||
// Create BasisTextureLoader and detect supported target formats.
|
||||
|
||||
const basisLoader = new THREE.BasisTextureLoader();
|
||||
basisLoader.detectSupport( renderer );
|
||||
|
||||
const formatName = basisLoader.etcSupported
|
||||
? 'ETC1'
|
||||
: ( basisLoader.dxtSupported ? 'BC1' : 'PVRTC' );
|
||||
log(`Transcode to: <strong>${formatName}</strong>`);
|
||||
|
||||
// Register BasisTextureLoader for .basis extension.
|
||||
|
||||
THREE.Loader.Handlers.add( /\.basis$/, basisLoader );
|
||||
|
||||
// Create GLTFLoader, load model, and render.
|
||||
|
||||
const loader = new THREE.GLTFLoader();
|
||||
|
||||
loader.load( 'assets/AgiHqSmall.gltf', ( gltf ) => {
|
||||
|
||||
const model = gltf.scene;
|
||||
model.scale.set( 0.01, 0.01, 0.01 );
|
||||
|
||||
scene.add( model );
|
||||
|
||||
document.body.appendChild( renderer.domElement );
|
||||
|
||||
animate();
|
||||
|
||||
}, undefined, ( e ) => console.error( e ) );
|
||||
|
||||
// Main render loop.
|
||||
|
||||
function animate() {
|
||||
|
||||
requestAnimationFrame( animate );
|
||||
controls.update();
|
||||
renderer.render( scene, camera );
|
||||
|
||||
}
|
||||
|
||||
// Support viewport resizing.
|
||||
|
||||
window.addEventListener( 'resize', () => {
|
||||
|
||||
camera.aspect = window.innerWidth / window.innerHeight;
|
||||
camera.updateProjectionMatrix();
|
||||
renderer.setSize( window.innerWidth, window.innerHeight );
|
||||
|
||||
}, false );
|
||||
|
||||
}
|
||||
|
||||
function log(s) {
|
||||
|
||||
const div = document.createElement('div');
|
||||
div.innerHTML = s;
|
||||
document.getElementById('log').appendChild(div);
|
||||
|
||||
}
|
||||
</script>
|
||||
<div id="panel">
|
||||
<strong>Basis Texture Transcoder glTF Demo</strong>
|
||||
<div id="log"></div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 563 KiB |
+14
-317
@@ -1,318 +1,15 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="basis.js"></script>
|
||||
<script src="renderer.js"></script>
|
||||
<script src="dxt-to-rgb565.js"></script>
|
||||
<script type="text/javascript">
|
||||
function log(s) {
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = s;
|
||||
document.getElementById('logger').appendChild(div);
|
||||
}
|
||||
|
||||
function logTime(desc, t) {
|
||||
log(t + 'ms ' + desc);
|
||||
}
|
||||
|
||||
function isDef(v) {
|
||||
return typeof v != 'undefined';
|
||||
}
|
||||
|
||||
function elem(id) {
|
||||
return document.getElementById(id);
|
||||
}
|
||||
|
||||
formatTable = function(rows) {
|
||||
var colLengths = [];
|
||||
|
||||
for (var i = 0; i < rows.length; i++) {
|
||||
var row = rows[i];
|
||||
for (var j = 0; j < row.length; j++) {
|
||||
if (colLengths.length <= j) colLengths.push(0);
|
||||
if (colLengths[j] < row[j].length) colLengths[j] = row[j].length;
|
||||
}
|
||||
<!DOCTYPE html>
|
||||
<style>
|
||||
body {
|
||||
max-width: 500px;
|
||||
}
|
||||
|
||||
function formatRow(row) {
|
||||
var parts = [];
|
||||
for (var i = 0; i < colLengths.length; i++) {
|
||||
var s = row.length > i ? row[i] : '';
|
||||
var padding = (new Array(1 + colLengths[i] - s.length)).join(' ');
|
||||
if (s && s[0] >= '0' && s[0] <= '9') {
|
||||
// Right-align numbers.
|
||||
parts.push(padding + s);
|
||||
} else {
|
||||
parts.push(s + padding);
|
||||
}
|
||||
}
|
||||
return parts.join(' | ');
|
||||
}
|
||||
|
||||
var width = 0;
|
||||
for (var i = 0; i < colLengths.length; i++) {
|
||||
width += colLengths[i];
|
||||
// Add another 3 for the separator.
|
||||
if (i != 0) width += 3;
|
||||
}
|
||||
|
||||
var lines = [];
|
||||
lines.push(formatRow(rows[0]));
|
||||
lines.push((new Array(width + 1)).join('-'));
|
||||
for (var i = 1; i < rows.length; i++) {
|
||||
lines.push(formatRow(rows[i]));
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
};
|
||||
|
||||
|
||||
function loadArrayBuffer(uri, callback) {
|
||||
log('Loading ' + uri + '...');
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.responseType = "arraybuffer";
|
||||
xhr.open('GET', uri, true);
|
||||
xhr.onreadystatechange = function(e) {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
callback(xhr.response);
|
||||
}
|
||||
}
|
||||
xhr.send(null);
|
||||
}
|
||||
|
||||
// DXT formats, from:
|
||||
// http://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_s3tc/
|
||||
COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
|
||||
COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
|
||||
COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
|
||||
COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
|
||||
|
||||
BASIS_FORMAT = {
|
||||
|
||||
cTFETC1: 0,
|
||||
cTFBC1: 1,
|
||||
cTFBC4: 2,
|
||||
cTFPVRTC1_4_OPAQUE_ONLY: 3,
|
||||
cTFBC7_M6_OPAQUE_ONLY: 4,
|
||||
cTFETC2: 5,
|
||||
cTFBC3: 6,
|
||||
cTFBC5: 7,
|
||||
};
|
||||
|
||||
BASIS_FORMAT_NAMES = {};
|
||||
for (var name in BASIS_FORMAT) {
|
||||
BASIS_FORMAT_NAMES[BASIS_FORMAT[name]] = name;
|
||||
}
|
||||
|
||||
DXT_FORMAT_MAP = {};
|
||||
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC1] = COMPRESSED_RGB_S3TC_DXT1_EXT;
|
||||
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC3] = COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
|
||||
var dxtSupported = true;
|
||||
var drawMode = 0;
|
||||
|
||||
var tex, width, height, images, levels, have_alpha, alignedWidth, alignedHeight, format;
|
||||
|
||||
function redraw()
|
||||
{
|
||||
if (!width)
|
||||
return;
|
||||
|
||||
if (dxtSupported)
|
||||
{
|
||||
renderer.drawTexture(tex, alignedWidth, alignedHeight, drawMode);
|
||||
}
|
||||
else if (format == BASIS_FORMAT.cTFBC1)
|
||||
{
|
||||
renderer.drawTexture(tex, alignedWidth, alignedHeight, drawMode);
|
||||
}
|
||||
else
|
||||
{
|
||||
log('TODO: Implement BC3->RGBA conversion for browsers that don\'t support BC3');
|
||||
}
|
||||
}
|
||||
|
||||
function dataLoaded(data)
|
||||
{
|
||||
log('Done loading .basis file, decoded header:');
|
||||
|
||||
Module._basis_init();
|
||||
|
||||
var srcSize = data.byteLength;
|
||||
var bytes = new Uint8Array(data);
|
||||
var src = Module._malloc(srcSize);
|
||||
arrayBufferCopy(bytes, Module.HEAPU8, src, srcSize);
|
||||
|
||||
var basisHandle = Module._basis_open(src, srcSize);
|
||||
if (!basisHandle)
|
||||
{
|
||||
log('Failed opening basis file!');
|
||||
width = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
width = Module._basis_get_image_width(basisHandle, 0, 0);
|
||||
height = Module._basis_get_image_height(basisHandle, 0, 0);
|
||||
images = Module._basis_get_num_images(basisHandle, 0);
|
||||
levels = Module._basis_get_num_levels(basisHandle, 0);
|
||||
has_alpha = Module._basis_get_has_alpha(basisHandle);
|
||||
|
||||
if (!width || !height || !images || !levels)
|
||||
{
|
||||
Module._basis_close(basisHandle);
|
||||
Module._free(src);
|
||||
width = 0;
|
||||
|
||||
log('Failed getting info from basis file!');
|
||||
return;
|
||||
}
|
||||
|
||||
format = BASIS_FORMAT.cTFBC1;
|
||||
if (has_alpha)
|
||||
{
|
||||
format = BASIS_FORMAT.cTFBC3;
|
||||
|
||||
log('Decoding .basis data to BC3');
|
||||
}
|
||||
else
|
||||
{
|
||||
log('Decoding .basis data to BC1');
|
||||
}
|
||||
|
||||
log('width: ' + width);
|
||||
log('height: ' + height);
|
||||
log('images: ' + images);
|
||||
log('first image mipmap levels: ' + levels);
|
||||
log('has_alpha: ' + has_alpha);
|
||||
|
||||
alignedWidth = (width + 3) & ~3;
|
||||
alignedHeight = (height + 3) & ~3;
|
||||
|
||||
var canvas = elem('canvas');
|
||||
canvas.width = alignedWidth;
|
||||
canvas.height = alignedHeight;
|
||||
|
||||
var status = Module._basis_start_transcoding(basisHandle);
|
||||
if (!status)
|
||||
{
|
||||
Module._basis_close(basisHandle);
|
||||
Module._free(src);
|
||||
width = 0;
|
||||
|
||||
log('basis_start_transcoding() failed!');
|
||||
return;
|
||||
}
|
||||
|
||||
var dstSize = Module._basis_get_image_transcoded_size_in_bytes(basisHandle, 0, 0, format);
|
||||
var dst = Module._malloc(dstSize);
|
||||
|
||||
if (!Module._basis_transcode_image(basisHandle, dst, dstSize, 0, 0, format, 1, 0))
|
||||
{
|
||||
Module._basis_close(basisHandle);
|
||||
Module._free(src);
|
||||
Module._free(dst);
|
||||
width = 0;
|
||||
|
||||
log('basis_transcode_image() failed!');
|
||||
return;
|
||||
}
|
||||
|
||||
var dxtData = new Uint8Array(Module.HEAPU8.buffer, dst, dstSize);
|
||||
|
||||
if (dxtSupported)
|
||||
{
|
||||
tex = renderer.createDxtTexture(dxtData, alignedWidth, alignedHeight, DXT_FORMAT_MAP[format]);
|
||||
}
|
||||
else
|
||||
{
|
||||
var rgb565Data = dxtToRgb565(Module.HEAPU16, dst / 2, alignedWidth, alignedHeight);
|
||||
|
||||
tex = renderer.createRgb565Texture(rgb565Data, alignedWidth, alignedHeight);
|
||||
}
|
||||
|
||||
redraw();
|
||||
|
||||
Module._free(dst);
|
||||
Module._basis_close(basisHandle);
|
||||
Module._free(src);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint8Array} src
|
||||
* @param {Uint8Array} dst
|
||||
*/
|
||||
function arrayBufferCopy(src, dst, dstByteOffset, numBytes) {
|
||||
dst32Offset = dstByteOffset / 4;
|
||||
var tail = (numBytes % 4);
|
||||
var src32 = new Uint32Array(src.buffer, 0, (numBytes - tail) / 4);
|
||||
var dst32 = new Uint32Array(dst.buffer);
|
||||
for (var i = 0; i < src32.length; i++) {
|
||||
dst32[dst32Offset + i] = src32[i];
|
||||
}
|
||||
for (var i = numBytes - tail; i < numBytes; i++) {
|
||||
dst[dstByteOffset + i] = src[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function run() {
|
||||
elem('logger').innerHTML = '';
|
||||
loadArrayBuffer(elem('file').value, dataLoaded);
|
||||
}
|
||||
|
||||
|
||||
function initialize() {
|
||||
var gl = elem('canvas').getContext('experimental-webgl');
|
||||
// Load the DXT extension, and verify it exists.
|
||||
if (!gl.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc')) {
|
||||
dxtSupported = false;
|
||||
elem('nodxt').style.display = 'block';
|
||||
}
|
||||
|
||||
window.renderer = new Renderer(gl);
|
||||
|
||||
elem('file').addEventListener('keydown', function(e) {
|
||||
if (e.keyCode == 13) {
|
||||
run();
|
||||
}
|
||||
}, false);
|
||||
|
||||
run();
|
||||
}
|
||||
|
||||
function alphaBlend() { drawMode = 0; redraw(); }
|
||||
function viewRGB() { drawMode = 1; redraw(); }
|
||||
function viewAlpha() { drawMode = 2; redraw(); }
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body onload="initialize()">
|
||||
<br>
|
||||
<div style="font-size: 24pt; font-weight: bold">
|
||||
.basis->BC1/BC3 transcoder test
|
||||
</div>
|
||||
|
||||
<br>This demo uses the Basis C++ transcoder (compiled to Javascript using Emscripten) to transcode a .basis file to BC1/BC3.
|
||||
<br>Thanks to Evan Parker for providing <a href="https://github.com/toji/webgl-texture-utils">webgl-texture-utils</a> and this test bed.
|
||||
<br>
|
||||
<br>
|
||||
.basis file:
|
||||
<input id="file" type="text" size=30 value="kodim20.basis"></input>
|
||||
<input type="button" value="Run!" onclick="run()"></input>
|
||||
<br>
|
||||
<br>
|
||||
<input type="button" value="Alpha blend" onclick="alphaBlend()"></input>
|
||||
<input type="button" value="View RGB" onclick="viewRGB()"></input>
|
||||
<input type="button" value="View Alpha" onclick="viewAlpha()"></input>
|
||||
|
||||
<div style="position:absolute; left: 525px; top:130px; font-size: 20pt; font-weight: bold; color: red">
|
||||
<div id="nodxt" style="display: none; width: 768px; font-size: 20pt; font-weight: bold; color: red">
|
||||
NOTE: Your browser does not support DXT, so using RGB565
|
||||
for the texture below. To get DXT, try Chrome 19+
|
||||
(beta channel as of 2012-04-20; graphics card DXT support also required).
|
||||
</div>
|
||||
<canvas id='canvas'></canvas>
|
||||
</div>
|
||||
<br><br>
|
||||
<div id='logger'></div>
|
||||
</body>
|
||||
</html>
|
||||
</style>
|
||||
<h1>Basis WebGL demos</h1>
|
||||
<p>
|
||||
Example webpages using the transcoder (compiled to WASM) to render
|
||||
Basis textures in WebGL. Both demos require WebAssembly and WebGL support.
|
||||
</p>
|
||||
<ul>
|
||||
<li><a href="texture/">Simple texture</a></li>
|
||||
<li><a href="gltf/">3D model (glTF 2.0)</a></li>
|
||||
</ul>
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="renderer.js"></script>
|
||||
<script src="dxt-to-rgb565.js"></script>
|
||||
<script src="../transcoder/build/basis_transcoder.js"></script>
|
||||
<script type="text/javascript">
|
||||
function log(s) {
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = s;
|
||||
document.getElementById('logger').appendChild(div);
|
||||
}
|
||||
|
||||
function logTime(desc, t) {
|
||||
log(t + 'ms ' + desc);
|
||||
}
|
||||
|
||||
function isDef(v) {
|
||||
return typeof v != 'undefined';
|
||||
}
|
||||
|
||||
function elem(id) {
|
||||
return document.getElementById(id);
|
||||
}
|
||||
|
||||
formatTable = function(rows) {
|
||||
var colLengths = [];
|
||||
|
||||
for (var i = 0; i < rows.length; i++) {
|
||||
var row = rows[i];
|
||||
for (var j = 0; j < row.length; j++) {
|
||||
if (colLengths.length <= j) colLengths.push(0);
|
||||
if (colLengths[j] < row[j].length) colLengths[j] = row[j].length;
|
||||
}
|
||||
}
|
||||
|
||||
function formatRow(row) {
|
||||
var parts = [];
|
||||
for (var i = 0; i < colLengths.length; i++) {
|
||||
var s = row.length > i ? row[i] : '';
|
||||
var padding = (new Array(1 + colLengths[i] - s.length)).join(' ');
|
||||
if (s && s[0] >= '0' && s[0] <= '9') {
|
||||
// Right-align numbers.
|
||||
parts.push(padding + s);
|
||||
} else {
|
||||
parts.push(s + padding);
|
||||
}
|
||||
}
|
||||
return parts.join(' | ');
|
||||
}
|
||||
|
||||
var width = 0;
|
||||
for (var i = 0; i < colLengths.length; i++) {
|
||||
width += colLengths[i];
|
||||
// Add another 3 for the separator.
|
||||
if (i != 0) width += 3;
|
||||
}
|
||||
|
||||
var lines = [];
|
||||
lines.push(formatRow(rows[0]));
|
||||
lines.push((new Array(width + 1)).join('-'));
|
||||
for (var i = 1; i < rows.length; i++) {
|
||||
lines.push(formatRow(rows[i]));
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
};
|
||||
|
||||
|
||||
function loadArrayBuffer(uri, callback) {
|
||||
log('Loading ' + uri + '...');
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.responseType = "arraybuffer";
|
||||
xhr.open('GET', uri, true);
|
||||
xhr.onreadystatechange = function(e) {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
callback(xhr.response);
|
||||
}
|
||||
}
|
||||
xhr.send(null);
|
||||
}
|
||||
|
||||
// DXT formats, from:
|
||||
// http://www.khronos.org/registry/webgl/extensions/WEBGL_compressed_texture_s3tc/
|
||||
COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
|
||||
COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
|
||||
COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
|
||||
COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
|
||||
|
||||
BASIS_FORMAT = {
|
||||
cTFETC1: 0,
|
||||
cTFBC1: 1,
|
||||
cTFBC4: 2,
|
||||
cTFPVRTC1_4_OPAQUE_ONLY: 3,
|
||||
cTFBC7_M6_OPAQUE_ONLY: 4,
|
||||
cTFETC2: 5,
|
||||
cTFBC3: 6,
|
||||
cTFBC5: 7,
|
||||
};
|
||||
|
||||
BASIS_FORMAT_NAMES = {};
|
||||
for (var name in BASIS_FORMAT) {
|
||||
BASIS_FORMAT_NAMES[BASIS_FORMAT[name]] = name;
|
||||
}
|
||||
|
||||
DXT_FORMAT_MAP = {};
|
||||
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC1] = COMPRESSED_RGB_S3TC_DXT1_EXT;
|
||||
DXT_FORMAT_MAP[BASIS_FORMAT.cTFBC3] = COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||
|
||||
var dxtSupported = true;
|
||||
var drawMode = 0;
|
||||
|
||||
var tex, width, height, images, levels, have_alpha, alignedWidth, alignedHeight, format;
|
||||
|
||||
function redraw()
|
||||
{
|
||||
if (!width)
|
||||
return;
|
||||
|
||||
if (dxtSupported)
|
||||
{
|
||||
renderer.drawTexture(tex, alignedWidth, alignedHeight, drawMode);
|
||||
}
|
||||
else if (format == BASIS_FORMAT.cTFBC1)
|
||||
{
|
||||
renderer.drawTexture(tex, alignedWidth, alignedHeight, drawMode);
|
||||
}
|
||||
else
|
||||
{
|
||||
log('TODO: Implement BC3->RGBA conversion for browsers that don\'t support BC3');
|
||||
}
|
||||
}
|
||||
|
||||
function dataLoaded(data)
|
||||
{
|
||||
log('Done loading .basis file, decoded header:');
|
||||
|
||||
const { BasisFile, initializeBasis } = Module;
|
||||
initializeBasis();
|
||||
|
||||
const startTime = performance.now();
|
||||
|
||||
const basisFile = new BasisFile(new Uint8Array(data));
|
||||
|
||||
width = basisFile.getImageWidth(0, 0);
|
||||
height = basisFile.getImageHeight(0, 0);
|
||||
images = basisFile.getNumImages();
|
||||
levels = basisFile.getNumLevels(0);
|
||||
has_alpha = basisFile.getHasAlpha();
|
||||
|
||||
if (!width || !height || !images || !levels) {
|
||||
console.warn('Invalid .basis file');
|
||||
basisFile.close();
|
||||
basisFile.delete();
|
||||
return;
|
||||
}
|
||||
|
||||
format = BASIS_FORMAT.cTFBC1;
|
||||
if (has_alpha)
|
||||
{
|
||||
format = BASIS_FORMAT.cTFBC3;
|
||||
|
||||
log('Decoding .basis data to BC3');
|
||||
}
|
||||
else
|
||||
{
|
||||
log('Decoding .basis data to BC1');
|
||||
}
|
||||
|
||||
if (!basisFile.startTranscoding()) {
|
||||
console.warn('startTranscoding failed');
|
||||
basisFile.close();
|
||||
basisFile.delete();
|
||||
return;
|
||||
}
|
||||
|
||||
const dstSize = basisFile.getImageTranscodedSizeInBytes(0, 0, format);
|
||||
const dst = new Uint8Array(dstSize);
|
||||
|
||||
if (!basisFile.transcodeImage(dst, 0, 0, format, 1, 0)) {
|
||||
console.warn('transcodeImage failed');
|
||||
basisFile.close();
|
||||
basisFile.delete();
|
||||
return;
|
||||
}
|
||||
|
||||
const elapsed = performance.now() - startTime;
|
||||
|
||||
basisFile.close();
|
||||
basisFile.delete();
|
||||
|
||||
log('width: ' + width);
|
||||
log('height: ' + height);
|
||||
log('images: ' + images);
|
||||
log('first image mipmap levels: ' + levels);
|
||||
log('has_alpha: ' + has_alpha);
|
||||
logTime('transcoding time', elapsed.toFixed(2));
|
||||
|
||||
alignedWidth = (width + 3) & ~3;
|
||||
alignedHeight = (height + 3) & ~3;
|
||||
|
||||
var canvas = elem('canvas');
|
||||
canvas.width = alignedWidth;
|
||||
canvas.height = alignedHeight;
|
||||
|
||||
if (dxtSupported)
|
||||
{
|
||||
tex = renderer.createDxtTexture(dst, alignedWidth, alignedHeight, DXT_FORMAT_MAP[format]);
|
||||
}
|
||||
else
|
||||
{
|
||||
var rgb565Data = dxtToRgb565(new Uint16Array(dst.buffer), 0, alignedWidth, alignedHeight);
|
||||
|
||||
tex = renderer.createRgb565Texture(rgb565Data, alignedWidth, alignedHeight);
|
||||
}
|
||||
|
||||
redraw();
|
||||
}
|
||||
|
||||
function runLoadFile() {
|
||||
elem('logger').innerHTML = '';
|
||||
loadArrayBuffer(elem('file').value, dataLoaded);
|
||||
}
|
||||
|
||||
function alphaBlend() { drawMode = 0; redraw(); }
|
||||
function viewRGB() { drawMode = 1; redraw(); }
|
||||
function viewAlpha() { drawMode = 2; redraw(); }
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<br>
|
||||
<div style="font-size: 24pt; font-weight: bold">
|
||||
.basis->BC1/BC3 transcoder test
|
||||
</div>
|
||||
|
||||
<br>This demo uses the Basis C++ transcoder (compiled to Javascript using Emscripten) to transcode a .basis file to BC1/BC3.
|
||||
<br>Thanks to Evan Parker for providing <a href="https://github.com/toji/webgl-texture-utils">webgl-texture-utils</a> and this test bed.
|
||||
<br>
|
||||
<br>
|
||||
.basis file:
|
||||
<input id="file" type="text" size=30 value="assets/kodim20.basis"></input>
|
||||
<input type="button" value="Run!" onclick="runLoadFile()"></input>
|
||||
<br>
|
||||
<br>
|
||||
<input type="button" value="Alpha blend" onclick="alphaBlend()"></input>
|
||||
<input type="button" value="View RGB" onclick="viewRGB()"></input>
|
||||
<input type="button" value="View Alpha" onclick="viewAlpha()"></input>
|
||||
|
||||
<div style="position:absolute; left: 525px; top:130px; font-size: 20pt; font-weight: bold; color: red">
|
||||
<div id="nodxt" style="display: none; width: 768px; font-size: 20pt; font-weight: bold; color: red">
|
||||
NOTE: Your browser does not support DXT, so using RGB565
|
||||
for the texture below. To get DXT, try Chrome 19+
|
||||
(beta channel as of 2012-04-20; graphics card DXT support also required).
|
||||
</div>
|
||||
<canvas id='canvas'></canvas>
|
||||
</div>
|
||||
<br><br>
|
||||
<div id='logger'></div>
|
||||
</body>
|
||||
<script>
|
||||
Module.onRuntimeInitialized = () => {
|
||||
var gl = elem('canvas').getContext('webgl');
|
||||
// Load the DXT extension, and verify it exists.
|
||||
if (!gl.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc')) {
|
||||
dxtSupported = false;
|
||||
elem('nodxt').style.display = 'block';
|
||||
}
|
||||
|
||||
window.renderer = new Renderer(gl);
|
||||
|
||||
elem('file').addEventListener('keydown', function(e) {
|
||||
if (e.keyCode == 13) {
|
||||
runLoadFile();
|
||||
}
|
||||
}, false);
|
||||
|
||||
runLoadFile();
|
||||
}
|
||||
</script>
|
||||
</html>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 522 KiB |
@@ -0,0 +1,3 @@
|
||||
build/*
|
||||
!build/basis_transcoder.js
|
||||
!build/basis_transcoder.wasm
|
||||
@@ -0,0 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
project(basisu_transcoder_js)
|
||||
|
||||
if (EMSCRIPTEN)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
add_executable(basis_transcoder.js
|
||||
../../transcoder/basisu_transcoder.cpp
|
||||
basis_wrappers.cpp
|
||||
)
|
||||
|
||||
target_include_directories(basis_transcoder.js PRIVATE ../../transcoder)
|
||||
|
||||
set_target_properties(basis_transcoder.js PROPERTIES
|
||||
OUTPUT_NAME "basis_transcoder"
|
||||
SUFFIX ".js"
|
||||
LINK_FLAGS "--bind -s ALLOW_MEMORY_GROWTH=1 -O3 -s ASSERTIONS=0")
|
||||
endif()
|
||||
@@ -0,0 +1,7 @@
|
||||
Prebuilt versions of `basis_transcoder.js` and `basis_transcoder.wasm` are included in the `build/` folder, and are sufficient for local demos. To build the transcoder yourself, first install emscripten ([tutorial](https://webassembly.org/getting-started/developers-guide/)) and cmake ([download](https://cmake.org/download/)). Then run:
|
||||
|
||||
```shell
|
||||
cd webgl/transcoder/build/
|
||||
emcmake cmake ../
|
||||
make
|
||||
```
|
||||
@@ -0,0 +1,205 @@
|
||||
// basis_wrappers.cpp - Simple C-style wrappers to the C++ transcoder for WebGL use.
|
||||
#include "basisu_transcoder.h"
|
||||
#include <emscripten/bind.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace emscripten;
|
||||
using namespace basist;
|
||||
|
||||
static basist::etc1_global_selector_codebook *g_pGlobal_codebook;
|
||||
|
||||
void basis_init()
|
||||
{
|
||||
basisu_transcoder_init();
|
||||
|
||||
if (!g_pGlobal_codebook)
|
||||
g_pGlobal_codebook = new basist::etc1_global_selector_codebook(g_global_selector_cb_size, g_global_selector_cb);
|
||||
}
|
||||
|
||||
#define MAGIC 0xDEADBEE1
|
||||
|
||||
struct basis_file
|
||||
{
|
||||
int m_magic = 0;
|
||||
basisu_transcoder m_transcoder;
|
||||
std::vector<uint8_t> m_file;
|
||||
|
||||
basis_file(const emscripten::val& jsBuffer)
|
||||
: m_file([&]() {
|
||||
size_t byteLength = jsBuffer["byteLength"].as<size_t>();
|
||||
return std::vector<uint8_t>(byteLength);
|
||||
}()),
|
||||
m_transcoder(g_pGlobal_codebook)
|
||||
{
|
||||
unsigned int length = jsBuffer["length"].as<unsigned int>();
|
||||
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
|
||||
emscripten::val memoryView = jsBuffer["constructor"].new_(memory, reinterpret_cast<uintptr_t>(m_file.data()), length);
|
||||
memoryView.call<void>("set", jsBuffer);
|
||||
|
||||
if (!m_transcoder.validate_header(m_file.data(), m_file.size())) {
|
||||
m_file.clear();
|
||||
std::cerr << "Invalid Basis header" << std::endl;
|
||||
}
|
||||
|
||||
// Initialized after validation
|
||||
m_magic = MAGIC;
|
||||
}
|
||||
|
||||
void close() {
|
||||
assert(m_magic == MAGIC);
|
||||
m_file.clear();
|
||||
}
|
||||
|
||||
uint32_t getHasAlpha() {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
basisu_image_level_info li;
|
||||
if (!m_transcoder.get_image_level_info(m_file.data(), m_file.size(), li, 0, 0))
|
||||
return 0;
|
||||
|
||||
return li.m_alpha_flag;
|
||||
}
|
||||
|
||||
uint32_t getNumImages() {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
return m_transcoder.get_total_images(m_file.data(), m_file.size());
|
||||
}
|
||||
|
||||
uint32_t getNumLevels(uint32_t image_index) {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
basisu_image_info ii;
|
||||
if (!m_transcoder.get_image_info(m_file.data(), m_file.size(), ii, image_index))
|
||||
return 0;
|
||||
|
||||
return ii.m_total_levels;
|
||||
}
|
||||
|
||||
uint32_t getImageWidth(uint32_t image_index, uint32_t level_index) {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
uint32_t orig_width, orig_height, total_blocks;
|
||||
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_blocks))
|
||||
return 0;
|
||||
|
||||
return orig_width;
|
||||
}
|
||||
|
||||
uint32_t getImageHeight(uint32_t image_index, uint32_t level_index) {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
uint32_t orig_width, orig_height, total_blocks;
|
||||
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_blocks))
|
||||
return 0;
|
||||
|
||||
return orig_height;
|
||||
}
|
||||
|
||||
uint32_t getImageTranscodedSizeInBytes(uint32_t image_index, uint32_t level_index, uint32_t format) {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
if (format >= cTFTotalTextureFormats)
|
||||
return 0;
|
||||
|
||||
uint32_t bytes_per_block = basis_get_bytes_per_block(static_cast<transcoder_texture_format>(format));
|
||||
|
||||
uint32_t orig_width, orig_height, total_blocks;
|
||||
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_blocks))
|
||||
return 0;
|
||||
|
||||
return total_blocks * bytes_per_block;
|
||||
}
|
||||
|
||||
uint32_t startTranscoding() {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
return m_transcoder.start_transcoding(m_file.data(), m_file.size());
|
||||
}
|
||||
|
||||
uint32_t transcodeImage(const emscripten::val& dst, uint32_t image_index, uint32_t level_index, uint32_t format, uint32_t pvrtc_wrap_addressing, uint32_t get_alpha_for_opaque_formats) {
|
||||
assert(m_magic == MAGIC);
|
||||
if (m_magic != MAGIC)
|
||||
return 0;
|
||||
|
||||
if (format >= cTFTotalTextureFormats)
|
||||
return 0;
|
||||
|
||||
uint32_t bytes_per_block = basis_get_bytes_per_block(static_cast<transcoder_texture_format>(format));
|
||||
|
||||
uint32_t orig_width, orig_height, total_blocks;
|
||||
if (!m_transcoder.get_image_level_desc(m_file.data(), m_file.size(), image_index, level_index, orig_width, orig_height, total_blocks))
|
||||
return 0;
|
||||
|
||||
uint32_t required_size = total_blocks * bytes_per_block;
|
||||
|
||||
std::vector<uint8_t> dst_data(required_size);
|
||||
|
||||
uint32_t status = m_transcoder.transcode_image_level(
|
||||
m_file.data(), m_file.size(), image_index, level_index,
|
||||
dst_data.data(), dst_data.size() / bytes_per_block,
|
||||
static_cast<basist::transcoder_texture_format>(format),
|
||||
(
|
||||
(pvrtc_wrap_addressing ? basisu_transcoder::cDecodeFlagsPVRTCWrapAddressing : 0) |
|
||||
(get_alpha_for_opaque_formats ? basisu_transcoder::cDecodeFlagsTranscodeAlphaDataToOpaqueFormats : 0)
|
||||
));
|
||||
|
||||
emscripten::val memory = emscripten::val::module_property("HEAP8")["buffer"];
|
||||
emscripten::val memoryView = emscripten::val::global("Uint8Array").new_(memory, reinterpret_cast<uintptr_t>(dst_data.data()), dst_data.size());
|
||||
|
||||
dst.call<void>("set", memoryView);
|
||||
return status;
|
||||
}
|
||||
};
|
||||
|
||||
EMSCRIPTEN_BINDINGS(basis_transcoder) {
|
||||
|
||||
function("initializeBasis", &basis_init);
|
||||
|
||||
class_<basis_file>("BasisFile")
|
||||
.constructor<const emscripten::val&>()
|
||||
.function("close", optional_override([](basis_file& self) {
|
||||
return self.close();
|
||||
}))
|
||||
.function("getHasAlpha", optional_override([](basis_file& self) {
|
||||
return self.getHasAlpha();
|
||||
}))
|
||||
.function("getNumImages", optional_override([](basis_file& self) {
|
||||
return self.getNumImages();
|
||||
}))
|
||||
.function("getNumLevels", optional_override([](basis_file& self, uint32_t imageIndex) {
|
||||
return self.getNumLevels(imageIndex);
|
||||
}))
|
||||
.function("getImageWidth", optional_override([](basis_file& self, uint32_t imageIndex, uint32_t levelIndex) {
|
||||
return self.getImageWidth(imageIndex, levelIndex);
|
||||
}))
|
||||
.function("getImageHeight", optional_override([](basis_file& self, uint32_t imageIndex, uint32_t levelIndex) {
|
||||
return self.getImageHeight(imageIndex, levelIndex);
|
||||
}))
|
||||
.function("getImageTranscodedSizeInBytes", optional_override([](basis_file& self, uint32_t imageIndex, uint32_t levelIndex, uint32_t format) {
|
||||
return self.getImageTranscodedSizeInBytes(imageIndex, levelIndex, format);
|
||||
}))
|
||||
.function("startTranscoding", optional_override([](basis_file& self) {
|
||||
return self.startTranscoding();
|
||||
}))
|
||||
.function("transcodeImage", optional_override([](basis_file& self, const emscripten::val& dst, uint32_t imageIndex, uint32_t levelIndex, uint32_t format, uint32_t pvrtcWrapAddressing, uint32_t getAlphaForOpaqueFormats) {
|
||||
return self.transcodeImage(dst, imageIndex, levelIndex, format, pvrtcWrapAddressing, getAlphaForOpaqueFormats);
|
||||
}))
|
||||
;
|
||||
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
Reference in New Issue
Block a user