Skip to content

Commit

Permalink
feat(3dtiles): add method to enable ktx2 loader for gltf
Browse files Browse the repository at this point in the history
  • Loading branch information
jailln committed Oct 5, 2023
1 parent b88de83 commit a260109
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 9 deletions.
4 changes: 4 additions & 0 deletions config/threeExamples.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ export default {
filesExamples: [
'./loaders/GLTFLoader.js',
'./loaders/DRACOLoader.js',
'./loaders/KTX2Loader.js',
'./loaders/DDSLoader.js',
'./utils/BufferGeometryUtils.js',
'./utils/WorkerPool.js',
'./capabilities/WebGL.js',
'./libs/ktx-parse.module.js',
'./libs/zstddec.module.js'
],
};
46 changes: 46 additions & 0 deletions examples/libs/basis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Basis Universal GPU Texture Compression

Basis Universal is a "[supercompressed](http://gamma.cs.unc.edu/GST/gst.pdf)"
GPU texture and texture video compression system that outputs a highly
compressed intermediate file format (.basis) that can be quickly transcoded to
a wide variety of GPU texture compression formats.

[GitHub](https://github.com/BinomialLLC/basis_universal)

## Transcoders

Basis Universal texture data may be used in two different file formats:
`.basis` and `.ktx2`, where `ktx2` is a standardized wrapper around basis texture data.

For further documentation about the Basis compressor and transcoder, refer to
the [Basis GitHub repository](https://github.com/BinomialLLC/basis_universal).

The folder contains two files required for transcoding `.basis` or `.ktx2` textures:

* `basis_transcoder.js` — JavaScript wrapper for the WebAssembly transcoder.
* `basis_transcoder.wasm` — WebAssembly transcoder.

Both are dependencies of `KTX2Loader`:

```js
const ktx2Loader = new KTX2Loader();
ktx2Loader.setTranscoderPath( 'examples/jsm/libs/basis/' );
ktx2Loader.detectSupport( renderer );
ktx2Loader.load( 'diffuse.ktx2', function ( texture ) {

const material = new THREE.MeshStandardMaterial( { map: texture } );

}, function () {

console.log( 'onProgress' );

}, function ( e ) {

console.error( e );

} );
```

## License

[Apache License 2.0](https://github.com/BinomialLLC/basis_universal/blob/master/LICENSE)
21 changes: 21 additions & 0 deletions examples/libs/basis/basis_transcoder.js

Large diffs are not rendered by default.

Binary file added examples/libs/basis/basis_transcoder.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export { default as LASParser } from 'Parser/LASParser';
export { default as ISGParser } from 'Parser/ISGParser';
export { default as GDFParser } from 'Parser/GDFParser';
export { default as GTXParser } from 'Parser/GTXParser';
export { enableDracoLoader, glTFLoader, legacyGLTFLoader } from 'Parser/B3dmParser';
export { enableDracoLoader, enableKtx2Loader, glTFLoader, legacyGLTFLoader } from 'Parser/B3dmParser';

// 3D Tiles classes and extensions
// Exported to allow one to implement its own 3D Tiles extension which needs to
Expand Down
40 changes: 32 additions & 8 deletions src/Parser/B3dmParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as THREE from 'three';
import Capabilities from 'Core/System/Capabilities';
import { GLTFLoader } from 'ThreeExtended/loaders/GLTFLoader';
import { DRACOLoader } from 'ThreeExtended/loaders/DRACOLoader';
import { KTX2Loader } from 'ThreeExtended/loaders/KTX2Loader';
import LegacyGLTFLoader from 'Parser/deprecated/LegacyGLTFLoader';
import shaderUtils from 'Renderer/Shader/ShaderUtils';
import utf8Decoder from 'Utils/Utf8Decoder';
Expand Down Expand Up @@ -46,19 +47,18 @@ function filterUnsupportedSemantics(obj) {
*/
/**
* Enable Draco decoding for gltf.
* @param {string} path to draco library folder.
* This library is mandatory to load b3dm with Draco compression.
* @param {object} config optional configuration for Draco compression.
*
* The Draco library files are in folder itowns/examples/libs/draco/.
* You are obliged to indicate this path when you want enable the Draco Decoding.
*
* For more information on Draco, read file in /itowns/examples/libs/draco/README.md.
* You must indicate this path when you want to enable Draco Decoding.
* For more information on Draco, read /itowns/examples/libs/draco/README.md.
*
* @example <caption>Enable draco decoder</caption>
* // if you copied the folder from /itowns/examples/libs/draco/ to your root project,
* // You could set path with './'.
* // if you copied /itowns/examples/libs/draco/ to the root folder of your project,you can set the path to './'.
* itowns.enableDracoLoader('./');
*
* @param {string} path path to draco library folder.
* This library is mandatory to load b3dm and gltf with Draco compression.
* @param {object} config optional configuration for Draco compression.
*/
export function enableDracoLoader(path, config) {
if (!path) {
Expand All @@ -72,6 +72,30 @@ export function enableDracoLoader(path, config) {
glTFLoader.setDRACOLoader(dracoLoader);
}

/**
* Enable KTX2 decoding for gltf. This library is mandatory to load b3dm and gltf with KTX2 compression.
*
* The KTX2 library files are in folder itowns/examples/libs/basis/.
* You must indicate this path when you want to enable KTX2 decoding.
* For more information about KTX2, read /itowns/examples/libs/basis/README.md.
*
* @example <caption>Enable ktx2 decoder</caption>
* // if you copied /itowns/examples/libs/draco/ to the root folder of your project,you can set the path to './'.
* itowns.enableKtx2Loader('./', view.mainLoop.gfxEngine.renderer);
*
* @param {string} path path to KTX2 library folder.
* @param {THREE.WebGLRenderer} renderer the threejs renderer
*/
export function enableKtx2Loader(path, renderer) {
if (!path || !renderer) {
throw new Error('Path to ktx2 folder and renderer are mandatory');
}
const ktx2Loader = new KTX2Loader();
ktx2Loader.setTranscoderPath(path);
ktx2Loader.detectSupport(renderer);
glTFLoader.setKTX2Loader(ktx2Loader);
}

export default {
/** Parse b3dm buffer and extract THREE.Scene and batch table
* @param {ArrayBuffer} buffer - the b3dm buffer.
Expand Down

0 comments on commit a260109

Please sign in to comment.