This library provides a simple JavaScript wrapper around the V-HACD library, which decomposes 3D surfaces into near-convex parts. It leverages Emscripten to compile the V-HACD C++ code into WebAssembly for use in web applications.
To use this library, you'll need to install Emscripten. Follow these steps:
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
emsdk_env.bat
export PATH=$PATH:$(pwd) # For bash
Here's a quick guide on how to use the vhacd-js
library to decompose a 3D mesh:
-
Import the Library
import { ConvexMeshDecomposition } from "vhacd-js";
-
Create a Decomposer Instance
You can create a decomposer instance which can be used to decompose multiple meshes:
const decomposer = await ConvexMeshDecomposition.create();
-
Configure Decomposition Options (Optional)
You can specify options to control the decomposition process. For example, you can set a maximum number of hulls:
const options = { maxHulls: 32 };
-
Compute Convex Hulls
Use the decomposer instance to compute an array of convex hulls from your mesh data:
const hulls = decomposer.computeConvexHulls({ positions, indices }, options);
Here,
positions
is an array of vertex positions andindices
is an array of indices defining the mesh's faces.
For detailed information on the JavaScript API, see vhacd.ts
.