@needle-tools/gltf-progressive 1.0.0-alpha.4 → 1.0.0-alpha.6
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/CHANGELOG.md +3 -1
- package/{dist/@needle-tools-gltf-progressive.js → gltf-progressive.js} +87 -69
- package/gltf-progressive.min.js +3 -0
- package/gltf-progressive.umd.cjs +3 -0
- package/lib/extension.d.ts +109 -109
- package/lib/extension.js +672 -672
- package/lib/extension.js.map +1 -1
- package/lib/index.d.ts +26 -10
- package/lib/index.js +37 -21
- package/lib/index.js.map +1 -1
- package/lib/loaders.d.ts +4 -4
- package/lib/loaders.js +35 -35
- package/lib/loaders.js.map +1 -1
- package/lib/lods_manager.d.ts +69 -51
- package/lib/lods_manager.js +354 -336
- package/lib/lods_manager.js.map +1 -1
- package/lib/plugins/index.d.ts +2 -2
- package/lib/plugins/index.js +2 -2
- package/lib/plugins/modelviewer.d.ts +4 -15
- package/lib/plugins/modelviewer.js +125 -102
- package/lib/plugins/modelviewer.js.map +1 -1
- package/lib/plugins/plugin.d.ts +24 -14
- package/lib/plugins/plugin.js +11 -4
- package/lib/plugins/plugin.js.map +1 -1
- package/lib/utils.d.ts +2 -2
- package/lib/utils.js +36 -36
- package/lib/utils.js.map +1 -1
- package/package.json +18 -3
- package/dist/@needle-tools-gltf-progressive.min.js +0 -3
- package/dist/@needle-tools-gltf-progressive.umd.cjs +0 -3
package/lib/lods_manager.js
CHANGED
|
@@ -1,337 +1,355 @@
|
|
|
1
|
-
import { Box3, Frustum, Matrix4, Mesh, Sphere, Vector3 } from "three";
|
|
2
|
-
import { NEEDLE_progressive } from "./extension.js";
|
|
3
|
-
import { createLoaders } from "./loaders.js";
|
|
4
|
-
import { getParam } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
const suppressProgressiveLoading = getParam("noprogressive");
|
|
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
|
-
if (
|
|
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
|
-
return
|
|
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
|
-
if (
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
LODsManager.
|
|
298
|
-
LODsManager.
|
|
299
|
-
LODsManager.
|
|
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
|
-
}
|
|
1
|
+
import { Box3, Frustum, Matrix4, Mesh, Sphere, Vector3 } from "three";
|
|
2
|
+
import { NEEDLE_progressive } from "./extension.js";
|
|
3
|
+
import { createLoaders } from "./loaders.js";
|
|
4
|
+
import { getParam } from "./utils.js";
|
|
5
|
+
const debugProgressiveLoading = getParam("debugprogressive");
|
|
6
|
+
const suppressProgressiveLoading = getParam("noprogressive");
|
|
7
|
+
/**
|
|
8
|
+
* The LODsManager class is responsible for managing the LODs and progressive assets in the scene. It will automatically update the LODs based on the camera position, screen coverage and mesh density of the objects.
|
|
9
|
+
* It must be enabled by calling the `enable` method.
|
|
10
|
+
*
|
|
11
|
+
* Instead of using the LODs manager directly you can also call `useNeedleProgressive` to enable progressive loading for a GLTFLoader
|
|
12
|
+
*/
|
|
13
|
+
export class LODsManager {
|
|
14
|
+
/** Assign a function to draw debug lines for the LODs. This function will be called with the start and end position of the line and the color of the line when the `debugprogressive` query parameter is set.
|
|
15
|
+
*/
|
|
16
|
+
static debugDrawLine;
|
|
17
|
+
/** @internal */
|
|
18
|
+
static getObjectLODState(object) {
|
|
19
|
+
return object.userData?.LOD_state;
|
|
20
|
+
}
|
|
21
|
+
renderer;
|
|
22
|
+
projectionScreenMatrix = new Matrix4();
|
|
23
|
+
cameraFrustrum = new Frustum();
|
|
24
|
+
/**
|
|
25
|
+
* The update interval in frames. If set to 0, the LODs will be updated every frame. If set to 1, the LODs will be updated every second frame, etc.
|
|
26
|
+
*/
|
|
27
|
+
updateInterval = 0;
|
|
28
|
+
/**
|
|
29
|
+
* If set to true, the LODsManager will not update the LODs.
|
|
30
|
+
*/
|
|
31
|
+
pause = false;
|
|
32
|
+
plugins = [];
|
|
33
|
+
constructor(renderer) {
|
|
34
|
+
this.renderer = renderer;
|
|
35
|
+
}
|
|
36
|
+
_originalRender;
|
|
37
|
+
/**
|
|
38
|
+
* Enable the LODsManager. This will replace the render method of the renderer with a method that updates the LODs.
|
|
39
|
+
*/
|
|
40
|
+
enable() {
|
|
41
|
+
if (this._originalRender)
|
|
42
|
+
return;
|
|
43
|
+
let stack = 0;
|
|
44
|
+
// Save the original render method
|
|
45
|
+
this._originalRender = this.renderer.render;
|
|
46
|
+
const self = this;
|
|
47
|
+
let frames = 0;
|
|
48
|
+
createLoaders(this.renderer);
|
|
49
|
+
this.renderer.render = function (scene, camera) {
|
|
50
|
+
const frame = frames++;
|
|
51
|
+
const stack_level = stack++;
|
|
52
|
+
self.onBeforeRender(scene, camera, stack_level, frame);
|
|
53
|
+
self._originalRender.call(this, scene, camera);
|
|
54
|
+
self.onAfterRender(scene, camera, stack_level, frame);
|
|
55
|
+
stack--;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
disable() {
|
|
59
|
+
if (!this._originalRender)
|
|
60
|
+
return;
|
|
61
|
+
this.renderer.render = this._originalRender;
|
|
62
|
+
this._originalRender = undefined;
|
|
63
|
+
}
|
|
64
|
+
onBeforeRender(_scene, _camera, _stack, _frame) {
|
|
65
|
+
}
|
|
66
|
+
onAfterRender(scene, camera, stack, frame) {
|
|
67
|
+
if (suppressProgressiveLoading)
|
|
68
|
+
return;
|
|
69
|
+
if (this.pause)
|
|
70
|
+
return;
|
|
71
|
+
if (this.updateInterval > 0 && frame % this.updateInterval != 0)
|
|
72
|
+
return;
|
|
73
|
+
this.projectionScreenMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
|
|
74
|
+
this.cameraFrustrum.setFromProjectionMatrix(this.projectionScreenMatrix, this.renderer.coordinateSystem);
|
|
75
|
+
const desiredDensity = 100_000;
|
|
76
|
+
// const isLowPerformanceDevice = false;// isMobileDevice();
|
|
77
|
+
// Experiment: quick & dirty performance-adaptive LODs
|
|
78
|
+
/*
|
|
79
|
+
if (this.context.time.smoothedFps < 59) {
|
|
80
|
+
currentAllowedDensity *= 0.5;
|
|
81
|
+
}
|
|
82
|
+
else if (this.context.time.smoothedFps >= 59) {
|
|
83
|
+
currentAllowedDensity *= 1.25;
|
|
84
|
+
}
|
|
85
|
+
*/
|
|
86
|
+
const renderList = this.renderer.renderLists.get(scene, stack);
|
|
87
|
+
const opaque = renderList.opaque;
|
|
88
|
+
for (const entry of opaque) {
|
|
89
|
+
const object = entry.object;
|
|
90
|
+
if (object instanceof Mesh || (object.isMesh)) {
|
|
91
|
+
this.updateLODs(scene, camera, object, desiredDensity);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Update the LOD levels for the renderer. */
|
|
96
|
+
updateLODs(scene, camera, object, desiredDensity) {
|
|
97
|
+
for (const plugin of this.plugins) {
|
|
98
|
+
plugin.onBeforeUpdateLOD?.(this.renderer, scene, camera, object);
|
|
99
|
+
}
|
|
100
|
+
// we currently only support auto LOD changes for meshes
|
|
101
|
+
let state = object.userData.LOD_state;
|
|
102
|
+
if (!state) {
|
|
103
|
+
state = new LOD_state();
|
|
104
|
+
object.userData.LOD_state = state;
|
|
105
|
+
}
|
|
106
|
+
let level = this.calculateLodLevel(camera, object, state, desiredDensity);
|
|
107
|
+
level = Math.round(level);
|
|
108
|
+
// if (object.name == "Object_20")
|
|
109
|
+
// console.log(object.name, state.lastScreenCoverage, level)
|
|
110
|
+
if (level >= 0) {
|
|
111
|
+
this.loadProgressiveMeshes(object, level);
|
|
112
|
+
}
|
|
113
|
+
// TODO: we currently can not switch texture lods because we need better caching for the textures internally (see copySettings in progressive + NE-4431)
|
|
114
|
+
let textureLOD = 0; // Math.max(0, LODlevel - 2)
|
|
115
|
+
if (object.material) {
|
|
116
|
+
const debugLevel = object["DEBUG:LOD"];
|
|
117
|
+
if (debugLevel != undefined)
|
|
118
|
+
textureLOD = debugLevel;
|
|
119
|
+
if (Array.isArray(object.material)) {
|
|
120
|
+
for (const mat of object.material) {
|
|
121
|
+
this.loadProgressiveTextures(mat, textureLOD);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
this.loadProgressiveTextures(object.material, textureLOD);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (const plugin of this.plugins) {
|
|
129
|
+
plugin.onAfterUpdatedLOD?.(this.renderer, scene, camera, object, level);
|
|
130
|
+
}
|
|
131
|
+
state.lastLodLevel = level;
|
|
132
|
+
}
|
|
133
|
+
/** Load progressive textures for the given material
|
|
134
|
+
* @param material the material to load the textures for
|
|
135
|
+
* @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
|
|
136
|
+
* @returns Promise with true if the LOD was loaded, false if not
|
|
137
|
+
*/
|
|
138
|
+
loadProgressiveTextures(material, level) {
|
|
139
|
+
if (!material)
|
|
140
|
+
return Promise.resolve(null);
|
|
141
|
+
if (material.userData && material.userData.LOD !== level) {
|
|
142
|
+
material.userData.LOD = level;
|
|
143
|
+
return NEEDLE_progressive.assignTextureLOD(material, level);
|
|
144
|
+
}
|
|
145
|
+
return Promise.resolve(null);
|
|
146
|
+
}
|
|
147
|
+
/** Load progressive meshes for the given mesh
|
|
148
|
+
* @param mesh the mesh to load the LOD for
|
|
149
|
+
* @param index the index of the mesh if it's part of a group
|
|
150
|
+
* @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
|
|
151
|
+
* @returns Promise with true if the LOD was loaded, false if not
|
|
152
|
+
*/
|
|
153
|
+
loadProgressiveMeshes(mesh, level) {
|
|
154
|
+
if (!mesh)
|
|
155
|
+
return Promise.resolve(null);
|
|
156
|
+
if (!mesh.userData)
|
|
157
|
+
mesh.userData = {};
|
|
158
|
+
if (mesh.userData.LOD !== level) {
|
|
159
|
+
mesh.userData.LOD = level;
|
|
160
|
+
const originalGeometry = mesh.geometry;
|
|
161
|
+
return NEEDLE_progressive.assignMeshLOD(mesh, level).then(res => {
|
|
162
|
+
if (res && mesh.userData.LOD == level && originalGeometry != mesh.geometry) {
|
|
163
|
+
// update the lightmap
|
|
164
|
+
// this.applyLightmapping();
|
|
165
|
+
// if (this.handles) {
|
|
166
|
+
// for (const inst of this.handles) {
|
|
167
|
+
// // if (inst["LOD"] < level) continue;
|
|
168
|
+
// // inst["LOD"] = level;
|
|
169
|
+
// inst.setGeometry(mesh.geometry);
|
|
170
|
+
// }
|
|
171
|
+
// }
|
|
172
|
+
}
|
|
173
|
+
return res;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return Promise.resolve(null);
|
|
177
|
+
}
|
|
178
|
+
// private testIfLODLevelsAreAvailable() {
|
|
179
|
+
_sphere = new Sphere();
|
|
180
|
+
_tempBox = new Box3();
|
|
181
|
+
tempMatrix = new Matrix4();
|
|
182
|
+
_tempWorldPosition = new Vector3();
|
|
183
|
+
_tempBoxSize = new Vector3();
|
|
184
|
+
_tempBox2Size = new Vector3();
|
|
185
|
+
static corner0 = new Vector3();
|
|
186
|
+
static corner1 = new Vector3();
|
|
187
|
+
static corner2 = new Vector3();
|
|
188
|
+
static corner3 = new Vector3();
|
|
189
|
+
calculateLodLevel(camera, mesh, state, desiredDensity) {
|
|
190
|
+
if (!mesh)
|
|
191
|
+
return -1;
|
|
192
|
+
// if this is using instancing we always load level 0
|
|
193
|
+
// if (this.isInstancingActive) return 0;
|
|
194
|
+
/** rough measure of "triangles on quadratic screen" – we're switching LODs based on this metric. */
|
|
195
|
+
/** highest LOD level we'd ever expect to be generated */
|
|
196
|
+
const maxLevel = 10;
|
|
197
|
+
let level = maxLevel + 1;
|
|
198
|
+
if (camera) {
|
|
199
|
+
if (debugProgressiveLoading && mesh["DEBUG:LOD"] != undefined) {
|
|
200
|
+
return mesh["DEBUG:LOD"];
|
|
201
|
+
}
|
|
202
|
+
// The mesh info contains also the density for all available LOD level so we can use this for selecting which level to show
|
|
203
|
+
const lodsInfo = NEEDLE_progressive.getMeshLODInformation(mesh.geometry);
|
|
204
|
+
const lods = lodsInfo?.lods;
|
|
205
|
+
// We can skip all this if we dont have any LOD information - we can ask the progressive extension for that
|
|
206
|
+
if (!lods || lods.length <= 0) {
|
|
207
|
+
return 99;
|
|
208
|
+
}
|
|
209
|
+
if (!this.cameraFrustrum?.intersectsObject(mesh)) {
|
|
210
|
+
// console.log("Mesh not visible");
|
|
211
|
+
// if (debugProgressiveLoading && mesh.geometry.boundingSphere) {
|
|
212
|
+
// const bounds = mesh.geometry.boundingSphere;
|
|
213
|
+
// this._sphere.copy(bounds);
|
|
214
|
+
// this._sphere.applyMatrix4(mesh.matrixWorld);
|
|
215
|
+
// Gizmos.DrawWireSphere(this._sphere.center, this._sphere.radius * 1.01, 0xff5555, .5);
|
|
216
|
+
// }
|
|
217
|
+
// the object is not visible by the camera
|
|
218
|
+
return 99;
|
|
219
|
+
}
|
|
220
|
+
const box = mesh.geometry.boundingBox;
|
|
221
|
+
if (box && camera.isPerspectiveCamera) {
|
|
222
|
+
const cam = camera;
|
|
223
|
+
// hack: if the mesh has vertex colors, has less than 100 vertices we always select the highest LOD
|
|
224
|
+
if (mesh.geometry.attributes.color && mesh.geometry.attributes.color.count < 100) {
|
|
225
|
+
if (mesh.geometry.boundingSphere) {
|
|
226
|
+
this._sphere.copy(mesh.geometry.boundingSphere);
|
|
227
|
+
this._sphere.applyMatrix4(mesh.matrixWorld);
|
|
228
|
+
const worldPosition = camera.getWorldPosition(this._tempWorldPosition);
|
|
229
|
+
if (this._sphere.containsPoint(worldPosition)) {
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// calculate size on screen
|
|
235
|
+
this._tempBox.copy(box);
|
|
236
|
+
this._tempBox.applyMatrix4(mesh.matrixWorld);
|
|
237
|
+
// Converting into projection space has the disadvantage that objects further to the side
|
|
238
|
+
// will have a much larger coverage, especially with high-field-of-view situations like in VR.
|
|
239
|
+
// Alternatively, we could attempt to calculate angular coverage (some kind of polar coordinates maybe?)
|
|
240
|
+
// or introduce a correction factor based on "expected distortion" of the object.
|
|
241
|
+
// High distortions would lead to lower LOD levels.
|
|
242
|
+
// "Centrality" of the calculated screen-space bounding box could be a factor here –
|
|
243
|
+
// what's the distance of the bounding box to the center of the screen?
|
|
244
|
+
this._tempBox.applyMatrix4(this.projectionScreenMatrix);
|
|
245
|
+
// TODO might need to be adjusted for cameras that are rendered during an XR session but are
|
|
246
|
+
// actually not XR cameras (e.g. a render texture)
|
|
247
|
+
if (this.renderer.xr.enabled && cam.fov > 70) {
|
|
248
|
+
// calculate centrality of the bounding box - how close is it to the screen center
|
|
249
|
+
const min = this._tempBox.min;
|
|
250
|
+
const max = this._tempBox.max;
|
|
251
|
+
let minX = min.x;
|
|
252
|
+
let minY = min.y;
|
|
253
|
+
let maxX = max.x;
|
|
254
|
+
let maxY = max.y;
|
|
255
|
+
// enlarge
|
|
256
|
+
const enlargementFactor = 2.0;
|
|
257
|
+
const centerBoost = 1.5;
|
|
258
|
+
const centerX = (min.x + max.x) * 0.5;
|
|
259
|
+
const centerY = (min.y + max.y) * 0.5;
|
|
260
|
+
minX = (minX - centerX) * enlargementFactor + centerX;
|
|
261
|
+
minY = (minY - centerY) * enlargementFactor + centerY;
|
|
262
|
+
maxX = (maxX - centerX) * enlargementFactor + centerX;
|
|
263
|
+
maxY = (maxY - centerY) * enlargementFactor + centerY;
|
|
264
|
+
const xCentrality = minX < 0 && maxX > 0 ? 0 : Math.min(Math.abs(min.x), Math.abs(max.x));
|
|
265
|
+
const yCentrality = minY < 0 && maxY > 0 ? 0 : Math.min(Math.abs(min.y), Math.abs(max.y));
|
|
266
|
+
const centrality = Math.max(xCentrality, yCentrality);
|
|
267
|
+
// heuristically determined to lower quality for objects at the edges of vision
|
|
268
|
+
state.lastCentrality = (centerBoost - centrality) * (centerBoost - centrality) * (centerBoost - centrality);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
state.lastCentrality = 1;
|
|
272
|
+
}
|
|
273
|
+
const boxSize = this._tempBox.getSize(this._tempBoxSize);
|
|
274
|
+
boxSize.multiplyScalar(0.5); // goes from -1..1, we want -0.5..0.5 for coverage in percent
|
|
275
|
+
if (screen.availHeight > 0)
|
|
276
|
+
boxSize.multiplyScalar(this.renderer.domElement.clientHeight / screen.availHeight); // correct for size of context on screen
|
|
277
|
+
boxSize.x *= cam.aspect;
|
|
278
|
+
const matView = camera.matrixWorldInverse;
|
|
279
|
+
const box2 = new Box3();
|
|
280
|
+
box2.copy(box);
|
|
281
|
+
box2.applyMatrix4(mesh.matrixWorld);
|
|
282
|
+
box2.applyMatrix4(matView);
|
|
283
|
+
const boxSize2 = box2.getSize(this._tempBox2Size);
|
|
284
|
+
// approximate depth coverage in relation to screenspace size
|
|
285
|
+
const max2 = Math.max(boxSize2.x, boxSize2.y);
|
|
286
|
+
const max1 = Math.max(boxSize.x, boxSize.y);
|
|
287
|
+
if (max1 != 0 && max2 != 0)
|
|
288
|
+
boxSize.z = boxSize2.z / Math.max(boxSize2.x, boxSize2.y) * Math.max(boxSize.x, boxSize.y);
|
|
289
|
+
state.lastScreenCoverage = Math.max(boxSize.x, boxSize.y, boxSize.z);
|
|
290
|
+
state.lastScreenspaceVolume.copy(boxSize);
|
|
291
|
+
state.lastScreenCoverage *= state.lastCentrality;
|
|
292
|
+
// draw screen size box
|
|
293
|
+
if (debugProgressiveLoading && LODsManager.debugDrawLine) {
|
|
294
|
+
const mat = this.tempMatrix.copy(this.projectionScreenMatrix);
|
|
295
|
+
mat.invert();
|
|
296
|
+
const corner0 = LODsManager.corner0;
|
|
297
|
+
const corner1 = LODsManager.corner1;
|
|
298
|
+
const corner2 = LODsManager.corner2;
|
|
299
|
+
const corner3 = LODsManager.corner3;
|
|
300
|
+
// get box corners, transform with camera space, and draw as quad lines
|
|
301
|
+
corner0.copy(this._tempBox.min);
|
|
302
|
+
corner1.copy(this._tempBox.max);
|
|
303
|
+
corner1.x = corner0.x;
|
|
304
|
+
corner2.copy(this._tempBox.max);
|
|
305
|
+
corner2.y = corner0.y;
|
|
306
|
+
corner3.copy(this._tempBox.max);
|
|
307
|
+
// draw outlines at the center of the box
|
|
308
|
+
const z = (corner0.z + corner3.z) * 0.5;
|
|
309
|
+
// all outlines should have the same depth in screen space
|
|
310
|
+
corner0.z = corner1.z = corner2.z = corner3.z = z;
|
|
311
|
+
corner0.applyMatrix4(mat);
|
|
312
|
+
corner1.applyMatrix4(mat);
|
|
313
|
+
corner2.applyMatrix4(mat);
|
|
314
|
+
corner3.applyMatrix4(mat);
|
|
315
|
+
LODsManager.debugDrawLine(corner0, corner1, 0x0000ff);
|
|
316
|
+
LODsManager.debugDrawLine(corner0, corner2, 0x0000ff);
|
|
317
|
+
LODsManager.debugDrawLine(corner1, corner3, 0x0000ff);
|
|
318
|
+
LODsManager.debugDrawLine(corner2, corner3, 0x0000ff);
|
|
319
|
+
}
|
|
320
|
+
let expectedLevel = 999;
|
|
321
|
+
// const framerate = this.context.time.smoothedFps;
|
|
322
|
+
if (lods && state.lastScreenCoverage > 0) {
|
|
323
|
+
for (let l = 0; l < lods.length; l++) {
|
|
324
|
+
const densityForThisLevel = lods[l].density;
|
|
325
|
+
const resultingDensity = densityForThisLevel / state.lastScreenCoverage;
|
|
326
|
+
if (resultingDensity < desiredDensity) {
|
|
327
|
+
expectedLevel = l;
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const isLowerLod = expectedLevel < level;
|
|
333
|
+
if (isLowerLod) {
|
|
334
|
+
level = expectedLevel;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
// }
|
|
338
|
+
}
|
|
339
|
+
// if (this._lastLodLevel != level) {
|
|
340
|
+
// this._nextLodTestTime = this.context.time.realtimeSinceStartup + .5;
|
|
341
|
+
// if (debugProgressiveLoading) {
|
|
342
|
+
// if (debugProgressiveLoading == "verbose") console.warn(`LOD Level changed from ${this._lastLodLevel} to ${level} for ${this.name}`);
|
|
343
|
+
// this.drawGizmoLodLevel(true);
|
|
344
|
+
// }
|
|
345
|
+
// }
|
|
346
|
+
return level;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
class LOD_state {
|
|
350
|
+
lastLodLevel = 0;
|
|
351
|
+
lastScreenCoverage = 0;
|
|
352
|
+
lastScreenspaceVolume = new Vector3();
|
|
353
|
+
lastCentrality = 0;
|
|
354
|
+
}
|
|
337
355
|
//# sourceMappingURL=lods_manager.js.map
|