Skip to content

Commit

Permalink
feat: add MeshoptDecoder WXWebAssembly
Browse files Browse the repository at this point in the history
  • Loading branch information
deepkolos committed May 11, 2021
1 parent b64a8a1 commit d311781
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
Binary file added tools/decoder_base.wasm
Binary file not shown.
103 changes: 103 additions & 0 deletions tools/meshopt_decoder.wasm.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// This file is part of meshoptimizer library and is distributed under the terms of MIT License.
// Copyright (C) 2016-2020, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
var MeshoptDecoder = (function (path) {
// Built with clang version 11.0.0 (https://github.com/llvm/llvm-project.git 0160ad802e899c2922bc9b29564080c22eb0908c)
// Built from meshoptimizer 0.14

if (typeof WXWebAssembly !== 'object') {
// This module requires WebAssembly to function
return {
supported: false,
};
}

var instance;
var readyResolve;
var promise = new Promise((resovle) => {
readyResolve = resovle
})

function setWasmPath(path) {
WXWebAssembly.instantiate(path, {}).then(function (result) {
instance = result.instance;
instance.exports.__wasm_call_ctors();
readyResolve()
});
}


function decode(fun, target, count, size, source, filter) {
var sbrk = instance.exports.sbrk;
var count4 = (count + 3) & ~3; // pad for SIMD filter
var tp = sbrk(count4 * size);
var sp = sbrk(source.length);
var heap = new Uint8Array(instance.exports.memory.buffer);
heap.set(source, sp);
var res = fun(tp, count, size, sp, source.length);
if (res == 0 && filter) {
filter(tp, count4, size);
}
target.set(heap.subarray(tp, tp + count * size));
sbrk(tp - sbrk(0));
if (res != 0) {
throw new Error('Malformed buffer data: ' + res);
}
}
var filters = {
// legacy index-based enums for glTF
0: '',
1: 'meshopt_decodeFilterOct',
2: 'meshopt_decodeFilterQuat',
3: 'meshopt_decodeFilterExp',
// string-based enums for glTF
NONE: '',
OCTAHEDRAL: 'meshopt_decodeFilterOct',
QUATERNION: 'meshopt_decodeFilterQuat',
EXPONENTIAL: 'meshopt_decodeFilterExp',
};

var decoders = {
// legacy index-based enums for glTF
0: 'meshopt_decodeVertexBuffer',
1: 'meshopt_decodeIndexBuffer',
2: 'meshopt_decodeIndexSequence',
// string-based enums for glTF
ATTRIBUTES: 'meshopt_decodeVertexBuffer',
TRIANGLES: 'meshopt_decodeIndexBuffer',
INDICES: 'meshopt_decodeIndexSequence',
};

return {
setWasmPath,
ready: promise,
supported: true,
decodeVertexBuffer: function (target, count, size, source, filter) {
decode(
instance.exports.meshopt_decodeVertexBuffer,
target,
count,
size,
source,
instance.exports[filters[filter]],
);
},
decodeIndexBuffer: function (target, count, size, source) {
decode(instance.exports.meshopt_decodeIndexBuffer, target, count, size, source);
},
decodeIndexSequence: function (target, count, size, source) {
decode(instance.exports.meshopt_decodeIndexSequence, target, count, size, source);
},
decodeGltfBuffer: function (target, count, size, source, mode, filter) {
decode(
instance.exports[decoders[mode]],
target,
count,
size,
source,
instance.exports[filters[filter]],
);
},
};
})();

export { MeshoptDecoder };

0 comments on commit d311781

Please sign in to comment.