Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/donmccurdy/three-to-cannon
Convert a THREE.Mesh to a CANNON.Shape.
https://github.com/donmccurdy/three-to-cannon
cannonjs physics threejs
Last synced: 6 days ago
JSON representation
Convert a THREE.Mesh to a CANNON.Shape.
- Host: GitHub
- URL: https://github.com/donmccurdy/three-to-cannon
- Owner: donmccurdy
- Created: 2016-08-06T23:46:27.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2025-02-02T00:56:31.000Z (18 days ago)
- Last Synced: 2025-02-07T18:03:55.164Z (13 days ago)
- Topics: cannonjs, physics, threejs
- Language: JavaScript
- Homepage:
- Size: 1.36 MB
- Stars: 341
- Watchers: 12
- Forks: 31
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# three-to-cannon
[](https://www.npmjs.com/package/three-to-cannon)
[](https://bundlephobia.com/package/three-to-cannon)
[](https://github.com/donmccurdy/three-to-cannon/blob/master/LICENSE)
[](https://github.com/donmccurdy/three-to-cannon/actions?query=workflow%3Abuild)
[](https://codecov.io/gh/donmccurdy/three-to-cannon)Convert a [THREE.Mesh](https://threejs.org/docs/?q=mesh#api/en/objects/Mesh) or [THREE.Object3D](https://threejs.org/docs/?q=object3d#api/en/core/Object3D) to a [CANNON.Shape](https://pmndrs.github.io/cannon-es/docs/classes/Shape.html), with optimizations to simplified bounding shapes (AABB, sphere, etc.).
## API
Installation:
```js
npm install --save three-to-cannon
```Use:
```js
/****************************************
* 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](https://pmndrs.github.io/cannon-es/docs/classes/Shape.html) class.