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