Package detail

three-to-cannon

donmccurdy1.6kMIT5.0.2

Convert a THREE.Mesh to a CANNON.Shape.

threejs, three, cannonjs, cannon

readme

three-to-cannon

Latest NPM release npm bundle size License Build Status Coverage

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 (Usually an AABB, except obvious cases like THREE.SphereGeometry).
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 — limitations: https://github.com/pmndrs/cannon-es/issues/21).
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, orientation} = result;

// Add the shape to a CANNON.Body.
body.addShape(shape, offset, orientation);

See further documentation on the CANNON.Shape class.

changelog

Changelog

v5

  • Removed support for deprecated THREE.Geometry, use THREE.BufferGeometry instead.

v4

  • Removed dependency on deprecated THREE.Geometry, which was removed in three.js >r125.
    • THREE.Geometry inputs are still supported, and converted automatically to THREE.BufferGeometry.
  • Converted the project to TypeScript.
  • Moved type enum from threeToCannon.Type to top-level ShapeType export.
  • Optional .offset and .quaternion properties are returned with the Shape, not attached to it:
// Before:
const shape = threeToCannon(object);
shape.offset; // → CANNON.Vec3 | undefined
shape.quaternion; // → CANNON.Quaternion | undefined

// After:
const {shape, offset, quaternion} = threeToCannon(object);

v3

...