Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CXX := Main.cpp
O3 := -O3
WASM := -s NO_FILESYSTEM=1 -s ALLOW_MEMORY_GROWTH=1 -s WASM=1 \
-s EXPORTED_FUNCTIONS='["_simplify", "_malloc", "_free"]' \
-s EXPORTED_RUNTIME_METHODS='["HEAPU8", "HEAPU32"]' \
-s ENVIRONMENT='web,worker' \
-s MODULARIZE=1 \
-s EXPORT_ES6=0 \
Expand Down
18 changes: 13 additions & 5 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ self.addEventListener('message', function(e) {
prepare_and_simplify(e.data.blob, e.data.percentage);
}, false);

var Module = {} // imported script installs functions on `Module`
var Module = null;
self.importScripts("Fast-Quadric-Mesh-Simplification.js");

function prepare_and_simplify(file, percentage) {
async function initializeModule() {
if (!Module) {
Module = await createSimplifyModule();
}
return Module;
}

async function prepare_and_simplify(file, percentage) {
await initializeModule();
var fr = new FileReader();
fr.readAsArrayBuffer(file);
fr.onloadend = function () {
const outputData = simplify(new Uint8Array(fr.result), percentage);
fr.onloadend = async function () {
const outputData = await simplify(new Uint8Array(fr.result), percentage);
let file = new Blob([outputData], {type: 'application/sla'});
self.postMessage({"blob":file});
}
}

function simplify(inputArray, percentage) {
async function simplify(inputArray, percentage) {
// Allocate memory for the input buffer and copy the data
const inputPtr = Module._malloc(inputArray.byteLength);
Module.HEAPU8.set(inputArray, inputPtr);
Expand Down