greedy-mesher

raw JSON →
1.0.3 verified Fri May 01 auth: no javascript

A flexible system for generating greedy meshes of ndarrays, version 1.0.3. It compiles a custom mesher function based on provided options like order, skip, merge, and append closures. Unlike hardcoded greedy meshing algorithms, this package allows full control over traversal order and voxel merging logic. The library is optimized for ndarray data structures and is commonly used in voxel-based graphics and geometry processing. The release cadence is archival; no updates since 2013. Key differentiators: runtime code generation, customizable merging criteria, and support for arbitrary dimensions.

error TypeError: Cannot read properties of undefined (reading 'length')
cause Order array not provided or has wrong length.
fix
Provide an order array with length equal to the number of dimensions of the input ndarray.
error ReferenceError: append is not defined
cause The 'append' option is required but not provided in options.
fix
Add an 'append' function to the options object.
error TypeError: array.get is not a function
cause Using an ndarray without setting useGetter: true.
fix
Set useGetter: true in the options when passing an ndarray.
gotcha The order array must have length equal to the number of dimensions of the input ndarray; otherwise, the generated code will be incorrect or crash.
fix Ensure order.length equals ndarray dimension.
gotcha The append closure receives coordinates in the order specified by 'order', not the original axis order.
fix Design append to match the permutation given in order.
gotcha If 'useGetter' is not set, the mesher assumes the array is a native typed array; for ndarrays, set useGetter: true or the generated code will fail.
fix Set useGetter: true when passing an ndarray.
breaking No breaking changes known; minor version bumps are unlikely.
fix N/A
npm install greedy-mesher
yarn add greedy-mesher
pnpm add greedy-mesher

Creates a custom mesher for a 2D ndarray, merging adjacent equal voxels (excluding 0) and appending rectangles.

const compileMesher = require('greedy-mesher');
const ndarrayPack = require('ndarray-pack');

const mesher = compileMesher({
  extraArgs: 1,
  order: [1, 0],
  append: function(lo_x, lo_y, hi_x, hi_y, val, result) {
    result.push([[lo_x, lo_y], [hi_x, hi_y]]);
  }
});

const input = ndarrayPack([
  [0, 2, 0, 0],
  [0, 1, 1, 0],
  [0, 1, 1, 0],
  [0, 0, 0, 0]
]);

const result = [];
mesher(input, result);
console.log(result);
// Output:
// [ [ [ 1, 0 ], [ 2, 1 ] ], [ [ 1, 1 ], [ 3, 3 ] ] ]