Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: pass object to convexPolyhedron #6

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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