@needle-tools/engine 4.7.2-next.d24ebbc → 4.7.3-next.24c33e7
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 +15 -0
- package/dist/gltf-progressive-BCZdu3Gc.min.js +8 -0
- package/dist/gltf-progressive-C6QbvrB4.umd.cjs +8 -0
- package/dist/{gltf-progressive-CNdBjvz6.js → gltf-progressive-CCddD-3B.js} +413 -399
- package/dist/{needle-engine.bundle-CGXifNgp.min.js → needle-engine.bundle-BWEF8NxU.min.js} +101 -101
- package/dist/{needle-engine.bundle-Dcn5L5IY.umd.cjs → needle-engine.bundle-d3zmAOp8.umd.cjs} +69 -69
- package/dist/{needle-engine.bundle-CHfSBXXT.js → needle-engine.bundle-wtPHWC_F.js} +1960 -1887
- package/dist/needle-engine.d.ts +21 -0
- package/dist/needle-engine.js +3 -3
- package/dist/needle-engine.min.js +1 -1
- package/dist/needle-engine.umd.cjs +1 -1
- package/dist/{postprocessing-xYQWCHFu.min.js → postprocessing-BzY0H7ry.min.js} +9 -9
- package/dist/{postprocessing-CjW23fio.umd.cjs → postprocessing-Dw2OCMp4.umd.cjs} +9 -9
- package/dist/{postprocessing-DYLNOL3W.js → postprocessing-vKBVFpSz.js} +10 -10
- package/dist/{three-examples-DaDLBuy6.min.js → three-examples-BMOhDaYR.min.js} +17 -17
- package/dist/{three-examples-X3OadjXB.umd.cjs → three-examples-DUcCNw9s.umd.cjs} +17 -17
- package/dist/{three-examples-B50TT3Iu.js → three-examples-tvuhV8Ne.js} +688 -688
- package/lib/engine/engine_create_objects.js +24 -1
- package/lib/engine/engine_create_objects.js.map +1 -1
- package/lib/engine/engine_input.js +3 -0
- package/lib/engine/engine_input.js.map +1 -1
- package/lib/engine/engine_physics.d.ts +13 -1
- package/lib/engine/engine_physics.js +15 -7
- package/lib/engine/engine_physics.js.map +1 -1
- package/lib/engine/webcomponents/needle-engine.js +14 -11
- package/lib/engine/webcomponents/needle-engine.js.map +1 -1
- package/lib/engine-components/OrbitControls.d.ts +57 -0
- package/lib/engine-components/OrbitControls.js +68 -9
- package/lib/engine-components/OrbitControls.js.map +1 -1
- package/lib/engine-components/Skybox.d.ts +1 -0
- package/lib/engine-components/Skybox.js +14 -18
- package/lib/engine-components/Skybox.js.map +1 -1
- package/lib/engine-components/ui/EventSystem.js +9 -10
- package/lib/engine-components/ui/EventSystem.js.map +1 -1
- package/package.json +3 -3
- package/plugins/common/logger.js +34 -16
- package/plugins/vite/logger.client.js +55 -22
- package/src/engine/engine_create_objects.ts +24 -1
- package/src/engine/engine_input.ts +8 -1
- package/src/engine/engine_physics.ts +26 -8
- package/src/engine/webcomponents/needle-engine.ts +15 -13
- package/src/engine-components/OrbitControls.ts +94 -14
- package/src/engine-components/Skybox.ts +15 -21
- package/src/engine-components/ui/EventSystem.ts +8 -9
- package/dist/gltf-progressive-C_oN6wCA.umd.cjs +0 -8
- package/dist/gltf-progressive-MgWOszRl.min.js +0 -8
|
@@ -869,19 +869,21 @@ function getDisplayName(str: string) {
|
|
|
869
869
|
|
|
870
870
|
|
|
871
871
|
function handleLoadingBlur(needleEngineElement: NeedleEngineWebComponent) {
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
872
|
+
onStart((ctx) => {
|
|
873
|
+
const userBlurSetting = needleEngineElement.getAttribute("loading-blur");
|
|
874
|
+
if (userBlurSetting !== null && userBlurSetting !== "0") {
|
|
875
875
|
if (ctx.domElement === needleEngineElement) {
|
|
876
876
|
|
|
877
877
|
const promise = ctx.lodsManager.manager?.awaitLoading({
|
|
878
|
+
frames: 5,
|
|
878
879
|
signal: AbortSignal.timeout(10_000), // Limit how long the page can be blurred
|
|
880
|
+
maxPromisesPerObject: 1,
|
|
879
881
|
}).catch(_ => {
|
|
880
882
|
// Ignore errors (none are expected tho...)
|
|
881
883
|
});
|
|
882
|
-
let blur = "
|
|
884
|
+
let blur = "20px";
|
|
883
885
|
if (userBlurSetting.endsWith("px")) blur = userBlurSetting;
|
|
884
|
-
const duration =
|
|
886
|
+
const duration = 170;
|
|
885
887
|
|
|
886
888
|
// If the scene has a transparent background we apply a blur to the canvas directly to not *also* blur images
|
|
887
889
|
// But don't always use this effect because the edges don't look as good as with a backdrop filter
|
|
@@ -896,30 +898,30 @@ function handleLoadingBlur(needleEngineElement: NeedleEngineWebComponent) {
|
|
|
896
898
|
const animation = canvas.animate([{
|
|
897
899
|
filter: "blur(0px)",
|
|
898
900
|
}
|
|
899
|
-
], { duration: duration, easing: "ease-in" });
|
|
901
|
+
], { duration: duration, easing: "ease-in", });
|
|
900
902
|
animation.onfinish = () => {
|
|
901
903
|
canvas.style.filter = originalFilterValue;
|
|
902
904
|
domElement.style.overflow = originalOverflowValue;
|
|
903
905
|
};
|
|
904
906
|
});
|
|
905
907
|
}
|
|
906
|
-
else
|
|
907
|
-
{
|
|
908
|
+
else {
|
|
908
909
|
const blurryElement = document.createElement("div");
|
|
909
910
|
ctx.domElement.prepend(blurryElement);
|
|
910
|
-
blurryElement.style.cssText = "position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 10;";
|
|
911
|
+
blurryElement.style.cssText = "position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 10; pointer-events: none";
|
|
911
912
|
blurryElement.style.backdropFilter = `blur(${blur})`;
|
|
912
913
|
promise?.then(() => {
|
|
913
914
|
const animation = blurryElement.animate([{
|
|
914
915
|
backdropFilter: "blur(0px)",
|
|
916
|
+
opacity: 0,
|
|
915
917
|
}
|
|
916
|
-
], { duration: duration, easing: "ease-in" });
|
|
918
|
+
], { duration: duration, easing: "ease-in", });
|
|
917
919
|
animation.onfinish = () => {
|
|
918
920
|
blurryElement.remove();
|
|
919
921
|
};
|
|
920
922
|
});
|
|
921
923
|
}
|
|
922
924
|
}
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
}
|
|
925
|
+
}
|
|
926
|
+
}, { once: true });
|
|
927
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Camera as Camera3, Object3D, PerspectiveCamera, Ray, Vector2, Vector3, Vector3Like } from "three";
|
|
2
2
|
import { OrbitControls as ThreeOrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
3
3
|
|
|
4
4
|
import { isDevEnvironment } from "../engine/debug/index.js";
|
|
@@ -6,9 +6,9 @@ import { setCameraController } from "../engine/engine_camera.js";
|
|
|
6
6
|
import { Gizmos } from "../engine/engine_gizmos.js";
|
|
7
7
|
import { InputEventQueue, NEPointerEvent } from "../engine/engine_input.js";
|
|
8
8
|
import { Mathf } from "../engine/engine_math.js";
|
|
9
|
-
import { RaycastOptions } from "../engine/engine_physics.js";
|
|
9
|
+
import { IRaycastOptions, RaycastOptions } from "../engine/engine_physics.js";
|
|
10
10
|
import { serializable } from "../engine/engine_serialization_decorator.js";
|
|
11
|
-
import { getBoundingBox, getTempVector,
|
|
11
|
+
import { getBoundingBox, getTempVector, getWorldPosition } from "../engine/engine_three_utils.js";
|
|
12
12
|
import type { ICameraController } from "../engine/engine_types.js";
|
|
13
13
|
import { DeviceUtilities, getParam } from "../engine/engine_utils.js";
|
|
14
14
|
import { Camera } from "./Camera.js";
|
|
@@ -43,6 +43,22 @@ export class CameraTargetReachedEvent extends CustomEvent<{ controls: OrbitContr
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
|
|
47
|
+
declare module 'three/examples/jsm/controls/OrbitControls.js' {
|
|
48
|
+
export interface OrbitControls {
|
|
49
|
+
_sphericalDelta: import("three").Spherical,
|
|
50
|
+
_rotateLeft: (angleInRadians: number) => void;
|
|
51
|
+
_rotateUp: (angleInRadians: number) => void;
|
|
52
|
+
_pan: (dx: number, dy: number) => void;
|
|
53
|
+
_dollyIn: (dollyScale: number) => void;
|
|
54
|
+
_dollyOut: (dollyScale: number) => void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface OrbitControlsEventMap {
|
|
58
|
+
endMovement: Event;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
46
62
|
/** The OrbitControls component is used to control a camera using the [OrbitControls from three.js](https://threejs.org/docs/#examples/en/controls/OrbitControls) library.
|
|
47
63
|
* The three OrbitControls object can be accessed via the `controls` property.
|
|
48
64
|
* The object being controlled by the OrbitControls (usually the camera) can be accessed via the `controllerObject` property.
|
|
@@ -222,6 +238,68 @@ export class OrbitControls extends Behaviour implements ICameraController {
|
|
|
222
238
|
@serializable()
|
|
223
239
|
targetLerpDuration = 1;
|
|
224
240
|
|
|
241
|
+
/**
|
|
242
|
+
* Rotate the camera left (or right) by the specified angle in radians.
|
|
243
|
+
* For positive angles the camera will rotate to the left, for negative angles it will rotate to the right.
|
|
244
|
+
* Tip: Use Mathf to convert between degrees and radians.
|
|
245
|
+
* @param angleInRadians The angle in radians to rotate the camera left
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* // Rotate the camera left by 0.1 radians
|
|
249
|
+
* orbitControls.rotateLeft(0.1);
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
rotateLeft(angleInRadians: number) {
|
|
253
|
+
this._controls?._rotateLeft(angleInRadians);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Rotate the camera up (or down) by the specified angle in radians.
|
|
258
|
+
* For positive angles the camera will rotate up, for negative angles it will rotate down.
|
|
259
|
+
* Tip: Use Mathf to convert between degrees and radians.
|
|
260
|
+
* @param angleInRadians The angle in radians to rotate the camera up
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* // Rotate the camera up by 0.1 radians
|
|
264
|
+
* orbitControls.rotateUp(0.1);
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
rotateUp(angleInRadians: number) {
|
|
268
|
+
this._controls?._rotateUp(angleInRadians);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Pan the camera by the specified amount in the x and y direction in pixels.
|
|
273
|
+
* @param dx The amount to pan the camera in the x direction in pixels.
|
|
274
|
+
* @param dy The amount to pan the camera in the y direction in pixels.
|
|
275
|
+
*/
|
|
276
|
+
pan(dx: number, dy: number) {
|
|
277
|
+
this._controls?._pan(dx, dy);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Zoom the camera in or out by the specified scale factor. The factor is applied to the current zoom radius / distance.
|
|
282
|
+
* If the scale is greater than 0 then the camera will zoom in, if it is less than 0 then the camera will zoom out.
|
|
283
|
+
* @param scale The scale factor to zoom the camera in or out. Expected range is between -1 and 1, where 0 means no zoom.
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* // Zoom in by 0.1
|
|
287
|
+
* orbitControls.zoomIn(0.1);
|
|
288
|
+
* // Zoom out by 0.1
|
|
289
|
+
* orbitControls.zoomIn(-0.1);
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
zoomIn(scale: number) {
|
|
293
|
+
if (scale > 0) {
|
|
294
|
+
this._controls?._dollyIn(1 - scale);
|
|
295
|
+
}
|
|
296
|
+
else if (scale < 0) {
|
|
297
|
+
this._controls?._dollyOut(1 + scale);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
225
303
|
private _controls: ThreeOrbitControls | null = null;
|
|
226
304
|
private _cameraObject: Object3D | null = null;
|
|
227
305
|
|
|
@@ -316,7 +394,7 @@ export class OrbitControls extends Behaviour implements ICameraController {
|
|
|
316
394
|
if (DeviceUtilities.isMobileDevice()) this.doubleClickToFocus = true;
|
|
317
395
|
}
|
|
318
396
|
this._controls.addEventListener("start", this.onControlsChangeStarted);
|
|
319
|
-
this._controls.addEventListener("
|
|
397
|
+
this._controls.addEventListener("endMovement", this.onControlsChangeEnded);
|
|
320
398
|
|
|
321
399
|
if (!this._startedListeningToKeyEvents && this.enableKeys) {
|
|
322
400
|
this._startedListeningToKeyEvents = true;
|
|
@@ -347,7 +425,7 @@ export class OrbitControls extends Behaviour implements ICameraController {
|
|
|
347
425
|
this._controls.enabled = false;
|
|
348
426
|
this._controls.autoRotate = false;
|
|
349
427
|
this._controls.removeEventListener("start", this.onControlsChangeStarted);
|
|
350
|
-
this._controls.removeEventListener("
|
|
428
|
+
this._controls.removeEventListener("endMovement", this.onControlsChangeEnded);
|
|
351
429
|
try {
|
|
352
430
|
this._controls.stopListenToKeyEvents();
|
|
353
431
|
} catch { /** this fails if we never listened to key events... */ }
|
|
@@ -420,9 +498,9 @@ export class OrbitControls extends Behaviour implements ICameraController {
|
|
|
420
498
|
// }
|
|
421
499
|
};
|
|
422
500
|
|
|
423
|
-
private updateTargetNow() {
|
|
501
|
+
private updateTargetNow(options?: IRaycastOptions) {
|
|
424
502
|
const ray = new Ray(this._cameraObject?.worldPosition, this._cameraObject?.worldForward.multiplyScalar(-1));
|
|
425
|
-
const hits = this.context.physics.raycastFromRay(ray);
|
|
503
|
+
const hits = this.context.physics.raycastFromRay(ray, options);
|
|
426
504
|
const hit = hits.length > 0 ? hits[0] : undefined;
|
|
427
505
|
if (hit && hit.distance > this.minZoom && hit.distance < this.maxZoom) {
|
|
428
506
|
if (debug) Gizmos.DrawWireSphere(hit.point, 0.1, 0xff0000, 2);
|
|
@@ -431,25 +509,27 @@ export class OrbitControls extends Behaviour implements ICameraController {
|
|
|
431
509
|
}
|
|
432
510
|
|
|
433
511
|
private _orbitStartAngle: number = 0;
|
|
512
|
+
private _zoomStartDistance: number = 0;
|
|
434
513
|
private onControlsChangeStarted = () => {
|
|
435
514
|
if (this._controls) {
|
|
436
515
|
this._orbitStartAngle = this._controls.getAzimuthalAngle() + this._controls.getPolarAngle();
|
|
516
|
+
this._zoomStartDistance = this._controls.getDistance();
|
|
437
517
|
}
|
|
438
518
|
if (this._syncedTransform) {
|
|
439
519
|
this._syncedTransform.requestOwnership();
|
|
440
520
|
}
|
|
441
521
|
}
|
|
442
522
|
private onControlsChangeEnded = () => {
|
|
443
|
-
|
|
444
523
|
if (this._controls) {
|
|
445
524
|
if (this.autoTarget) {
|
|
446
525
|
const newAngle = this._controls.getAzimuthalAngle() + this._controls.getPolarAngle();
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
526
|
+
const deltaAngle = newAngle - this._orbitStartAngle;
|
|
527
|
+
// TODO: "just zoom" probably shouldnt update the target either, unless zoomToCursor is enabled
|
|
528
|
+
if (Math.abs(deltaAngle) < .01) {
|
|
529
|
+
if (debug) console.debug("OrbitControls: Update target", { deltaAngle });
|
|
530
|
+
this.updateTargetNow({ allowSlowRaycastFallback: false });
|
|
451
531
|
}
|
|
452
|
-
else if (debug) console.debug("OrbitControls:
|
|
532
|
+
else if (debug) console.debug("OrbitControls: No target update", { deltaAngle });
|
|
453
533
|
}
|
|
454
534
|
}
|
|
455
535
|
|
|
@@ -903,7 +983,7 @@ export class OrbitControls extends Behaviour implements ICameraController {
|
|
|
903
983
|
// If the user passed in an object as first argument and options as second argument
|
|
904
984
|
else if (objectsOrOptions && typeof objectsOrOptions === "object") {
|
|
905
985
|
if (!(objectsOrOptions instanceof Object3D) && !Array.isArray(objectsOrOptions)) {
|
|
906
|
-
options = objectsOrOptions;
|
|
986
|
+
options = objectsOrOptions;
|
|
907
987
|
objects = options.objects;
|
|
908
988
|
}
|
|
909
989
|
}
|
|
@@ -24,7 +24,7 @@ function createRemoteSkyboxComponent(context: IContext, url: string, skybox: boo
|
|
|
24
24
|
// when the user sets the attribute to a color we can not handle it as a skybox url.
|
|
25
25
|
if (url === "transparent" || url?.startsWith("rgb") || url?.startsWith("#")) {
|
|
26
26
|
console.warn(`Needle Engine: Invalid ${attribute} value (${url}). Did you mean to set background-color instead?`);
|
|
27
|
-
return;
|
|
27
|
+
return null;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
const remote = new RemoteSkybox();
|
|
@@ -71,7 +71,7 @@ ContextRegistry.registerCallback(ContextEvent.ContextCreationStart, () => {
|
|
|
71
71
|
})
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
-
declare type SkyboxCacheEntry = { src: string, texture: Promise<Texture> };
|
|
74
|
+
declare type SkyboxCacheEntry = { src: string, texture: Promise<Texture | null> };
|
|
75
75
|
function ensureGlobalCache() {
|
|
76
76
|
if (!globalThis["NEEDLE_ENGINE_SKYBOX_TEXTURES"])
|
|
77
77
|
globalThis["NEEDLE_ENGINE_SKYBOX_TEXTURES"] = new Array<SkyboxCacheEntry>();
|
|
@@ -87,12 +87,13 @@ function tryGetPreviouslyLoadedTexture(src: string) {
|
|
|
87
87
|
}
|
|
88
88
|
return null;
|
|
89
89
|
}
|
|
90
|
-
async function disposeCachedTexture(tex: Promise<Texture>) {
|
|
90
|
+
async function disposeCachedTexture(tex: Promise<Texture | null>) {
|
|
91
91
|
const texture = await tex;
|
|
92
|
+
if (!texture) return;
|
|
92
93
|
setDisposable(texture, true);
|
|
93
94
|
disposeObjectResources(texture);
|
|
94
95
|
}
|
|
95
|
-
function registerLoadedTexture(src: string, texture: Promise<Texture>) {
|
|
96
|
+
function registerLoadedTexture(src: string, texture: Promise<Texture | null>) {
|
|
96
97
|
const cache = ensureGlobalCache();
|
|
97
98
|
// Make sure the cache doesnt get too big
|
|
98
99
|
while (cache.length > 5) {
|
|
@@ -226,24 +227,10 @@ export class RemoteSkybox extends Behaviour {
|
|
|
226
227
|
|
|
227
228
|
if (!url) return false;
|
|
228
229
|
|
|
229
|
-
try {
|
|
230
|
-
if (url.startsWith("/") || url.startsWith(".")) {
|
|
231
|
-
new URL(url, window.location.href);
|
|
232
|
-
}
|
|
233
|
-
// here we handle blob, http stuff. This should fail for e.g. transparent or rgba or #
|
|
234
|
-
else {
|
|
235
|
-
new URL(url);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
catch (err) {
|
|
239
|
-
if (debug) console.warn("RemoteSkybox: Invalid URL", url, this.name);
|
|
240
|
-
return false; // Invalid URL
|
|
241
|
-
}
|
|
242
|
-
|
|
243
230
|
name ??= url;
|
|
244
231
|
|
|
245
232
|
if (!this.isValidTextureType(name)) {
|
|
246
|
-
console.warn("Potentially invalid skybox
|
|
233
|
+
console.warn("Potentially invalid skybox URL: \"" + name + "\" on " + (this.name || this.gameObject?.name || "context"));
|
|
247
234
|
}
|
|
248
235
|
|
|
249
236
|
if (debug) console.log("Set remote skybox url: " + url);
|
|
@@ -291,7 +278,7 @@ export class RemoteSkybox extends Behaviour {
|
|
|
291
278
|
const cached = tryGetPreviouslyLoadedTexture(name);
|
|
292
279
|
if (cached) {
|
|
293
280
|
const res = await cached;
|
|
294
|
-
if (res
|
|
281
|
+
if (res?.source?.data?.length > 0 || res?.source?.data?.data?.length) return res;
|
|
295
282
|
}
|
|
296
283
|
const isEXR = name.endsWith(".exr");
|
|
297
284
|
const isHdr = name.endsWith(".hdr");
|
|
@@ -316,7 +303,10 @@ export class RemoteSkybox extends Behaviour {
|
|
|
316
303
|
}
|
|
317
304
|
|
|
318
305
|
if (debug) console.log("Loading skybox: " + url);
|
|
319
|
-
const loadingTask = this._loader.loadAsync(url)
|
|
306
|
+
const loadingTask = this._loader.loadAsync(url).catch(_err => {
|
|
307
|
+
console.warn("RemoteSkybox: Failed to load texture from url:", url);
|
|
308
|
+
return null;
|
|
309
|
+
});
|
|
320
310
|
registerLoadedTexture(name, loadingTask);
|
|
321
311
|
const envMap = await loadingTask;
|
|
322
312
|
return envMap;
|
|
@@ -351,6 +341,7 @@ export class RemoteSkybox extends Behaviour {
|
|
|
351
341
|
}
|
|
352
342
|
|
|
353
343
|
|
|
344
|
+
private readonly validProtocols = ["file:", "blob:", "data:"];
|
|
354
345
|
private readonly validTextureTypes = [".ktx2", ".hdr", ".exr", ".jpg", ".jpeg", ".png"];
|
|
355
346
|
|
|
356
347
|
private isRemoteTexture(url: string): boolean {
|
|
@@ -361,6 +352,9 @@ export class RemoteSkybox extends Behaviour {
|
|
|
361
352
|
for (const type of this.validTextureTypes) {
|
|
362
353
|
if (url.endsWith(type)) return true;
|
|
363
354
|
}
|
|
355
|
+
for (const protocol of this.validProtocols) {
|
|
356
|
+
if (url.startsWith(protocol)) return true;
|
|
357
|
+
}
|
|
364
358
|
return false;
|
|
365
359
|
}
|
|
366
360
|
|
|
@@ -149,12 +149,6 @@ export class EventSystem extends Behaviour {
|
|
|
149
149
|
data.isUp = pointerEvent.type == InputEvents.PointerUp;
|
|
150
150
|
data.isPressed = this.context.input.getPointerPressed(pointerEvent.pointerId);
|
|
151
151
|
|
|
152
|
-
if (debug) {
|
|
153
|
-
if (data.isDown) console.log("DOWN", data.pointerId);
|
|
154
|
-
else if (data.isUp) console.log("UP", data.pointerId);
|
|
155
|
-
if (data.isClick) console.log("CLICK", data.pointerId);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
152
|
// raycast
|
|
159
153
|
const options = new RaycastOptions();
|
|
160
154
|
if (pointerEvent.hasRay) {
|
|
@@ -163,8 +157,15 @@ export class EventSystem extends Behaviour {
|
|
|
163
157
|
else {
|
|
164
158
|
options.screenPoint = this.context.input.getPointerPositionRC(pointerEvent.pointerId)!;
|
|
165
159
|
}
|
|
160
|
+
options.allowSlowRaycastFallback = pointerEvent.isClick || pointerEvent.isDoubleClick;
|
|
166
161
|
|
|
167
162
|
const hits = this.performRaycast(options) as Array<NEPointerEventIntersection>;
|
|
163
|
+
if (debug) {
|
|
164
|
+
if (data.isDown) console.log("DOWN", { id: data.pointerId, hits: hits.length });
|
|
165
|
+
else if (data.isUp) console.log("UP", { id: data.pointerId, hits: hits.length });
|
|
166
|
+
if (data.isClick) console.log("CLICK", { id: data.pointerId, hits: hits.length });
|
|
167
|
+
}
|
|
168
|
+
|
|
168
169
|
if (hits) {
|
|
169
170
|
for (const hit of hits) {
|
|
170
171
|
hit.event = pointerEvent;
|
|
@@ -282,13 +283,11 @@ export class EventSystem extends Behaviour {
|
|
|
282
283
|
}
|
|
283
284
|
|
|
284
285
|
/** the raycast filter is always overriden */
|
|
285
|
-
private performRaycast(opts: RaycastOptions
|
|
286
|
+
private performRaycast(opts: RaycastOptions): Intersection[] | null {
|
|
286
287
|
if (!this.raycaster) return null;
|
|
287
288
|
// we clear the cache of previously seen objects
|
|
288
289
|
this._testObjectsCache.clear();
|
|
289
290
|
this._sortedHits.length = 0;
|
|
290
|
-
|
|
291
|
-
if (!opts) opts = new RaycastOptions();
|
|
292
291
|
opts.testObject = this.shouldRaycastObject;
|
|
293
292
|
|
|
294
293
|
for (const rc of this.raycaster) {
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";var qe=Object.defineProperty;var Ee=n=>{throw TypeError(n)};var Ke=(n,t,e)=>t in n?qe(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var c=(n,t,e)=>Ke(n,typeof t!="symbol"?t+"":t,e),Ge=(n,t,e)=>t.has(n)||Ee("Cannot "+e);var y=(n,t,e)=>(Ge(n,t,"read from private field"),e?e.call(n):t.get(n)),j=(n,t,e)=>t.has(n)?Ee("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(n):t.set(n,e),z=(n,t,e,r)=>(Ge(n,t,"write to private field"),r?r.call(n,e):t.set(n,e),e);const p=require("./three-B_hneGZr.umd.cjs"),te=require("./three-examples-X3OadjXB.umd.cjs"),Ye="";globalThis.GLTF_PROGRESSIVE_VERSION=Ye;console.debug("[gltf-progressive] version -");let W="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",re="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const He=W,je=re,ke=new URL(W+"draco_decoder.js");ke.searchParams.append("range","true");fetch(ke,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(n=>{console.debug(`Failed to fetch remote Draco decoder from ${W} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),W===He&&$e("./include/draco/"),re===je&&Fe("./include/ktx2/")}).finally(()=>{Ue()});function $e(n){W=n,B&&B[be]!=W?(console.debug("Updating Draco decoder path to "+n),B[be]=W,B.setDecoderPath(W),B.preload()):console.debug("Setting Draco decoder path to "+n)}function Fe(n){re=n,U&&U.transcoderPath!=re?(console.debug("Updating KTX2 transcoder path to "+n),U.setTranscoderPath(re),U.init()):console.debug("Setting KTX2 transcoder path to "+n)}const be=Symbol("dracoDecoderPath");let B,me,U;function Ue(){B||(B=new te.DRACOLoader,B[be]=W,B.setDecoderPath(W),B.setDecoderConfig({type:"js"}),B.preload()),U||(U=new te.KTX2Loader,U.setTranscoderPath(re),U.init()),me||(me=te.MeshoptDecoder)}function Pe(n){return Ue(),n?U.detectSupport(n):n!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:B,ktx2Loader:U,meshoptDecoder:me}}function Ae(n){n.dracoLoader||n.setDRACOLoader(B),n.ktx2Loader||n.setKTX2Loader(U),n.meshoptDecoder||n.setMeshoptDecoder(me)}const Se=new WeakMap;function Ce(n,t){let e=Se.get(n);e?e=Object.assign(e,t):e=t,Se.set(n,e)}const De=te.GLTFLoader.prototype.load;function Je(...n){const t=Se.get(this);let e=n[0];const r=new URL(e,window.location.href);if(r.hostname.endsWith("needle.tools")){const o=(t==null?void 0:t.progressive)!==void 0?t.progressive:!0,s=t!=null&&t.usecase?t.usecase:"default";o?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${s}`:this.requestHeader.Accept=`*/*;usecase=${s}`,e=r.toString()}return n[0]=e,De==null?void 0:De.call(this,...n)}te.GLTFLoader.prototype.load=Je;de("debugprogressive");function de(n){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(n);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function Qe(n,t){if(t===void 0||t.startsWith("./")||t.startsWith("http")||n===void 0)return t;const e=n.lastIndexOf("/");if(e>=0){const r=n.substring(0,e+1);for(;r.endsWith("/")&&t.startsWith("/");)t=t.substring(1);return r+t}return t}let ne;function Ze(){return ne!==void 0||(ne=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),de("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",ne)),ne}function et(){if(typeof window>"u")return!1;const n=new URL(window.location.href),t=n.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(n.hostname);return n.hostname==="127.0.0.1"||t}const tt=typeof window>"u"&&typeof document>"u",Te=Symbol("needle:raycast-mesh");function oe(n){return(n==null?void 0:n[Te])instanceof p.BufferGeometry?n[Te]:null}function rt(n,t){if((n.type==="Mesh"||n.type==="SkinnedMesh")&&!oe(n)){const r=ot(t);r.userData={isRaycastMesh:!0},n[Te]=r}}function st(n=!0){if(n){if(ae)return;const t=ae=p.Mesh.prototype.raycast;p.Mesh.prototype.raycast=function(e,r){const i=this,o=oe(i);let s;o&&i.isMesh&&(s=i.geometry,i.geometry=o),t.call(this,e,r),s&&(i.geometry=s)}}else{if(!ae)return;p.Mesh.prototype.raycast=ae,ae=null}}let ae=null;function ot(n){const t=new p.BufferGeometry;for(const e in n.attributes)t.setAttribute(e,n.getAttribute(e));return t.setIndex(n.getIndex()),t}const Q=new Array,M=de("debugprogressive");let pe,ee=-1;if(M){let n=function(){ee+=1,ee>=t&&(ee=-1),console.log(`Toggle LOD level [${ee}]`)},t=6;window.addEventListener("keyup",e=>{e.key==="p"&&n(),e.key==="w"&&(pe=!pe,console.log(`Toggle wireframe [${pe}]`));const r=parseInt(e.key);!isNaN(r)&&r>=0&&(ee=r,console.log(`Set LOD level to [${ee}]`))})}function Ne(n){if(M)if(Array.isArray(n))for(const t of n)Ne(t);else n&&"wireframe"in n&&(n.wireframe=pe===!0)}const K="NEEDLE_progressive",ve=Symbol("needle-progressive-texture"),w=class w{constructor(t,e){c(this,"parser");c(this,"url");c(this,"_isLoadingMesh");c(this,"loadMesh",t=>{var r,i;if(this._isLoadingMesh)return null;const e=(i=(r=this.parser.json.meshes[t])==null?void 0:r.extensions)==null?void 0:i[K];return e?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",t).then(o=>{var s;return this._isLoadingMesh=!1,o&&w.registerMesh(this.url,e.guid,o,(s=e.lods)==null?void 0:s.length,0,e),o})):null});M&&console.log("Progressive extension registered for",e),this.parser=t,this.url=e}get name(){return K}static getMeshLODExtension(t){const e=this.getAssignedLODInformation(t);return e!=null&&e.key?this.lodInfos.get(e.key):null}static getPrimitiveIndex(t){var r;const e=(r=this.getAssignedLODInformation(t))==null?void 0:r.index;return e??-1}static getMaterialMinMaxLODsCount(t,e){const r=this,i="LODS:minmax",o=t[i];if(o!=null)return o;if(e||(e={min_count:1/0,max_count:0,lods:[]}),Array.isArray(t)){for(const a of t)this.getMaterialMinMaxLODsCount(a,e);return t[i]=e,e}if(M==="verbose"&&console.log("getMaterialMinMaxLODsCount",t),t.type==="ShaderMaterial"||t.type==="RawShaderMaterial"){const a=t;for(const l of Object.keys(a.uniforms)){const u=a.uniforms[l].value;(u==null?void 0:u.isTexture)===!0&&s(u,e)}}else if(t.isMaterial)for(const a of Object.keys(t)){const l=t[a];(l==null?void 0:l.isTexture)===!0&&s(l,e)}return t[i]=e,e;function s(a,l){const u=r.getAssignedLODInformation(a);if(u){const d=r.lodInfos.get(u.key);if(d&&d.lods){l.min_count=Math.min(l.min_count,d.lods.length),l.max_count=Math.max(l.max_count,d.lods.length);for(let h=0;h<d.lods.length;h++){const g=d.lods[h];g.width&&(l.lods[h]=l.lods[h]||{min_height:1/0,max_height:0},l.lods[h].min_height=Math.min(l.lods[h].min_height,g.height),l.lods[h].max_height=Math.max(l.lods[h].max_height,g.height))}}}}}static hasLODLevelAvailable(t,e){var o;if(Array.isArray(t)){for(const s of t)if(this.hasLODLevelAvailable(s,e))return!0;return!1}if(t.isMaterial===!0){for(const s of Object.keys(t)){const a=t[s];if(a&&a.isTexture&&this.hasLODLevelAvailable(a,e))return!0}return!1}else if(t.isGroup===!0){for(const s of t.children)if(s.isMesh===!0&&this.hasLODLevelAvailable(s,e))return!0}let r,i;if(t.isMesh?r=t.geometry:(t.isBufferGeometry||t.isTexture)&&(r=t),r&&(o=r==null?void 0:r.userData)!=null&&o.LODS){const s=r.userData.LODS;if(i=this.lodInfos.get(s.key),e===void 0)return i!=null;if(i)return Array.isArray(i.lods)?e<i.lods.length:e===0}return!1}static assignMeshLOD(t,e){var r;if(!t)return Promise.resolve(null);if(t instanceof p.Mesh||t.isMesh===!0){const i=t.geometry,o=this.getAssignedLODInformation(i);if(!o)return Promise.resolve(null);for(const s of Q)(r=s.onBeforeGetLODMesh)==null||r.call(s,t,e);return t["LOD:requested level"]=e,w.getOrLoadLOD(i,e).then(s=>{if(Array.isArray(s)){const a=o.index||0;s=s[a]}return t["LOD:requested level"]===e&&(delete t["LOD:requested level"],s&&i!=s&&((s==null?void 0:s.isBufferGeometry)?t.geometry=s:M&&console.error("Invalid LOD geometry",s))),s}).catch(s=>(console.error("Error loading mesh LOD",t,s),null))}else M&&console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh",t);return Promise.resolve(null)}static assignTextureLOD(t,e=0){if(!t)return Promise.resolve(null);if(t.isMesh===!0){const r=t;if(Array.isArray(r.material)){const i=new Array;for(const o of r.material){const s=this.assignTextureLOD(o,e);i.push(s)}return Promise.all(i).then(o=>{const s=new Array;for(const a of o)Array.isArray(a)&&s.push(...a);return s})}else return this.assignTextureLOD(r.material,e)}if(t.isMaterial===!0){const r=t,i=[],o=new Array;if(r.uniforms&&(r.isRawShaderMaterial||r.isShaderMaterial===!0)){const s=r;for(const a of Object.keys(s.uniforms)){const l=s.uniforms[a].value;if((l==null?void 0:l.isTexture)===!0){const u=this.assignTextureLODForSlot(l,e,r,a).then(d=>(d&&s.uniforms[a].value!=d&&(s.uniforms[a].value=d,s.uniformsNeedUpdate=!0),d));i.push(u),o.push(a)}}}else for(const s of Object.keys(r)){const a=r[s];if((a==null?void 0:a.isTexture)===!0){const l=this.assignTextureLODForSlot(a,e,r,s);i.push(l),o.push(s)}}return Promise.all(i).then(s=>{const a=new Array;for(let l=0;l<s.length;l++){const u=s[l],d=o[l];u&&u.isTexture===!0?a.push({material:r,slot:d,texture:u,level:e}):a.push({material:r,slot:d,texture:null,level:e})}return a})}if(t instanceof p.Texture||t.isTexture===!0){const r=t;return this.assignTextureLODForSlot(r,e,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(t,e,r,i){return(t==null?void 0:t.isTexture)!==!0?Promise.resolve(null):i==="glyphMap"?Promise.resolve(t):w.getOrLoadLOD(t,e).then(o=>{if(Array.isArray(o))return null;if((o==null?void 0:o.isTexture)===!0){if(o!=t&&r&&i){const s=r[i];if(s&&!M){const a=this.getAssignedLODInformation(s);if(a&&(a==null?void 0:a.level)<e)return M==="verbose"&&console.warn("Assigned texture level is already higher: ",a.level,e,r,s,o),null}r[i]=o}return o}else M=="verbose"&&console.warn("No LOD found for",t,e);return null}).catch(o=>(console.error("Error loading LOD",t,o),null))}afterRoot(t){var e,r;return M&&console.log("AFTER",this.url,t),(e=this.parser.json.textures)==null||e.forEach((i,o)=>{var s;if(i!=null&&i.extensions){const a=i==null?void 0:i.extensions[K];if(a){if(!a.lods){M&&console.warn("Texture has no LODs",a);return}let l=!1;for(const u of this.parser.associations.keys())if(u.isTexture===!0){const d=this.parser.associations.get(u);(d==null?void 0:d.textures)===o&&(l=!0,w.registerTexture(this.url,u,(s=a.lods)==null?void 0:s.length,o,a))}l||this.parser.getDependency("texture",o).then(u=>{var d;u&&w.registerTexture(this.url,u,(d=a.lods)==null?void 0:d.length,o,a)})}}}),(r=this.parser.json.meshes)==null||r.forEach((i,o)=>{if(i!=null&&i.extensions){const s=i==null?void 0:i.extensions[K];if(s&&s.lods){for(const a of this.parser.associations.keys())if(a.isMesh){const l=this.parser.associations.get(a);(l==null?void 0:l.meshes)===o&&w.registerMesh(this.url,s.guid,a,s.lods.length,l.primitives,s)}}}}),null}static async getOrLoadLOD(t,e){var a,l,u,d;const r=M=="verbose",i=t.userData.LODS;if(!i)return null;const o=i==null?void 0:i.key;let s;if(t.isTexture===!0){const h=t;h.source&&h.source[ve]&&(s=h.source[ve])}if(s||(s=w.lodInfos.get(o)),s){if(e>0){let v=!1;const S=Array.isArray(s.lods);if(S&&e>=s.lods.length?v=!0:S||(v=!0),v)return this.lowresCache.get(o)}const h=Array.isArray(s.lods)?(a=s.lods[e])==null?void 0:a.path:s.lods;if(!h)return M&&!s["missing:uri"]&&(s["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+e,s)),null;const g=Qe(i.url,h);if(g.endsWith(".glb")||g.endsWith(".gltf")){if(!s.guid)return console.warn("missing pointer for glb/gltf texture",s),null;const v=g+"_"+s.guid,S=this.previouslyLoaded.get(v);if(S!==void 0){r&&console.log(`LOD ${e} was already loading/loaded: ${v}`);let _=await S.catch(m=>(console.error(`Error loading LOD ${e} from ${g}
|
|
2
|
-
`,m),null)),b=!1;if(_==null||(_ instanceof p.Texture&&t instanceof p.Texture?(l=_.image)!=null&&l.data||(u=_.source)!=null&&u.data?_=this.copySettings(t,_):(b=!0,this.previouslyLoaded.delete(v)):_ instanceof p.BufferGeometry&&t instanceof p.BufferGeometry&&((d=_.attributes.position)!=null&&d.array||(b=!0,this.previouslyLoaded.delete(v)))),!b)return _}const D=s,N=new Promise(async(_,b)=>{const m=new te.GLTFLoader;Ae(m),M&&(await new Promise(T=>setTimeout(T,1e3)),r&&console.warn("Start loading (delayed) "+g,D.guid));let q=g;if(D&&Array.isArray(D.lods)){const T=D.lods[e];T.hash&&(q+="?v="+T.hash)}const A=await m.loadAsync(q).catch(T=>(console.error(`Error loading LOD ${e} from ${g}
|
|
3
|
-
`,T),null));if(!A)return null;const R=A.parser;r&&console.log("Loading finished "+g,D.guid);let I=0;if(A.parser.json.textures){let T=!1;for(const f of A.parser.json.textures){if(f!=null&&f.extensions){const P=f==null?void 0:f.extensions[K];if(P!=null&&P.guid&&P.guid===D.guid){T=!0;break}}I++}if(T){let f=await R.getDependency("texture",I);return f&&w.assignLODInformation(i.url,f,o,e,void 0),r&&console.log('change "'+t.name+'" → "'+f.name+'"',g,I,f,v),t instanceof p.Texture&&(f=this.copySettings(t,f)),f&&(f.guid=D.guid),_(f)}else M&&console.warn("Could not find texture with guid",D.guid,A.parser.json)}if(I=0,A.parser.json.meshes){let T=!1;for(const f of A.parser.json.meshes){if(f!=null&&f.extensions){const P=f==null?void 0:f.extensions[K];if(P!=null&&P.guid&&P.guid===D.guid){T=!0;break}}I++}if(T){const f=await R.getDependency("mesh",I);if(r&&console.log(`Loaded Mesh "${f.name}"`,g,I,f,v),f.isMesh===!0){const P=f.geometry;return w.assignLODInformation(i.url,P,o,e,0),_(P)}else{const P=new Array;for(let x=0;x<f.children.length;x++){const O=f.children[x];if(O.isMesh===!0){const C=O.geometry;w.assignLODInformation(i.url,C,o,e,x),P.push(C)}}return _(P)}}else M&&console.warn("Could not find mesh with guid",D.guid,A.parser.json)}return _(null)});return this.previouslyLoaded.set(v,N),await N}else if(t instanceof p.Texture){r&&console.log("Load texture from uri: "+g);const S=await new p.TextureLoader().loadAsync(g);return S?(S.guid=s.guid,S.flipY=!1,S.needsUpdate=!0,S.colorSpace=t.colorSpace,r&&console.log(s,S)):M&&console.warn("failed loading",g),S}}else M&&console.warn(`Can not load LOD ${e}: no LOD info found for "${o}" ${t.name}`,t.type);return null}static assignLODInformation(t,e,r,i,o){if(!e)return;e.userData||(e.userData={});const s=new it(t,r,i,o);e.userData.LODS=s}static getAssignedLODInformation(t){var e;return((e=t==null?void 0:t.userData)==null?void 0:e.LODS)||null}static copySettings(t,e){return e?(M&&console.warn(`Copy texture settings
|
|
4
|
-
`,t.uuid,`
|
|
5
|
-
`,e.uuid),e=e.clone(),e.offset=t.offset,e.repeat=t.repeat,e.colorSpace=t.colorSpace,e.magFilter=t.magFilter,e.minFilter=t.minFilter,e.wrapS=t.wrapS,e.wrapT=t.wrapT,e.flipY=t.flipY,e.anisotropy=t.anisotropy,e.mipmaps||(e.generateMipmaps=t.generateMipmaps),e):t}};c(w,"registerTexture",(t,e,r,i,o)=>{if(M&&console.log("> Progressive: register texture",i,e.name,e.uuid,e,o),!e){M&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[ve]=o);const s=o.guid;w.assignLODInformation(t,e,s,r,i),w.lodInfos.set(s,o),w.lowresCache.set(s,e)}),c(w,"registerMesh",(t,e,r,i,o,s)=>{var u;const a=r.geometry;if(!a){M&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),M&&console.log("> Progressive: register mesh "+r.name,{index:o,uuid:r.uuid},s,r),w.assignLODInformation(t,a,e,i,o),w.lodInfos.set(e,s);let l=w.lowresCache.get(e);l?l.push(r.geometry):l=[r.geometry],w.lowresCache.set(e,l),i>0&&!oe(r)&&rt(r,a);for(const d of Q)(u=d.onRegisteredNewMesh)==null||u.call(d,r,s)}),c(w,"lodInfos",new Map),c(w,"previouslyLoaded",new Map),c(w,"lowresCache",new Map);let F=w;class it{constructor(t,e,r,i){c(this,"url");c(this,"key");c(this,"level");c(this,"index");this.url=t,this.key=e,this.level=r,i!=null&&(this.index=i)}}class ye{constructor(t,e){c(this,"frame_start");c(this,"frame_capture_end");c(this,"ready");c(this,"_resolve");c(this,"_signal");c(this,"_resolved",!1);c(this,"_addedCount",0);c(this,"_resolvedCount",0);c(this,"_awaiting",[]);c(this,"_currentFrame",0);var o;const r=t===0?2:1,i=Math.max(e.frames??r,r);this.frame_start=t,this.frame_capture_end=t+i,this.ready=new Promise(s=>{this._resolve=s}),this.ready.finally(()=>{this._resolved=!0,this._awaiting.length=0}),this._signal=e.signal,(o=this._signal)==null||o.addEventListener("abort",()=>{this.resolveNow()})}get awaitedCount(){return this._addedCount}get resolvedCount(){return this._resolvedCount}get currentlyAwaiting(){return this._awaiting.length}update(t){var e;this._currentFrame=t,((e=this._signal)!=null&&e.aborted||this._currentFrame>this.frame_capture_end&&this._awaiting.length===0)&&this.resolveNow()}add(t,e){if(this._resolved){console.warn("PromiseGroup: Trying to add a promise to a resolved group, ignoring.");return}this._currentFrame>this.frame_capture_end||(this._awaiting.push(e),this._addedCount++,e.finally(()=>{this._resolvedCount++,this._awaiting.splice(this._awaiting.indexOf(e),1)}))}resolveNow(){var t,e;this._resolved||(e=this._resolve)==null||e.call(this,{awaited_count:this._addedCount,resolved_count:this._resolvedCount,cancelled:((t=this._signal)==null?void 0:t.aborted)??!1})}}c(ye,"addPromise",(t,e,r)=>{r.forEach(i=>{i.add(t,e)})});const $=de("debugprogressive"),nt=de("noprogressive"),_e=Symbol("Needle:LODSManager"),Oe=Symbol("Needle:LODState"),J=Symbol("Needle:CurrentLOD"),E={mesh_lod:-1,texture_lod:-1};var L,G,Y,Le,Z,se,xe,H;let ce=(L=class{constructor(t,e){c(this,"renderer");c(this,"context");c(this,"projectionScreenMatrix",new p.Matrix4);c(this,"targetTriangleDensity",2e5);c(this,"skinnedMeshAutoUpdateBoundsInterval",30);c(this,"updateInterval","auto");j(this,G,1);c(this,"pause",!1);c(this,"manual",!1);c(this,"_newPromiseGroups",[]);c(this,"_promiseGroupIds",0);c(this,"_lodchangedlisteners",[]);j(this,Y);j(this,Le,new p.Clock);j(this,Z,0);j(this,se,0);j(this,xe,0);j(this,H,0);c(this,"_fpsBuffer",[60,60,60,60,60]);c(this,"_sphere",new p.Sphere);c(this,"_tempBox",new p.Box3);c(this,"_tempBox2",new p.Box3);c(this,"tempMatrix",new p.Matrix4);c(this,"_tempWorldPosition",new p.Vector3);c(this,"_tempBoxSize",new p.Vector3);c(this,"_tempBox2Size",new p.Vector3);this.renderer=t,this.context={...e}}static getObjectLODState(t){return t[Oe]}static addPlugin(t){Q.push(t)}static removePlugin(t){const e=Q.indexOf(t);e>=0&&Q.splice(e,1)}static get(t,e){if(t[_e])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),t[_e];const r=new L(t,{engine:"unknown",...e});return t[_e]=r,r}get plugins(){return Q}awaitLoading(t){const e=this._promiseGroupIds++,r=new ye(y(this,Z),{...t});this._newPromiseGroups.push(r);const i=performance.now();return r.ready.finally(()=>{const o=this._newPromiseGroups.indexOf(r);o>=0&&(this._newPromiseGroups.splice(o,1),et()&&performance.measure("LODsManager:awaitLoading",{start:i,detail:{id:e,name:t==null?void 0:t.name,awaited:r.awaitedCount,resolved:r.resolvedCount}}))}),r.ready}_postprocessPromiseGroups(){if(this._newPromiseGroups.length!==0)for(let t=this._newPromiseGroups.length-1;t>=0;t--)this._newPromiseGroups[t].update(y(this,Z))}addEventListener(t,e){t==="changed"&&this._lodchangedlisteners.push(e)}removeEventListener(t,e){if(t==="changed"){const r=this._lodchangedlisteners.indexOf(e);r>=0&&this._lodchangedlisteners.splice(r,1)}}enable(){if(y(this,Y))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let t=0;z(this,Y,this.renderer.render);const e=this;Pe(this.renderer),this.renderer.render=function(r,i){const o=e.renderer.getRenderTarget();(o==null||"isXRRenderTarget"in o&&o.isXRRenderTarget)&&(t=0,z(e,Z,y(e,Z)+1),z(e,se,y(e,Le).getDelta()),z(e,xe,y(e,xe)+y(e,se)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/y(e,se)),z(e,H,e._fpsBuffer.reduce((a,l)=>a+l)/e._fpsBuffer.length),$&&y(e,Z)%200===0&&console.log("FPS",Math.round(y(e,H)),"Interval:",y(e,G)));const s=t++;y(e,Y).call(this,r,i),e.onAfterRender(r,i,s)}}disable(){y(this,Y)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=y(this,Y),z(this,Y,void 0))}update(t,e){this.internalUpdate(t,e)}onAfterRender(t,e,r){if(this.pause)return;const o=this.renderer.renderLists.get(t,0).opaque;let s=!0;if(o.length===1){const a=o[0].material;(a.name==="EffectMaterial"||a.name==="CopyShader")&&(s=!1)}if((e.parent&&e.parent.type==="CubeCamera"||r>=1&&e.type==="OrthographicCamera")&&(s=!1),s){if(nt||(this.updateInterval==="auto"?y(this,H)<40&&y(this,G)<10?(z(this,G,y(this,G)+1),$&&console.warn("↓ Reducing LOD updates",y(this,G),y(this,H).toFixed(0))):y(this,H)>=60&&y(this,G)>1&&(z(this,G,y(this,G)-1),$&&console.warn("↑ Increasing LOD updates",y(this,G),y(this,H).toFixed(0))):z(this,G,this.updateInterval),y(this,G)>0&&y(this,Z)%y(this,G)!=0))return;this.internalUpdate(t,e),this._postprocessPromiseGroups()}}internalUpdate(t,e){var l,u;const r=this.renderer.renderLists.get(t,0),i=r.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const o=this.targetTriangleDensity;for(const d of i){if(d.material&&(((l=d.geometry)==null?void 0:l.type)==="BoxGeometry"||((u=d.geometry)==null?void 0:u.type)==="BufferGeometry")&&(d.material.name==="SphericalGaussianBlur"||d.material.name=="BackgroundCubeMaterial"||d.material.name==="CubemapFromEquirect"||d.material.name==="EquirectangularToCubeUV")){$&&(d.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(d.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",d,d.material.name,d.material.type)));continue}switch(d.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if($==="color"&&d.material&&!d.object.progressive_debug_color){d.object.progressive_debug_color=!0;const g=Math.random()*16777215,v=new p.MeshStandardMaterial({color:g});d.object.material=v}const h=d.object;(h instanceof p.Mesh||h.isMesh)&&this.updateLODs(t,e,h,o)}const s=r.transparent;for(const d of s){const h=d.object;(h instanceof p.Mesh||h.isMesh)&&this.updateLODs(t,e,h,o)}const a=r.transmissive;for(const d of a){const h=d.object;(h instanceof p.Mesh||h.isMesh)&&this.updateLODs(t,e,h,o)}}updateLODs(t,e,r,i){var a,l;r.userData||(r.userData={});let o=r[Oe];if(o||(o=new at,r[Oe]=o),o.frames++<2)return;for(const u of Q)(a=u.onBeforeUpdateLOD)==null||a.call(u,this.renderer,t,e,r);const s=L.overrideGlobalLodLevel!==void 0?L.overrideGlobalLodLevel:ee;s>=0?(E.mesh_lod=s,E.texture_lod=s):(this.calculateLodLevel(e,r,o,i,E),E.mesh_lod=Math.round(E.mesh_lod),E.texture_lod=Math.round(E.texture_lod)),E.mesh_lod>=0&&this.loadProgressiveMeshes(r,E.mesh_lod),r.material&&E.texture_lod>=0&&this.loadProgressiveTextures(r.material,E.texture_lod),M&&r.material&&!r.isGizmo&&Ne(r.material);for(const u of Q)(l=u.onAfterUpdatedLOD)==null||l.call(u,this.renderer,t,e,r,E);o.lastLodLevel_Mesh=E.mesh_lod,o.lastLodLevel_Texture=E.texture_lod}loadProgressiveTextures(t,e){if(!t)return;if(Array.isArray(t)){for(const o of t)this.loadProgressiveTextures(o,e);return}let r=!1;(t[J]===void 0||e<t[J])&&(r=!0);const i=t["DEBUG:LOD"];if(i!=null&&(r=t[J]!=i,e=i),r){t[J]=e;const o=F.assignTextureLOD(t,e).then(s=>{this._lodchangedlisteners.forEach(a=>a({type:"texture",level:e,object:t}))});ye.addPromise("texture",o,this._newPromiseGroups)}}loadProgressiveMeshes(t,e){if(!t)return Promise.resolve(null);let r=t[J]!==e;const i=t["DEBUG:LOD"];if(i!=null&&(r=t[J]!=i,e=i),r){t[J]=e;const o=t.geometry,s=F.assignMeshLOD(t,e).then(a=>(a&&t[J]==e&&o!=t.geometry&&this._lodchangedlisteners.forEach(l=>l({type:"mesh",level:e,object:t})),a));return ye.addPromise("mesh",s,this._newPromiseGroups),s}return Promise.resolve(null)}static isInside(t,e){const r=t.min,i=t.max,o=(r.x+i.x)*.5,s=(r.y+i.y)*.5;return this._tempPtInside.set(o,s,r.z).applyMatrix4(e).z<0}calculateLodLevel(t,e,r,i,o){var N,X,_;if(!e){o.mesh_lod=-1,o.texture_lod=-1;return}if(!t){o.mesh_lod=-1,o.texture_lod=-1;return}let a=10+1,l=!1;if($&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const u=(N=F.getMeshLODExtension(e.geometry))==null?void 0:N.lods,d=F.getPrimitiveIndex(e.geometry),h=u&&u.length>0,g=F.getMaterialMinMaxLODsCount(e.material),v=(g==null?void 0:g.min_count)!=1/0&&g.min_count>0&&g.max_count>0;if(!h&&!v){o.mesh_lod=0,o.texture_lod=0;return}h||(l=!0,a=0);const S=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let D=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const b=e;if(!b.boundingBox)b.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0&&r.frames%this.skinnedMeshAutoUpdateBoundsInterval===0){const m=oe(b),q=b.geometry;m&&(b.geometry=m),b.computeBoundingBox(),b.geometry=q}D=b.boundingBox}if(D){const b=t;if(e.geometry.attributes.color&&e.geometry.attributes.color.count<100&&e.geometry.boundingSphere){this._sphere.copy(e.geometry.boundingSphere),this._sphere.applyMatrix4(e.matrixWorld);const x=t.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(x)){o.mesh_lod=0,o.texture_lod=0;return}}if(this._tempBox.copy(D),this._tempBox.applyMatrix4(e.matrixWorld),b.isPerspectiveCamera&&L.isInside(this._tempBox,this.projectionScreenMatrix)){o.mesh_lod=0,o.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&b.isPerspectiveCamera&&b.fov>70){const x=this._tempBox.min,O=this._tempBox.max;let C=x.x,k=x.y,V=O.x,ie=O.y;const ue=2,we=1.5,fe=(x.x+O.x)*.5,he=(x.y+O.y)*.5;C=(C-fe)*ue+fe,k=(k-he)*ue+he,V=(V-fe)*ue+fe,ie=(ie-he)*ue+he;const We=C<0&&V>0?0:Math.min(Math.abs(x.x),Math.abs(O.x)),Xe=k<0&&ie>0?0:Math.min(Math.abs(x.y),Math.abs(O.y)),Me=Math.max(We,Xe);r.lastCentrality=(we-Me)*(we-Me)*(we-Me)}else r.lastCentrality=1;const m=this._tempBox.getSize(this._tempBoxSize);m.multiplyScalar(.5),screen.availHeight>0&&S>0&&m.multiplyScalar(S/screen.availHeight),t.isPerspectiveCamera?m.x*=t.aspect:t.isOrthographicCamera;const q=t.matrixWorldInverse,A=this._tempBox2;A.copy(D),A.applyMatrix4(e.matrixWorld),A.applyMatrix4(q);const R=A.getSize(this._tempBox2Size),I=Math.max(R.x,R.y);if(Math.max(m.x,m.y)!=0&&I!=0&&(m.z=R.z/Math.max(R.x,R.y)*Math.max(m.x,m.y)),r.lastScreenCoverage=Math.max(m.x,m.y,m.z),r.lastScreenspaceVolume.copy(m),r.lastScreenCoverage*=r.lastCentrality,$&&L.debugDrawLine){const x=this.tempMatrix.copy(this.projectionScreenMatrix);x.invert();const O=L.corner0,C=L.corner1,k=L.corner2,V=L.corner3;O.copy(this._tempBox.min),C.copy(this._tempBox.max),C.x=O.x,k.copy(this._tempBox.max),k.y=O.y,V.copy(this._tempBox.max);const ie=(O.z+V.z)*.5;O.z=C.z=k.z=V.z=ie,O.applyMatrix4(x),C.applyMatrix4(x),k.applyMatrix4(x),V.applyMatrix4(x),L.debugDrawLine(O,C,255),L.debugDrawLine(O,k,255),L.debugDrawLine(C,V,255),L.debugDrawLine(k,V,255)}let f=999;if(u&&r.lastScreenCoverage>0)for(let x=0;x<u.length;x++){const O=u[x];if((((X=O.densities)==null?void 0:X[d])||O.density||1e-5)/r.lastScreenCoverage<i){f=x;break}}f<a&&(a=f,l=!0)}if(l?o.mesh_lod=a:o.mesh_lod=r.lastLodLevel_Mesh,$&&o.mesh_lod!=r.lastLodLevel_Mesh){const m=u==null?void 0:u[o.mesh_lod];m&&console.log(`Mesh LOD changed: ${r.lastLodLevel_Mesh} → ${o.mesh_lod} (${m.density.toFixed(0)}) - ${e.name}`)}if(v){const b="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(r.lastLodLevel_Texture<0){if(o.texture_lod=g.max_count-1,$){const m=g.lods[g.max_count-1];$&&console.log(`First Texture LOD ${o.texture_lod} (${m.max_height}px) - ${e.name}`)}}else{const m=r.lastScreenspaceVolume.x+r.lastScreenspaceVolume.y+r.lastScreenspaceVolume.z;let q=r.lastScreenCoverage*4;((_=this.context)==null?void 0:_.engine)==="model-viewer"&&(q*=1.5);const R=S/window.devicePixelRatio*q;let I=!1;for(let T=g.lods.length-1;T>=0;T--){const f=g.lods[T];if(!(b&&f.max_height>=2048)&&!(Ze()&&f.max_height>4096)&&(f.max_height>R||!I&&T===0)){if(I=!0,o.texture_lod=T,o.texture_lod<r.lastLodLevel_Texture){const P=f.max_height;$&&console.log(`Texture LOD changed: ${r.lastLodLevel_Texture} → ${o.texture_lod} = ${P}px
|
|
6
|
-
Screensize: ${R.toFixed(0)}px, Coverage: ${(100*r.lastScreenCoverage).toFixed(2)}%, Volume ${m.toFixed(1)}
|
|
7
|
-
${e.name}`)}break}}}}else o.texture_lod=0}},G=new WeakMap,Y=new WeakMap,Le=new WeakMap,Z=new WeakMap,se=new WeakMap,xe=new WeakMap,H=new WeakMap,c(L,"debugDrawLine"),c(L,"overrideGlobalLodLevel"),c(L,"corner0",new p.Vector3),c(L,"corner1",new p.Vector3),c(L,"corner2",new p.Vector3),c(L,"corner3",new p.Vector3),c(L,"_tempPtInside",new p.Vector3),L);class at{constructor(){c(this,"frames",0);c(this,"lastLodLevel_Mesh",-1);c(this,"lastLodLevel_Texture",-1);c(this,"lastScreenCoverage",0);c(this,"lastScreenspaceVolume",new p.Vector3);c(this,"lastCentrality",0)}}const Be=Symbol("NEEDLE_mesh_lod"),ge=Symbol("NEEDLE_texture_lod");let le=null;function Ve(){const n=lt();n&&(n.mapURLs(function(t){return Ie(),t}),Ie(),le==null||le.disconnect(),le=new MutationObserver(t=>{t.forEach(e=>{e.addedNodes.forEach(r=>{r instanceof HTMLElement&&r.tagName.toLowerCase()==="model-viewer"&&ze(r)})})}),le.observe(document,{childList:!0,subtree:!0}))}function lt(){if(typeof customElements>"u")return null;const n=customElements.get("model-viewer");return n||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),Ve()}),null)}function Ie(){if(typeof document>"u")return;document.querySelectorAll("model-viewer").forEach(t=>{ze(t)})}const Re=new WeakSet;let ct=0;function ze(n){if(!n||Re.has(n))return null;Re.add(n),console.debug("[gltf-progressive] found new model-viewer..."+ ++ct+`
|
|
8
|
-
`,n.getAttribute("src"));let t=null,e=null,r=null;for(let i=n;i!=null;i=Object.getPrototypeOf(i)){const o=Object.getOwnPropertySymbols(i),s=o.find(u=>u.toString()=="Symbol(renderer)"),a=o.find(u=>u.toString()=="Symbol(scene)"),l=o.find(u=>u.toString()=="Symbol(needsRender)");!t&&s!=null&&(t=n[s].threeRenderer),!e&&a!=null&&(e=n[a]),!r&&l!=null&&(r=n[l])}if(t&&e){let i=function(){if(r){let s=0,a=setInterval(()=>{if(s++>5){clearInterval(a);return}r==null||r.call(n)},300)}};console.debug("[gltf-progressive] setup model-viewer");const o=ce.get(t,{engine:"model-viewer"});return ce.addPlugin(new dt),o.enable(),o.addEventListener("changed",()=>{r==null||r.call(n)}),n.addEventListener("model-visibility",s=>{s.detail.visible&&(r==null||r.call(n))}),n.addEventListener("load",()=>{i()}),()=>{o.disable()}}return null}class dt{constructor(){c(this,"_didWarnAboutMissingUrl",!1)}onBeforeUpdateLOD(t,e,r,i){this.tryParseMeshLOD(e,i),this.tryParseTextureLOD(e,i)}getUrl(t){if(!t)return null;let e=t.getAttribute("src");return e||(e=t.src),e||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",t),this._didWarnAboutMissingUrl=!0),e}tryGetCurrentGLTF(t){return t._currentGLTF}tryGetCurrentModelViewer(t){return t.element}tryParseTextureLOD(t,e){if(e[ge]==!0)return;e[ge]=!0;const r=this.tryGetCurrentGLTF(t),i=this.tryGetCurrentModelViewer(t),o=this.getUrl(i);if(o&&r&&e.material){let s=function(l){var d,h,g;if(l[ge]==!0)return;l[ge]=!0,l.userData&&(l.userData.LOD=-1);const u=Object.keys(l);for(let v=0;v<u.length;v++){const S=u[v],D=l[S];if((D==null?void 0:D.isTexture)===!0){const N=(h=(d=D.userData)==null?void 0:d.associations)==null?void 0:h.textures;if(N==null)continue;const X=r.parser.json.textures[N];if(!X){console.warn("Texture data not found for texture index "+N);continue}if((g=X==null?void 0:X.extensions)!=null&&g[K]){const _=X.extensions[K];_&&o&&F.registerTexture(o,D,_.lods.length,N,_)}}}};const a=e.material;if(Array.isArray(a))for(const l of a)s(l);else s(a)}}tryParseMeshLOD(t,e){var s,a;if(e[Be]==!0)return;e[Be]=!0;const r=this.tryGetCurrentModelViewer(t),i=this.getUrl(r);if(!i)return;const o=(a=(s=e.userData)==null?void 0:s.gltfExtensions)==null?void 0:a[K];if(o&&i){const l=e.uuid;F.registerMesh(i,l,e,0,o.lods.length,o)}}}function ut(n,t,e,r){Pe(t),Ae(e),Ce(e,{progressive:!0,...r==null?void 0:r.hints}),e.register(o=>new F(o,n));const i=ce.get(t);return(r==null?void 0:r.enableLODsManager)!==!1&&i.enable(),i}Ve();if(!tt){const n={gltfProgressive:{useNeedleProgressive:ut,LODsManager:ce,configureLoader:Ce,getRaycastMesh:oe,useRaycastMeshes:st}};if(!globalThis.Needle)globalThis.Needle=n;else for(const t in n)globalThis.Needle[t]=n[t]}exports.LODsManager=ce;exports.NEEDLE_progressive=F;exports.addDracoAndKTX2Loaders=Ae;exports.configureLoader=Ce;exports.createLoaders=Pe;exports.getRaycastMesh=oe;exports.setDracoDecoderLocation=$e;exports.setKTX2TranscoderLocation=Fe;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
var Ke=Object.defineProperty,Ee=r=>{throw TypeError(r)},He=(r,e,t)=>e in r?Ke(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,c=(r,e,t)=>He(r,typeof e!="symbol"?e+"":e,t),Ce=(r,e,t)=>e.has(r)||Ee("Cannot "+t),v=(r,e,t)=>(Ce(r,e,"read from private field"),t?t.call(r):e.get(r)),J=(r,e,t)=>e.has(r)?Ee("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(r):e.set(r,t),F=(r,e,t,o)=>(Ce(r,e,"write to private field"),o?o.call(r,t):e.set(r,t),t);import{BufferGeometry as de,Mesh as Q,Texture as re,TextureLoader as Ye,Matrix4 as Ie,Clock as Je,MeshStandardMaterial as Qe,Sphere as Ze,Box3 as Be,Vector3 as W}from"./three-DuDKwKB8.min.js";import{DRACOLoader as et,KTX2Loader as tt,MeshoptDecoder as rt,GLTFLoader as Le}from"./three-examples-DaDLBuy6.min.js";const st="";globalThis.GLTF_PROGRESSIVE_VERSION=st,console.debug("[gltf-progressive] version -");let $="https://www.gstatic.com/draco/versioned/decoders/1.5.7/",ee="https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/";const ot=$,nt=ee,Ge=new URL($+"draco_decoder.js");Ge.searchParams.append("range","true"),fetch(Ge,{method:"GET",headers:{Range:"bytes=0-1"}}).catch(r=>{console.debug(`Failed to fetch remote Draco decoder from ${$} (offline: ${typeof navigator<"u"?navigator.onLine:"unknown"})`),$===ot&&ke("./include/draco/"),ee===nt&&Re("./include/ktx2/")}).finally(()=>{je()});function ke(r){$=r,A&&A[we]!=$?(console.debug("Updating Draco decoder path to "+r),A[we]=$,A.setDecoderPath($),A.preload()):console.debug("Setting Draco decoder path to "+r)}function Re(r){ee=r,B&&B.transcoderPath!=ee?(console.debug("Updating KTX2 transcoder path to "+r),B.setTranscoderPath(ee),B.init()):console.debug("Setting KTX2 transcoder path to "+r)}const we=Symbol("dracoDecoderPath");let A,ce,B;function je(){A||(A=new et,A[we]=$,A.setDecoderPath($),A.setDecoderConfig({type:"js"}),A.preload()),B||(B=new tt,B.setTranscoderPath(ee),B.init()),ce||(ce=rt)}function _e(r){return je(),r?B.detectSupport(r):r!==null&&console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures might fail"),{dracoLoader:A,ktx2Loader:B,meshoptDecoder:ce}}function De(r){r.dracoLoader||r.setDRACOLoader(A),r.ktx2Loader||r.setKTX2Loader(B),r.meshoptDecoder||r.setMeshoptDecoder(ce)}const Me=new WeakMap;function be(r,e){let t=Me.get(r);t?t=Object.assign(t,e):t=e,Me.set(r,t)}const Ne=Le.prototype.load;function it(...r){const e=Me.get(this);let t=r[0];const o=new URL(t,window.location.href);if(o.hostname.endsWith("needle.tools")){const s=e?.progressive!==void 0?e.progressive:!0,i=e!=null&&e.usecase?e.usecase:"default";s?this.requestHeader.Accept=`*/*;progressive=allowed;usecase=${i}`:this.requestHeader.Accept=`*/*;usecase=${i}`,t=o.toString()}return r[0]=t,Ne?.call(this,...r)}Le.prototype.load=it,se("debugprogressive");function se(r){if(typeof window>"u")return!1;const e=new URL(window.location.href).searchParams.get(r);return e==null||e==="0"||e==="false"?!1:e===""?!0:e}function at(r,e){if(e===void 0||e.startsWith("./")||e.startsWith("http")||r===void 0)return e;const t=r.lastIndexOf("/");if(t>=0){const o=r.substring(0,t+1);for(;o.endsWith("/")&&e.startsWith("/");)e=e.substring(1);return o+e}return e}let he;function lt(){return he!==void 0||(he=/iPhone|iPad|iPod|Android|IEMobile/i.test(navigator.userAgent),se("debugprogressive")&&console.log("[glTF Progressive]: isMobileDevice",he)),he}function ut(){if(typeof window>"u")return!1;const r=new URL(window.location.href),e=r.hostname==="localhost"||/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(r.hostname);return r.hostname==="127.0.0.1"||e}const dt=typeof window>"u"&&typeof document>"u",Oe=Symbol("needle:raycast-mesh");function te(r){return r?.[Oe]instanceof de?r[Oe]:null}function ct(r,e){if((r.type==="Mesh"||r.type==="SkinnedMesh")&&!te(r)){const t=gt(e);t.userData={isRaycastMesh:!0},r[Oe]=t}}function ht(r=!0){if(r){if(oe)return;const e=oe=Q.prototype.raycast;Q.prototype.raycast=function(t,o){const s=this,i=te(s);let n;i&&s.isMesh&&(n=s.geometry,s.geometry=i),e.call(this,t,o),n&&(s.geometry=n)}}else{if(!oe)return;Q.prototype.raycast=oe,oe=null}}let oe=null;function gt(r){const e=new de;for(const t in r.attributes)e.setAttribute(t,r.getAttribute(t));return e.setIndex(r.getIndex()),e}const X=new Array,L=se("debugprogressive");let ge,Z=-1;if(L){let r=function(){Z+=1,Z>=e&&(Z=-1),console.log(`Toggle LOD level [${Z}]`)},e=6;window.addEventListener("keyup",t=>{t.key==="p"&&r(),t.key==="w"&&(ge=!ge,console.log(`Toggle wireframe [${ge}]`));const o=parseInt(t.key);!isNaN(o)&&o>=0&&(Z=o,console.log(`Set LOD level to [${Z}]`))})}function $e(r){if(L)if(Array.isArray(r))for(const e of r)$e(e);else r&&"wireframe"in r&&(r.wireframe=ge===!0)}const U="NEEDLE_progressive",Se=Symbol("needle-progressive-texture"),E=class k{constructor(e,t){c(this,"parser"),c(this,"url"),c(this,"_isLoadingMesh"),c(this,"loadMesh",o=>{var s,i;if(this._isLoadingMesh)return null;const n=(i=(s=this.parser.json.meshes[o])==null?void 0:s.extensions)==null?void 0:i[U];return n?(this._isLoadingMesh=!0,this.parser.getDependency("mesh",o).then(a=>{var l;return this._isLoadingMesh=!1,a&&k.registerMesh(this.url,n.guid,a,(l=n.lods)==null?void 0:l.length,0,n),a})):null}),L&&console.log("Progressive extension registered for",t),this.parser=e,this.url=t}get name(){return U}static getMeshLODExtension(e){const t=this.getAssignedLODInformation(e);return t!=null&&t.key?this.lodInfos.get(t.key):null}static getPrimitiveIndex(e){var t;return((t=this.getAssignedLODInformation(e))==null?void 0:t.index)??-1}static getMaterialMinMaxLODsCount(e,t){const o=this,s="LODS:minmax",i=e[s];if(i!=null)return i;if(t||(t={min_count:1/0,max_count:0,lods:[]}),Array.isArray(e)){for(const a of e)this.getMaterialMinMaxLODsCount(a,t);return e[s]=t,t}if(L==="verbose"&&console.log("getMaterialMinMaxLODsCount",e),e.type==="ShaderMaterial"||e.type==="RawShaderMaterial"){const a=e;for(const l of Object.keys(a.uniforms)){const u=a.uniforms[l].value;u?.isTexture===!0&&n(u,t)}}else if(e.isMaterial)for(const a of Object.keys(e)){const l=e[a];l?.isTexture===!0&&n(l,t)}return e[s]=t,t;function n(a,l){const u=o.getAssignedLODInformation(a);if(u){const d=o.lodInfos.get(u.key);if(d&&d.lods){l.min_count=Math.min(l.min_count,d.lods.length),l.max_count=Math.max(l.max_count,d.lods.length);for(let w=0;w<d.lods.length;w++){const y=d.lods[w];y.width&&(l.lods[w]=l.lods[w]||{min_height:1/0,max_height:0},l.lods[w].min_height=Math.min(l.lods[w].min_height,y.height),l.lods[w].max_height=Math.max(l.lods[w].max_height,y.height))}}}}}static hasLODLevelAvailable(e,t){var o;if(Array.isArray(e)){for(const n of e)if(this.hasLODLevelAvailable(n,t))return!0;return!1}if(e.isMaterial===!0){for(const n of Object.keys(e)){const a=e[n];if(a&&a.isTexture&&this.hasLODLevelAvailable(a,t))return!0}return!1}else if(e.isGroup===!0){for(const n of e.children)if(n.isMesh===!0&&this.hasLODLevelAvailable(n,t))return!0}let s,i;if(e.isMesh?s=e.geometry:(e.isBufferGeometry||e.isTexture)&&(s=e),s&&(o=s?.userData)!=null&&o.LODS){const n=s.userData.LODS;if(i=this.lodInfos.get(n.key),t===void 0)return i!=null;if(i)return Array.isArray(i.lods)?t<i.lods.length:t===0}return!1}static assignMeshLOD(e,t){var o;if(!e)return Promise.resolve(null);if(e instanceof Q||e.isMesh===!0){const s=e.geometry,i=this.getAssignedLODInformation(s);if(!i)return Promise.resolve(null);for(const n of X)(o=n.onBeforeGetLODMesh)==null||o.call(n,e,t);return e["LOD:requested level"]=t,k.getOrLoadLOD(s,t).then(n=>{if(Array.isArray(n)){const a=i.index||0;n=n[a]}return e["LOD:requested level"]===t&&(delete e["LOD:requested level"],n&&s!=n&&(n?.isBufferGeometry?e.geometry=n:L&&console.error("Invalid LOD geometry",n))),n}).catch(n=>(console.error("Error loading mesh LOD",e,n),null))}else L&&console.error("Invalid call to assignMeshLOD: Request mesh LOD but the object is not a mesh",e);return Promise.resolve(null)}static assignTextureLOD(e,t=0){if(!e)return Promise.resolve(null);if(e.isMesh===!0){const o=e;if(Array.isArray(o.material)){const s=new Array;for(const i of o.material){const n=this.assignTextureLOD(i,t);s.push(n)}return Promise.all(s).then(i=>{const n=new Array;for(const a of i)Array.isArray(a)&&n.push(...a);return n})}else return this.assignTextureLOD(o.material,t)}if(e.isMaterial===!0){const o=e,s=[],i=new Array;if(o.uniforms&&(o.isRawShaderMaterial||o.isShaderMaterial===!0)){const n=o;for(const a of Object.keys(n.uniforms)){const l=n.uniforms[a].value;if(l?.isTexture===!0){const u=this.assignTextureLODForSlot(l,t,o,a).then(d=>(d&&n.uniforms[a].value!=d&&(n.uniforms[a].value=d,n.uniformsNeedUpdate=!0),d));s.push(u),i.push(a)}}}else for(const n of Object.keys(o)){const a=o[n];if(a?.isTexture===!0){const l=this.assignTextureLODForSlot(a,t,o,n);s.push(l),i.push(n)}}return Promise.all(s).then(n=>{const a=new Array;for(let l=0;l<n.length;l++){const u=n[l],d=i[l];u&&u.isTexture===!0?a.push({material:o,slot:d,texture:u,level:t}):a.push({material:o,slot:d,texture:null,level:t})}return a})}if(e instanceof re||e.isTexture===!0){const o=e;return this.assignTextureLODForSlot(o,t,null,null)}return Promise.resolve(null)}static assignTextureLODForSlot(e,t,o,s){return e?.isTexture!==!0?Promise.resolve(null):s==="glyphMap"?Promise.resolve(e):k.getOrLoadLOD(e,t).then(i=>{if(Array.isArray(i))return null;if(i?.isTexture===!0){if(i!=e&&o&&s){const n=o[s];if(n&&!L){const a=this.getAssignedLODInformation(n);if(a&&a?.level<t)return L==="verbose"&&console.warn("Assigned texture level is already higher: ",a.level,t,o,n,i),null}o[s]=i}return i}else L=="verbose"&&console.warn("No LOD found for",e,t);return null}).catch(i=>(console.error("Error loading LOD",e,i),null))}afterRoot(e){var t,o;return L&&console.log("AFTER",this.url,e),(t=this.parser.json.textures)==null||t.forEach((s,i)=>{var n;if(s!=null&&s.extensions){const a=s?.extensions[U];if(a){if(!a.lods){L&&console.warn("Texture has no LODs",a);return}let l=!1;for(const u of this.parser.associations.keys())if(u.isTexture===!0){const d=this.parser.associations.get(u);d?.textures===i&&(l=!0,k.registerTexture(this.url,u,(n=a.lods)==null?void 0:n.length,i,a))}l||this.parser.getDependency("texture",i).then(u=>{var d;u&&k.registerTexture(this.url,u,(d=a.lods)==null?void 0:d.length,i,a)})}}}),(o=this.parser.json.meshes)==null||o.forEach((s,i)=>{if(s!=null&&s.extensions){const n=s?.extensions[U];if(n&&n.lods){for(const a of this.parser.associations.keys())if(a.isMesh){const l=this.parser.associations.get(a);l?.meshes===i&&k.registerMesh(this.url,n.guid,a,n.lods.length,l.primitives,n)}}}}),null}static async getOrLoadLOD(e,t){var o,s,i,n;const a=L=="verbose",l=e.userData.LODS;if(!l)return null;const u=l?.key;let d;if(e.isTexture===!0){const w=e;w.source&&w.source[Se]&&(d=w.source[Se])}if(d||(d=k.lodInfos.get(u)),d){if(t>0){let m=!1;const R=Array.isArray(d.lods);if(R&&t>=d.lods.length?m=!0:R||(m=!0),m)return this.lowresCache.get(u)}const w=Array.isArray(d.lods)?(o=d.lods[t])==null?void 0:o.path:d.lods;if(!w)return L&&!d["missing:uri"]&&(d["missing:uri"]=!0,console.warn("Missing uri for progressive asset for LOD "+t,d)),null;const y=at(l.url,w);if(y.endsWith(".glb")||y.endsWith(".gltf")){if(!d.guid)return console.warn("missing pointer for glb/gltf texture",d),null;const m=y+"_"+d.guid,R=this.previouslyLoaded.get(m);if(R!==void 0){a&&console.log(`LOD ${t} was already loading/loaded: ${m}`);let g=await R.catch(I=>(console.error(`Error loading LOD ${t} from ${y}
|
|
2
|
-
`,I),null)),p=!1;if(g==null||(g instanceof re&&e instanceof re?(s=g.image)!=null&&s.data||(i=g.source)!=null&&i.data?g=this.copySettings(e,g):(p=!0,this.previouslyLoaded.delete(m)):g instanceof de&&e instanceof de&&((n=g.attributes.position)!=null&&n.array||(p=!0,this.previouslyLoaded.delete(m)))),!p)return g}const D=d,C=new Promise(async(g,p)=>{const I=new Le;De(I),L&&(await new Promise(f=>setTimeout(f,1e3)),a&&console.warn("Start loading (delayed) "+y,D.guid));let j=y;if(D&&Array.isArray(D.lods)){const f=D.lods[t];f.hash&&(j+="?v="+f.hash)}const M=await I.loadAsync(j).catch(f=>(console.error(`Error loading LOD ${t} from ${y}
|
|
3
|
-
`,f),null));if(!M)return null;const N=M.parser;a&&console.log("Loading finished "+y,D.guid);let b=0;if(M.parser.json.textures){let f=!1;for(const h of M.parser.json.textures){if(h!=null&&h.extensions){const x=h?.extensions[U];if(x!=null&&x.guid&&x.guid===D.guid){f=!0;break}}b++}if(f){let h=await N.getDependency("texture",b);return h&&k.assignLODInformation(l.url,h,u,t,void 0),a&&console.log('change "'+e.name+'" \u2192 "'+h.name+'"',y,b,h,m),e instanceof re&&(h=this.copySettings(e,h)),h&&(h.guid=D.guid),g(h)}else L&&console.warn("Could not find texture with guid",D.guid,M.parser.json)}if(b=0,M.parser.json.meshes){let f=!1;for(const h of M.parser.json.meshes){if(h!=null&&h.extensions){const x=h?.extensions[U];if(x!=null&&x.guid&&x.guid===D.guid){f=!0;break}}b++}if(f){const h=await N.getDependency("mesh",b);if(a&&console.log(`Loaded Mesh "${h.name}"`,y,b,h,m),h.isMesh===!0){const x=h.geometry;return k.assignLODInformation(l.url,x,u,t,0),g(x)}else{const x=new Array;for(let O=0;O<h.children.length;O++){const P=h.children[O];if(P.isMesh===!0){const V=P.geometry;k.assignLODInformation(l.url,V,u,t,O),x.push(V)}}return g(x)}}else L&&console.warn("Could not find mesh with guid",D.guid,M.parser.json)}return g(null)});return this.previouslyLoaded.set(m,C),await C}else if(e instanceof re){a&&console.log("Load texture from uri: "+y);const m=await new Ye().loadAsync(y);return m?(m.guid=d.guid,m.flipY=!1,m.needsUpdate=!0,m.colorSpace=e.colorSpace,a&&console.log(d,m)):L&&console.warn("failed loading",y),m}}else L&&console.warn(`Can not load LOD ${t}: no LOD info found for "${u}" ${e.name}`,e.type);return null}static assignLODInformation(e,t,o,s,i){if(!t)return;t.userData||(t.userData={});const n=new ft(e,o,s,i);t.userData.LODS=n}static getAssignedLODInformation(e){var t;return((t=e?.userData)==null?void 0:t.LODS)||null}static copySettings(e,t){return t?(L&&console.warn(`Copy texture settings
|
|
4
|
-
`,e.uuid,`
|
|
5
|
-
`,t.uuid),t=t.clone(),t.offset=e.offset,t.repeat=e.repeat,t.colorSpace=e.colorSpace,t.magFilter=e.magFilter,t.minFilter=e.minFilter,t.wrapS=e.wrapS,t.wrapT=e.wrapT,t.flipY=e.flipY,t.anisotropy=e.anisotropy,t.mipmaps||(t.generateMipmaps=e.generateMipmaps),t):e}};c(E,"registerTexture",(r,e,t,o,s)=>{if(L&&console.log("> Progressive: register texture",o,e.name,e.uuid,e,s),!e){L&&console.error("gltf-progressive: Register texture without texture");return}e.source&&(e.source[Se]=s);const i=s.guid;E.assignLODInformation(r,e,i,t,o),E.lodInfos.set(i,s),E.lowresCache.set(i,e)}),c(E,"registerMesh",(r,e,t,o,s,i)=>{var n;const a=t.geometry;if(!a){L&&console.warn("gltf-progressive: Register mesh without geometry");return}a.userData||(a.userData={}),L&&console.log("> Progressive: register mesh "+t.name,{index:s,uuid:t.uuid},i,t),E.assignLODInformation(r,a,e,o,s),E.lodInfos.set(e,i);let l=E.lowresCache.get(e);l?l.push(t.geometry):l=[t.geometry],E.lowresCache.set(e,l),o>0&&!te(t)&&ct(t,a);for(const u of X)(n=u.onRegisteredNewMesh)==null||n.call(u,t,i)}),c(E,"lodInfos",new Map),c(E,"previouslyLoaded",new Map),c(E,"lowresCache",new Map);let z=E;class ft{constructor(e,t,o,s){c(this,"url"),c(this,"key"),c(this,"level"),c(this,"index"),this.url=e,this.key=t,this.level=o,s!=null&&(this.index=s)}}class fe{constructor(e,t){c(this,"frame_start"),c(this,"frame_capture_end"),c(this,"ready"),c(this,"_resolve"),c(this,"_signal"),c(this,"_resolved",!1),c(this,"_addedCount",0),c(this,"_resolvedCount",0),c(this,"_awaiting",[]),c(this,"_currentFrame",0);var o;const s=e===0?2:1,i=Math.max(t.frames??s,s);this.frame_start=e,this.frame_capture_end=e+i,this.ready=new Promise(n=>{this._resolve=n}),this.ready.finally(()=>{this._resolved=!0,this._awaiting.length=0}),this._signal=t.signal,(o=this._signal)==null||o.addEventListener("abort",()=>{this.resolveNow()})}get awaitedCount(){return this._addedCount}get resolvedCount(){return this._resolvedCount}get currentlyAwaiting(){return this._awaiting.length}update(e){var t;this._currentFrame=e,((t=this._signal)!=null&&t.aborted||this._currentFrame>this.frame_capture_end&&this._awaiting.length===0)&&this.resolveNow()}add(e,t){if(this._resolved){console.warn("PromiseGroup: Trying to add a promise to a resolved group, ignoring.");return}this._currentFrame>this.frame_capture_end||(this._awaiting.push(t),this._addedCount++,t.finally(()=>{this._resolvedCount++,this._awaiting.splice(this._awaiting.indexOf(t),1)}))}resolveNow(){var e,t;this._resolved||(t=this._resolve)==null||t.call(this,{awaited_count:this._addedCount,resolved_count:this._resolvedCount,cancelled:((e=this._signal)==null?void 0:e.aborted)??!1})}}c(fe,"addPromise",(r,e,t)=>{t.forEach(o=>{o.add(r,e)})});const G=se("debugprogressive"),mt=se("noprogressive"),Te=Symbol("Needle:LODSManager"),Pe=Symbol("Needle:LODState"),q=Symbol("Needle:CurrentLOD"),S={mesh_lod:-1,texture_lod:-1};var _,T,K,Ae,H,ne,me,Y;let ie=(_=class{constructor(r,e){c(this,"renderer"),c(this,"context"),c(this,"projectionScreenMatrix",new Ie),c(this,"targetTriangleDensity",2e5),c(this,"skinnedMeshAutoUpdateBoundsInterval",30),c(this,"updateInterval","auto"),J(this,T,1),c(this,"pause",!1),c(this,"manual",!1),c(this,"_newPromiseGroups",[]),c(this,"_promiseGroupIds",0),c(this,"_lodchangedlisteners",[]),J(this,K),J(this,Ae,new Je),J(this,H,0),J(this,ne,0),J(this,me,0),J(this,Y,0),c(this,"_fpsBuffer",[60,60,60,60,60]),c(this,"_sphere",new Ze),c(this,"_tempBox",new Be),c(this,"_tempBox2",new Be),c(this,"tempMatrix",new Ie),c(this,"_tempWorldPosition",new W),c(this,"_tempBoxSize",new W),c(this,"_tempBox2Size",new W),this.renderer=r,this.context={...e}}static getObjectLODState(r){return r[Pe]}static addPlugin(r){X.push(r)}static removePlugin(r){const e=X.indexOf(r);e>=0&&X.splice(e,1)}static get(r,e){if(r[Te])return console.debug("[gltf-progressive] LODsManager already exists for this renderer"),r[Te];const t=new _(r,{engine:"unknown",...e});return r[Te]=t,t}get plugins(){return X}awaitLoading(r){const e=this._promiseGroupIds++,t=new fe(v(this,H),{...r});this._newPromiseGroups.push(t);const o=performance.now();return t.ready.finally(()=>{const s=this._newPromiseGroups.indexOf(t);s>=0&&(this._newPromiseGroups.splice(s,1),ut()&&performance.measure("LODsManager:awaitLoading",{start:o,detail:{id:e,name:r?.name,awaited:t.awaitedCount,resolved:t.resolvedCount}}))}),t.ready}_postprocessPromiseGroups(){if(this._newPromiseGroups.length!==0)for(let r=this._newPromiseGroups.length-1;r>=0;r--)this._newPromiseGroups[r].update(v(this,H))}addEventListener(r,e){r==="changed"&&this._lodchangedlisteners.push(e)}removeEventListener(r,e){if(r==="changed"){const t=this._lodchangedlisteners.indexOf(e);t>=0&&this._lodchangedlisteners.splice(t,1)}}enable(){if(v(this,K))return;console.debug("[gltf-progressive] Enabling LODsManager for renderer");let r=0;F(this,K,this.renderer.render);const e=this;_e(this.renderer),this.renderer.render=function(t,o){const s=e.renderer.getRenderTarget();(s==null||"isXRRenderTarget"in s&&s.isXRRenderTarget)&&(r=0,F(e,H,v(e,H)+1),F(e,ne,v(e,Ae).getDelta()),F(e,me,v(e,me)+v(e,ne)),e._fpsBuffer.shift(),e._fpsBuffer.push(1/v(e,ne)),F(e,Y,e._fpsBuffer.reduce((n,a)=>n+a)/e._fpsBuffer.length),G&&v(e,H)%200===0&&console.log("FPS",Math.round(v(e,Y)),"Interval:",v(e,T)));const i=r++;v(e,K).call(this,t,o),e.onAfterRender(t,o,i)}}disable(){v(this,K)&&(console.debug("[gltf-progressive] Disabling LODsManager for renderer"),this.renderer.render=v(this,K),F(this,K,void 0))}update(r,e){this.internalUpdate(r,e)}onAfterRender(r,e,t){if(this.pause)return;const o=this.renderer.renderLists.get(r,0).opaque;let s=!0;if(o.length===1){const i=o[0].material;(i.name==="EffectMaterial"||i.name==="CopyShader")&&(s=!1)}if((e.parent&&e.parent.type==="CubeCamera"||t>=1&&e.type==="OrthographicCamera")&&(s=!1),s){if(mt||(this.updateInterval==="auto"?v(this,Y)<40&&v(this,T)<10?(F(this,T,v(this,T)+1),G&&console.warn("\u2193 Reducing LOD updates",v(this,T),v(this,Y).toFixed(0))):v(this,Y)>=60&&v(this,T)>1&&(F(this,T,v(this,T)-1),G&&console.warn("\u2191 Increasing LOD updates",v(this,T),v(this,Y).toFixed(0))):F(this,T,this.updateInterval),v(this,T)>0&&v(this,H)%v(this,T)!=0))return;this.internalUpdate(r,e),this._postprocessPromiseGroups()}}internalUpdate(r,e){var t,o;const s=this.renderer.renderLists.get(r,0),i=s.opaque;this.projectionScreenMatrix.multiplyMatrices(e.projectionMatrix,e.matrixWorldInverse);const n=this.targetTriangleDensity;for(const u of i){if(u.material&&(((t=u.geometry)==null?void 0:t.type)==="BoxGeometry"||((o=u.geometry)==null?void 0:o.type)==="BufferGeometry")&&(u.material.name==="SphericalGaussianBlur"||u.material.name=="BackgroundCubeMaterial"||u.material.name==="CubemapFromEquirect"||u.material.name==="EquirectangularToCubeUV")){G&&(u.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]||(u.material["NEEDLE_PROGRESSIVE:IGNORE-WARNING"]=!0,console.warn("Ignoring skybox or BLIT object",u,u.material.name,u.material.type)));continue}switch(u.material.type){case"LineBasicMaterial":case"LineDashedMaterial":case"PointsMaterial":case"ShadowMaterial":case"MeshDistanceMaterial":case"MeshDepthMaterial":continue}if(G==="color"&&u.material&&!u.object.progressive_debug_color){u.object.progressive_debug_color=!0;const w=Math.random()*16777215,y=new Qe({color:w});u.object.material=y}const d=u.object;(d instanceof Q||d.isMesh)&&this.updateLODs(r,e,d,n)}const a=s.transparent;for(const u of a){const d=u.object;(d instanceof Q||d.isMesh)&&this.updateLODs(r,e,d,n)}const l=s.transmissive;for(const u of l){const d=u.object;(d instanceof Q||d.isMesh)&&this.updateLODs(r,e,d,n)}}updateLODs(r,e,t,o){var s,i;t.userData||(t.userData={});let n=t[Pe];if(n||(n=new pt,t[Pe]=n),n.frames++<2)return;for(const l of X)(s=l.onBeforeUpdateLOD)==null||s.call(l,this.renderer,r,e,t);const a=_.overrideGlobalLodLevel!==void 0?_.overrideGlobalLodLevel:Z;a>=0?(S.mesh_lod=a,S.texture_lod=a):(this.calculateLodLevel(e,t,n,o,S),S.mesh_lod=Math.round(S.mesh_lod),S.texture_lod=Math.round(S.texture_lod)),S.mesh_lod>=0&&this.loadProgressiveMeshes(t,S.mesh_lod),t.material&&S.texture_lod>=0&&this.loadProgressiveTextures(t.material,S.texture_lod),L&&t.material&&!t.isGizmo&&$e(t.material);for(const l of X)(i=l.onAfterUpdatedLOD)==null||i.call(l,this.renderer,r,e,t,S);n.lastLodLevel_Mesh=S.mesh_lod,n.lastLodLevel_Texture=S.texture_lod}loadProgressiveTextures(r,e){if(!r)return;if(Array.isArray(r)){for(const s of r)this.loadProgressiveTextures(s,e);return}let t=!1;(r[q]===void 0||e<r[q])&&(t=!0);const o=r["DEBUG:LOD"];if(o!=null&&(t=r[q]!=o,e=o),t){r[q]=e;const s=z.assignTextureLOD(r,e).then(i=>{this._lodchangedlisteners.forEach(n=>n({type:"texture",level:e,object:r}))});fe.addPromise("texture",s,this._newPromiseGroups)}}loadProgressiveMeshes(r,e){if(!r)return Promise.resolve(null);let t=r[q]!==e;const o=r["DEBUG:LOD"];if(o!=null&&(t=r[q]!=o,e=o),t){r[q]=e;const s=r.geometry,i=z.assignMeshLOD(r,e).then(n=>(n&&r[q]==e&&s!=r.geometry&&this._lodchangedlisteners.forEach(a=>a({type:"mesh",level:e,object:r})),n));return fe.addPromise("mesh",i,this._newPromiseGroups),i}return Promise.resolve(null)}static isInside(r,e){const t=r.min,o=r.max,s=(t.x+o.x)*.5,i=(t.y+o.y)*.5;return this._tempPtInside.set(s,i,t.z).applyMatrix4(e).z<0}calculateLodLevel(r,e,t,o,s){var i,n,a;if(!e){s.mesh_lod=-1,s.texture_lod=-1;return}if(!r){s.mesh_lod=-1,s.texture_lod=-1;return}let l=10+1,u=!1;if(G&&e["DEBUG:LOD"]!=null)return e["DEBUG:LOD"];const d=(i=z.getMeshLODExtension(e.geometry))==null?void 0:i.lods,w=z.getPrimitiveIndex(e.geometry),y=d&&d.length>0,m=z.getMaterialMinMaxLODsCount(e.material),R=m?.min_count!=1/0&&m.min_count>0&&m.max_count>0;if(!y&&!R){s.mesh_lod=0,s.texture_lod=0;return}y||(u=!0,l=0);const D=this.renderer.domElement.clientHeight||this.renderer.domElement.height;let C=e.geometry.boundingBox;if(e.type==="SkinnedMesh"){const g=e;if(!g.boundingBox)g.computeBoundingBox();else if(this.skinnedMeshAutoUpdateBoundsInterval>0&&t.frames%this.skinnedMeshAutoUpdateBoundsInterval===0){const p=te(g),I=g.geometry;p&&(g.geometry=p),g.computeBoundingBox(),g.geometry=I}C=g.boundingBox}if(C){const g=r;if(e.geometry.attributes.color&&e.geometry.attributes.color.count<100&&e.geometry.boundingSphere){this._sphere.copy(e.geometry.boundingSphere),this._sphere.applyMatrix4(e.matrixWorld);const f=r.getWorldPosition(this._tempWorldPosition);if(this._sphere.containsPoint(f)){s.mesh_lod=0,s.texture_lod=0;return}}if(this._tempBox.copy(C),this._tempBox.applyMatrix4(e.matrixWorld),g.isPerspectiveCamera&&_.isInside(this._tempBox,this.projectionScreenMatrix)){s.mesh_lod=0,s.texture_lod=0;return}if(this._tempBox.applyMatrix4(this.projectionScreenMatrix),this.renderer.xr.enabled&&g.isPerspectiveCamera&&g.fov>70){const f=this._tempBox.min,h=this._tempBox.max;let x=f.x,O=f.y,P=h.x,V=h.y;const ae=2,ye=1.5,le=(f.x+h.x)*.5,ue=(f.y+h.y)*.5;x=(x-le)*ae+le,O=(O-ue)*ae+ue,P=(P-le)*ae+le,V=(V-ue)*ae+ue;const Xe=x<0&&P>0?0:Math.min(Math.abs(f.x),Math.abs(h.x)),qe=O<0&&V>0?0:Math.min(Math.abs(f.y),Math.abs(h.y)),xe=Math.max(Xe,qe);t.lastCentrality=(ye-xe)*(ye-xe)*(ye-xe)}else t.lastCentrality=1;const p=this._tempBox.getSize(this._tempBoxSize);p.multiplyScalar(.5),screen.availHeight>0&&D>0&&p.multiplyScalar(D/screen.availHeight),r.isPerspectiveCamera?p.x*=r.aspect:r.isOrthographicCamera;const I=r.matrixWorldInverse,j=this._tempBox2;j.copy(C),j.applyMatrix4(e.matrixWorld),j.applyMatrix4(I);const M=j.getSize(this._tempBox2Size),N=Math.max(M.x,M.y);if(Math.max(p.x,p.y)!=0&&N!=0&&(p.z=M.z/Math.max(M.x,M.y)*Math.max(p.x,p.y)),t.lastScreenCoverage=Math.max(p.x,p.y,p.z),t.lastScreenspaceVolume.copy(p),t.lastScreenCoverage*=t.lastCentrality,G&&_.debugDrawLine){const f=this.tempMatrix.copy(this.projectionScreenMatrix);f.invert();const h=_.corner0,x=_.corner1,O=_.corner2,P=_.corner3;h.copy(this._tempBox.min),x.copy(this._tempBox.max),x.x=h.x,O.copy(this._tempBox.max),O.y=h.y,P.copy(this._tempBox.max);const V=(h.z+P.z)*.5;h.z=x.z=O.z=P.z=V,h.applyMatrix4(f),x.applyMatrix4(f),O.applyMatrix4(f),P.applyMatrix4(f),_.debugDrawLine(h,x,255),_.debugDrawLine(h,O,255),_.debugDrawLine(x,P,255),_.debugDrawLine(O,P,255)}let b=999;if(d&&t.lastScreenCoverage>0)for(let f=0;f<d.length;f++){const h=d[f];if((((n=h.densities)==null?void 0:n[w])||h.density||1e-5)/t.lastScreenCoverage<o){b=f;break}}b<l&&(l=b,u=!0)}if(u?s.mesh_lod=l:s.mesh_lod=t.lastLodLevel_Mesh,G&&s.mesh_lod!=t.lastLodLevel_Mesh){const g=d?.[s.mesh_lod];g&&console.log(`Mesh LOD changed: ${t.lastLodLevel_Mesh} \u2192 ${s.mesh_lod} (${g.density.toFixed(0)}) - ${e.name}`)}if(R){const g="saveData"in globalThis.navigator&&globalThis.navigator.saveData===!0;if(t.lastLodLevel_Texture<0){if(s.texture_lod=m.max_count-1,G){const p=m.lods[m.max_count-1];G&&console.log(`First Texture LOD ${s.texture_lod} (${p.max_height}px) - ${e.name}`)}}else{const p=t.lastScreenspaceVolume.x+t.lastScreenspaceVolume.y+t.lastScreenspaceVolume.z;let I=t.lastScreenCoverage*4;((a=this.context)==null?void 0:a.engine)==="model-viewer"&&(I*=1.5);const j=D/window.devicePixelRatio*I;let M=!1;for(let N=m.lods.length-1;N>=0;N--){const b=m.lods[N];if(!(g&&b.max_height>=2048)&&!(lt()&&b.max_height>4096)&&(b.max_height>j||!M&&N===0)){if(M=!0,s.texture_lod=N,s.texture_lod<t.lastLodLevel_Texture){const f=b.max_height;G&&console.log(`Texture LOD changed: ${t.lastLodLevel_Texture} \u2192 ${s.texture_lod} = ${f}px
|
|
6
|
-
Screensize: ${j.toFixed(0)}px, Coverage: ${(100*t.lastScreenCoverage).toFixed(2)}%, Volume ${p.toFixed(1)}
|
|
7
|
-
${e.name}`)}break}}}}else s.texture_lod=0}},T=new WeakMap,K=new WeakMap,Ae=new WeakMap,H=new WeakMap,ne=new WeakMap,me=new WeakMap,Y=new WeakMap,c(_,"debugDrawLine"),c(_,"overrideGlobalLodLevel"),c(_,"corner0",new W),c(_,"corner1",new W),c(_,"corner2",new W),c(_,"corner3",new W),c(_,"_tempPtInside",new W),_);class pt{constructor(){c(this,"frames",0),c(this,"lastLodLevel_Mesh",-1),c(this,"lastLodLevel_Texture",-1),c(this,"lastScreenCoverage",0),c(this,"lastScreenspaceVolume",new W),c(this,"lastCentrality",0)}}const Fe=Symbol("NEEDLE_mesh_lod"),pe=Symbol("NEEDLE_texture_lod");let ve=null;function We(){const r=vt();r&&(r.mapURLs(function(e){return Ue(),e}),Ue(),ve?.disconnect(),ve=new MutationObserver(e=>{e.forEach(t=>{t.addedNodes.forEach(o=>{o instanceof HTMLElement&&o.tagName.toLowerCase()==="model-viewer"&&Ve(o)})})}),ve.observe(document,{childList:!0,subtree:!0}))}function vt(){return typeof customElements>"u"?null:customElements.get("model-viewer")||(customElements.whenDefined("model-viewer").then(()=>{console.debug("[gltf-progressive] model-viewer defined"),We()}),null)}function Ue(){typeof document>"u"||document.querySelectorAll("model-viewer").forEach(r=>{Ve(r)})}const ze=new WeakSet;let yt=0;function Ve(r){if(!r||ze.has(r))return null;ze.add(r),console.debug("[gltf-progressive] found new model-viewer..."+ ++yt+`
|
|
8
|
-
`,r.getAttribute("src"));let e=null,t=null,o=null;for(let s=r;s!=null;s=Object.getPrototypeOf(s)){const i=Object.getOwnPropertySymbols(s),n=i.find(u=>u.toString()=="Symbol(renderer)"),a=i.find(u=>u.toString()=="Symbol(scene)"),l=i.find(u=>u.toString()=="Symbol(needsRender)");!e&&n!=null&&(e=r[n].threeRenderer),!t&&a!=null&&(t=r[a]),!o&&l!=null&&(o=r[l])}if(e&&t){let s=function(){if(o){let n=0,a=setInterval(()=>{if(n++>5){clearInterval(a);return}o?.call(r)},300)}};console.debug("[gltf-progressive] setup model-viewer");const i=ie.get(e,{engine:"model-viewer"});return ie.addPlugin(new xt),i.enable(),i.addEventListener("changed",()=>{o?.call(r)}),r.addEventListener("model-visibility",n=>{n.detail.visible&&o?.call(r)}),r.addEventListener("load",()=>{s()}),()=>{i.disable()}}return null}class xt{constructor(){c(this,"_didWarnAboutMissingUrl",!1)}onBeforeUpdateLOD(e,t,o,s){this.tryParseMeshLOD(t,s),this.tryParseTextureLOD(t,s)}getUrl(e){if(!e)return null;let t=e.getAttribute("src");return t||(t=e.src),t||(this._didWarnAboutMissingUrl||console.warn("No url found in modelviewer",e),this._didWarnAboutMissingUrl=!0),t}tryGetCurrentGLTF(e){return e._currentGLTF}tryGetCurrentModelViewer(e){return e.element}tryParseTextureLOD(e,t){if(t[pe]==!0)return;t[pe]=!0;const o=this.tryGetCurrentGLTF(e),s=this.tryGetCurrentModelViewer(e),i=this.getUrl(s);if(i&&o&&t.material){let n=function(l){var u,d,w;if(l[pe]==!0)return;l[pe]=!0,l.userData&&(l.userData.LOD=-1);const y=Object.keys(l);for(let m=0;m<y.length;m++){const R=y[m],D=l[R];if(D?.isTexture===!0){const C=(d=(u=D.userData)==null?void 0:u.associations)==null?void 0:d.textures;if(C==null)continue;const g=o.parser.json.textures[C];if(!g){console.warn("Texture data not found for texture index "+C);continue}if((w=g?.extensions)!=null&&w[U]){const p=g.extensions[U];p&&i&&z.registerTexture(i,D,p.lods.length,C,p)}}}};const a=t.material;if(Array.isArray(a))for(const l of a)n(l);else n(a)}}tryParseMeshLOD(e,t){var o,s;if(t[Fe]==!0)return;t[Fe]=!0;const i=this.tryGetCurrentModelViewer(e),n=this.getUrl(i);if(!n)return;const a=(s=(o=t.userData)==null?void 0:o.gltfExtensions)==null?void 0:s[U];if(a&&n){const l=t.uuid;z.registerMesh(n,l,t,0,a.lods.length,a)}}}function Lt(r,e,t,o){_e(e),De(t),be(t,{progressive:!0,...o?.hints}),t.register(i=>new z(i,r));const s=ie.get(e);return o?.enableLODsManager!==!1&&s.enable(),s}if(We(),!dt){const r={gltfProgressive:{useNeedleProgressive:Lt,LODsManager:ie,configureLoader:be,getRaycastMesh:te,useRaycastMeshes:ht}};if(!globalThis.Needle)globalThis.Needle=r;else for(const e in r)globalThis.Needle[e]=r[e]}export{ie as LODsManager,z as NEEDLE_progressive,De as addDracoAndKTX2Loaders,be as configureLoader,_e as createLoaders,te as getRaycastMesh,ke as setDracoDecoderLocation,Re as setKTX2TranscoderLocation};
|