-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathPerspectiveFrustum.js
459 lines (412 loc) · 13.5 KB
/
PerspectiveFrustum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import Check from "./Check.js";
import Frozen from "./Frozen.js";
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
import CesiumMath from "./Math.js";
import PerspectiveOffCenterFrustum from "./PerspectiveOffCenterFrustum.js";
/**
* The viewing frustum is defined by 6 planes.
* Each plane is represented by a {@link Cartesian4} object, where the x, y, and z components
* define the unit vector normal to the plane, and the w component is the distance of the
* plane from the origin/camera position.
*
* @alias PerspectiveFrustum
* @constructor
*
* @param {object} [options] An object with the following properties:
* @param {number} [options.fov] The angle of the field of view (FOV), in radians.
* @param {number} [options.aspectRatio] The aspect ratio of the frustum's width to it's height.
* @param {number} [options.near=1.0] The distance of the near plane.
* @param {number} [options.far=500000000.0] The distance of the far plane.
* @param {number} [options.xOffset=0.0] The offset in the x direction.
* @param {number} [options.yOffset=0.0] The offset in the y direction.
*
* @example
* const frustum = new Cesium.PerspectiveFrustum({
* fov : Cesium.Math.PI_OVER_THREE,
* aspectRatio : canvas.clientWidth / canvas.clientHeight
* near : 1.0,
* far : 1000.0
* });
*
* @see PerspectiveOffCenterFrustum
*/
function PerspectiveFrustum(options) {
options = options ?? Frozen.EMPTY_OBJECT;
this._offCenterFrustum = new PerspectiveOffCenterFrustum();
/**
* The angle of the field of view (FOV), in radians. This angle will be used
* as the horizontal FOV if the width is greater than the height, otherwise
* it will be the vertical FOV.
* @type {number|undefined}
* @default undefined
*/
this.fov = options.fov;
this._fov = undefined;
this._fovy = undefined;
this._sseDenominator = undefined;
/**
* The aspect ratio of the frustum's width to it's height.
* @type {number|undefined}
* @default undefined
*/
this.aspectRatio = options.aspectRatio;
this._aspectRatio = undefined;
/**
* The distance of the near plane.
* @type {number}
* @default 1.0
*/
this.near = options.near ?? 1.0;
this._near = this.near;
/**
* The distance of the far plane.
* @type {number}
* @default 500000000.0
*/
this.far = options.far ?? 500000000.0;
this._far = this.far;
/**
* Offsets the frustum in the x direction.
* @type {number}
* @default 0.0
*/
this.xOffset = options.xOffset ?? 0.0;
this._xOffset = this.xOffset;
/**
* Offsets the frustum in the y direction.
* @type {number}
* @default 0.0
*/
this.yOffset = options.yOffset ?? 0.0;
this._yOffset = this.yOffset;
}
/**
* The number of elements used to pack the object into an array.
* @type {number}
*/
PerspectiveFrustum.packedLength = 6;
/**
* Stores the provided instance into the provided array.
*
* @param {PerspectiveFrustum} value The value to pack.
* @param {number[]} array The array to pack into.
* @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
*
* @returns {number[]} The array that was packed into
*/
PerspectiveFrustum.pack = function (value, array, startingIndex) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("value", value);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = startingIndex ?? 0;
array[startingIndex++] = value.fov;
array[startingIndex++] = value.aspectRatio;
array[startingIndex++] = value.near;
array[startingIndex++] = value.far;
array[startingIndex++] = value.xOffset;
array[startingIndex] = value.yOffset;
return array;
};
/**
* Retrieves an instance from a packed array.
*
* @param {number[]} array The packed array.
* @param {number} [startingIndex=0] The starting index of the element to be unpacked.
* @param {PerspectiveFrustum} [result] The object into which to store the result.
* @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
*/
PerspectiveFrustum.unpack = function (array, startingIndex, result) {
//>>includeStart('debug', pragmas.debug);
Check.defined("array", array);
//>>includeEnd('debug');
startingIndex = startingIndex ?? 0;
if (!defined(result)) {
result = new PerspectiveFrustum();
}
result.fov = array[startingIndex++];
result.aspectRatio = array[startingIndex++];
result.near = array[startingIndex++];
result.far = array[startingIndex++];
result.xOffset = array[startingIndex++];
result.yOffset = array[startingIndex];
return result;
};
function update(frustum) {
//>>includeStart('debug', pragmas.debug);
if (
!defined(frustum.fov) ||
!defined(frustum.aspectRatio) ||
!defined(frustum.near) ||
!defined(frustum.far)
) {
throw new DeveloperError(
"fov, aspectRatio, near, or far parameters are not set.",
);
}
//>>includeEnd('debug');
const changed =
frustum.fov !== frustum._fov ||
frustum.aspectRatio !== frustum._aspectRatio ||
frustum.near !== frustum._near ||
frustum.far !== frustum._far ||
frustum.xOffset !== frustum._xOffset ||
frustum.yOffset !== frustum._yOffset;
if (!changed) {
return;
}
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number.greaterThanOrEquals("fov", frustum.fov, 0.0);
Check.typeOf.number.lessThan("fov", frustum.fov, Math.PI);
Check.typeOf.number.greaterThanOrEquals(
"aspectRatio",
frustum.aspectRatio,
0.0,
);
Check.typeOf.number.greaterThanOrEquals("near", frustum.near, 0.0);
if (frustum.near > frustum.far) {
throw new DeveloperError("near must be less than far.");
}
//>>includeEnd('debug');
frustum._aspectRatio = frustum.aspectRatio;
frustum._fov = frustum.fov;
frustum._fovy =
frustum.aspectRatio <= 1
? frustum.fov
: Math.atan(Math.tan(frustum.fov * 0.5) / frustum.aspectRatio) * 2.0;
frustum._near = frustum.near;
frustum._far = frustum.far;
frustum._sseDenominator = 2.0 * Math.tan(0.5 * frustum._fovy);
frustum._xOffset = frustum.xOffset;
frustum._yOffset = frustum.yOffset;
const f = frustum._offCenterFrustum;
f.top = frustum.near * Math.tan(0.5 * frustum._fovy);
f.bottom = -f.top;
f.right = frustum.aspectRatio * f.top;
f.left = -f.right;
f.near = frustum.near;
f.far = frustum.far;
f.right += frustum.xOffset;
f.left += frustum.xOffset;
f.top += frustum.yOffset;
f.bottom += frustum.yOffset;
}
Object.defineProperties(PerspectiveFrustum.prototype, {
/**
* Gets the perspective projection matrix computed from the view frustum.
* If necessary, the projection matrix will be recomputed.
*
* @memberof PerspectiveFrustum.prototype
* @type {Matrix4}
* @readonly
*
* @see PerspectiveOffCenterFrustum#projectionMatrix.
* @see PerspectiveFrustum#infiniteProjectionMatrix
*/
projectionMatrix: {
get: function () {
update(this);
return this._offCenterFrustum.projectionMatrix;
},
},
/**
* The perspective projection matrix computed from the view frustum with an infinite far plane.
* @memberof PerspectiveFrustum.prototype
* @type {Matrix4}
* @readonly
*
* @see PerspectiveFrustum#projectionMatrix
*/
infiniteProjectionMatrix: {
get: function () {
update(this);
return this._offCenterFrustum.infiniteProjectionMatrix;
},
},
/**
* Gets the angle of the vertical field of view, in radians.
* @memberof PerspectiveFrustum.prototype
* @type {number|undefined}
* @readonly
* @default undefined
*/
fovy: {
get: function () {
update(this);
return this._fovy;
},
},
/**
* @readonly
* @private
*/
sseDenominator: {
get: function () {
update(this);
return this._sseDenominator;
},
},
/**
* Gets the orthographic projection matrix computed from the view frustum.
* @memberof PerspectiveFrustum.prototype
* @type {PerspectiveOffCenterFrustum}
* @readonly
* @private
*/
offCenterFrustum: {
get: function () {
update(this);
return this._offCenterFrustum;
},
},
});
/**
* Creates a culling volume for this frustum.
*
* @param {Cartesian3} position The eye position.
* @param {Cartesian3} direction The view direction.
* @param {Cartesian3} up The up direction.
* @returns {CullingVolume} A culling volume at the given position and orientation.
*
* @example
* // Check if a bounding volume intersects the frustum.
* const cullingVolume = frustum.computeCullingVolume(cameraPosition, cameraDirection, cameraUp);
* const intersect = cullingVolume.computeVisibility(boundingVolume);
*/
PerspectiveFrustum.prototype.computeCullingVolume = function (
position,
direction,
up,
) {
update(this);
return this._offCenterFrustum.computeCullingVolume(position, direction, up);
};
/**
* Returns the pixel's width and height in meters.
*
* @param {number} drawingBufferWidth The width of the drawing buffer.
* @param {number} drawingBufferHeight The height of the drawing buffer.
* @param {number} distance The distance to the near plane in meters.
* @param {number} pixelRatio The scaling factor from pixel space to coordinate space.
* @param {Cartesian2} result The object onto which to store the result.
* @returns {Cartesian2} The modified result parameter or a new instance of {@link Cartesian2} with the pixel's width and height in the x and y properties, respectively.
*
* @exception {DeveloperError} drawingBufferWidth must be greater than zero.
* @exception {DeveloperError} drawingBufferHeight must be greater than zero.
* @exception {DeveloperError} pixelRatio must be greater than zero.
*
* @example
* // Example 1
* // Get the width and height of a pixel.
* const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, 1.0, scene.pixelRatio, new Cesium.Cartesian2());
*
* @example
* // Example 2
* // Get the width and height of a pixel if the near plane was set to 'distance'.
* // For example, get the size of a pixel of an image on a billboard.
* const position = camera.position;
* const direction = camera.direction;
* const toCenter = Cesium.Cartesian3.subtract(primitive.boundingVolume.center, position, new Cesium.Cartesian3()); // vector from camera to a primitive
* const toCenterProj = Cesium.Cartesian3.multiplyByScalar(direction, Cesium.Cartesian3.dot(direction, toCenter), new Cesium.Cartesian3()); // project vector onto camera direction vector
* const distance = Cesium.Cartesian3.magnitude(toCenterProj);
* const pixelSize = camera.frustum.getPixelDimensions(scene.drawingBufferWidth, scene.drawingBufferHeight, distance, scene.pixelRatio, new Cesium.Cartesian2());
*/
PerspectiveFrustum.prototype.getPixelDimensions = function (
drawingBufferWidth,
drawingBufferHeight,
distance,
pixelRatio,
result,
) {
update(this);
return this._offCenterFrustum.getPixelDimensions(
drawingBufferWidth,
drawingBufferHeight,
distance,
pixelRatio,
result,
);
};
/**
* Returns a duplicate of a PerspectiveFrustum instance.
*
* @param {PerspectiveFrustum} [result] The object onto which to store the result.
* @returns {PerspectiveFrustum} The modified result parameter or a new PerspectiveFrustum instance if one was not provided.
*/
PerspectiveFrustum.prototype.clone = function (result) {
if (!defined(result)) {
result = new PerspectiveFrustum();
}
result.aspectRatio = this.aspectRatio;
result.fov = this.fov;
result.near = this.near;
result.far = this.far;
// force update of clone to compute matrices
result._aspectRatio = undefined;
result._fov = undefined;
result._near = undefined;
result._far = undefined;
this._offCenterFrustum.clone(result._offCenterFrustum);
return result;
};
/**
* Compares the provided PerspectiveFrustum componentwise and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
*
* @param {PerspectiveFrustum} [other] The right hand side PerspectiveFrustum.
* @returns {boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
*/
PerspectiveFrustum.prototype.equals = function (other) {
if (!defined(other) || !(other instanceof PerspectiveFrustum)) {
return false;
}
update(this);
update(other);
return (
this.fov === other.fov &&
this.aspectRatio === other.aspectRatio &&
this._offCenterFrustum.equals(other._offCenterFrustum)
);
};
/**
* Compares the provided PerspectiveFrustum componentwise and returns
* <code>true</code> if they pass an absolute or relative tolerance test,
* <code>false</code> otherwise.
*
* @param {PerspectiveFrustum} other The right hand side PerspectiveFrustum.
* @param {number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
* @param {number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
* @returns {boolean} <code>true</code> if this and other are within the provided epsilon, <code>false</code> otherwise.
*/
PerspectiveFrustum.prototype.equalsEpsilon = function (
other,
relativeEpsilon,
absoluteEpsilon,
) {
if (!defined(other) || !(other instanceof PerspectiveFrustum)) {
return false;
}
update(this);
update(other);
return (
CesiumMath.equalsEpsilon(
this.fov,
other.fov,
relativeEpsilon,
absoluteEpsilon,
) &&
CesiumMath.equalsEpsilon(
this.aspectRatio,
other.aspectRatio,
relativeEpsilon,
absoluteEpsilon,
) &&
this._offCenterFrustum.equalsEpsilon(
other._offCenterFrustum,
relativeEpsilon,
absoluteEpsilon,
)
);
};
export default PerspectiveFrustum;