Skip to content

Commit

Permalink
Merge 70b51b7 into 3f0cda7
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Dec 20, 2019
2 parents 3f0cda7 + 70b51b7 commit c11bf9c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 129 deletions.
21 changes: 6 additions & 15 deletions modules/culling/src/lib/culling-volume.js
Expand Up @@ -54,10 +54,10 @@ export default class CullingVolume {
let plane1 = this.planes[planeIndex + 1];

if (!plane0) {
plane0 = this.planes[planeIndex] = new Vector4();
plane0 = this.planes[planeIndex] = new Plane();
}
if (!plane1) {
plane1 = this.planes[planeIndex + 1] = new Vector4();
plane1 = this.planes[planeIndex + 1] = new Plane();
}

const plane0Center = scratchPlaneCenter
Expand All @@ -66,11 +66,7 @@ export default class CullingVolume {
.add(center);
const plane0Distance = -faceNormal.dot(plane0Center);

// plane0.fromNormalDistance(faceNormal, plane0Distance);
plane0.x = faceNormal.x;
plane0.y = faceNormal.y;
plane0.z = faceNormal.z;
plane0.w = plane0Distance;
plane0.fromPointNormal(plane0Center, faceNormal);

const plane1Center = scratchPlaneCenter
.copy(faceNormal)
Expand All @@ -81,11 +77,7 @@ export default class CullingVolume {

const plane1Distance = -negatedFaceNormal.dot(plane1Center);

// plane1.fromNormalDistance(negatedFaceNormal, plane1Distance);
plane1.x = negatedFaceNormal.x;
plane1.y = negatedFaceNormal.y;
plane1.z = negatedFaceNormal.z;
plane1.w = plane1Distance;
plane1.fromPointNormal(plane1Center, negatedFaceNormal);

planeIndex += 2;
}
Expand All @@ -98,8 +90,7 @@ export default class CullingVolume {
assert(boundingVolume);
// const planes = this.planes;
let intersect = Intersect.INSIDE;
for (const planeCoefficients of this.planes) {
const plane = scratchPlane.fromCoefficients(...planeCoefficients);
for (const plane of this.planes) {
const result = boundingVolume.intersectPlane(plane);
switch (result) {
case Intersect.OUTSIDE:
Expand Down Expand Up @@ -150,7 +141,7 @@ export default class CullingVolume {
continue;
}

const plane = scratchPlane.fromCoefficients(...planes[k]);
const plane = planes[k];
const result = boundingVolume.intersectPlane(plane);
if (result === Intersect.OUTSIDE) {
return CullingVolume.MASK_OUTSIDE;
Expand Down
82 changes: 28 additions & 54 deletions modules/culling/src/lib/perspective-off-center-frustum.js
Expand Up @@ -5,9 +5,11 @@
// - It has not been fully adapted to math.gl conventions
// - Documentation has not been ported

import {Vector3, Vector4, Matrix4, assert} from 'math.gl';
import {Vector3, Matrix4, assert} from 'math.gl';
import CullingVolume from './culling-volume';
import Plane from './plane';

const scratchPlaneUpVector = new Vector3();
const scratchPlaneRightVector = new Vector3();
const scratchPlaneNearCenter = new Vector3();
const scratchPlaneFarCenter = new Vector3();
Expand Down Expand Up @@ -94,7 +96,14 @@ export default class PerspectiveOffCenterFrustum {
this.far = options.far;
this._far = this.far;

this._cullingVolume = new CullingVolume();
this._cullingVolume = new CullingVolume([
new Plane(),
new Plane(),
new Plane(),
new Plane(),
new Plane(),
new Plane()
]);
this._perspectiveMatrix = new Matrix4();
this._infinitePerspective = new Matrix4();
}
Expand Down Expand Up @@ -181,7 +190,11 @@ export default class PerspectiveOffCenterFrustum {

const planes = this._cullingVolume.planes;

const right = scratchPlaneRightVector.copy(direction).cross(up);
up = scratchPlaneUpVector.copy(up).normalize();
const right = scratchPlaneRightVector
.copy(direction)
.cross(up)
.normalize();

const nearCenter = scratchPlaneNearCenter
.copy(direction)
Expand All @@ -201,90 +214,51 @@ export default class PerspectiveOffCenterFrustum {
.multiplyByScalar(this.left)
.add(nearCenter)
.subtract(position)
.normalize()
.cross(up)
.normalize();
.cross(up);

planes[0] = planes[0] || new Vector4();
let plane = planes[0];
plane.x = normal.x;
plane.y = normal.y;
plane.z = normal.z;
plane.w = -normal.dot(position);
planes[0].fromPointNormal(position, normal);

// Right plane computation
normal
.copy(right)
.multiplyByScalar(this.right)
.add(nearCenter)
.subtract(position)
.normalize()
.cross(up)
.normalize();
.negate();

planes[1] = planes[1] || new Vector4();
plane = planes[1];
plane.x = normal.x;
plane.y = normal.y;
plane.z = normal.z;
plane.w = -normal.dot(position);
planes[1].fromPointNormal(position, normal);

// Bottom plane computation
normal
.copy(up)
.multiplyByScalar(this.bottom)
.add(nearCenter)
.subtract(position)
.normalize()
.cross(right)
.normalize();
.negate();

planes[2] = planes[2] || new Vector4();
plane = planes[2];
plane.x = normal.x;
plane.y = normal.y;
plane.z = normal.z;
plane.w = -normal.dot(position);
planes[2].fromPointNormal(position, normal);

// Top plane computation
normal
.copy(up)
.multiplyByScalar(this.top)
.add(nearCenter)
.subtract(position)
.normalize()
.cross(right)
.normalize();
.cross(right);

planes[3] = planes[3] || new Vector4();
plane = planes[3];
plane.x = normal.x;
plane.y = normal.y;
plane.z = normal.z;
plane.w = -normal.dot(position);
planes[3].fromPointNormal(position, normal);

normal = new Vector3().copy(direction).normalize();
normal = new Vector3().copy(direction);

// Near plane computation
planes[4] = planes[4] || new Vector4();
plane = planes[4];
plane.x = direction.x;
plane.y = direction.y;
plane.z = direction.z;
plane.w = -direction.dot(nearCenter);
planes[4].fromPointNormal(nearCenter, normal);

// Far plane computation
normal
.copy(direction)
.negate()
.normalize();
normal.negate();

planes[5] = planes[5] || new Vector4();
plane = planes[5];
plane.x = normal.x;
plane.y = normal.y;
plane.z = normal.z;
plane.w = -normal.dot(farCenter);
planes[5].fromPointNormal(farCenter, normal);

return this._cullingVolume;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/culling/src/lib/plane.js
Expand Up @@ -35,7 +35,7 @@ export default class Plane {
// Creates a plane from the general equation
fromCoefficients(a, b, c, d) {
this.normal.set(a, b, c);
assert(this.normal.len() === 1);
assert(equals(this.normal.len(), 1));
this.distance = d;
return this;
}
Expand Down

0 comments on commit c11bf9c

Please sign in to comment.