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: 1 day 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-01-01T12:52:34.000Z (24 days ago)
- Last Synced: 2025-01-17T15:05:57.642Z (8 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
[![Latest NPM release](https://img.shields.io/npm/v/three-to-cannon.svg)](https://www.npmjs.com/package/three-to-cannon)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/three-to-cannon)](https://bundlephobia.com/package/three-to-cannon)
[![License](https://img.shields.io/badge/license-MIT-007ec6.svg)](https://github.com/donmccurdy/three-to-cannon/blob/master/LICENSE)
[![Build Status](https://github.com/donmccurdy/three-to-cannon/workflows/build/badge.svg?branch=main&event=push)](https://github.com/donmccurdy/three-to-cannon/actions?query=workflow%3Abuild)
[![Coverage](https://codecov.io/gh/donmccurdy/three-to-cannon/branch/main/graph/badge.svg?token=S30LCC3L04)](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.