@needle-tools/gltf-progressive 3.4.0-rc → 4.0.0-alpha

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,747 +0,0 @@
1
- import { Box3, Clock, Matrix4, Mesh, MeshStandardMaterial, Sphere, Vector3 } from "three";
2
- import { NEEDLE_progressive } from "./extension.js";
3
- import { createLoaders } from "./loaders.js";
4
- import { getParam, isDevelopmentServer, isMobileDevice } from "./utils.internal.js";
5
- import { plugins } from "./plugins/plugin.js";
6
- import { getRaycastMesh } from "./utils.js";
7
- import { applyDebugSettings, debug, debug_OverrideLodLevel } from "./lods.debug.js";
8
- import { PromiseGroup } from "./lods.promise.js";
9
- const debugProgressiveLoading = getParam("debugprogressive");
10
- const suppressProgressiveLoading = getParam("noprogressive");
11
- const $lodsManager = Symbol("Needle:LODSManager");
12
- const $lodstate = Symbol("Needle:LODState");
13
- const $currentLOD = Symbol("Needle:CurrentLOD");
14
- const levels = { mesh_lod: -1, texture_lod: -1 };
15
- /**
16
- * 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.
17
- * It must be enabled by calling the `enable` method.
18
- *
19
- * Instead of using the LODs manager directly you can also call `useNeedleProgressive` to enable progressive loading for a GLTFLoader
20
- *
21
- * ### Plugins
22
- * Use {@link LODsManager.addPlugin} to add a plugin to the LODsManager. A plugin can be used to hook into the LOD update process and modify the LOD levels or perform other actions.
23
- *
24
- * @example Adding a LODsManager to a Three.js scene:
25
- * ```ts
26
- * import { LODsManager } from "@needle-tools/gltf-progressive";
27
- * import { WebGLRenderer, Scene, Camera, Mesh } from "three";
28
- *
29
- * const renderer = new WebGLRenderer();
30
- * const lodsManager = LODsManager.get(renderer);
31
- * lodsManager.enable();
32
- * ```
33
- *
34
- * @example Using the LODsManager with a GLTFLoader:
35
- * ```ts
36
- * import { useNeedleProgressive } from "@needle-tools/gltf-progressive";
37
- * import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
38
- *
39
- * const url = 'https://yourdomain.com/yourmodel.glb';
40
- * const loader = new GLTFLoader();
41
- * const lodsManager = useNeedleProgressive(url, renderer, loader);
42
- * ```
43
- */
44
- export class LODsManager {
45
- /**
46
- * 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.
47
- */
48
- static debugDrawLine;
49
- /** @internal */
50
- static getObjectLODState(object) {
51
- return object[$lodstate];
52
- }
53
- static addPlugin(plugin) {
54
- plugins.push(plugin);
55
- }
56
- static removePlugin(plugin) {
57
- const index = plugins.indexOf(plugin);
58
- if (index >= 0)
59
- plugins.splice(index, 1);
60
- }
61
- /**
62
- * Gets the LODsManager for the given renderer. If the LODsManager does not exist yet, it will be created.
63
- * @param renderer The renderer to get the LODsManager for.
64
- * @returns The LODsManager instance.
65
- */
66
- static get(renderer, context) {
67
- if (renderer[$lodsManager]) {
68
- console.debug("[gltf-progressive] LODsManager already exists for this renderer");
69
- return renderer[$lodsManager];
70
- }
71
- const lodsManager = new LODsManager(renderer, {
72
- engine: "unknown",
73
- ...context,
74
- });
75
- renderer[$lodsManager] = lodsManager;
76
- return lodsManager;
77
- }
78
- renderer;
79
- context;
80
- projectionScreenMatrix = new Matrix4();
81
- /** @deprecated use static `LODsManager.addPlugin()` method. This getter will be removed in later versions */
82
- get plugins() { return plugins; }
83
- /**
84
- * Force override the LOD level for all objects (meshes + textures) rendered in the scene
85
- * @default undefined automatically calculate LOD level
86
- */
87
- overrideLodLevel = undefined;
88
- /**
89
- * The target triangle density is the desired max amount of triangles on screen when the mesh is filling the screen.
90
- * @default 200_000
91
- */
92
- targetTriangleDensity = 200_000;
93
- /**
94
- * The interval in frames to automatically update the bounds of skinned meshes.
95
- * Set to 0 or a negative value to disable automatic bounds updates.
96
- * @default 30
97
- */
98
- skinnedMeshAutoUpdateBoundsInterval = 30;
99
- /**
100
- * The update interval in frames. If set to 0, the LODs will be updated every frame. If set to 2, the LODs will be updated every second frame, etc.
101
- * @default "auto"
102
- */
103
- updateInterval = "auto";
104
- #updateInterval = 1;
105
- /**
106
- * If set to true, the LODsManager will not update the LODs.
107
- * @default false
108
- */
109
- pause = false;
110
- /**
111
- * When set to true the LODsManager will not update the LODs. This can be used to manually update the LODs using the `update` method.
112
- * Otherwise the LODs will be updated automatically when the renderer renders the scene.
113
- * @default false
114
- */
115
- manual = false;
116
- _newPromiseGroups = [];
117
- _promiseGroupIds = 0;
118
- /**
119
- * Call to await LODs loading during the next render cycle.
120
- */
121
- awaitLoading(opts) {
122
- const id = this._promiseGroupIds++;
123
- const newGroup = new PromiseGroup(this.#frame, { ...opts, });
124
- this._newPromiseGroups.push(newGroup);
125
- const start = performance.now();
126
- newGroup.ready.finally(() => {
127
- const index = this._newPromiseGroups.indexOf(newGroup);
128
- if (index >= 0) {
129
- this._newPromiseGroups.splice(index, 1);
130
- if (isDevelopmentServer())
131
- performance.measure("LODsManager:awaitLoading", {
132
- start,
133
- detail: { id, name: opts?.name, awaited: newGroup.awaitedCount, resolved: newGroup.resolvedCount }
134
- });
135
- }
136
- });
137
- return newGroup.ready;
138
- }
139
- _postprocessPromiseGroups() {
140
- if (this._newPromiseGroups.length === 0)
141
- return;
142
- for (let i = this._newPromiseGroups.length - 1; i >= 0; i--) {
143
- const group = this._newPromiseGroups[i];
144
- group.update(this.#frame);
145
- }
146
- }
147
- _lodchangedlisteners = [];
148
- addEventListener(evt, listener) {
149
- if (evt === "changed") {
150
- this._lodchangedlisteners.push(listener);
151
- }
152
- }
153
- removeEventListener(evt, listener) {
154
- if (evt === "changed") {
155
- const index = this._lodchangedlisteners.indexOf(listener);
156
- if (index >= 0)
157
- this._lodchangedlisteners.splice(index, 1);
158
- }
159
- }
160
- // readonly plugins: NEEDLE_progressive_plugin[] = [];
161
- constructor(renderer, context) {
162
- this.renderer = renderer;
163
- this.context = { ...context };
164
- // createGLTFLoaderWorker().then(res => {
165
- // res.load("https://cloud.needle.tools/-/assets/Z23hmXBZ20RjNk-Z20RjNk-optimized/file").then(res2 => {
166
- // console.log("DONE", res2);
167
- // })
168
- // res.load("https://cloud.needle.tools/-/assets/Z23hmXBZ20RjNk-Z20RjNk-world/file").then(res2 => {
169
- // console.log("DONE2", res2);
170
- // })
171
- // })
172
- }
173
- #originalRender;
174
- #clock = new Clock();
175
- #frame = 0;
176
- #delta = 0;
177
- #time = 0;
178
- #fps = 0;
179
- _fpsBuffer = [60, 60, 60, 60, 60];
180
- /**
181
- * Enable the LODsManager. This will replace the render method of the renderer with a method that updates the LODs.
182
- */
183
- enable() {
184
- if (this.#originalRender)
185
- return;
186
- console.debug("[gltf-progressive] Enabling LODsManager for renderer");
187
- let stack = 0;
188
- // Save the original render method
189
- this.#originalRender = this.renderer.render;
190
- const self = this;
191
- createLoaders(this.renderer);
192
- this.renderer.render = function (scene, camera) {
193
- // check if this render call is rendering to a texture or the canvas
194
- // if it's rendering to a texture we don't want to update the LODs
195
- // This might need to be loosened later - e.g. we might want to update LODs for a render texture - but then we need to store the last LOD level differently and we also might not want to perform all the plugin calls?
196
- const renderTarget = self.renderer.getRenderTarget();
197
- if (renderTarget == null || ("isXRRenderTarget" in renderTarget && renderTarget.isXRRenderTarget)) {
198
- stack = 0;
199
- self.#frame += 1;
200
- self.#delta = self.#clock.getDelta();
201
- self.#time += self.#delta;
202
- self._fpsBuffer.shift();
203
- self._fpsBuffer.push(1 / self.#delta);
204
- self.#fps = self._fpsBuffer.reduce((a, b) => a + b) / self._fpsBuffer.length;
205
- if (debugProgressiveLoading && self.#frame % 200 === 0)
206
- console.log("FPS", Math.round(self.#fps), "Interval:", self.#updateInterval);
207
- }
208
- const stack_level = stack++;
209
- self.#originalRender.call(this, scene, camera);
210
- self.onAfterRender(scene, camera, stack_level);
211
- };
212
- }
213
- disable() {
214
- if (!this.#originalRender)
215
- return;
216
- console.debug("[gltf-progressive] Disabling LODsManager for renderer");
217
- this.renderer.render = this.#originalRender;
218
- this.#originalRender = undefined;
219
- }
220
- update(scene, camera) {
221
- this.internalUpdate(scene, camera);
222
- }
223
- onAfterRender(scene, camera, _stack) {
224
- if (this.pause)
225
- return;
226
- const renderList = this.renderer.renderLists.get(scene, 0);
227
- const opaque = renderList.opaque;
228
- let updateLODs = true;
229
- // check if we're rendering a postprocessing pass
230
- if (opaque.length === 1) {
231
- const material = opaque[0].material;
232
- // pmndrs postprocessing
233
- if (material.name === "EffectMaterial") {
234
- updateLODs = false;
235
- }
236
- // builtin three postprocessing
237
- else if (material.name === "CopyShader") {
238
- updateLODs = false;
239
- }
240
- }
241
- // don't update LODs for cube map rendering cameras
242
- if (camera.parent && camera.parent.type === "CubeCamera") {
243
- updateLODs = false;
244
- }
245
- else if (_stack >= 1) {
246
- // don't update LODs if we're e.g. rendering a shadow map
247
- if (camera.type === "OrthographicCamera") {
248
- updateLODs = false;
249
- }
250
- }
251
- if (updateLODs) {
252
- if (suppressProgressiveLoading)
253
- return;
254
- // If the update interval is set to auto then we check the FPS and adjust the update interval accordingly
255
- // If performance is low we increase the update interval to reduce the amount of LOD updates
256
- if (this.updateInterval === "auto") {
257
- if (this.#fps < 40 && this.#updateInterval < 10) {
258
- this.#updateInterval += 1;
259
- if (debugProgressiveLoading)
260
- console.warn("↓ Reducing LOD updates", this.#updateInterval, this.#fps.toFixed(0));
261
- }
262
- else if (this.#fps >= 60 && this.#updateInterval > 1) {
263
- this.#updateInterval -= 1;
264
- if (debugProgressiveLoading)
265
- console.warn("↑ Increasing LOD updates", this.#updateInterval, this.#fps.toFixed(0));
266
- }
267
- }
268
- else {
269
- this.#updateInterval = this.updateInterval;
270
- }
271
- // Check if we should update LODs this frame
272
- if (this.#updateInterval > 0 && this.#frame % this.#updateInterval != 0) {
273
- return;
274
- }
275
- this.internalUpdate(scene, camera);
276
- this._postprocessPromiseGroups();
277
- }
278
- }
279
- /**
280
- * Update LODs in a scene
281
- */
282
- internalUpdate(scene, camera) {
283
- const renderList = this.renderer.renderLists.get(scene, 0);
284
- const opaque = renderList.opaque;
285
- this.projectionScreenMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
286
- const desiredDensity = this.targetTriangleDensity;
287
- for (const entry of opaque) {
288
- if (entry.material && (entry.geometry?.type === "BoxGeometry" || entry.geometry?.type === "BufferGeometry")) {
289
- // Ignore the skybox
290
- if (entry.material.name === "SphericalGaussianBlur" || entry.material.name == "BackgroundCubeMaterial" || entry.material.name === "CubemapFromEquirect" || entry.material.name === "EquirectangularToCubeUV") {
291
- if (debugProgressiveLoading) {
292
- if (!entry.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]) {
293
- entry.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"] = true;
294
- console.warn("Ignoring skybox or BLIT object", entry, entry.material.name, entry.material.type);
295
- }
296
- }
297
- continue;
298
- }
299
- }
300
- switch (entry.material.type) {
301
- case "LineBasicMaterial":
302
- case "LineDashedMaterial":
303
- case "PointsMaterial":
304
- case "ShadowMaterial":
305
- case "MeshDistanceMaterial":
306
- case "MeshDepthMaterial":
307
- continue;
308
- }
309
- if (debugProgressiveLoading === "color") {
310
- if (entry.material) {
311
- if (!entry.object["progressive_debug_color"]) {
312
- entry.object["progressive_debug_color"] = true;
313
- const randomColor = Math.random() * 0xffffff;
314
- const newMaterial = new MeshStandardMaterial({ color: randomColor });
315
- entry.object.material = newMaterial;
316
- }
317
- }
318
- }
319
- const object = entry.object;
320
- if (object instanceof Mesh || (object.isMesh)) {
321
- this.updateLODs(scene, camera, object, desiredDensity);
322
- }
323
- }
324
- const transparent = renderList.transparent;
325
- for (const entry of transparent) {
326
- const object = entry.object;
327
- if (object instanceof Mesh || (object.isMesh)) {
328
- this.updateLODs(scene, camera, object, desiredDensity);
329
- }
330
- }
331
- const transmissive = renderList.transmissive;
332
- for (const entry of transmissive) {
333
- const object = entry.object;
334
- if (object instanceof Mesh || (object.isMesh)) {
335
- this.updateLODs(scene, camera, object, desiredDensity);
336
- }
337
- }
338
- }
339
- /** Update the LOD levels for the renderer. */
340
- updateLODs(scene, camera, object, desiredDensity) {
341
- if (!object.userData) {
342
- object.userData = {};
343
- }
344
- let state = object[$lodstate];
345
- if (!state) {
346
- state = new LOD_state();
347
- object[$lodstate] = state;
348
- }
349
- // Wait a few frames before updating the LODs to make sure the object is loaded, matrices are updated, etc.
350
- if (state.frames++ < 2) {
351
- return;
352
- }
353
- for (const plugin of plugins) {
354
- plugin.onBeforeUpdateLOD?.(this.renderer, scene, camera, object);
355
- }
356
- const debugLodLevel = this.overrideLodLevel !== undefined ? this.overrideLodLevel : debug_OverrideLodLevel;
357
- if (debugLodLevel >= 0) {
358
- levels.mesh_lod = debugLodLevel;
359
- levels.texture_lod = debugLodLevel;
360
- }
361
- else {
362
- this.calculateLodLevel(camera, object, state, desiredDensity, levels);
363
- levels.mesh_lod = Math.round(levels.mesh_lod);
364
- levels.texture_lod = Math.round(levels.texture_lod);
365
- }
366
- // we currently only support auto LOD changes for meshes
367
- if (levels.mesh_lod >= 0) {
368
- this.loadProgressiveMeshes(object, levels.mesh_lod);
369
- }
370
- // TODO: we currently can not switch texture lods because we need better caching for the textures internally (see copySettings in progressive + NE-4431)
371
- if (object.material && levels.texture_lod >= 0) {
372
- this.loadProgressiveTextures(object.material, levels.texture_lod, debugLodLevel);
373
- }
374
- if (debug && object.material && !object["isGizmo"]) {
375
- applyDebugSettings(object.material);
376
- }
377
- for (const plugin of plugins) {
378
- plugin.onAfterUpdatedLOD?.(this.renderer, scene, camera, object, levels);
379
- }
380
- state.lastLodLevel_Mesh = levels.mesh_lod;
381
- state.lastLodLevel_Texture = levels.texture_lod;
382
- }
383
- /** Load progressive textures for the given material
384
- * @param material the material to load the textures for
385
- * @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
386
- * @returns Promise with true if the LOD was loaded, false if not
387
- */
388
- loadProgressiveTextures(material, level, overrideLodLevel) {
389
- if (!material)
390
- return;
391
- if (Array.isArray(material)) {
392
- for (const mat of material) {
393
- this.loadProgressiveTextures(mat, level);
394
- }
395
- return;
396
- }
397
- // Check if the material LOD was already updated to a certain level
398
- // We don't use the userData here because we want to re-run assigning textures if the material has been cloned
399
- let update = false;
400
- if (material[$currentLOD] === undefined) {
401
- update = true;
402
- }
403
- else if (level < material[$currentLOD]) {
404
- update = true;
405
- }
406
- if (overrideLodLevel !== undefined && overrideLodLevel >= 0) {
407
- update = material[$currentLOD] != overrideLodLevel;
408
- level = overrideLodLevel;
409
- }
410
- if (update) {
411
- material[$currentLOD] = level;
412
- const promise = NEEDLE_progressive.assignTextureLOD(material, level).then(_ => {
413
- this._lodchangedlisteners.forEach(l => l({ type: "texture", level, object: material }));
414
- });
415
- PromiseGroup.addPromise("texture", material, promise, this._newPromiseGroups);
416
- }
417
- }
418
- /** Load progressive meshes for the given mesh
419
- * @param mesh the mesh to load the LOD for
420
- * @param index the index of the mesh if it's part of a group
421
- * @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
422
- * @returns Promise with true if the LOD was loaded, false if not
423
- */
424
- loadProgressiveMeshes(mesh, level) {
425
- if (!mesh)
426
- return Promise.resolve(null);
427
- let update = mesh[$currentLOD] !== level;
428
- const debugLevel = mesh["DEBUG:LOD"];
429
- if (debugLevel != undefined) {
430
- update = mesh[$currentLOD] != debugLevel;
431
- level = debugLevel;
432
- }
433
- if (update) {
434
- mesh[$currentLOD] = level;
435
- const originalGeometry = mesh.geometry;
436
- const promise = NEEDLE_progressive.assignMeshLOD(mesh, level).then(res => {
437
- if (res && mesh[$currentLOD] == level && originalGeometry != mesh.geometry) {
438
- this._lodchangedlisteners.forEach(l => l({ type: "mesh", level, object: mesh }));
439
- }
440
- return res;
441
- });
442
- PromiseGroup.addPromise("mesh", mesh, promise, this._newPromiseGroups);
443
- return promise;
444
- }
445
- return Promise.resolve(null);
446
- }
447
- // private testIfLODLevelsAreAvailable() {
448
- _sphere = new Sphere();
449
- _tempBox = new Box3();
450
- _tempBox2 = new Box3();
451
- tempMatrix = new Matrix4();
452
- _tempWorldPosition = new Vector3();
453
- _tempBoxSize = new Vector3();
454
- _tempBox2Size = new Vector3();
455
- static corner0 = new Vector3();
456
- static corner1 = new Vector3();
457
- static corner2 = new Vector3();
458
- static corner3 = new Vector3();
459
- static _tempPtInside = new Vector3();
460
- static isInside(box, matrix) {
461
- const min = box.min;
462
- const max = box.max;
463
- const centerx = (min.x + max.x) * 0.5;
464
- const centery = (min.y + max.y) * 0.5;
465
- const pt1 = this._tempPtInside.set(centerx, centery, min.z).applyMatrix4(matrix);
466
- return pt1.z < 0;
467
- }
468
- static skinnedMeshBoundsFrameOffsetCounter = 0;
469
- static $skinnedMeshBoundsOffset = Symbol("gltf-progressive-skinnedMeshBoundsOffset");
470
- // #region calculateLodLevel
471
- calculateLodLevel(camera, mesh, state, desiredDensity, result) {
472
- if (!mesh) {
473
- result.mesh_lod = -1;
474
- result.texture_lod = -1;
475
- return;
476
- }
477
- if (!camera) {
478
- result.mesh_lod = -1;
479
- result.texture_lod = -1;
480
- return;
481
- }
482
- // if this is using instancing we always load level 0
483
- // if (this.isInstancingActive) return 0;
484
- /** rough measure of "triangles on quadratic screen" – we're switching LODs based on this metric. */
485
- /** highest LOD level we'd ever expect to be generated */
486
- const maxLevel = 10;
487
- let mesh_level = maxLevel + 1;
488
- let mesh_level_calculated = false;
489
- if (debugProgressiveLoading && mesh["DEBUG:LOD"] != undefined) {
490
- return mesh["DEBUG:LOD"];
491
- }
492
- // The mesh info contains also the density for all available LOD level so we can use this for selecting which level to show
493
- const mesh_lods = NEEDLE_progressive.getMeshLODExtension(mesh.geometry)?.lods;
494
- const primitive_index = NEEDLE_progressive.getPrimitiveIndex(mesh.geometry);
495
- const has_mesh_lods = mesh_lods && mesh_lods.length > 0;
496
- const texture_lods_minmax = NEEDLE_progressive.getMaterialMinMaxLODsCount(mesh.material);
497
- const has_texture_lods = texture_lods_minmax.min_count !== Infinity && texture_lods_minmax.min_count >= 0 && texture_lods_minmax.max_count >= 0;
498
- // We can skip all this if we dont have any LOD information
499
- if (!has_mesh_lods && !has_texture_lods) {
500
- result.mesh_lod = 0;
501
- result.texture_lod = 0;
502
- return;
503
- }
504
- if (!has_mesh_lods) {
505
- mesh_level_calculated = true;
506
- mesh_level = 0;
507
- }
508
- const canvasHeight = this.renderer.domElement.clientHeight || this.renderer.domElement.height;
509
- let boundingBox = mesh.geometry.boundingBox;
510
- if (mesh.type === "SkinnedMesh") {
511
- const skinnedMesh = mesh;
512
- if (!skinnedMesh.boundingBox) {
513
- skinnedMesh.computeBoundingBox();
514
- }
515
- // Fix: https://linear.app/needle/issue/NE-5264
516
- else if (this.skinnedMeshAutoUpdateBoundsInterval > 0) {
517
- // Save a frame offset per object to stagger updates of skinned meshes across multiple frames
518
- // This isn't a perfect solution to improve perf impact of skinned mesh updates (e.g. large skinned meshes would still be costly)
519
- // But for many smaller meshes it helps to avoid spikes in performance
520
- if (!skinnedMesh[LODsManager.$skinnedMeshBoundsOffset]) {
521
- const offset = LODsManager.skinnedMeshBoundsFrameOffsetCounter++;
522
- skinnedMesh[LODsManager.$skinnedMeshBoundsOffset] = offset;
523
- }
524
- const frameOffset = skinnedMesh[LODsManager.$skinnedMeshBoundsOffset];
525
- if ((state.frames + frameOffset) % this.skinnedMeshAutoUpdateBoundsInterval === 0) {
526
- // use lowres geometry for bounding box calculation
527
- const raycastmesh = getRaycastMesh(skinnedMesh);
528
- const originalGeometry = skinnedMesh.geometry;
529
- if (raycastmesh) {
530
- skinnedMesh.geometry = raycastmesh;
531
- }
532
- skinnedMesh.computeBoundingBox();
533
- skinnedMesh.geometry = originalGeometry;
534
- }
535
- }
536
- boundingBox = skinnedMesh.boundingBox;
537
- }
538
- if (boundingBox) {
539
- const cam = camera;
540
- // hack: if the mesh has vertex colors, has less than 100 vertices we always select the highest LOD
541
- if (mesh.geometry.attributes.color && mesh.geometry.attributes.color.count < 100) {
542
- if (mesh.geometry.boundingSphere) {
543
- this._sphere.copy(mesh.geometry.boundingSphere);
544
- this._sphere.applyMatrix4(mesh.matrixWorld);
545
- const worldPosition = camera.getWorldPosition(this._tempWorldPosition);
546
- if (this._sphere.containsPoint(worldPosition)) {
547
- result.mesh_lod = 0;
548
- result.texture_lod = 0;
549
- return;
550
- }
551
- }
552
- }
553
- // calculate size on screen
554
- this._tempBox.copy(boundingBox);
555
- this._tempBox.applyMatrix4(mesh.matrixWorld);
556
- // Converting into projection space has the disadvantage that objects further to the side
557
- // will have a much larger coverage, especially with high-field-of-view situations like in VR.
558
- // Alternatively, we could attempt to calculate angular coverage (some kind of polar coordinates maybe?)
559
- // or introduce a correction factor based on "expected distortion" of the object.
560
- // High distortions would lead to lower LOD levels.
561
- // "Centrality" of the calculated screen-space bounding box could be a factor here –
562
- // what's the distance of the bounding box to the center of the screen?
563
- if (cam.isPerspectiveCamera && LODsManager.isInside(this._tempBox, this.projectionScreenMatrix)) {
564
- result.mesh_lod = 0;
565
- result.texture_lod = 0;
566
- return;
567
- }
568
- this._tempBox.applyMatrix4(this.projectionScreenMatrix);
569
- // TODO might need to be adjusted for cameras that are rendered during an XR session but are
570
- // actually not XR cameras (e.g. a render texture)
571
- if (this.renderer.xr.enabled && (cam.isPerspectiveCamera) && cam.fov > 70) {
572
- // calculate centrality of the bounding box - how close is it to the screen center
573
- const min = this._tempBox.min;
574
- const max = this._tempBox.max;
575
- let minX = min.x;
576
- let minY = min.y;
577
- let maxX = max.x;
578
- let maxY = max.y;
579
- // enlarge
580
- const enlargementFactor = 2.0;
581
- const centerBoost = 1.5;
582
- const centerX = (min.x + max.x) * 0.5;
583
- const centerY = (min.y + max.y) * 0.5;
584
- minX = (minX - centerX) * enlargementFactor + centerX;
585
- minY = (minY - centerY) * enlargementFactor + centerY;
586
- maxX = (maxX - centerX) * enlargementFactor + centerX;
587
- maxY = (maxY - centerY) * enlargementFactor + centerY;
588
- const xCentrality = minX < 0 && maxX > 0 ? 0 : Math.min(Math.abs(min.x), Math.abs(max.x));
589
- const yCentrality = minY < 0 && maxY > 0 ? 0 : Math.min(Math.abs(min.y), Math.abs(max.y));
590
- const centrality = Math.max(xCentrality, yCentrality);
591
- // heuristically determined to lower quality for objects at the edges of vision
592
- state.lastCentrality = (centerBoost - centrality) * (centerBoost - centrality) * (centerBoost - centrality);
593
- }
594
- else {
595
- state.lastCentrality = 1;
596
- }
597
- const boxSize = this._tempBox.getSize(this._tempBoxSize);
598
- boxSize.multiplyScalar(0.5); // goes from -1..1, we want -0.5..0.5 for coverage in percent
599
- if (screen.availHeight > 0) {
600
- // correct for size of context on screen
601
- if (canvasHeight > 0)
602
- boxSize.multiplyScalar(canvasHeight / screen.availHeight);
603
- }
604
- if (camera.isPerspectiveCamera) {
605
- boxSize.x *= camera.aspect;
606
- }
607
- else if (camera.isOrthographicCamera) {
608
- // const cam = camera as OrthographicCamera;
609
- // boxSize.x *= cam.zoom * .01;
610
- }
611
- const matView = camera.matrixWorldInverse;
612
- const box2 = this._tempBox2;
613
- box2.copy(boundingBox);
614
- box2.applyMatrix4(mesh.matrixWorld);
615
- box2.applyMatrix4(matView);
616
- const boxSize2 = box2.getSize(this._tempBox2Size);
617
- // approximate depth coverage in relation to screenspace size
618
- const max2 = Math.max(boxSize2.x, boxSize2.y);
619
- const max1 = Math.max(boxSize.x, boxSize.y);
620
- if (max1 != 0 && max2 != 0)
621
- boxSize.z = boxSize2.z / Math.max(boxSize2.x, boxSize2.y) * Math.max(boxSize.x, boxSize.y);
622
- state.lastScreenCoverage = Math.max(boxSize.x, boxSize.y, boxSize.z);
623
- state.lastScreenspaceVolume.copy(boxSize);
624
- state.lastScreenCoverage *= state.lastCentrality;
625
- // draw screen size box
626
- if (debugProgressiveLoading && LODsManager.debugDrawLine) {
627
- const mat = this.tempMatrix.copy(this.projectionScreenMatrix);
628
- mat.invert();
629
- const corner0 = LODsManager.corner0;
630
- const corner1 = LODsManager.corner1;
631
- const corner2 = LODsManager.corner2;
632
- const corner3 = LODsManager.corner3;
633
- // get box corners, transform with camera space, and draw as quad lines
634
- corner0.copy(this._tempBox.min);
635
- corner1.copy(this._tempBox.max);
636
- corner1.x = corner0.x;
637
- corner2.copy(this._tempBox.max);
638
- corner2.y = corner0.y;
639
- corner3.copy(this._tempBox.max);
640
- // draw outlines at the center of the box
641
- const z = (corner0.z + corner3.z) * 0.5;
642
- // all outlines should have the same depth in screen space
643
- corner0.z = corner1.z = corner2.z = corner3.z = z;
644
- corner0.applyMatrix4(mat);
645
- corner1.applyMatrix4(mat);
646
- corner2.applyMatrix4(mat);
647
- corner3.applyMatrix4(mat);
648
- LODsManager.debugDrawLine(corner0, corner1, 0x0000ff);
649
- LODsManager.debugDrawLine(corner0, corner2, 0x0000ff);
650
- LODsManager.debugDrawLine(corner1, corner3, 0x0000ff);
651
- LODsManager.debugDrawLine(corner2, corner3, 0x0000ff);
652
- }
653
- let expectedLevel = 999;
654
- // const framerate = this.context.time.smoothedFps;
655
- if (mesh_lods && state.lastScreenCoverage > 0) {
656
- for (let l = 0; l < mesh_lods.length; l++) {
657
- const lod = mesh_lods[l];
658
- const densityForThisLevel = lod.densities?.[primitive_index] || lod.density || .00001;
659
- const resultingDensity = densityForThisLevel / state.lastScreenCoverage;
660
- if (primitive_index > 0 && isDevelopmentServer() && !lod.densities && !globalThis["NEEDLE:MISSING_LOD_PRIMITIVE_DENSITIES"]) {
661
- window["NEEDLE:MISSING_LOD_PRIMITIVE_DENSITIES"] = true;
662
- console.warn(`[Needle Progressive] Detected usage of mesh without primitive densities. This might cause incorrect LOD level selection: Consider re-optimizing your model by updating your Needle Integration, Needle glTF Pipeline or running optimization again on Needle Cloud.`);
663
- }
664
- if (resultingDensity < desiredDensity) {
665
- expectedLevel = l;
666
- break;
667
- }
668
- }
669
- }
670
- const isLowerLod = expectedLevel < mesh_level;
671
- if (isLowerLod) {
672
- mesh_level = expectedLevel;
673
- mesh_level_calculated = true;
674
- }
675
- }
676
- if (mesh_level_calculated) {
677
- result.mesh_lod = mesh_level;
678
- }
679
- else {
680
- result.mesh_lod = state.lastLodLevel_Mesh;
681
- }
682
- if (debugProgressiveLoading) {
683
- const changed = result.mesh_lod != state.lastLodLevel_Mesh;
684
- if (changed) {
685
- const level = mesh_lods?.[result.mesh_lod];
686
- if (level) {
687
- console.log(`Mesh LOD changed: ${state.lastLodLevel_Mesh} → ${result.mesh_lod} (density: ${level.densities?.[primitive_index].toFixed(0)}) | ${mesh.name}`);
688
- }
689
- }
690
- }
691
- if (has_texture_lods) {
692
- const saveDataEnabled = "saveData" in globalThis.navigator && globalThis.navigator.saveData === true;
693
- // If this is the first time a texture LOD is requested we want to get the highest LOD to not display the minimal resolution that the root glTF contains as long while we wait for loading of e.g. the 8k LOD 0 texture
694
- if (state.lastLodLevel_Texture < 0) {
695
- result.texture_lod = texture_lods_minmax.max_count - 1;
696
- if (debugProgressiveLoading) {
697
- const level = texture_lods_minmax.lods[texture_lods_minmax.max_count - 1];
698
- if (debugProgressiveLoading)
699
- console.log(`First Texture LOD ${result.texture_lod} (${level.max_height}px) - ${mesh.name}`);
700
- }
701
- }
702
- else {
703
- // TODO: should we use the volume as a factor instead?
704
- const volume = state.lastScreenspaceVolume.x + state.lastScreenspaceVolume.y + state.lastScreenspaceVolume.z;
705
- let factor = state.lastScreenCoverage * 4;
706
- if (this.context?.engine === "model-viewer") {
707
- factor *= 1.5;
708
- }
709
- const screenSize = canvasHeight / window.devicePixelRatio;
710
- const pixelSizeOnScreen = screenSize * factor;
711
- let foundLod = false;
712
- for (let i = texture_lods_minmax.lods.length - 1; i >= 0; i--) {
713
- const lod = texture_lods_minmax.lods[i];
714
- if (saveDataEnabled && lod.max_height >= 2048) {
715
- continue; // skip 2k textures when saveData is enabled
716
- }
717
- if (isMobileDevice() && lod.max_height > 4096)
718
- continue; // skip 8k textures on mobile devices (for now)
719
- if (lod.max_height > pixelSizeOnScreen || (!foundLod && i === 0)) {
720
- foundLod = true;
721
- result.texture_lod = i;
722
- if (debugProgressiveLoading) {
723
- if (result.texture_lod < state.lastLodLevel_Texture) {
724
- const lod_pixel_height = lod.max_height;
725
- console.log(`Texture LOD changed: ${state.lastLodLevel_Texture} → ${result.texture_lod} = ${lod_pixel_height}px \nScreensize: ${pixelSizeOnScreen.toFixed(0)}px, Coverage: ${(100 * state.lastScreenCoverage).toFixed(2)}%, Volume ${volume.toFixed(1)} \n${mesh.name}`);
726
- }
727
- }
728
- break;
729
- }
730
- }
731
- // const t = Math.min(1, Math.max(0, state.lastScreenCoverage * 1.1));
732
- // result.texture_lod = lerp(texture_lods_minmax.max_count, 0, t);
733
- }
734
- }
735
- else {
736
- result.texture_lod = 0;
737
- }
738
- }
739
- }
740
- class LOD_state {
741
- frames = 0;
742
- lastLodLevel_Mesh = -1;
743
- lastLodLevel_Texture = -1;
744
- lastScreenCoverage = 0;
745
- lastScreenspaceVolume = new Vector3();
746
- lastCentrality = 0;
747
- }