Skip to content

Commit 223c572

Browse files
authored
GLTFLoader: Update MeshoptDecoder support to support WebWorkers (mrdoob#24460)
MeshoptDecoder can decode at ~1 GB/sec (with filters) on modern desktop CPUs, which mostly means we don't need to offload the processing to other threads... except when we're dealing with scenes with ~800 MB of geometry, at which point we might be stalling the main thread for 700-900ms. To solve this, meshoptimizer is implementing support for WebWorkers: zeux/meshoptimizer#454 The decoder gains a new function, decodeGltfBufferAsync, which works regardless of whether WebWorkers are enabled; when decoder.useWorkers is called with the desired worker count, it switches to asynchronous decoding which almost entirely eliminates main thread overhead, with the exception of copying the input buffer. This change is structured to still work with the old versions of the library, the library will be updated separately. Note that when decodeGltfBufferAsync is used, `ready` promise doesn't need to be used.
1 parent c3180c5 commit 223c572

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

examples/jsm/loaders/GLTFLoader.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -1341,19 +1341,36 @@ class GLTFMeshoptCompression {
13411341

13421342
}
13431343

1344-
return Promise.all( [ buffer, decoder.ready ] ).then( function ( res ) {
1344+
return buffer.then( function ( res ) {
13451345

13461346
const byteOffset = extensionDef.byteOffset || 0;
13471347
const byteLength = extensionDef.byteLength || 0;
13481348

13491349
const count = extensionDef.count;
13501350
const stride = extensionDef.byteStride;
13511351

1352-
const result = new ArrayBuffer( count * stride );
1353-
const source = new Uint8Array( res[ 0 ], byteOffset, byteLength );
1352+
const source = new Uint8Array( res, byteOffset, byteLength );
13541353

1355-
decoder.decodeGltfBuffer( new Uint8Array( result ), count, stride, source, extensionDef.mode, extensionDef.filter );
1356-
return result;
1354+
if ( decoder.decodeGltfBufferAsync ) {
1355+
1356+
return decoder.decodeGltfBufferAsync( count, stride, source, extensionDef.mode, extensionDef.filter ).then( function ( res ) {
1357+
1358+
return res.buffer;
1359+
1360+
} );
1361+
1362+
} else {
1363+
1364+
// Support for MeshoptDecoder 0.18 or earlier, without decodeGltfBufferAsync
1365+
return decoder.ready.then( function () {
1366+
1367+
const result = new ArrayBuffer( count * stride );
1368+
decoder.decodeGltfBuffer( new Uint8Array( result ), count, stride, source, extensionDef.mode, extensionDef.filter );
1369+
return result;
1370+
1371+
} );
1372+
1373+
}
13571374

13581375
} );
13591376

0 commit comments

Comments
 (0)