Skip to content

Commit

Permalink
Merge pull request zeux#25 from zeux/nanowasm
Browse files Browse the repository at this point in the history
js:  Replace Emscripten runtime with manual .wasm loading
  • Loading branch information
zeux authored Mar 8, 2019
2 parents 84dcff6 + 15e82d2 commit e6575d0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 82 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ format:
meshencoder: $(ENCODER_OBJECTS) $(LIBRARY)
$(CXX) $^ $(LDFLAGS) -o $@

js/decoder.js: src/vertexcodec.cpp src/indexcodec.cpp js/decoder-post.js
emcc $(filter %.cpp,$^) -O3 -DNDEBUG -s EXPORTED_FUNCTIONS='["_meshopt_decodeVertexBuffer", "_meshopt_decodeIndexBuffer", "_sbrk"]' -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s MALLOC=emmalloc -s TOTAL_STACK=32768 -s TOTAL_MEMORY=65536 -s MODULARIZE=1 -s SINGLE_FILE=1 -s EXPORT_NAME=MeshoptDecoder --closure 1 --post-js js/decoder-post.js -o $@
sed -i '1s;^;// This file is part of meshoptimizer library and is distributed under the terms of MIT License.\n// Copyright (C) 2016-2019, by Arseny Kapoulkine ([email protected]);' $@
js/decoder.js: src/vertexcodec.cpp src/indexcodec.cpp
@mkdir -p build
emcc $(filter %.cpp,$^) -O3 -DNDEBUG -s EXPORTED_FUNCTIONS='["_meshopt_decodeVertexBuffer", "_meshopt_decodeIndexBuffer"]' -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_STACK=32768 -s TOTAL_MEMORY=65536 -o build/decoder.wasm
sed -i "s#\(var wasm = \)\".*\";#\\1\"$$(cat build/decoder.wasm | base64 -w 0)\";#" $@

$(EXECUTABLE): $(DEMO_OBJECTS) $(LIBRARY)
$(CXX) $^ $(LDFLAGS) -o $@
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,12 @@ Decoding functions are heavily optimized and can directly target write-combined
Due to a very high decoding performance and compatibility with general purpose lossless compressors, the compression is a good fit for the use on the web. To that end, meshoptimizer provides both vertex and index decoders compiled into WebAssembly and wrapped into a module with JavaScript-friendly interface, `js/decoder.js`, that you can use to decode meshes that were encoded offline:

```js
var decoder = MeshoptDecoder(); // from js/decoder.js

// decoder is a Promise that is resolved when (asynchronous) WebAssembly compilation finishes
decoder.then(function () {
// decode from *Data (Uint8Array) into *Buffer (Uint8Array)
decoder.decodeVertexBuffer(vertexBuffer, vertexCount, vertexSize, vertexData);
decoder.decodeIndexBuffer(indexBuffer, indexCount, indexSize, indexData);
});
// ready is a Promise that is resolved when (asynchronous) WebAssembly compilation finishes
await MeshoptDecoder.ready;

// decode from *Data (Uint8Array) into *Buffer (Uint8Array)
MeshoptDecoder.decodeVertexBuffer(vertexBuffer, vertexCount, vertexSize, vertexData);
MeshoptDecoder.decodeIndexBuffer(indexBuffer, indexCount, indexSize, indexData);
```

A THREE.js mesh loader is provided as an example in `tools/OptMeshLoader.js`; it loads meshes encoded using `tools/meshencoder.cpp`. [Usage example](https://zeuxcg.org/meshoptimizer/demo/) is available, with source in `demo/index.html`.
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
var onError = function () {};

new THREE.OptMeshLoader()
.setDecoder(MeshoptDecoder())
.setDecoder(MeshoptDecoder)
.setMaterials(null) // materials can be fetched using MTLLoader
.setPath('./')
.load('pirate.optmesh', function (object)
Expand Down
33 changes: 0 additions & 33 deletions js/decoder-post.js

This file was deleted.

Loading

0 comments on commit e6575d0

Please sign in to comment.