Compiling meshopt_generateVertexRemapMulti to wasm and use in js #548
kzhsw
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In readme there are documents about indexing the mesh with
meshopt_generateVertexRemap
, after digging a bit deeper into gltfpack, in function reindexMesh, functionmeshopt_generateVertexRemapMulti
is called for every mesh being processed.Using wasm-compiled gltfpack is seeming the only way to use
meshopt_generateVertexRemapMulti
in web platform, but since this project is open source, it is possible to manually compilemeshopt_generateVertexRemapMulti
to wasm and use it in js.The wasm can be compiled with Makefile, which path to the wasi-sdk should be passed to, so simpily add this lines to Makefile can compile a wasm binary:
Makefile code
Should notice that in other targets the function
__wasm_call_ctors
is also exported to initialize it, but after compiling and analyzing with the wasm2wat tools, the function in empty, so it is not exported here.Binaries could be futher optimized using wasm-opt from binaryen.
The meshoptimizer library uses wasmpack.py to encode binary to text, making it possible to be embedded into js code.
Also note that the
simd
version does not actrually uses simd instuctions, only the bulk-memory inst is used.Here is an example of compiled wasm embedding in js:
Compiled wasm in js
Should note that
new WebAssembly.Module
andnew WebAssembly.Instance
is used as the wasm codes are less that 4096 bytes limited by google.And type defs of it:
type def
The exposed function
sbrk
is used for linear memory allocation and deallocation, but since wasm can not return allocated memory to host, see more info at #522.So here is the js wrapper:
js wrapper
Should notice that instance is passed in as an argument, so callers can control the life cycle of the wasm instance, so allocated memory can be released by garbage-collecting the instance.
And defining the types:
Indices and vertex attributes are passed in as arguments, and since js object is extendable, the reindexed attributes are stored back into the
attributes
argument, the reindexed indices are returned back as return value.Should note that unindexed value is not covered in this example, but supported if directly call the wasm exports.Edit: this case should be supported now.
That making it possible to use it in glTF-Transform as an example, which is simliar to reorder.ts:
Example code
Also note here that in
reindexWrapper
ifclone
is true, cloned memory are allocated at exact the used size after indexing, but if clone it with gltf-transform accessor, it it allocated at original size, and copied again to shrink used memory.Note that in gltf-transform, weld.ts existed to do a similar job os this, but the "lossless" mode when
tolerance
is0
simply creates index instead of merging it.Due to the limitations ofcreateLayoutPlan
of glTF-Transform, unindexed primitives are ignored in the output, so for unindexed models, users can first callweld
with{tolerance: 0}
to create missing index, and use thisreindex
to losslessly merge redundant vertices.Edit: updated and unindexed primitives are supported now, see also donmccurdy/glTF-Transform#927 (comment)
Beta Was this translation helpful? Give feedback.
All reactions