222 lines
24 KiB
JavaScript
222 lines
24 KiB
JavaScript
// This file is part of meshoptimizer library and is distributed under the terms of MIT License.
|
|||
|
|
// Copyright (C) 2016-2026, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
|
||
|
|
var MeshoptEncoder = (function () {
|
||
|
|
// Built with clang version 22.1.0-wasi-sdk
|
||
|
|
// Built from meshoptimizer 1.1
|
||
|
|
var wasm =
|
||
|
|
'b9H79Tebbbe9ok9Geueu9Geub9Gbb9Gruuuuuuueu9Gvuuuuueu9Gduueu9Gluuuueu9Gvuuuuub9Gouuuuuub9Gluuuub9GiuuueuiE8AdilveoveovrrwrrrDDoDrbqqbelve9Weiiviebeoweuecj:Gdkr:PlCo9TW9T9VV95dbH9F9F939H79T9F9J9H229F9Jt9VV7bb8F9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949c919M9MWV9mW4W2be8A9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949c919M9MWVbd8F9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949c919M9MWV9c9V919U9KbiE9TW79O9V9Wt9FW9U9J9V9KW9wWVtW949wWV79P9V9UblY9TW79O9V9Wt9FW9U9J9V9KW69U9KW949c919M9MWVbv8E9TW79O9V9Wt9FW9U9J9V9KW69U9KW949c919M9MWV9c9V919U9Kbo8A9TW79O9V9Wt9FW9U9J9V9KW69U9KW949wWV79P9V9UbrE9TW79O9V9Wt9FW9U9J9V9KW69U9KW949tWG91W9U9JWbwa9TW79O9V9Wt9FW9U9J9V9KW69U9KW949tWG91W9U9JW9c9V919U9KbDL9TW79O9V9Wt9FW9U9J9V9KWS9P2tWV9p9JtbqK9TW79O9V9Wt9FW9U9J9V9KWS9P2tWV9r919HtbkL9TW79O9V9Wt9FW9U9J9V9KWS9P2tWVT949WbxY9TW79O9V9Wt9FW9U9J9V9KWS9P2tWVJ9V29VVbmE9TW79O9V9Wt9F9V9Wt9P9T9P96W9wWVtW94J9H9J9OWbza9TW79O9V9Wt9F9V9Wt9P9T9P96W9wWVtW94J9H9J9OW9ttV9P9WbHa9TW79O9V9Wt9F9V9Wt9P9T9P96W9wWVtW94SWt9J9O9sW9T9H9WbOK9TW79O9V9Wt9F79W9Ht9P9H29t9VVt9sW9T9H9WbAl79IV9RbXDwebcekdKYq:8f8Adbk;wadhud9:8Jjjjjbc;qw9Rgr8KjjjjbcbhwdnaeTmbabcbyd;i:I:cjbaoaocb9iEgDc:GeV86bbarc;adfcbcjdz:xjjjb8AdnaiTmbarc;adfadalz:wjjjb8Akarc;abfalfcbcbcjdal9RalcFe0Ez:xjjjb8Aarc;abfarc;adfalz:wjjjb8Aar9cb83iUar9cb83i8War9cb83iyar9cb83iaar9cb83iKar9cb83izar9cb83iwar9cb83ibcj;abal9Uc;WFbGcjdalca0Ehqdnaicd6mbavcd9imbaDTmbadcefhkaqci2gxal2hmarc;alfclfhParc;qlfceVhsarc;qofclVhzcbhHincdhOcbhAdnavci6mbar9cb83i;Ooar9cb83i;Goar9cb83i;yoar9cb83i;qoadaHfgoybbhCcbhXincbhwcbhQdninaoalfhLaoybbgKaC7aQVhQawcP0meaLhoaKhCawcefgwaXfai6mbkkcbhCarc;qofhwincwhYcwh8AdnaQaC93gocFeGgEcs0mbclh8AaEci0mbcdcbaEEh8Akdnaocw4cFeGgEcs0mbclhYaEci0mbcdcbaEEhYkaYa8AfhEawydbh3cwhYcwh8Adnaocz4cFeGg5cs0mbclh8Aa5ci0mbcdcba5Eh8AkaEa3fhEdnaocFFFFb0mbclhYaocFFF8F0mbcbcdaocjjjw6EhYkawaEa8AfaYfBdbawclfhwaCcefgCcw9hmbkaLhoaKhCaXczfgXai6mbkcbhocehwazhQinawaoaQydbarc;qofaocdtfydb6EhoaQclfhQawcefgwcw9hmbkaoclthAcihOkcbhEarc;qlfcbcjdz:xjjjb8AarcbBd;ilar9cb83i;aladh8Eaqh8Fakh3inarc;qlfadaEaEcb9h9Ral2falz:wjjjb8Aaia8Faia8F6EhadnaqaiaE9RaEaqfai6EgKcsfc9WGgoaK9nmbarc;qofaKfcbaoaK9Rz:xjjjb8AkadaEal2fhhcbhginagaAVcl4hXarc;alfagcdtfh8JaHh8Kcbh8Lina8LaHfhwdndndndndndndnagPlbedibkaKTmvahawfhoarc;qlfawfRbbhQarc;qofhwaahCinawaoRbbgYaQ9RgQcetaQcKtc8F91786bbawcefhwaoalfhoaYhQaEaCcufgC9hmbxvkkaKTmla8Kc9:Ghoa8LcitcwGh8Aarc;qlfawceVfRbbcwtarc;qlfawc9:GfRbbVhQarc;qofhwaahCinawa3aofRbbcwta8EaofRbbVgYaQ9RgQcetaQcztc8F917cFFiGa8A486bbaoalfhoawcefhwaYhQaEaCcufgC9hmbxlkkasa8Kc98GgQfhoa3aQfhYarc;qlfawc98GgQfRbbhCcwhwinaoRbbawtaCVhCaocefhoawcwfgwca9hmbxdkkaKTmdxekaKTmea8Lcith5ahaQfh8AcbhLina8ARbbhQcwhoaYhwinawRbbaotaQVhQawcefhwaocwfgoca9hmbkarc;qofaLfaQaC7aX93a5486bbaYalfhYa8Aalfh8AaQhCaLcefgLaK9hmbkka8Jydbh8AcbhLarc;qofhoincdhQcbhwinaQaoawfRbbcb9hfhQawcefgwcz9hmbkclhCcbhwinaCaoawfRbbcd0fhCawcefgwcz9hmbkcwhYcbhwinaYaoawfRbbcP0fhYawcefgwcz9hmbkaQaCaQaC6EgwaYawaY6Egwczawcz6Ea8Afh8AaoczfhoaLczfgLaK6mbka8Ja8ABdbka8Kcefh8Ka8Lcefg8Lcl9hmbkagcefggaO9hmbka8Eamfh8Ea8Faxfh8Fa3amfh3aEaxfgEai6mbkcbhocehwaPhQinawaoaQydbarc;alfaocdtfydb6EhoaQclfhQaOawcefgw9hmbkaraHcd4faAcdVaoaocdSE86bbaHclfgHal6mbkkabaefhgabcefhoalcd4g8McbaDEhkadcefh8Narc;abfceVhecbhmdndninaiam9nmearc;qofcbcjdz:xjjjb8Aagao9Rak6mdadamal2gwfhxcbhHa8Nawfhzaocbakz:xjjjbg8Fakfh3aqaiam9Ramaqfai6Egscsfgocl4cifcd4hOaoc9WGg8JThPindndndndndndndndndndnaDTmbaraHcd4fRbbgQciGPlbedlbkasTmdaxaHfhoarc;abfaHfRbbhQarc;qofhwashCinawaoRbbgYaQ9RgQcetaQcKtc8F91786bbawcefhwaoalfhoaYhQaCcufgCmbxikkasTmiaHcitcwGh8Aarc;abfaHceVfRbbcwtarc;abfaHc9:GgofRbbVhQaxaofhoarc;qofhwashCinawao8VbbgYaQ9RgQcetaQcztc8F917cFFiGa8A486bbawcefhwaoalfhoaYhQaCcufgCmbxikkaeaHc98Gg8Afhoaza8AfhYarc;abfa8AfRbbhCcwhwinaoRbbawtaCVhCaocefhoawcwfgwca9hmbkasTmdaQcl4hKaHcitcKGhEaxa8Afh8AcbhLina8ARbbhQcwhoaYhwinawRbbaotaQVhQawcefhwaocwfgoca9hmbkarc;qofaLfaQaC7aK93aE486bbaYalfhYa8Aalfh8AaQhCaLcefgLas9hmbkkaDmbcbhoxlka8JTmbcbhodninarc;qofaofgwcwf8Pibaw8Pib:e9qTmeaoczfgoa8J9pmdxbkkdnavmbcehoxikcbh8AaOhLaOhKinarc;qofa8Afgocwf8Pibhyao8Pibh8PcdhQcbhwinaQaoawfRbbcb9hfhQawcefgwcz9hmbkclhCcbhwinaCaoawfRbbcd0fhCawcefgwcz9hmbkcwhYcbhwinaYaoawfRbbcP0fhYawcefgwcz9hmbkaQaCaQaC6EgoaYaoaY6Egoczaocz6EaKfhKaocucbaya
|
||
|
|
|
||
|
|
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(1);
|
||
|
|
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) {
|
||
|
|
// mutates indices in place, requiring a copy later
|
||
|
|
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) {
|
||
|
|
assert(indices instanceof Uint32Array || indices instanceof Int32Array);
|
||
|
|
assert(!triangles || indices.length % 3 == 0);
|
||
|
|
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 === undefined ? -1 : version);
|
||
|
|
},
|
||
|
|
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, version) {
|
||
|
|
var table = {
|
||
|
|
ATTRIBUTES: this.encodeVertexBufferLevel,
|
||
|
|
TRIANGLES: this.encodeIndexBuffer,
|
||
|
|
INDICES: this.encodeIndexSequence,
|
||
|
|
};
|
||
|
|
assert(table[mode]);
|
||
|
|
return table[mode](source, count, size, /* level= */ 2, version === undefined ? 0 : version);
|
||
|
|
},
|
||
|
|
encodeFilterOct: function (source, count, stride, bits) {
|
||
|
|
assert(stride == 4 || stride == 8);
|
||
|
|
assert(bits >= 2 && 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,
|
||
|
|
};
|
||
|
|
assert(!mode || mode in table);
|
||
|
|
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 };
|