218 lines
24 KiB
JavaScript
218 lines
24 KiB
JavaScript
|
|
// This file is part of meshoptimizer library and is distributed under the terms of MIT License.
|
||
|
|
// Copyright (C) 2016-2025, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
|
||
|
|
var MeshoptEncoder = (function () {
|
||
|
|
// Built with clang version 19.1.5-wasi-sdk
|
||
|
|
// Built from meshoptimizer 0.25
|
||
|
|
var wasm =
|
||
|
|
'b9H79Tebbbe9ok9Geueu9Geub9Gbb9Gruuuuuuueu9Gvuuuuueu9Gduueu9Gluuuueu9Gvuuuuub9Gouuuuuub9Gluuuub9GiuuueuiE8AdilveoveovrrwrrrDDoDrbqqbelve9Weiiviebeoweuec;G:Qdkr:PlCo9TW9T9VV95dbH9F9F939H79T9F9J9H229F9Jt9VV7bb8F9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949c919M9MWV9mW4W2be8A9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949c919M9MWVbd8F9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949c919M9MWV9c9V919U9KbiE9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949wWV79P9V9UblY9TW79O9V9Wt9FW9U9J9V9KW69U9KW949c919M9MWVbv8E9TW79O9V9Wt9FW9U9J9V9KW69U9KW949c919M9MWV9c9V919U9Kbo8A9TW79O9V9Wt9FW9U9J9V9KW69U9KW949wWV79P9V9UbrE9TW79O9V9Wt9FW9U9J9V9KW69U9KW949tWG91W9U9JWbwa9TW79O9V9Wt9FW9U9J9V9KW69U9KW949tWG91W9U9JW9c9V919U9KbDL9TW79O9V9Wt9FW9U9J9V9KWS9P2tWV9p9JtbqK9TW79O9V9Wt9FW9U9J9V9KWS9P2tWV9r919HtbkL9TW79O9V9Wt9FW9U9J9V9KWS9P2tWVT949WbxY9TW79O9V9Wt9FW9U9J9V9KWS9P2tWVJ9V29VVbmE9TW79O9V9Wt9F9V9Wt9P9T9P96W9wWVtW94J9H9J9OWbza9TW79O9V9Wt9F9V9Wt9P9T9P96W9wWVtW94J9H9J9OW9ttV9P9WbHa9TW79O9V9Wt9F9V9Wt9P9T9P96W9wWVtW94SWt9J9O9sW9T9H9WbOK9TW79O9V9Wt9F79W9Ht9P9H29t9VVt9sW9T9H9WbAl79IV9RbXDwebcekdKYq;i28Adbk:Bhdhud9:8Jjjjjbc;qw9Rgr8KjjjjbcbhwdnaeTmbabcbyd;C:kjjbaoaocb9iEgDc:GeV86bbarc;adfcbcjdz:xjjjb8AdnaiTmbarc;adfadalz:wjjjb8Akarc;abfalfcbcbcjdal9RalcFe0Ez:xjjjb8Aarc;abfarc;adfalz:wjjjb8AarcUf9cb83ibarc8Wf9cb83ibarcyf9cb83ibarcaf9cb83ibarcKf9cb83ibarczf9cb83ibar9cb83iwar9cb83ibcj;abal9Uc;WFbGcjdalca0Ehqdnaicd6mbavcd9imbaDTmbadcefhkaqci2gxal2hmarc;alfclfhParc;qlfceVhsarc;qofclVhzarc;qofcKfhHarc;qofczfhOcbhAincdhCcbhodnavci6mbaH9cb83ibaO9cb83ibar9cb83i;yoar9cb83i;qoadaAfgoybbhXcbhQincbhwcbhLdninaoalfhKaoybbgYaX7aLVhLawcP0meaKhoaYhXawcefgwaQfai6mbkkcbhXarc;qofhwincwh8AcwhEdnaLaX93gocFeGg3cs0mbclhEa3ci0mba3cb9hcethEkdnaocw4cFeGg3cs0mbclh8Aa3ci0mba3cb9hceth8Aka8AaEfh3awydbh5cwh8AcwhEdnaocz4cFeGg8Ecs0mbclhEa8Eci0mba8Ecb9hcethEka3a5fh3dnaocFFFFb0mbclh8AaocFFF8F0mbaocFFFr0ceth8Akawa3aEfa8AfBdbawclfhwaXcefgXcw9hmbkaKhoaYhXaQczfgQai6mbkcbhocehwazhLinawaoaLydbarc;qofaocdtfydb6EhoaLclfhLawcefgwcw9hmbkcihCkcbh3arc;qlfcbcjdz:xjjjb8Aarc;alfcwfcbBdbar9cb83i;alaoclth8Fadhaaqhhakh5inarc;qlfadcba3cufgoaoa30Eal2falz:wjjjb8Aaiahaiah6Ehgdnaqaia39Ra3aqfai6EgYcsfc9WGgoaY9nmbarc;qofaYfcbaoaY9Rz:xjjjb8Akada3al2fh8Jcbh8Kina8Ka8FVcl4hQarc;alfa8Kcdtfh8LaAh8Mcbh8Nina8NaAfhwdndndndndndna8KPldebidkasa8Mc98GgLfhoa5aLfh8Aarc;qlfawc98GgLfRbbhXcwhwinaoRbbawtaXVhXaocefhoawcwfgwca9hmbkaYTmla8Ncith8Ea8JaLfhEcbhKinaERbbhLcwhoa8AhwinawRbbaotaLVhLawcefhwaocwfgoca9hmbkarc;qofaKfaLaX7aQ93a8E486bba8Aalfh8AaEalfhEaLhXaKcefgKaY9hmbxlkkaYTmia8Mc9:Ghoa8NcitcwGhEarc;qlfawceVfRbbcwtarc;qlfawc9:GfRbbVhLarc;qofhwaghXinawa5aofRbbcwtaaaofRbbVg8AaL9RgLcetaLcztcz91cs47cFFiGaE486bbaoalfhoawcefhwa8AhLa3aXcufgX9hmbxikkaYTmda8Jawfhoarc;qlfawfRbbhLarc;qofhwaghXinawaoRbbg8AaL9RgLcetaLcKtcK91cr4786bbawcefhwaoalfhoa8AhLa3aXcufgX9hmbxdkkaYTmeka8LydbhEcbhKarc;qofhoincdhLcbhwinaLaoawfRbbcb9hfhLawcefgwcz9hmbkclhXcbhwinaXaoawfRbbcd0fhXawcefgwcz9hmbkcwh8Acbhwina8AaoawfRbbcP0fh8Aawcefgwcz9hmbkaLaXaLaX6Egwa8Aawa8A6Egwczawcz6EaEfhEaoczfhoaKczfgKaY6mbka8LaEBdbka8Mcefh8Ma8Ncefg8Ncl9hmbka8Kcefg8KaC9hmbkaaamfhaahaxfhha5amfh5a3axfg3ai6mbkcbhocehwaPhLinawaoaLydbarc;alfaocdtfydb6EhoaLclfhLawcefgXhwaCaX9hmbkaraAcd4fa8FcdVaoaocdSE86bbaAclfgAal6mbkkabaefh8Kabcefhoalcd4gecbaDEhkadcefhOarc;abfceVhHcbhmdndninaiam9nmearc;qofcbcjdz:xjjjb8Aa8Kao9Rak6mdadamal2gwfhxcbh8JaOawfhzaocbakz:xjjjbghakfh5aqaiam9Ramaqfai6Egscsfgocl4cifcd4hCaoc9WGg8LThPindndndndndndndndndndnaDTmbara8Jcd4fRbbgLciGPlbedlbkasTmdaxa8Jfhoarc;abfa8JfRbbhLarc;qofhwashXinawaoRbbg8AaL9RgLcetaLcKtcK91cr4786bbawcefhwaoalfhoa8AhLaXcufgXmbxikkasTmia8JcitcwGhEarc;abfa8JceVfRbbcwtarc;abfa8Jc9:GgofRbbVhLaxaofhoarc;qofhwashXinawao8Vbbg8AaL9RgLcetaLcztcz91cs47cFFiGaE486bbawcefhwaoalfhoa8AhLaXcufgXmbxdkkaHa8Jc98GgEfhoazaEfh8Aarc;abfaEfRbbhXcwhwinaoRbbawtaXVhXaocefhoawcwfgwca9hmbkasTmbaLcl4hYa8JcitcKGh3axaEfhEcbhKinaERbbhLcwhoa8AhwinawRbbaotaLVhLawcefhwaocwfgoca9hmbkarc;qofaKfaLaX7aY93a3486bba8Aalfh8AaEalfhEaLhXaKcefgKas9hmbkkaDmbcbhoxlka8LTmbcbhodninarc;qofaofgwcwf8Pibaw8Pib:e9qTmeaoczfgoa8L9pmdxbkkdnavmbcehoxikcbhEaChKaChYinarc;qofaEfgocwf8Pibhyao8Pibh8PcdhLcbhwinaLaoawfRbbcb9hfhLawcefgwcz9hmbkclhXcbhwinaXaoawfRbbcd0fhXawcefgwcz9hmbkcwh8Acbhwi
|
||
|
|
|
||
|
|
var wasmpack = new Uint8Array([
|
||
|
|
32, 0, 65, 2, 1, 106, 34, 33, 3, 128, 11, 4, 13, 64, 6, 253, 10, 7, 15, 116, 127, 5, 8, 12, 40, 16, 19, 54, 20, 9, 27, 255, 113, 17, 42, 67,
|
||
|
|
24, 23, 146, 148, 18, 14, 22, 45, 70, 69, 56, 114, 101, 21, 25, 63, 75, 136, 108, 28, 118, 29, 73, 115,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (typeof WebAssembly !== 'object') {
|
||
|
|
return {
|
||
|
|
supported: false,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
var instance;
|
||
|
|
|
||
|
|
var ready = WebAssembly.instantiate(unpack(wasm), {}).then(function (result) {
|
||
|
|
instance = result.instance;
|
||
|
|
instance.exports.__wasm_call_ctors();
|
||
|
|
instance.exports.meshopt_encodeVertexVersion(0);
|
||
|
|
instance.exports.meshopt_encodeIndexVersion(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
function unpack(data) {
|
||
|
|
var result = new Uint8Array(data.length);
|
||
|
|
for (var i = 0; i < data.length; ++i) {
|
||
|
|
var ch = data.charCodeAt(i);
|
||
|
|
result[i] = ch > 96 ? ch - 97 : ch > 64 ? ch - 39 : ch + 4;
|
||
|
|
}
|
||
|
|
var write = 0;
|
||
|
|
for (var i = 0; i < data.length; ++i) {
|
||
|
|
result[write++] = result[i] < 60 ? wasmpack[result[i]] : (result[i] - 60) * 64 + result[++i];
|
||
|
|
}
|
||
|
|
return result.buffer.slice(0, write);
|
||
|
|
}
|
||
|
|
|
||
|
|
function assert(cond) {
|
||
|
|
if (!cond) {
|
||
|
|
throw new Error('Assertion failed');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function bytes(view) {
|
||
|
|
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
||
|
|
}
|
||
|
|
|
||
|
|
function reorder(fun, indices, vertices, optf) {
|
||
|
|
var sbrk = instance.exports.sbrk;
|
||
|
|
var ip = sbrk(indices.length * 4);
|
||
|
|
var rp = sbrk(vertices * 4);
|
||
|
|
var heap = new Uint8Array(instance.exports.memory.buffer);
|
||
|
|
var indices8 = bytes(indices);
|
||
|
|
heap.set(indices8, ip);
|
||
|
|
if (optf) {
|
||
|
|
optf(ip, ip, indices.length, vertices);
|
||
|
|
}
|
||
|
|
var unique = fun(rp, ip, indices.length, vertices);
|
||
|
|
// heap may have grown
|
||
|
|
heap = new Uint8Array(instance.exports.memory.buffer);
|
||
|
|
var remap = new Uint32Array(vertices);
|
||
|
|
new Uint8Array(remap.buffer).set(heap.subarray(rp, rp + vertices * 4));
|
||
|
|
indices8.set(heap.subarray(ip, ip + indices.length * 4));
|
||
|
|
sbrk(ip - sbrk(0));
|
||
|
|
|
||
|
|
for (var i = 0; i < indices.length; ++i) indices[i] = remap[indices[i]];
|
||
|
|
|
||
|
|
return [remap, unique];
|
||
|
|
}
|
||
|
|
|
||
|
|
function spatialsort(fun, positions, count, stride) {
|
||
|
|
var sbrk = instance.exports.sbrk;
|
||
|
|
var ip = sbrk(count * 4);
|
||
|
|
var sp = sbrk(count * stride);
|
||
|
|
var heap = new Uint8Array(instance.exports.memory.buffer);
|
||
|
|
heap.set(bytes(positions), sp);
|
||
|
|
fun(ip, sp, count, stride);
|
||
|
|
// heap may have grown
|
||
|
|
heap = new Uint8Array(instance.exports.memory.buffer);
|
||
|
|
var remap = new Uint32Array(count);
|
||
|
|
new Uint8Array(remap.buffer).set(heap.subarray(ip, ip + count * 4));
|
||
|
|
sbrk(ip - sbrk(0));
|
||
|
|
return remap;
|
||
|
|
}
|
||
|
|
|
||
|
|
function encode(fun, bound, source, count, size, level, version) {
|
||
|
|
var sbrk = instance.exports.sbrk;
|
||
|
|
var tp = sbrk(bound);
|
||
|
|
var sp = sbrk(count * size);
|
||
|
|
var heap = new Uint8Array(instance.exports.memory.buffer);
|
||
|
|
heap.set(bytes(source), sp);
|
||
|
|
var res = fun(tp, bound, sp, count, size, level, version);
|
||
|
|
var target = new Uint8Array(res);
|
||
|
|
target.set(heap.subarray(tp, tp + res));
|
||
|
|
sbrk(tp - sbrk(0));
|
||
|
|
return target;
|
||
|
|
}
|
||
|
|
|
||
|
|
function maxindex(source) {
|
||
|
|
var result = 0;
|
||
|
|
for (var i = 0; i < source.length; ++i) {
|
||
|
|
var index = source[i];
|
||
|
|
result = result < index ? index : result;
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
function index32(source, size) {
|
||
|
|
assert(size == 2 || size == 4);
|
||
|
|
if (size == 4) {
|
||
|
|
return new Uint32Array(source.buffer, source.byteOffset, source.byteLength / 4);
|
||
|
|
} else {
|
||
|
|
var view = new Uint16Array(source.buffer, source.byteOffset, source.byteLength / 2);
|
||
|
|
return new Uint32Array(view); // copies each element
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function filter(fun, source, count, stride, bits, insize, mode) {
|
||
|
|
var sbrk = instance.exports.sbrk;
|
||
|
|
var tp = sbrk(count * stride);
|
||
|
|
var sp = sbrk(count * insize);
|
||
|
|
var heap = new Uint8Array(instance.exports.memory.buffer);
|
||
|
|
heap.set(bytes(source), sp);
|
||
|
|
fun(tp, count, stride, bits, sp, mode);
|
||
|
|
var target = new Uint8Array(count * stride);
|
||
|
|
target.set(heap.subarray(tp, tp + count * stride));
|
||
|
|
sbrk(tp - sbrk(0));
|
||
|
|
return target;
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
ready: ready,
|
||
|
|
supported: true,
|
||
|
|
reorderMesh: function (indices, triangles, optsize) {
|
||
|
|
var optf = triangles
|
||
|
|
? optsize
|
||
|
|
? instance.exports.meshopt_optimizeVertexCacheStrip
|
||
|
|
: instance.exports.meshopt_optimizeVertexCache
|
||
|
|
: undefined;
|
||
|
|
return reorder(instance.exports.meshopt_optimizeVertexFetchRemap, indices, maxindex(indices) + 1, optf);
|
||
|
|
},
|
||
|
|
reorderPoints: function (positions, positions_stride) {
|
||
|
|
assert(positions instanceof Float32Array);
|
||
|
|
assert(positions.length % positions_stride == 0);
|
||
|
|
assert(positions_stride >= 3);
|
||
|
|
return spatialsort(instance.exports.meshopt_spatialSortRemap, positions, positions.length / positions_stride, positions_stride * 4);
|
||
|
|
},
|
||
|
|
encodeVertexBuffer: function (source, count, size) {
|
||
|
|
assert(size > 0 && size <= 256);
|
||
|
|
assert(size % 4 == 0);
|
||
|
|
var bound = instance.exports.meshopt_encodeVertexBufferBound(count, size);
|
||
|
|
return encode(instance.exports.meshopt_encodeVertexBuffer, bound, source, count, size);
|
||
|
|
},
|
||
|
|
encodeVertexBufferLevel: function (source, count, size, level, version) {
|
||
|
|
assert(size > 0 && size <= 256);
|
||
|
|
assert(size % 4 == 0);
|
||
|
|
assert(level >= 0 && level <= 3);
|
||
|
|
assert(version === undefined || version == 0 || version == 1);
|
||
|
|
var bound = instance.exports.meshopt_encodeVertexBufferBound(count, size);
|
||
|
|
return encode(instance.exports.meshopt_encodeVertexBufferLevel, bound, source, count, size, level, version || 0);
|
||
|
|
},
|
||
|
|
encodeIndexBuffer: function (source, count, size) {
|
||
|
|
assert(size == 2 || size == 4);
|
||
|
|
assert(count % 3 == 0);
|
||
|
|
var indices = index32(source, size);
|
||
|
|
var bound = instance.exports.meshopt_encodeIndexBufferBound(count, maxindex(indices) + 1);
|
||
|
|
return encode(instance.exports.meshopt_encodeIndexBuffer, bound, indices, count, 4);
|
||
|
|
},
|
||
|
|
encodeIndexSequence: function (source, count, size) {
|
||
|
|
assert(size == 2 || size == 4);
|
||
|
|
var indices = index32(source, size);
|
||
|
|
var bound = instance.exports.meshopt_encodeIndexSequenceBound(count, maxindex(indices) + 1);
|
||
|
|
return encode(instance.exports.meshopt_encodeIndexSequence, bound, indices, count, 4);
|
||
|
|
},
|
||
|
|
encodeGltfBuffer: function (source, count, size, mode) {
|
||
|
|
var table = {
|
||
|
|
ATTRIBUTES: this.encodeVertexBuffer,
|
||
|
|
TRIANGLES: this.encodeIndexBuffer,
|
||
|
|
INDICES: this.encodeIndexSequence,
|
||
|
|
};
|
||
|
|
assert(table[mode]);
|
||
|
|
return table[mode](source, count, size);
|
||
|
|
},
|
||
|
|
encodeFilterOct: function (source, count, stride, bits) {
|
||
|
|
assert(stride == 4 || stride == 8);
|
||
|
|
assert(bits >= 1 && bits <= 16);
|
||
|
|
return filter(instance.exports.meshopt_encodeFilterOct, source, count, stride, bits, 16);
|
||
|
|
},
|
||
|
|
encodeFilterQuat: function (source, count, stride, bits) {
|
||
|
|
assert(stride == 8);
|
||
|
|
assert(bits >= 4 && bits <= 16);
|
||
|
|
return filter(instance.exports.meshopt_encodeFilterQuat, source, count, stride, bits, 16);
|
||
|
|
},
|
||
|
|
encodeFilterExp: function (source, count, stride, bits, mode) {
|
||
|
|
assert(stride > 0 && stride % 4 == 0);
|
||
|
|
assert(bits >= 1 && bits <= 24);
|
||
|
|
var table = {
|
||
|
|
Separate: 0,
|
||
|
|
SharedVector: 1,
|
||
|
|
SharedComponent: 2,
|
||
|
|
Clamped: 3,
|
||
|
|
};
|
||
|
|
return filter(instance.exports.meshopt_encodeFilterExp, source, count, stride, bits, stride, mode ? table[mode] : 1);
|
||
|
|
},
|
||
|
|
encodeFilterColor: function (source, count, stride, bits) {
|
||
|
|
assert(stride == 4 || stride == 8);
|
||
|
|
assert(bits >= 2 && bits <= 16);
|
||
|
|
return filter(instance.exports.meshopt_encodeFilterColor, source, count, stride, bits, 16);
|
||
|
|
},
|
||
|
|
};
|
||
|
|
})();
|
||
|
|
|
||
|
|
export { MeshoptEncoder };
|