-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathLOD.js
More file actions
329 lines (220 loc) · 6.31 KB
/
Copy pathLOD.js
File metadata and controls
329 lines (220 loc) · 6.31 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
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
import { Vector3 } from '../math/Vector3.js';
import { Object3D } from '../core/Object3D.js';
const _v1 = /*@__PURE__*/ new Vector3();
const _v2 = /*@__PURE__*/ new Vector3();
/**
* A component for providing a basic Level of Detail (LOD) mechanism.
*
* Every LOD level is associated with an object, and rendering can be switched
* between them at the distances specified. Typically you would create, say,
* three meshes, one for far away (low detail), one for mid range (medium
* detail) and one for close up (high detail).
*
* ```js
* const lod = new THREE.LOD();
* const material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
*
* //Create spheres with 3 levels of detail and create new LOD levels for them
* for( let i = 0; i < 3; i++ ) {
*
* const geometry = new THREE.IcosahedronGeometry( 10, 3 - i );
* const mesh = new THREE.Mesh( geometry, material );
* lod.addLevel( mesh, i * 75 );
*
* }
*
* scene.add( lod );
* ```
*
* @augments Object3D
*/
class LOD extends Object3D {
/**
* Constructs a new LOD.
*/
constructor() {
super();
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isLOD = true;
/**
* The current LOD index.
*
* @private
* @type {number}
* @default 0
*/
this._currentLevel = 0;
this.type = 'LOD';
Object.defineProperties( this, {
/**
* This array holds the LOD levels.
*
* @name LOD#levels
* @type {Array<{object:Object3D,distance:number,hysteresis:number}>}
*/
levels: {
enumerable: true,
value: []
}
} );
/**
* Whether the LOD object is updated automatically by the renderer per frame
* or not. If set to `false`, you have to call {@link LOD#update} in the
* render loop by yourself.
*
* @type {boolean}
* @default true
*/
this.autoUpdate = true;
}
copy( source ) {
super.copy( source, false );
const levels = source.levels;
for ( let i = 0, l = levels.length; i < l; i ++ ) {
const level = levels[ i ];
this.addLevel( level.object.clone(), level.distance, level.hysteresis );
}
this.autoUpdate = source.autoUpdate;
return this;
}
/**
* Adds a mesh that will display at a certain distance and greater. Typically
* the further away the distance, the lower the detail on the mesh.
*
* @param {Object3D} object - The 3D object to display at this level.
* @param {number} [distance=0] - The distance at which to display this level of detail.
* @param {number} [hysteresis=0] - Threshold used to avoid flickering at LOD boundaries, as a fraction of distance.
* @return {LOD} A reference to this instance.
*/
addLevel( object, distance = 0, hysteresis = 0 ) {
distance = Math.abs( distance );
const levels = this.levels;
let l;
for ( l = 0; l < levels.length; l ++ ) {
if ( distance < levels[ l ].distance ) {
break;
}
}
levels.splice( l, 0, { distance: distance, hysteresis: hysteresis, object: object } );
this.add( object );
return this;
}
/**
* Removes an existing level, based on the distance from the camera.
* Returns `true` when the level has been removed. Otherwise `false`.
*
* @param {number} distance - Distance of the level to remove.
* @return {boolean} Whether the level has been removed or not.
*/
removeLevel( distance ) {
const levels = this.levels;
for ( let i = 0; i < levels.length; i ++ ) {
if ( levels[ i ].distance === distance ) {
const removedElements = levels.splice( i, 1 );
this.remove( removedElements[ 0 ].object );
return true;
}
}
return false;
}
/**
* Returns the currently active LOD level index.
*
* @return {number} The current active LOD level index.
*/
getCurrentLevel() {
return this._currentLevel;
}
/**
* Returns a reference to the first 3D object that is greater than
* the given distance.
*
* @param {number} distance - The LOD distance.
* @return {?Object3D} The found 3D object. `null` if no 3D object has been found.
*/
getObjectForDistance( distance ) {
const levels = this.levels;
if ( levels.length > 0 ) {
let i, l;
for ( i = 1, l = levels.length; i < l; i ++ ) {
let levelDistance = levels[ i ].distance;
if ( levels[ i ].object.visible ) {
levelDistance -= levelDistance * levels[ i ].hysteresis;
}
if ( distance < levelDistance ) {
break;
}
}
return levels[ i - 1 ].object;
}
return null;
}
/**
* Computes intersection points between a casted ray and this LOD.
*
* @param {Raycaster} raycaster - The raycaster.
* @param {Array<Object>} intersects - The target array that holds the intersection points.
*/
raycast( raycaster, intersects ) {
const levels = this.levels;
if ( levels.length > 0 ) {
_v1.setFromMatrixPosition( this.matrixWorld );
const distance = raycaster.ray.origin.distanceTo( _v1 );
this.getObjectForDistance( distance ).raycast( raycaster, intersects );
}
}
/**
* Updates the LOD by computing which LOD level should be visible according
* to the current distance of the given camera.
*
* @param {Camera} camera - The camera the scene is rendered with.
*/
update( camera ) {
const levels = this.levels;
if ( levels.length > 1 ) {
_v1.setFromMatrixPosition( camera.matrixWorld );
_v2.setFromMatrixPosition( this.matrixWorld );
const distance = _v1.distanceTo( _v2 ) / camera.zoom;
levels[ 0 ].object.visible = true;
let i, l;
for ( i = 1, l = levels.length; i < l; i ++ ) {
let levelDistance = levels[ i ].distance;
if ( levels[ i ].object.visible ) {
levelDistance -= levelDistance * levels[ i ].hysteresis;
}
if ( distance >= levelDistance ) {
levels[ i - 1 ].object.visible = false;
levels[ i ].object.visible = true;
} else {
break;
}
}
this._currentLevel = i - 1;
for ( ; i < l; i ++ ) {
levels[ i ].object.visible = false;
}
}
}
toJSON( meta ) {
const data = super.toJSON( meta );
data.object.autoUpdate = this.autoUpdate;
data.object.levels = [];
const levels = this.levels;
for ( let i = 0, l = levels.length; i < l; i ++ ) {
const level = levels[ i ];
data.object.levels.push( {
object: level.object.uuid,
distance: level.distance,
hysteresis: level.hysteresis
} );
}
return data;
}
}
export { LOD };