Skip to content

Commit

Permalink
js: Expose meshopt_simplifyPoints as meshopt_simplifier @ simplifyPoints
Browse files Browse the repository at this point in the history
We now support point simplification via a JS interface, both with and
without colors. The order of arguments in the JS wrapper is set so that
simplification without points can just omit the last 3 arguments,
although the resulting order is a little odd when colors *are* supplied.

The function returns the point indices; it's up to the application to
compact the point streams - for triangle meshes we provide compactMesh,
however that only remaps the index buffer. Point vertex data compaction
should be easy enough to implement with a JS for loop.
  • Loading branch information
zeux committed Oct 14, 2023
1 parent e6b2f70 commit 2c3d631
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ WASM_ENCODER_SOURCES=src/vertexcodec.cpp src/indexcodec.cpp src/vertexfilter.cpp
WASM_ENCODER_EXPORTS=meshopt_encodeVertexBuffer meshopt_encodeVertexBufferBound meshopt_encodeIndexBuffer meshopt_encodeIndexBufferBound meshopt_encodeIndexSequence meshopt_encodeIndexSequenceBound meshopt_encodeVertexVersion meshopt_encodeIndexVersion meshopt_encodeFilterOct meshopt_encodeFilterQuat meshopt_encodeFilterExp meshopt_optimizeVertexCache meshopt_optimizeVertexCacheStrip meshopt_optimizeVertexFetchRemap meshopt_spatialSortRemap sbrk __wasm_call_ctors

WASM_SIMPLIFIER_SOURCES=src/simplifier.cpp src/vfetchoptimizer.cpp tools/wasmstubs.cpp
WASM_SIMPLIFIER_EXPORTS=meshopt_simplify meshopt_simplifyWithAttributes meshopt_simplifyScale meshopt_optimizeVertexFetchRemap sbrk __wasm_call_ctors
WASM_SIMPLIFIER_EXPORTS=meshopt_simplify meshopt_simplifyWithAttributes meshopt_simplifyScale meshopt_simplifyPoints meshopt_optimizeVertexFetchRemap sbrk __wasm_call_ctors

ifeq ($(config),iphone)
IPHONESDK=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
Expand Down
47 changes: 42 additions & 5 deletions js/meshopt_simplifier.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions js/meshopt_simplifier.module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export const MeshoptSimplifier: {
simplifyWithAttributes: (indices: Uint32Array, vertex_positions: Float32Array, vertex_positions_stride: number, vertex_attributes: Float32Array, vertex_attributes_stride: number, attribute_weights: number[], target_index_count: number, target_error: number, flags?: Flags[]) => [Uint32Array, number];

getScale: (vertex_positions: Float32Array, vertex_positions_stride: number) => number;

simplifyPoints: (vertex_positions: Float32Array, vertex_positions_stride: number, target_vertex_count: number, vertex_colors?: Float32Array, vertex_colors_stride?: number, color_weight?: number) => Uint32Array;
};
47 changes: 42 additions & 5 deletions js/meshopt_simplifier.module.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions js/meshopt_simplifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ var tests = {

assert(simplifier.getScale(positions, 3) == 3.0);
},

simplifyPoints: function() {
var positions = new Float32Array([
0, 0, 0, 100, 0, 0, 100, 1, 1, 110, 0, 0,
]);
var colors = new Float32Array([
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
]);

var expected = new Uint32Array([
0, 1
]);

var expectedC = new Uint32Array([
0, 2
]);

var res = simplifier.simplifyPoints(positions, 3, 2);
assert.deepEqual(res, expected);

// note: recommended value for color_weight is 1e-2 but here we push color weight to be very high to bias candidate selection for testing
var resC1 = simplifier.simplifyPoints(positions, 3, 2, colors, 3, 1e-1);
assert.deepEqual(resC1, expectedC);

var resC2 = simplifier.simplifyPoints(positions, 3, 2, colors, 3, 1e-2);
assert.deepEqual(resC2, expected);
}
};

Promise.all([simplifier.ready]).then(() => {
Expand Down

0 comments on commit 2c3d631

Please sign in to comment.