three-to-cannon

Convert a THREE.Mesh to a CANNON.Shape.

Usage no npm install needed!

<script type="module">
  import threeToCannon from 'https://cdn.skypack.dev/three-to-cannon';
</script>

README

three-to-cannon

Latest NPM release Minzipped size License Build Status

Convert a THREE.Mesh or THREE.Object3D to a CANNON.Shape, with optimizations to simplified bounding shapes (AABB, sphere, etc.).

API

Installation:

npm install --save three-to-cannon

Use:

/****************************************
 * Import:
 */

// ES6
import { threeToCannon, ShapeType } from 'three-to-cannon';

// CommonJS
const { threeToCannon, ShapeType } = require('three-to-cannon');

/****************************************
 * Generate a CANNON.Shape:
 */

// Automatic.
const result = threeToCannon(object3D);

// Bounding box (AABB).
const result = threeToCannon(object3D, {type: ShapeType.BOX});

// Bounding sphere.
const result = threeToCannon(object3D, {type: ShapeType.SPHERE});

// Cylinder.
const result = threeToCannon(object3D, {type: ShapeType.CYLINDER});

// Convex hull.
const result = threeToCannon(object3D, {type: ShapeType.HULL});

// Mesh (not recommended).
const result = threeToCannon(object3D, {type: ShapeType.MESH});

/****************************************
 * Using the result:
 */

// Result object includes a CANNON.Shape instance, and (optional)
// an offset or quaternion for that shape.
const {shape, offset, quaternion} = result;

See further documentation on the CANNON.Shape class.