@math.gl/culling 3.6.0-alpha.8 → 3.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.ts +5 -5
- package/dist/constants.d.ts.map +1 -1
- package/dist/es5/constants.js +5 -7
- package/dist/es5/constants.js.map +1 -1
- package/dist/es5/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
- package/dist/es5/lib/bounding-volumes/bounding-sphere.js.map +1 -1
- package/dist/es5/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
- package/dist/es5/lib/culling-volume.js.map +1 -1
- package/dist/esm/constants.js +5 -7
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
- package/dist/esm/lib/bounding-volumes/bounding-sphere.js.map +1 -1
- package/dist/esm/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
- package/dist/esm/lib/culling-volume.js.map +1 -1
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +1 -2
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -1
- package/dist/lib/bounding-volumes/bounding-sphere.d.ts +1 -2
- package/dist/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -1
- package/dist/lib/bounding-volumes/bounding-volume.d.ts +1 -2
- package/dist/lib/bounding-volumes/bounding-volume.d.ts.map +1 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +1 -2
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -1
- package/dist/lib/culling-volume.d.ts +1 -2
- package/dist/lib/culling-volume.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/constants.ts +5 -5
- package/src/lib/bounding-volumes/axis-aligned-bounding-box.ts +1 -1
- package/src/lib/bounding-volumes/bounding-sphere.ts +1 -1
- package/src/lib/bounding-volumes/bounding-volume.ts +1 -1
- package/src/lib/bounding-volumes/oriented-bounding-box.ts +1 -1
- package/src/lib/culling-volume.ts +2 -2
- package/dist/constants.js +0 -8
- package/dist/index.js +0 -13
- package/dist/lib/algorithms/bounding-box-from-points.js +0 -131
- package/dist/lib/algorithms/bounding-sphere-from-points.js +0 -140
- package/dist/lib/algorithms/compute-eigen-decomposition.js +0 -131
- package/dist/lib/bounding-volumes/axis-aligned-bounding-box.js +0 -111
- package/dist/lib/bounding-volumes/bounding-sphere.js +0 -118
- package/dist/lib/bounding-volumes/bounding-volume.js +0 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.js +0 -256
- package/dist/lib/culling-volume.js +0 -121
- package/dist/lib/perspective-frustum.js +0 -190
- package/dist/lib/perspective-off-center-frustum.js +0 -270
- package/dist/lib/plane.js +0 -63
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
// This file is derived from the Cesium math library under Apache 2 license
|
|
2
|
-
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
|
|
3
|
-
/* eslint-disable */
|
|
4
|
-
import { Vector3, assert } from '@math.gl/core';
|
|
5
|
-
import { INTERSECTION } from '../constants';
|
|
6
|
-
import Plane from './plane';
|
|
7
|
-
// X, Y, Z Unit vectors
|
|
8
|
-
const faces = [new Vector3([1, 0, 0]), new Vector3([0, 1, 0]), new Vector3([0, 0, 1])];
|
|
9
|
-
const scratchPlaneCenter = new Vector3();
|
|
10
|
-
const scratchPlaneNormal = new Vector3();
|
|
11
|
-
const scratchPlane = new Plane(new Vector3(1.0, 0.0, 0.0), 0.0);
|
|
12
|
-
/** A culling volume defined by planes. */
|
|
13
|
-
export default class CullingVolume {
|
|
14
|
-
/**
|
|
15
|
-
* Create a new `CullingVolume` bounded by an array of clipping planed
|
|
16
|
-
* @param planes Array of clipping planes.
|
|
17
|
-
* */
|
|
18
|
-
constructor(planes = []) {
|
|
19
|
-
this.planes = planes;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Constructs a culling volume from a bounding sphere. Creates six planes that create a box containing the sphere.
|
|
23
|
-
* The planes are aligned to the x, y, and z axes in world coordinates.
|
|
24
|
-
*/
|
|
25
|
-
fromBoundingSphere(boundingSphere) {
|
|
26
|
-
this.planes.length = 2 * faces.length;
|
|
27
|
-
const center = boundingSphere.center;
|
|
28
|
-
const radius = boundingSphere.radius;
|
|
29
|
-
let planeIndex = 0;
|
|
30
|
-
for (const faceNormal of faces) {
|
|
31
|
-
let plane0 = this.planes[planeIndex];
|
|
32
|
-
let plane1 = this.planes[planeIndex + 1];
|
|
33
|
-
if (!plane0) {
|
|
34
|
-
plane0 = this.planes[planeIndex] = new Plane();
|
|
35
|
-
}
|
|
36
|
-
if (!plane1) {
|
|
37
|
-
plane1 = this.planes[planeIndex + 1] = new Plane();
|
|
38
|
-
}
|
|
39
|
-
const plane0Center = scratchPlaneCenter.copy(faceNormal).scale(-radius).add(center);
|
|
40
|
-
const plane0Distance = -faceNormal.dot(plane0Center);
|
|
41
|
-
plane0.fromPointNormal(plane0Center, faceNormal);
|
|
42
|
-
const plane1Center = scratchPlaneCenter.copy(faceNormal).scale(radius).add(center);
|
|
43
|
-
const negatedFaceNormal = scratchPlaneNormal.copy(faceNormal).negate();
|
|
44
|
-
const plane1Distance = -negatedFaceNormal.dot(plane1Center);
|
|
45
|
-
plane1.fromPointNormal(plane1Center, negatedFaceNormal);
|
|
46
|
-
planeIndex += 2;
|
|
47
|
-
}
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
/** Determines whether a bounding volume intersects the culling volume. */
|
|
51
|
-
computeVisibility(boundingVolume) {
|
|
52
|
-
// const planes = this.planes;
|
|
53
|
-
let intersect = INTERSECTION.INSIDE;
|
|
54
|
-
for (const plane of this.planes) {
|
|
55
|
-
const result = boundingVolume.intersectPlane(plane);
|
|
56
|
-
switch (result) {
|
|
57
|
-
case INTERSECTION.OUTSIDE:
|
|
58
|
-
// We are done
|
|
59
|
-
return INTERSECTION.OUTSIDE;
|
|
60
|
-
case INTERSECTION.INTERSECTING:
|
|
61
|
-
// If no other intersection is outside, return INTERSECTING
|
|
62
|
-
intersect = INTERSECTION.INTERSECTING;
|
|
63
|
-
break;
|
|
64
|
-
default:
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return intersect;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Determines whether a bounding volume intersects the culling volume.
|
|
71
|
-
*
|
|
72
|
-
* @param parentPlaneMask A bit mask from the boundingVolume's parent's check against the same culling
|
|
73
|
-
* volume, such that if (planeMask & (1 << planeIndex) === 0), for k < 31, then
|
|
74
|
-
* the parent (and therefore this) volume is completely inside plane[planeIndex]
|
|
75
|
-
* and that plane check can be skipped.
|
|
76
|
-
*/
|
|
77
|
-
computeVisibilityWithPlaneMask(boundingVolume, parentPlaneMask) {
|
|
78
|
-
assert(Number.isFinite(parentPlaneMask), 'parentPlaneMask is required.');
|
|
79
|
-
if (parentPlaneMask === CullingVolume.MASK_OUTSIDE ||
|
|
80
|
-
parentPlaneMask === CullingVolume.MASK_INSIDE) {
|
|
81
|
-
// parent is completely outside or completely inside, so this child is as well.
|
|
82
|
-
return parentPlaneMask;
|
|
83
|
-
}
|
|
84
|
-
// Start with MASK_INSIDE (all zeros) so that after the loop, the return value can be compared with MASK_INSIDE.
|
|
85
|
-
// (Because if there are fewer than 31 planes, the upper bits wont be changed.)
|
|
86
|
-
let mask = CullingVolume.MASK_INSIDE;
|
|
87
|
-
const planes = this.planes;
|
|
88
|
-
for (let k = 0; k < this.planes.length; ++k) {
|
|
89
|
-
// For k greater than 31 (since 31 is the maximum number of INSIDE/INTERSECTING bits we can store), skip the optimization.
|
|
90
|
-
const flag = k < 31 ? 1 << k : 0;
|
|
91
|
-
if (k < 31 && (parentPlaneMask & flag) === 0) {
|
|
92
|
-
// boundingVolume is known to be INSIDE this plane.
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
const plane = planes[k];
|
|
96
|
-
const result = boundingVolume.intersectPlane(plane);
|
|
97
|
-
if (result === INTERSECTION.OUTSIDE) {
|
|
98
|
-
return CullingVolume.MASK_OUTSIDE;
|
|
99
|
-
}
|
|
100
|
-
else if (result === INTERSECTION.INTERSECTING) {
|
|
101
|
-
mask |= flag;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return mask;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* For plane masks (as used in {@link CullingVolume#computeVisibilityWithPlaneMask}), this special value
|
|
109
|
-
* represents the case where the object bounding volume is entirely outside the culling volume.
|
|
110
|
-
*/
|
|
111
|
-
CullingVolume.MASK_OUTSIDE = 0xffffffff;
|
|
112
|
-
/**
|
|
113
|
-
* For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
|
|
114
|
-
* represents the case where the object bounding volume is entirely inside the culling volume.
|
|
115
|
-
*/
|
|
116
|
-
CullingVolume.MASK_INSIDE = 0x00000000;
|
|
117
|
-
/**
|
|
118
|
-
* For plane masks (as used in {@link CullingVolume.prototype.computeVisibilityWithPlaneMask}), this value
|
|
119
|
-
* represents the case where the object bounding volume (may) intersect all planes of the culling volume.
|
|
120
|
-
*/
|
|
121
|
-
CullingVolume.MASK_INDETERMINATE = 0x7fffffff;
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
// This file is derived from the Cesium math library under Apache 2 license
|
|
2
|
-
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
|
|
3
|
-
// Note: This class is still an experimental export, mainly used by other test cases
|
|
4
|
-
// - It has not been fully adapted to math.gl conventions
|
|
5
|
-
// - Documentation has not been ported
|
|
6
|
-
import { assert, Vector2 } from '@math.gl/core';
|
|
7
|
-
import PerspectiveOffCenterFrustum from './perspective-off-center-frustum';
|
|
8
|
-
const defined = (val) => val !== null && typeof val !== 'undefined';
|
|
9
|
-
/**
|
|
10
|
-
* The viewing frustum is defined by 6 planes.
|
|
11
|
-
* Each plane is represented by a {@link Vector4} object, where the x, y, and z components
|
|
12
|
-
* define the unit vector normal to the plane, and the w component is the distance of the
|
|
13
|
-
* plane from the origin/camera position.
|
|
14
|
-
*
|
|
15
|
-
* @alias PerspectiveFrustum
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* var frustum = new PerspectiveFrustum({
|
|
19
|
-
* fov : Math.PI_OVER_THREE,
|
|
20
|
-
* aspectRatio : canvas.clientWidth / canvas.clientHeight
|
|
21
|
-
* near : 1.0,
|
|
22
|
-
* far : 1000.0
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* @see PerspectiveOffCenterFrustum
|
|
26
|
-
*/
|
|
27
|
-
export default class PerspectiveFrustum {
|
|
28
|
-
constructor(options = {}) {
|
|
29
|
-
this._offCenterFrustum = new PerspectiveOffCenterFrustum();
|
|
30
|
-
const { fov, aspectRatio, near = 1.0, far = 500000000.0, xOffset = 0.0, yOffset = 0.0 } = options;
|
|
31
|
-
this.fov = fov;
|
|
32
|
-
this.aspectRatio = aspectRatio;
|
|
33
|
-
this.near = near;
|
|
34
|
-
this.far = far;
|
|
35
|
-
this.xOffset = xOffset;
|
|
36
|
-
this.yOffset = yOffset;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Returns a duplicate of a PerspectiveFrustum instance.
|
|
40
|
-
*/
|
|
41
|
-
clone() {
|
|
42
|
-
return new PerspectiveFrustum({
|
|
43
|
-
aspectRatio: this.aspectRatio,
|
|
44
|
-
fov: this.fov,
|
|
45
|
-
near: this.near,
|
|
46
|
-
far: this.far
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Compares the provided PerspectiveFrustum componentwise and returns
|
|
51
|
-
* <code>true</code> if they are equal, <code>false</code> otherwise.
|
|
52
|
-
*/
|
|
53
|
-
equals(other) {
|
|
54
|
-
if (!defined(other) || !(other instanceof PerspectiveFrustum)) {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
this._update();
|
|
58
|
-
other._update();
|
|
59
|
-
return (this.fov === other.fov &&
|
|
60
|
-
this.aspectRatio === other.aspectRatio &&
|
|
61
|
-
this.near === other.near &&
|
|
62
|
-
this.far === other.far &&
|
|
63
|
-
this._offCenterFrustum.equals(other._offCenterFrustum));
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Gets the perspective projection matrix computed from the view this.
|
|
67
|
-
*/
|
|
68
|
-
get projectionMatrix() {
|
|
69
|
-
this._update();
|
|
70
|
-
return this._offCenterFrustum.projectionMatrix;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* The perspective projection matrix computed from the view frustum with an infinite far plane.
|
|
74
|
-
*/
|
|
75
|
-
get infiniteProjectionMatrix() {
|
|
76
|
-
this._update();
|
|
77
|
-
return this._offCenterFrustum.infiniteProjectionMatrix;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Gets the angle of the vertical field of view, in radians.
|
|
81
|
-
*/
|
|
82
|
-
get fovy() {
|
|
83
|
-
this._update();
|
|
84
|
-
return this._fovy;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* @private
|
|
88
|
-
*/
|
|
89
|
-
get sseDenominator() {
|
|
90
|
-
this._update();
|
|
91
|
-
return this._sseDenominator;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Creates a culling volume for this this.ion.
|
|
95
|
-
* @returns {CullingVolume} A culling volume at the given position and orientation.
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* // Check if a bounding volume intersects the this.
|
|
99
|
-
* var cullingVolume = this.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
|
|
100
|
-
* var intersect = cullingVolume.computeVisibility(boundingVolume);
|
|
101
|
-
*/
|
|
102
|
-
computeCullingVolume(
|
|
103
|
-
/** A Vector3 defines the eye position. */
|
|
104
|
-
position,
|
|
105
|
-
/** A Vector3 defines the view direction. */
|
|
106
|
-
direction,
|
|
107
|
-
/** A Vector3 defines the up direction. */
|
|
108
|
-
up) {
|
|
109
|
-
this._update();
|
|
110
|
-
return this._offCenterFrustum.computeCullingVolume(position, direction, up);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Returns the pixel's width and height in meters.
|
|
114
|
-
* @returns {Vector2} The modified result parameter or a new instance of {@link Vector2} with the pixel's width and height in the x and y properties, respectively.
|
|
115
|
-
*
|
|
116
|
-
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
|
|
117
|
-
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* // Example 1
|
|
121
|
-
* // Get the width and height of a pixel.
|
|
122
|
-
* var pixelSize = camera.this.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, new Vector2());
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* // Example 2
|
|
126
|
-
* // Get the width and height of a pixel if the near plane was set to 'distance'.
|
|
127
|
-
* // For example, get the size of a pixel of an image on a billboard.
|
|
128
|
-
* var position = camera.position;
|
|
129
|
-
* var direction = camera.direction;
|
|
130
|
-
* var toCenter = Vector3.subtract(primitive.boundingVolume.center, position, new Vector3()); // vector from camera to a primitive
|
|
131
|
-
* var toCenterProj = Vector3.multiplyByScalar(direction, Vector3.dot(direction, toCenter), new Vector3()); // project vector onto camera direction vector
|
|
132
|
-
* var distance = Vector3.magnitude(toCenterProj);
|
|
133
|
-
* var pixelSize = camera.this.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, new Vector2());
|
|
134
|
-
*/
|
|
135
|
-
getPixelDimensions(
|
|
136
|
-
/** The width of the drawing buffer. */
|
|
137
|
-
drawingBufferWidth,
|
|
138
|
-
/** The height of the drawing buffer. */
|
|
139
|
-
drawingBufferHeight,
|
|
140
|
-
/** The distance to the near plane in meters. */
|
|
141
|
-
distance,
|
|
142
|
-
/** The object onto which to store the result. */
|
|
143
|
-
result) {
|
|
144
|
-
this._update();
|
|
145
|
-
return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result || new Vector2());
|
|
146
|
-
}
|
|
147
|
-
// eslint-disable-next-line complexity, max-statements
|
|
148
|
-
_update() {
|
|
149
|
-
assert(Number.isFinite(this.fov) &&
|
|
150
|
-
Number.isFinite(this.aspectRatio) &&
|
|
151
|
-
Number.isFinite(this.near) &&
|
|
152
|
-
Number.isFinite(this.far));
|
|
153
|
-
// 'fov, aspectRatio, near, or far parameters are not set.'
|
|
154
|
-
const f = this._offCenterFrustum;
|
|
155
|
-
if (this.fov !== this._fov ||
|
|
156
|
-
this.aspectRatio !== this._aspectRatio ||
|
|
157
|
-
this.near !== this._near ||
|
|
158
|
-
this.far !== this._far ||
|
|
159
|
-
this.xOffset !== this._xOffset ||
|
|
160
|
-
this.yOffset !== this._yOffset) {
|
|
161
|
-
assert(this.fov >= 0 && this.fov < Math.PI);
|
|
162
|
-
// throw new DeveloperError('fov must be in the range [0, PI).');
|
|
163
|
-
assert(this.aspectRatio > 0);
|
|
164
|
-
// throw new DeveloperError('aspectRatio must be positive.');
|
|
165
|
-
assert(this.near >= 0 && this.near < this.far);
|
|
166
|
-
// throw new DeveloperError('near must be greater than zero and less than far.');
|
|
167
|
-
this._aspectRatio = this.aspectRatio;
|
|
168
|
-
this._fov = this.fov;
|
|
169
|
-
this._fovy =
|
|
170
|
-
this.aspectRatio <= 1
|
|
171
|
-
? this.fov
|
|
172
|
-
: Math.atan(Math.tan(this.fov * 0.5) / this.aspectRatio) * 2.0;
|
|
173
|
-
this._near = this.near;
|
|
174
|
-
this._far = this.far;
|
|
175
|
-
this._sseDenominator = 2.0 * Math.tan(0.5 * this._fovy);
|
|
176
|
-
this._xOffset = this.xOffset;
|
|
177
|
-
this._yOffset = this.yOffset;
|
|
178
|
-
f.top = this.near * Math.tan(0.5 * this._fovy);
|
|
179
|
-
f.bottom = -f.top;
|
|
180
|
-
f.right = this.aspectRatio * f.top;
|
|
181
|
-
f.left = -f.right;
|
|
182
|
-
f.near = this.near;
|
|
183
|
-
f.far = this.far;
|
|
184
|
-
f.right += this.xOffset;
|
|
185
|
-
f.left += this.xOffset;
|
|
186
|
-
f.top += this.yOffset;
|
|
187
|
-
f.bottom += this.yOffset;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
// This file is derived from the Cesium math library under Apache 2 license
|
|
2
|
-
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
|
|
3
|
-
// Note: This class is still an experimental export, mainly used by other test cases
|
|
4
|
-
// - It has not been fully adapted to math.gl conventions
|
|
5
|
-
// - Documentation has not been ported
|
|
6
|
-
import { Vector3, Matrix4, assert } from '@math.gl/core';
|
|
7
|
-
import CullingVolume from './culling-volume';
|
|
8
|
-
import Plane from './plane';
|
|
9
|
-
const scratchPlaneUpVector = new Vector3();
|
|
10
|
-
const scratchPlaneRightVector = new Vector3();
|
|
11
|
-
const scratchPlaneNearCenter = new Vector3();
|
|
12
|
-
const scratchPlaneFarCenter = new Vector3();
|
|
13
|
-
const scratchPlaneNormal = new Vector3();
|
|
14
|
-
export default class PerspectiveOffCenterFrustum {
|
|
15
|
-
/**
|
|
16
|
-
* The viewing frustum is defined by 6 planes.
|
|
17
|
-
* Each plane is represented by a {@link Vector4} object, where the x, y, and z components
|
|
18
|
-
* define the unit vector normal to the plane, and the w component is the distance of the
|
|
19
|
-
* plane from the origin/camera position.
|
|
20
|
-
*
|
|
21
|
-
* @alias PerspectiveOffCenterFrustum
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* const frustum = new PerspectiveOffCenterFrustum({
|
|
25
|
-
* left : -1.0,
|
|
26
|
-
* right : 1.0,
|
|
27
|
-
* top : 1.0,
|
|
28
|
-
* bottom : -1.0,
|
|
29
|
-
* near : 1.0,
|
|
30
|
-
* far : 100.0
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* @see PerspectiveFrustum
|
|
34
|
-
*/
|
|
35
|
-
constructor(options = {}) {
|
|
36
|
-
this._cullingVolume = new CullingVolume([
|
|
37
|
-
new Plane(),
|
|
38
|
-
new Plane(),
|
|
39
|
-
new Plane(),
|
|
40
|
-
new Plane(),
|
|
41
|
-
new Plane(),
|
|
42
|
-
new Plane()
|
|
43
|
-
]);
|
|
44
|
-
this._perspectiveMatrix = new Matrix4();
|
|
45
|
-
this._infinitePerspective = new Matrix4();
|
|
46
|
-
const { near = 1.0, far = 500000000.0 } = options;
|
|
47
|
-
this.left = options.left;
|
|
48
|
-
this._left = undefined;
|
|
49
|
-
this.right = options.right;
|
|
50
|
-
this._right = undefined;
|
|
51
|
-
this.top = options.top;
|
|
52
|
-
this._top = undefined;
|
|
53
|
-
this.bottom = options.bottom;
|
|
54
|
-
this._bottom = undefined;
|
|
55
|
-
this.near = near;
|
|
56
|
-
this._near = near;
|
|
57
|
-
this.far = far;
|
|
58
|
-
this._far = far;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Returns a duplicate of a PerspectiveOffCenterFrustum instance.
|
|
62
|
-
* @returns {PerspectiveOffCenterFrustum} A new PerspectiveFrustum instance.
|
|
63
|
-
* */
|
|
64
|
-
clone() {
|
|
65
|
-
return new PerspectiveOffCenterFrustum({
|
|
66
|
-
right: this.right,
|
|
67
|
-
left: this.left,
|
|
68
|
-
top: this.top,
|
|
69
|
-
bottom: this.bottom,
|
|
70
|
-
near: this.near,
|
|
71
|
-
far: this.far
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Compares the provided PerspectiveOffCenterFrustum componentwise and returns
|
|
76
|
-
* <code>true</code> if they are equal, <code>false</code> otherwise.
|
|
77
|
-
*
|
|
78
|
-
* @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
|
|
79
|
-
*/
|
|
80
|
-
equals(other) {
|
|
81
|
-
return (other &&
|
|
82
|
-
other instanceof PerspectiveOffCenterFrustum &&
|
|
83
|
-
this.right === other.right &&
|
|
84
|
-
this.left === other.left &&
|
|
85
|
-
this.top === other.top &&
|
|
86
|
-
this.bottom === other.bottom &&
|
|
87
|
-
this.near === other.near &&
|
|
88
|
-
this.far === other.far);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Gets the perspective projection matrix computed from the view frustum.
|
|
92
|
-
* @memberof PerspectiveOffCenterFrustum.prototype
|
|
93
|
-
* @type {Matrix4}
|
|
94
|
-
*
|
|
95
|
-
* @see PerspectiveOffCenterFrustum#infiniteProjectionMatrix
|
|
96
|
-
*/
|
|
97
|
-
get projectionMatrix() {
|
|
98
|
-
this._update();
|
|
99
|
-
return this._perspectiveMatrix;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Gets the perspective projection matrix computed from the view frustum with an infinite far plane.
|
|
103
|
-
* @memberof PerspectiveOffCenterFrustum.prototype
|
|
104
|
-
* @type {Matrix4}
|
|
105
|
-
*
|
|
106
|
-
* @see PerspectiveOffCenterFrustum#projectionMatrix
|
|
107
|
-
*/
|
|
108
|
-
get infiniteProjectionMatrix() {
|
|
109
|
-
this._update();
|
|
110
|
-
return this._infinitePerspective;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Creates a culling volume for this frustum.
|
|
114
|
-
* @returns {CullingVolume} A culling volume at the given position and orientation.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* // Check if a bounding volume intersects the frustum.
|
|
118
|
-
* const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
|
|
119
|
-
* const intersect = cullingVolume.computeVisibility(boundingVolume);
|
|
120
|
-
*/
|
|
121
|
-
// eslint-disable-next-line complexity, max-statements
|
|
122
|
-
computeCullingVolume(
|
|
123
|
-
/** A Vector3 defines the eye position. */
|
|
124
|
-
position,
|
|
125
|
-
/** A Vector3 defines the view direction. */
|
|
126
|
-
direction,
|
|
127
|
-
/** A Vector3 defines the up direction. */
|
|
128
|
-
up) {
|
|
129
|
-
assert(position, 'position is required.');
|
|
130
|
-
assert(direction, 'direction is required.');
|
|
131
|
-
assert(up, 'up is required.');
|
|
132
|
-
const planes = this._cullingVolume.planes;
|
|
133
|
-
up = scratchPlaneUpVector.copy(up).normalize();
|
|
134
|
-
const right = scratchPlaneRightVector.copy(direction).cross(up).normalize();
|
|
135
|
-
const nearCenter = scratchPlaneNearCenter
|
|
136
|
-
.copy(direction)
|
|
137
|
-
.multiplyByScalar(this.near)
|
|
138
|
-
.add(position);
|
|
139
|
-
const farCenter = scratchPlaneFarCenter
|
|
140
|
-
.copy(direction)
|
|
141
|
-
.multiplyByScalar(this.far)
|
|
142
|
-
.add(position);
|
|
143
|
-
let normal = scratchPlaneNormal;
|
|
144
|
-
// Left plane computation
|
|
145
|
-
normal.copy(right).multiplyByScalar(this.left).add(nearCenter).subtract(position).cross(up);
|
|
146
|
-
planes[0].fromPointNormal(position, normal);
|
|
147
|
-
// Right plane computation
|
|
148
|
-
normal
|
|
149
|
-
.copy(right)
|
|
150
|
-
.multiplyByScalar(this.right)
|
|
151
|
-
.add(nearCenter)
|
|
152
|
-
.subtract(position)
|
|
153
|
-
.cross(up)
|
|
154
|
-
.negate();
|
|
155
|
-
planes[1].fromPointNormal(position, normal);
|
|
156
|
-
// Bottom plane computation
|
|
157
|
-
normal
|
|
158
|
-
.copy(up)
|
|
159
|
-
.multiplyByScalar(this.bottom)
|
|
160
|
-
.add(nearCenter)
|
|
161
|
-
.subtract(position)
|
|
162
|
-
.cross(right)
|
|
163
|
-
.negate();
|
|
164
|
-
planes[2].fromPointNormal(position, normal);
|
|
165
|
-
// Top plane computation
|
|
166
|
-
normal.copy(up).multiplyByScalar(this.top).add(nearCenter).subtract(position).cross(right);
|
|
167
|
-
planes[3].fromPointNormal(position, normal);
|
|
168
|
-
normal = new Vector3().copy(direction);
|
|
169
|
-
// Near plane computation
|
|
170
|
-
planes[4].fromPointNormal(nearCenter, normal);
|
|
171
|
-
// Far plane computation
|
|
172
|
-
normal.negate();
|
|
173
|
-
planes[5].fromPointNormal(farCenter, normal);
|
|
174
|
-
return this._cullingVolume;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Returns the pixel's width and height in meters.
|
|
178
|
-
*
|
|
179
|
-
* @returns {Vector2} The modified result parameter or a new instance of {@link Vector2} with the pixel's width and height in the x and y properties, respectively.
|
|
180
|
-
*
|
|
181
|
-
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
|
|
182
|
-
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
|
|
183
|
-
*
|
|
184
|
-
* @example
|
|
185
|
-
* // Example 1
|
|
186
|
-
* // Get the width and height of a pixel.
|
|
187
|
-
* const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, new Vector2());
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* // Example 2
|
|
191
|
-
* // Get the width and height of a pixel if the near plane was set to 'distance'.
|
|
192
|
-
* // For example, get the size of a pixel of an image on a billboard.
|
|
193
|
-
* const position = camera.position;
|
|
194
|
-
* const direction = camera.direction;
|
|
195
|
-
* const toCenter = Vector3.subtract(primitive.boundingVolume.center, position, new Vector3()); // vector from camera to a primitive
|
|
196
|
-
* const toCenterProj = Vector3.multiplyByScalar(direction, Vector3.dot(direction, toCenter), new Vector3()); // project vector onto camera direction vector
|
|
197
|
-
* const distance = Vector3.magnitude(toCenterProj);
|
|
198
|
-
* const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, new Vector2());
|
|
199
|
-
*/
|
|
200
|
-
getPixelDimensions(
|
|
201
|
-
/** The width of the drawing buffer. */
|
|
202
|
-
drawingBufferWidth,
|
|
203
|
-
/** The height of the drawing buffer. */
|
|
204
|
-
drawingBufferHeight,
|
|
205
|
-
/** The distance to the near plane in meters. */
|
|
206
|
-
distance,
|
|
207
|
-
/** The object onto which to store the result. */
|
|
208
|
-
result) {
|
|
209
|
-
this._update();
|
|
210
|
-
assert(Number.isFinite(drawingBufferWidth) && Number.isFinite(drawingBufferHeight));
|
|
211
|
-
// 'Both drawingBufferWidth and drawingBufferHeight are required.'
|
|
212
|
-
assert(drawingBufferWidth > 0);
|
|
213
|
-
// 'drawingBufferWidth must be greater than zero.'
|
|
214
|
-
assert(drawingBufferHeight > 0);
|
|
215
|
-
// 'drawingBufferHeight must be greater than zero.'
|
|
216
|
-
assert(distance > 0);
|
|
217
|
-
// 'distance is required.');
|
|
218
|
-
assert(result);
|
|
219
|
-
// 'A result object is required.');
|
|
220
|
-
const inverseNear = 1.0 / this.near;
|
|
221
|
-
let tanTheta = this.top * inverseNear;
|
|
222
|
-
const pixelHeight = (2.0 * distance * tanTheta) / drawingBufferHeight;
|
|
223
|
-
tanTheta = this.right * inverseNear;
|
|
224
|
-
const pixelWidth = (2.0 * distance * tanTheta) / drawingBufferWidth;
|
|
225
|
-
result.x = pixelWidth;
|
|
226
|
-
result.y = pixelHeight;
|
|
227
|
-
return result;
|
|
228
|
-
}
|
|
229
|
-
// eslint-disable-next-line complexity, max-statements
|
|
230
|
-
_update() {
|
|
231
|
-
assert(Number.isFinite(this.right) &&
|
|
232
|
-
Number.isFinite(this.left) &&
|
|
233
|
-
Number.isFinite(this.top) &&
|
|
234
|
-
Number.isFinite(this.bottom) &&
|
|
235
|
-
Number.isFinite(this.near) &&
|
|
236
|
-
Number.isFinite(this.far));
|
|
237
|
-
// throw new DeveloperError('right, left, top, bottom, near, or far parameters are not set.');
|
|
238
|
-
const { top, bottom, right, left, near, far } = this;
|
|
239
|
-
if (top !== this._top ||
|
|
240
|
-
bottom !== this._bottom ||
|
|
241
|
-
left !== this._left ||
|
|
242
|
-
right !== this._right ||
|
|
243
|
-
near !== this._near ||
|
|
244
|
-
far !== this._far) {
|
|
245
|
-
assert(this.near > 0 && this.near < this.far, 'near must be greater than zero and less than far.');
|
|
246
|
-
this._left = left;
|
|
247
|
-
this._right = right;
|
|
248
|
-
this._top = top;
|
|
249
|
-
this._bottom = bottom;
|
|
250
|
-
this._near = near;
|
|
251
|
-
this._far = far;
|
|
252
|
-
this._perspectiveMatrix = new Matrix4().frustum({
|
|
253
|
-
left,
|
|
254
|
-
right,
|
|
255
|
-
bottom,
|
|
256
|
-
top,
|
|
257
|
-
near,
|
|
258
|
-
far
|
|
259
|
-
});
|
|
260
|
-
this._infinitePerspective = new Matrix4().frustum({
|
|
261
|
-
left,
|
|
262
|
-
right,
|
|
263
|
-
bottom,
|
|
264
|
-
top,
|
|
265
|
-
near,
|
|
266
|
-
far: Infinity
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
package/dist/lib/plane.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// This file is derived from the Cesium math library under Apache 2 license
|
|
2
|
-
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
|
|
3
|
-
/* eslint-disable */
|
|
4
|
-
import { Vector3, equals, assert } from '@math.gl/core';
|
|
5
|
-
const scratchPosition = new Vector3();
|
|
6
|
-
const scratchNormal = new Vector3();
|
|
7
|
-
// A plane in Hessian Normal Form
|
|
8
|
-
export default class Plane {
|
|
9
|
-
constructor(normal = [0, 0, 1], distance = 0) {
|
|
10
|
-
this.normal = new Vector3();
|
|
11
|
-
this.distance = -0;
|
|
12
|
-
this.fromNormalDistance(normal, distance);
|
|
13
|
-
}
|
|
14
|
-
/** Creates a plane from a normal and a distance from the origin. */
|
|
15
|
-
fromNormalDistance(normal, distance) {
|
|
16
|
-
assert(Number.isFinite(distance));
|
|
17
|
-
this.normal.from(normal).normalize();
|
|
18
|
-
this.distance = distance;
|
|
19
|
-
return this;
|
|
20
|
-
}
|
|
21
|
-
/** Creates a plane from a normal and a point on the plane. */
|
|
22
|
-
fromPointNormal(point, normal) {
|
|
23
|
-
point = scratchPosition.from(point);
|
|
24
|
-
this.normal.from(normal).normalize();
|
|
25
|
-
const distance = -this.normal.dot(point);
|
|
26
|
-
this.distance = distance;
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
/** Creates a plane from the general equation */
|
|
30
|
-
fromCoefficients(a, b, c, d) {
|
|
31
|
-
this.normal.set(a, b, c);
|
|
32
|
-
assert(equals(this.normal.len(), 1));
|
|
33
|
-
this.distance = d;
|
|
34
|
-
return this;
|
|
35
|
-
}
|
|
36
|
-
/** Duplicates a Plane instance. */
|
|
37
|
-
clone() {
|
|
38
|
-
return new Plane(this.normal, this.distance);
|
|
39
|
-
}
|
|
40
|
-
/** Compares the provided Planes by normal and distance */
|
|
41
|
-
equals(right) {
|
|
42
|
-
return equals(this.distance, right.distance) && equals(this.normal, right.normal);
|
|
43
|
-
}
|
|
44
|
-
/** Computes the signed shortest distance of a point to a plane.
|
|
45
|
-
* The sign of the distance determines which side of the plane the point is on.
|
|
46
|
-
*/
|
|
47
|
-
getPointDistance(point) {
|
|
48
|
-
return this.normal.dot(point) + this.distance;
|
|
49
|
-
}
|
|
50
|
-
/** Transforms the plane by the given transformation matrix. */
|
|
51
|
-
transform(matrix4) {
|
|
52
|
-
const normal = scratchNormal.copy(this.normal).transformAsVector(matrix4).normalize();
|
|
53
|
-
const point = this.normal.scale(-this.distance).transform(matrix4);
|
|
54
|
-
return this.fromPointNormal(point, normal);
|
|
55
|
-
}
|
|
56
|
-
projectPointOntoPlane(point, result = [0, 0, 0]) {
|
|
57
|
-
point = scratchPosition.from(point);
|
|
58
|
-
// projectedPoint = point - (normal.point + scale) * normal
|
|
59
|
-
const pointDistance = this.getPointDistance(point);
|
|
60
|
-
const scaledNormal = scratchNormal.copy(this.normal).scale(pointDistance);
|
|
61
|
-
return point.subtract(scaledNormal).to(result);
|
|
62
|
-
}
|
|
63
|
-
}
|