Created
September 21, 2020 19:08
-
-
Save a-type/b65058da523b6759c8e769a2f05bf464 to your computer and use it in GitHub Desktop.
ThreeJS mesh geometry collider in cannon.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// supposing "geometry" is a ThreeJS BufferGeometry - if it's a geometry, just skip this step | |
const geo = new Geometry().fromBufferGeometry(geometry); | |
const vertices = []; | |
const indices = []; | |
// "scale" is a uniform scale you may or may not have applied to your mesh as a whole. | |
// just set it to 1 if you don't scale the mesh. | |
for (const vert of geo.vertices) { | |
vertices.push(vert.x * scale); | |
vertices.push(vert.y * scale); | |
vertices.push(vert.z * scale); | |
} | |
for (const face of geo.faces) { | |
indices.push(face.a); | |
indices.push(face.b); | |
indices.push(face.c); | |
} | |
const collider = new Trimesh(vertices, indices); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment