Skip to content

Commit

Permalink
js: Use regular number array for attribute weights
Browse files Browse the repository at this point in the history
This is hopefully a little more idiomatic; technically a Float32Array
would also work here if not for assertion, but we can correct this in
the future if need be.

As a drive-by, extract gradient eval in simplifier core into a variable.
  • Loading branch information
zeux committed Jun 2, 2023
1 parent e7d45c9 commit 6381f24
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
5 changes: 2 additions & 3 deletions js/meshopt_simplifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ var MeshoptSimplifier = (function() {
return [target, error[0]];
}

// TODO: unify this and simplify
function simplifyAttr(fun, indices, index_count, vertex_positions, vertex_count, vertex_positions_stride, vertex_attributes, vertex_attributes_stride, attribute_weights, target_index_count, target_error, options) {
var sbrk = instance.exports.sbrk;
var te = sbrk(4);
Expand Down Expand Up @@ -180,7 +179,7 @@ var MeshoptSimplifier = (function() {
assert(vertex_attributes_stride >= 0);
assert(target_index_count % 3 == 0);
assert(target_error >= 0 && target_error <= 1);
assert(attribute_weights instanceof Float32Array); // TODO: allow regular arrays?
assert(Array.isArray(attribute_weights));
assert(vertex_attributes_stride >= attribute_weights.length);

var options = 0;
Expand All @@ -189,7 +188,7 @@ var MeshoptSimplifier = (function() {
}

var indices32 = indices.BYTES_PER_ELEMENT == 4 ? indices : new Uint32Array(indices);
var result = simplifyAttr(instance.exports.meshopt_simplifyWithAttributes, indices32, indices.length, vertex_positions, vertex_positions.length / vertex_positions_stride, vertex_positions_stride * 4, vertex_attributes, vertex_attributes_stride * 4, attribute_weights, target_index_count, target_error, options);
var result = simplifyAttr(instance.exports.meshopt_simplifyWithAttributes, indices32, indices.length, vertex_positions, vertex_positions.length / vertex_positions_stride, vertex_positions_stride * 4, vertex_attributes, vertex_attributes_stride * 4, new Float32Array(attribute_weights), target_index_count, target_error, options);
result[0] = (indices instanceof Uint32Array) ? result[0] : new indices.constructor(result[0]);

return result;
Expand Down
2 changes: 1 addition & 1 deletion js/meshopt_simplifier.module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const MeshoptSimplifier: {

simplify: (indices: Uint32Array, vertex_positions: Float32Array, vertex_positions_stride: number, target_index_count: number, target_error: number, flags?: Flags[]) => [Uint32Array, number];

simplifyWithAttributes: (indices: Uint32Array, vertex_positions: Float32Array, vertex_positions_stride: number, vertex_attributes: Float32Array, vertex_attributes_stride: number, attribute_weights: Float32Array, target_index_count: number, target_error: number, flags?: Flags[]) => [Uint32Array, number];
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;
};
5 changes: 2 additions & 3 deletions js/meshopt_simplifier.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ var MeshoptSimplifier = (function() {
return [target, error[0]];
}

// TODO: unify this and simplify
function simplifyAttr(fun, indices, index_count, vertex_positions, vertex_count, vertex_positions_stride, vertex_attributes, vertex_attributes_stride, attribute_weights, target_index_count, target_error, options) {
var sbrk = instance.exports.sbrk;
var te = sbrk(4);
Expand Down Expand Up @@ -180,7 +179,7 @@ var MeshoptSimplifier = (function() {
assert(vertex_attributes_stride >= 0);
assert(target_index_count % 3 == 0);
assert(target_error >= 0 && target_error <= 1);
assert(attribute_weights instanceof Float32Array); // TODO: allow regular arrays?
assert(Array.isArray(attribute_weights));
assert(vertex_attributes_stride >= attribute_weights.length);

var options = 0;
Expand All @@ -189,7 +188,7 @@ var MeshoptSimplifier = (function() {
}

var indices32 = indices.BYTES_PER_ELEMENT == 4 ? indices : new Uint32Array(indices);
var result = simplifyAttr(instance.exports.meshopt_simplifyWithAttributes, indices32, indices.length, vertex_positions, vertex_positions.length / vertex_positions_stride, vertex_positions_stride * 4, vertex_attributes, vertex_attributes_stride * 4, attribute_weights, target_index_count, target_error, options);
var result = simplifyAttr(instance.exports.meshopt_simplifyWithAttributes, indices32, indices.length, vertex_positions, vertex_positions.length / vertex_positions_stride, vertex_positions_stride * 4, vertex_attributes, vertex_attributes_stride * 4, new Float32Array(attribute_weights), target_index_count, target_error, options);
result[0] = (indices instanceof Uint32Array) ? result[0] : new indices.constructor(result[0]);

return result;
Expand Down

0 comments on commit 6381f24

Please sign in to comment.