Repository navigation
Expand file tree
/
Copy pathFrustumArray.js
More file actions
228 lines (160 loc) · 5.14 KB
/
Copy pathFrustumArray.js
File metadata and controls
228 lines (160 loc) · 5.14 KB
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
import { WebGLCoordinateSystem } from '../constants.js';
import { Frustum } from './Frustum.js';
import { Matrix4 } from './Matrix4.js';
const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
/**
* FrustumArray is used to determine if an object is visible in at least one camera
* from an array of cameras. This is particularly useful for multi-view renderers.
*/
class FrustumArray {
/**
* Constructs a new frustum array.
*
*/
constructor() {
/**
* The coordinate system to use.
*
* @type {WebGLCoordinateSystem|WebGPUCoordinateSystem}
* @default WebGLCoordinateSystem
*/
this.coordinateSystem = WebGLCoordinateSystem;
/**
* A pool of frustum instances. It may hold more entries than are
* currently in use; surplus instances are kept for reuse to avoid
* reallocating when array cameras of different lengths are rendered.
*
* @private
* @type {Array<Frustum>}
*/
this._frustums = [];
/**
* The number of frustums in {@link FrustumArray#_frustums} that are currently
* in use.
*
* @private
* @type {number}
* @default 0
*/
this._count = 0;
}
/**
* Computes and caches a frustum for each camera of the given array camera.
*
* @param {ArrayCamera} cameraArray - The array camera whose sub-cameras define the frustums.
* @return {FrustumArray} A reference to this frustum array.
*/
setFromArrayCamera( cameraArray ) {
const cameras = cameraArray.cameras;
const frustums = this._frustums;
for ( let i = 0; i < cameras.length; i ++ ) {
const camera = cameras[ i ];
_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
if ( frustums[ i ] === undefined ) frustums[ i ] = new Frustum();
frustums[ i ].setFromProjectionMatrix( _projScreenMatrix, camera.coordinateSystem, camera.reversedDepth );
}
this._count = cameras.length;
return this;
}
/**
* Returns `true` if the 3D object's bounding sphere is intersecting any cached frustum.
*
* {@link FrustumArray#setFromArrayCamera} must be called once per render before this method.
*
* @param {Object3D} object - The 3D object to test.
* @return {boolean} Whether the 3D object is visible in any camera.
*/
intersectsObject( object ) {
const frustums = this._frustums;
for ( let i = 0; i < this._count; i ++ ) {
if ( frustums[ i ].intersectsObject( object ) ) return true;
}
return false;
}
/**
* Returns `true` if the given sprite is intersecting any cached frustum.
*
* {@link FrustumArray#setFromArrayCamera} must be called once per render before this method.
*
* @param {Sprite} sprite - The sprite to test.
* @return {boolean} Whether the sprite is visible in any camera.
*/
intersectsSprite( sprite ) {
const frustums = this._frustums;
for ( let i = 0; i < this._count; i ++ ) {
if ( frustums[ i ].intersectsSprite( sprite ) ) return true;
}
return false;
}
/**
* Returns `true` if the given bounding sphere is intersecting any cached frustum.
*
* {@link FrustumArray#setFromArrayCamera} must be called once per render before this method.
*
* @param {Sphere} sphere - The bounding sphere to test.
* @return {boolean} Whether the sphere is visible in any camera.
*/
intersectsSphere( sphere ) {
const frustums = this._frustums;
for ( let i = 0; i < this._count; i ++ ) {
if ( frustums[ i ].intersectsSphere( sphere ) ) return true;
}
return false;
}
/**
* Returns `true` if the given bounding box is intersecting any cached frustum.
*
* {@link FrustumArray#setFromArrayCamera} must be called once per render before this method.
*
* @param {Box3} box - The bounding box to test.
* @return {boolean} Whether the box is visible in any camera.
*/
intersectsBox( box ) {
const frustums = this._frustums;
for ( let i = 0; i < this._count; i ++ ) {
if ( frustums[ i ].intersectsBox( box ) ) return true;
}
return false;
}
/**
* Returns `true` if the given point lies within any cached frustum.
*
* {@link FrustumArray#setFromArrayCamera} must be called once per render before this method.
*
* @param {Vector3} point - The point to test.
* @return {boolean} Whether the point is visible in any camera.
*/
containsPoint( point ) {
const frustums = this._frustums;
for ( let i = 0; i < this._count; i ++ ) {
if ( frustums[ i ].containsPoint( point ) ) return true;
}
return false;
}
/**
* Copies the values of the given frustum array to this instance.
*
* @param {FrustumArray} source - The frustum array to copy.
* @return {FrustumArray} A reference to this frustum array.
*/
copy( source ) {
this.coordinateSystem = source.coordinateSystem;
const frustums = this._frustums;
const sourceFrustums = source._frustums;
for ( let i = 0; i < source._count; i ++ ) {
if ( frustums[ i ] === undefined ) frustums[ i ] = new Frustum();
frustums[ i ].copy( sourceFrustums[ i ] );
}
this._count = source._count;
return this;
}
/**
* Returns a new frustum array with copied values from this instance.
*
* @return {FrustumArray} A clone of this instance.
*/
clone() {
return new FrustumArray().copy( this );
}
}
export { FrustumArray };