Skip to content

Commit

Permalink
Merge pull request #6 from react-spring/developement-CovexPolyhedron
Browse files Browse the repository at this point in the history
Proposal: pass object to convexPolyhedron
  • Loading branch information
stockhuman committed Mar 17, 2020
2 parents d525849 + f5c563d commit 090b8c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/shapes/Box.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Box extends Shape {
new V(-sx, sy, sz),
]

const indices = [
const faces = [
[3, 2, 1, 0], // -z
[4, 5, 6, 7], // +z
[5, 4, 0, 1], // -y
Expand All @@ -65,7 +65,7 @@ export class Box extends Shape {

const axes = [new V(0, 0, 1), new V(0, 1, 0), new V(1, 0, 0)]

const h = new ConvexPolyhedron(vertices, indices, axes)
const h = new ConvexPolyhedron({vertices, faces, axes})
this.convexPolyhedronRepresentation = h
h.material = this.material
}
Expand Down
57 changes: 27 additions & 30 deletions src/shapes/ConvexPolyhedron.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,38 @@ import { Transform } from '../math/Transform'
*/

export class ConvexPolyhedron extends Shape {
constructor(points, faces, uniqueAxes, precomputedGeometry = null) {
constructor({ vertices = [], faces = [], normals = [], axes, boundingSphereRadius }) {
super({
type: Shape.types.CONVEXPOLYHEDRON,
})

if (precomputedGeometry) {
// assume valid THREE.Geometry structure and data
this.vertices = precomputedGeometry.vertices.map(v => new Vec3(v.x, v.y, v.z))
this.faces = precomputedGeometry.faces.map(f => [f.a, f.b, f.c])
this.faceNormals = precomputedGeometry.faces.map(f => new Vec3(f.normal.x, f.normal.y, f.normal.z))
// THREE.ConeGeometry does not provide a boundingSphere, for example
this.boundingSphereRadius = precomputedGeometry.boundingSphere
? precomputedGeometry.boundingSphere.radius
: this.updateBoundingSphereRadius()
} else {
/**
* Array of Vec3
* @property vertices
* @type {Array}
*/
this.vertices = points || []
/**
* Array of integer arrays, indicating which vertices each face consists of
* @property faces
* @type {Array}
*/
this.faces = faces || []
/**
* Array of Vec3
* @property faceNormals
* @type {Array}
*/
this.faceNormals = []
/**
* Array of Vec3
* @property vertices
* @type {Array}
*/
this.vertices = vertices
/**
* Array of integer arrays, indicating which vertices each face consists of
* @property faces
* @type {Array}
*/
this.faces = faces
/**
* Array of Vec3
* @property faceNormals
* @type {Array}
*/
this.faceNormals = normals

if (this.faceNormals == []) {
this.computeNormals()
}

if (!boundingSphereRadius) {
this.updateBoundingSphereRadius()
} else {
this.boundingSphereRadius = boundingSphereRadius
}

this.worldVertices = [] // World transformed version of .vertices
Expand All @@ -69,7 +66,7 @@ export class ConvexPolyhedron extends Shape {
* If given, these locally defined, normalized axes are the only ones being checked when doing separating axis check.
* @property {Array} uniqueAxes
*/
this.uniqueAxes = uniqueAxes ? uniqueAxes.slice() : null
this.uniqueAxes = axes ? axes.slice() : null

/**
* Array of Vec3
Expand Down

0 comments on commit 090b8c1

Please sign in to comment.