@needle-tools/gltf-progressive 1.0.0-alpha → 1.0.0-alpha.10
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 +24 -0
- package/README.md +10 -0
- package/gltf-progressive.js +679 -0
- package/gltf-progressive.min.js +3 -0
- package/gltf-progressive.umd.cjs +3 -0
- package/lib/extension.d.ts +109 -0
- package/{src/extension.ts → lib/extension.js} +674 -768
- package/lib/index.d.ts +26 -0
- package/lib/index.js +37 -0
- package/lib/loaders.d.ts +4 -0
- package/lib/loaders.js +35 -0
- package/lib/lods_manager.d.ts +70 -0
- package/{src/lods_manager.ts → lib/lods_manager.js} +381 -387
- package/lib/plugins/index.d.ts +2 -0
- package/lib/plugins/index.js +2 -0
- package/lib/plugins/modelviewer.d.ts +4 -0
- package/lib/plugins/modelviewer.js +134 -0
- package/lib/plugins/plugin.d.ts +24 -0
- package/lib/plugins/plugin.js +11 -0
- package/lib/utils.d.ts +2 -0
- package/{src/utils.ts → lib/utils.js} +36 -41
- package/package.json +31 -11
- package/examples/@needle-tools-gltf-progressive.min.js +0 -3
- package/examples/modelviewer.html +0 -27
- package/src/index.ts +0 -14
- package/src/loaders.ts +0 -49
- package/src/plugins/index.ts +0 -2
- package/src/plugins/modelviewer.ts +0 -113
- package/src/plugins/plugin.ts +0 -21
- package/tsconfig.json +0 -33
- package/vite.config.ts +0 -69
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from "./extension.js";
|
|
2
|
+
export * from "./plugins/index.js";
|
|
3
|
+
export { LODsManager } from "./lods_manager.js";
|
|
4
|
+
import { WebGLRenderer } from "three";
|
|
5
|
+
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
6
|
+
import { LODsManager } from "./lods_manager.js";
|
|
7
|
+
declare class UseNeedleGLTFProgressive {
|
|
8
|
+
/** */
|
|
9
|
+
enableLODsManager?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/** Use this function to enable progressive loading of gltf models.
|
|
12
|
+
* @param url The url of the gltf model.
|
|
13
|
+
* @param renderer The renderer of the scene.
|
|
14
|
+
* @param loader The gltf loader.
|
|
15
|
+
* @param opts Options.
|
|
16
|
+
* @returns The LODsManager instance.
|
|
17
|
+
* @example In react-three-fiber:
|
|
18
|
+
* ```ts
|
|
19
|
+
* const url = 'https://yourdomain.com/yourmodel.glb'
|
|
20
|
+
* const { scene } = useGLTF(url, false, false, (loader) => {
|
|
21
|
+
* useNeedleGLTFProgressive(url, gl, loader)
|
|
22
|
+
* })
|
|
23
|
+
* return <primitive object={scene} />
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function useNeedleProgressive(url: string, renderer: WebGLRenderer, loader: GLTFLoader, opts?: UseNeedleGLTFProgressive): LODsManager;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export * from "./extension.js";
|
|
2
|
+
export * from "./plugins/index.js";
|
|
3
|
+
export { LODsManager } from "./lods_manager.js";
|
|
4
|
+
import { addDracoAndKTX2Loaders, createLoaders } from "./loaders.js";
|
|
5
|
+
import { NEEDLE_progressive } from "./extension.js";
|
|
6
|
+
import { LODsManager } from "./lods_manager.js";
|
|
7
|
+
;
|
|
8
|
+
/** Use this function to enable progressive loading of gltf models.
|
|
9
|
+
* @param url The url of the gltf model.
|
|
10
|
+
* @param renderer The renderer of the scene.
|
|
11
|
+
* @param loader The gltf loader.
|
|
12
|
+
* @param opts Options.
|
|
13
|
+
* @returns The LODsManager instance.
|
|
14
|
+
* @example In react-three-fiber:
|
|
15
|
+
* ```ts
|
|
16
|
+
* const url = 'https://yourdomain.com/yourmodel.glb'
|
|
17
|
+
* const { scene } = useGLTF(url, false, false, (loader) => {
|
|
18
|
+
* useNeedleGLTFProgressive(url, gl, loader)
|
|
19
|
+
* })
|
|
20
|
+
* return <primitive object={scene} />
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function useNeedleProgressive(url, renderer, loader, opts) {
|
|
24
|
+
createLoaders(renderer);
|
|
25
|
+
addDracoAndKTX2Loaders(loader);
|
|
26
|
+
loader.register(p => new NEEDLE_progressive(p, url));
|
|
27
|
+
const lod = new LODsManager(renderer);
|
|
28
|
+
if (opts?.enableLODsManager !== false) {
|
|
29
|
+
lod.enable();
|
|
30
|
+
}
|
|
31
|
+
return lod;
|
|
32
|
+
}
|
|
33
|
+
import { patchModelViewer } from "./plugins/modelviewer.js";
|
|
34
|
+
// Query once for model viewer. If a user does not have model-viewer in their page, this will return null.
|
|
35
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
36
|
+
patchModelViewer(document.querySelector("model-viewer"));
|
|
37
|
+
});
|
package/lib/loaders.d.ts
ADDED
package/lib/loaders.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { MeshoptDecoder } from 'three/examples/jsm/libs/meshopt_decoder.module.js';
|
|
2
|
+
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
|
|
3
|
+
import { KTX2Loader } from 'three/examples/jsm/loaders/KTX2Loader.js';
|
|
4
|
+
const DEFAULT_DRACO_DECODER_LOCATION = 'https://www.gstatic.com/draco/versioned/decoders/1.4.1/';
|
|
5
|
+
const DEFAULT_KTX2_TRANSCODER_LOCATION = 'https://www.gstatic.com/basis-universal/versioned/2021-04-15-ba1c3e4/';
|
|
6
|
+
let dracoLoader;
|
|
7
|
+
let meshoptDecoder;
|
|
8
|
+
let ktx2Loader;
|
|
9
|
+
export function createLoaders(renderer) {
|
|
10
|
+
if (!dracoLoader) {
|
|
11
|
+
dracoLoader = new DRACOLoader();
|
|
12
|
+
dracoLoader.setDecoderPath(DEFAULT_DRACO_DECODER_LOCATION);
|
|
13
|
+
dracoLoader.setDecoderConfig({ type: 'js' });
|
|
14
|
+
}
|
|
15
|
+
if (!ktx2Loader) {
|
|
16
|
+
ktx2Loader = new KTX2Loader();
|
|
17
|
+
ktx2Loader.setTranscoderPath(DEFAULT_KTX2_TRANSCODER_LOCATION);
|
|
18
|
+
}
|
|
19
|
+
if (!meshoptDecoder) {
|
|
20
|
+
meshoptDecoder = MeshoptDecoder;
|
|
21
|
+
}
|
|
22
|
+
if (renderer) {
|
|
23
|
+
ktx2Loader.detectSupport(renderer);
|
|
24
|
+
}
|
|
25
|
+
else
|
|
26
|
+
console.warn("No renderer provided to detect ktx2 support - loading KTX2 textures will probably fail");
|
|
27
|
+
}
|
|
28
|
+
export function addDracoAndKTX2Loaders(loader) {
|
|
29
|
+
if (!loader.dracoLoader)
|
|
30
|
+
loader.setDRACOLoader(dracoLoader);
|
|
31
|
+
if (!loader.ktx2Loader)
|
|
32
|
+
loader.setKTX2Loader(ktx2Loader);
|
|
33
|
+
if (!loader.meshoptDecoder)
|
|
34
|
+
loader.setMeshoptDecoder(meshoptDecoder);
|
|
35
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Frustum, Matrix4, Object3D, Vector3, WebGLRenderer } from "three";
|
|
2
|
+
import { NEEDLE_progressive_plugin } from "./plugins/plugin.js";
|
|
3
|
+
/**
|
|
4
|
+
* 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.
|
|
5
|
+
* It must be enabled by calling the `enable` method.
|
|
6
|
+
*
|
|
7
|
+
* Instead of using the LODs manager directly you can also call `useNeedleProgressive` to enable progressive loading for a GLTFLoader
|
|
8
|
+
*/
|
|
9
|
+
export declare class LODsManager {
|
|
10
|
+
/** 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.
|
|
11
|
+
*/
|
|
12
|
+
static debugDrawLine?: (a: Vector3, b: Vector3, color: number) => void;
|
|
13
|
+
/** @internal */
|
|
14
|
+
static getObjectLODState(object: Object3D): LOD_state | undefined;
|
|
15
|
+
readonly renderer: WebGLRenderer;
|
|
16
|
+
readonly projectionScreenMatrix: Matrix4;
|
|
17
|
+
readonly cameraFrustrum: Frustum;
|
|
18
|
+
/**
|
|
19
|
+
* The update interval in frames. If set to 0, the LODs will be updated every frame. If set to 1, the LODs will be updated every second frame, etc.
|
|
20
|
+
*/
|
|
21
|
+
updateInterval: number;
|
|
22
|
+
/**
|
|
23
|
+
* If set to true, the LODsManager will not update the LODs.
|
|
24
|
+
*/
|
|
25
|
+
pause: boolean;
|
|
26
|
+
readonly plugins: NEEDLE_progressive_plugin[];
|
|
27
|
+
constructor(renderer: WebGLRenderer);
|
|
28
|
+
private _frame;
|
|
29
|
+
private _originalRender?;
|
|
30
|
+
/**
|
|
31
|
+
* Enable the LODsManager. This will replace the render method of the renderer with a method that updates the LODs.
|
|
32
|
+
*/
|
|
33
|
+
enable(): void;
|
|
34
|
+
disable(): void;
|
|
35
|
+
private onBeforeRender;
|
|
36
|
+
private onAfterRender;
|
|
37
|
+
/** Update the LOD levels for the renderer. */
|
|
38
|
+
private updateLODs;
|
|
39
|
+
/** Load progressive textures for the given material
|
|
40
|
+
* @param material the material to load the textures for
|
|
41
|
+
* @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
|
|
42
|
+
* @returns Promise with true if the LOD was loaded, false if not
|
|
43
|
+
*/
|
|
44
|
+
private loadProgressiveTextures;
|
|
45
|
+
/** Load progressive meshes for the given mesh
|
|
46
|
+
* @param mesh the mesh to load the LOD for
|
|
47
|
+
* @param index the index of the mesh if it's part of a group
|
|
48
|
+
* @param level the LOD level to load. Level 0 is the best quality, higher levels are lower quality
|
|
49
|
+
* @returns Promise with true if the LOD was loaded, false if not
|
|
50
|
+
*/
|
|
51
|
+
private loadProgressiveMeshes;
|
|
52
|
+
private readonly _sphere;
|
|
53
|
+
private readonly _tempBox;
|
|
54
|
+
private readonly tempMatrix;
|
|
55
|
+
private readonly _tempWorldPosition;
|
|
56
|
+
private readonly _tempBoxSize;
|
|
57
|
+
private readonly _tempBox2Size;
|
|
58
|
+
private static corner0;
|
|
59
|
+
private static corner1;
|
|
60
|
+
private static corner2;
|
|
61
|
+
private static corner3;
|
|
62
|
+
private calculateLodLevel;
|
|
63
|
+
}
|
|
64
|
+
declare class LOD_state {
|
|
65
|
+
lastLodLevel: number;
|
|
66
|
+
lastScreenCoverage: number;
|
|
67
|
+
readonly lastScreenspaceVolume: Vector3;
|
|
68
|
+
lastCentrality: number;
|
|
69
|
+
}
|
|
70
|
+
export {};
|