@vctrl/hooks 0.9.5 → 0.16.2
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 +380 -0
- package/README.md +202 -383
- package/package.json +66 -55
- package/project.json +55 -0
- package/src/index.ts +38 -0
- package/src/use-export-model/use-export-model.ts +113 -0
- package/src/use-load-model/event-system.ts +46 -0
- package/src/use-load-model/index.ts +15 -0
- package/src/use-load-model/model-context.tsx +174 -0
- package/src/use-load-model/state.ts +75 -0
- package/src/use-load-model/types.ts +284 -0
- package/src/use-load-model/use-load-model.ts +682 -0
- package/src/use-load-model/utils/calculate-referenced-bytes.ts +123 -0
- package/src/use-load-model/utils/index.ts +7 -0
- package/src/use-load-model/utils/read-directory.ts +35 -0
- package/src/use-load-model/utils/reconstruct-files.ts +85 -0
- package/src/use-load-model/utils/resolve-scene-payload.ts +151 -0
- package/{use-optimize-model/index.d.ts → src/use-optimize-model/index.ts} +2 -1
- package/src/use-optimize-model/state.ts +40 -0
- package/src/use-optimize-model/types.ts +37 -0
- package/src/use-optimize-model/use-calc-optimization-info.ts +101 -0
- package/src/use-optimize-model/use-optimize-model.ts +467 -0
- package/src/use-optimize-model/utils/index.ts +29 -0
- package/src/use-optimize-model/utils/texture-optimization.ts +292 -0
- package/src/use-optimize-model/utils/validation.ts +62 -0
- package/src/utils/server-communication.ts +351 -0
- package/tsconfig.json +21 -0
- package/tsconfig.lib.json +16 -0
- package/vite.config.ts +77 -0
- package/GLTFExporter-BmLF430n.cjs +0 -18
- package/GLTFExporter-C4hCeb4H.js +0 -1263
- package/index.cjs.js +0 -1
- package/index.d.ts +0 -3
- package/index.es.js +0 -12
- package/model-context-CQJta_vi.cjs +0 -9
- package/model-context-D1Q8WdpT.js +0 -3431
- package/use-export-model/types.d.ts +0 -19
- package/use-export-model/use-export-model.d.ts +0 -14
- package/use-export-model/utils/data-uri-to-blob.d.ts +0 -9
- package/use-export-model/utils/export-handlers.d.ts +0 -23
- package/use-export-model/utils/file-helpers.d.ts +0 -17
- package/use-export-model/utils/index.d.ts +0 -3
- package/use-export-model-AFharGg5.js +0 -77
- package/use-export-model-CWcKp4A3.cjs +0 -1
- package/use-export-model.cjs.js +0 -1
- package/use-export-model.es.js +0 -4
- package/use-load-model/event-system.d.ts +0 -7
- package/use-load-model/file-type-hooks/index.d.ts +0 -2
- package/use-load-model/file-type-hooks/use-load-binary.d.ts +0 -11
- package/use-load-model/file-type-hooks/use-load-gltf.d.ts +0 -5
- package/use-load-model/index.d.ts +0 -3
- package/use-load-model/loaders/create-gltf-loader.d.ts +0 -3
- package/use-load-model/loaders/create-usdz-loader.d.ts +0 -3
- package/use-load-model/loaders/index.d.ts +0 -2
- package/use-load-model/model-context.d.ts +0 -78
- package/use-load-model/state.d.ts +0 -19
- package/use-load-model/types.d.ts +0 -51
- package/use-load-model/use-load-model.d.ts +0 -74
- package/use-load-model/utils/array-buffer-to-base64.d.ts +0 -2
- package/use-load-model/utils/index.d.ts +0 -2
- package/use-load-model/utils/read-directory.d.ts +0 -8
- package/use-load-model.cjs.js +0 -1
- package/use-load-model.es.js +0 -8
- package/use-optimize-model/state.d.ts +0 -13
- package/use-optimize-model/types.d.ts +0 -37
- package/use-optimize-model/use-optimize-model.d.ts +0 -29
- package/use-optimize-model-Cqkp__Vi.cjs +0 -1
- package/use-optimize-model-DZWIb_40.js +0 -127
- package/use-optimize-model.cjs.js +0 -1
- package/use-optimize-model.es.js +0 -4
- /package/{use-export-model/index.d.ts → src/use-export-model/index.ts} +0 -0
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export interface URIBufferObject {
|
|
2
|
-
uri: string;
|
|
3
|
-
byteLength: number;
|
|
4
|
-
}
|
|
5
|
-
export interface TexturesObject {
|
|
6
|
-
name: string;
|
|
7
|
-
sampler: number;
|
|
8
|
-
source: number;
|
|
9
|
-
}
|
|
10
|
-
export interface ImageDataObject {
|
|
11
|
-
data?: Uint8Array | ArrayBuffer;
|
|
12
|
-
uri?: string;
|
|
13
|
-
mimeType?: string;
|
|
14
|
-
}
|
|
15
|
-
export interface ExportResult {
|
|
16
|
-
buffers?: ArrayBuffer[] | URIBufferObject[];
|
|
17
|
-
images?: ImageDataObject[];
|
|
18
|
-
textures?: TexturesObject[];
|
|
19
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ModelFile } from '../use-load-model';
|
|
2
|
-
/**
|
|
3
|
-
* A React hook for exporting a 3D model from a Three.js scene.
|
|
4
|
-
*
|
|
5
|
-
* @param {Function} [onSaved] Called when the export is complete.
|
|
6
|
-
* @param {Function} [onError] Called when an error occurs during exporting.
|
|
7
|
-
* @returns An object with a single property, `handleGltfExport`. `handleGltfExport` is a function
|
|
8
|
-
* that takes a file object and a boolean indicating whether to export in binary format.
|
|
9
|
-
* It exports the scene in the format specified by the boolean parameter.
|
|
10
|
-
*/
|
|
11
|
-
declare const useExportModel: (onSaved?: () => void, onError?: (error: ErrorEvent) => void) => {
|
|
12
|
-
handleGltfExport: (file: ModelFile | null, binary: boolean) => void;
|
|
13
|
-
};
|
|
14
|
-
export default useExportModel;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { default as JSZip } from 'jszip';
|
|
2
|
-
import { ImageDataObject, TexturesObject, URIBufferObject } from '../types';
|
|
3
|
-
/**
|
|
4
|
-
* Handles processing and adding images to a zip file.
|
|
5
|
-
*
|
|
6
|
-
* @param images The images property of the GLTF model's asset.
|
|
7
|
-
* @param textures The textures property of the GLTF model's asset.
|
|
8
|
-
* @param zip The zip file to add images to.
|
|
9
|
-
*/
|
|
10
|
-
export declare const handleImages: (images: Array<ImageDataObject>, textures: Array<TexturesObject>, zip: JSZip) => void;
|
|
11
|
-
/**
|
|
12
|
-
* Handles processing and adding buffers to a zip file.
|
|
13
|
-
*
|
|
14
|
-
* @param buffers The buffers property of the GLTF model's asset.
|
|
15
|
-
* @param zip The zip file to add buffers to.
|
|
16
|
-
* @param baseFileName The base file name to use for the exported file.
|
|
17
|
-
*
|
|
18
|
-
* Only ArrayBuffers and URIBufferObjects with a uri of type data URL are supported.
|
|
19
|
-
* If the buffer is not an ArrayBuffer, it must have a uri that starts with 'data:'.
|
|
20
|
-
* If the buffer is not an ArrayBuffer and does not have a uri, a warning is logged
|
|
21
|
-
* to the console.
|
|
22
|
-
*/
|
|
23
|
-
export declare const handleBuffers: (buffers: Array<URIBufferObject | ArrayBuffer>, zip: JSZip, baseFileName: string) => void;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Determines the extension of a file based on its URI and MIME type.
|
|
3
|
-
*
|
|
4
|
-
* If the MIME type is not provided, it is inferred from the URI.
|
|
5
|
-
*
|
|
6
|
-
* Returns a default `png` extension if the extension is unknown.
|
|
7
|
-
*
|
|
8
|
-
* @param {string} uri - The URI of the image
|
|
9
|
-
* @param {string} mimeType - The MIME type of the image
|
|
10
|
-
*/
|
|
11
|
-
export declare function getFileExtension(uri: string, mimeType?: string): string;
|
|
12
|
-
/**
|
|
13
|
-
* Extracts the base name of a file from its URI.
|
|
14
|
-
*
|
|
15
|
-
* @param {string} filename - The name of the file
|
|
16
|
-
*/
|
|
17
|
-
export declare function getFileBasename(filename: string): string;
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import y from "jszip";
|
|
2
|
-
import { saveAs as u } from "file-saver";
|
|
3
|
-
import { G as m } from "./GLTFExporter-C4hCeb4H.js";
|
|
4
|
-
const l = (t) => {
|
|
5
|
-
const r = atob(t.split(",")[1]), o = t.split(",")[0].split(":")[1].split(";")[0], e = new ArrayBuffer(r.length), s = new Uint8Array(e);
|
|
6
|
-
for (let n = 0; n < r.length; n++)
|
|
7
|
-
s[n] = r.charCodeAt(n);
|
|
8
|
-
return new Blob([e], { type: o });
|
|
9
|
-
};
|
|
10
|
-
function g(t, r) {
|
|
11
|
-
var e;
|
|
12
|
-
const o = (e = t.split(".").pop()) == null ? void 0 : e.split("?")[0];
|
|
13
|
-
return o && o !== t ? o : r && r.split("/").pop() || "png";
|
|
14
|
-
}
|
|
15
|
-
function b(t) {
|
|
16
|
-
return (t == null ? void 0 : t.split(".").shift()) || "scene";
|
|
17
|
-
}
|
|
18
|
-
const d = (t, r, o) => {
|
|
19
|
-
r.forEach((e, s) => {
|
|
20
|
-
const n = t[e.source];
|
|
21
|
-
if (!n.uri) return;
|
|
22
|
-
const a = g(n.uri, n.mimeType), i = `${e.name || `texture_${s}`}.${a}`;
|
|
23
|
-
if (n.data instanceof ArrayBuffer || n.data instanceof Uint8Array) {
|
|
24
|
-
const f = n.data instanceof Uint8Array ? n.data.buffer : n.data, p = new Uint8Array(f);
|
|
25
|
-
o.file(i, p, { binary: !0 });
|
|
26
|
-
} else if (n.uri.startsWith("data:")) {
|
|
27
|
-
const f = l(n.uri);
|
|
28
|
-
o.file(i, f, { binary: !0 });
|
|
29
|
-
} else
|
|
30
|
-
console.warn(`Image at index ${s} has unsupported data format`);
|
|
31
|
-
});
|
|
32
|
-
}, x = (t, r, o) => {
|
|
33
|
-
t.forEach((e, s) => {
|
|
34
|
-
const n = `${o}.bin`;
|
|
35
|
-
if (e instanceof ArrayBuffer) {
|
|
36
|
-
const a = new Uint8Array(e);
|
|
37
|
-
r.file(n, a, { binary: !0 });
|
|
38
|
-
} else if (e.uri) {
|
|
39
|
-
const a = l(e.uri);
|
|
40
|
-
r.file(n, a, { binary: !0 });
|
|
41
|
-
} else
|
|
42
|
-
console.warn(
|
|
43
|
-
`Buffer at index ${s} is not an ArrayBuffer or does not have a URI`
|
|
44
|
-
);
|
|
45
|
-
});
|
|
46
|
-
}, A = (t, r) => {
|
|
47
|
-
const o = new y();
|
|
48
|
-
o.file(`${r}.gltf`, JSON.stringify(t, null, 2)), t.buffers && x(t.buffers, o, r), t.images && t.textures && d(t.images, t.textures, o), o.generateAsync({ type: "blob" }).then((e) => {
|
|
49
|
-
u(e, `${r}.zip`);
|
|
50
|
-
});
|
|
51
|
-
}, $ = (t, r) => ({
|
|
52
|
-
handleGltfExport: (e, s) => {
|
|
53
|
-
const n = e == null ? void 0 : e.model;
|
|
54
|
-
if (!n) {
|
|
55
|
-
console.error("Scene not initialized");
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const a = new m(), c = {
|
|
59
|
-
binary: s,
|
|
60
|
-
includeCustomExtensions: !0
|
|
61
|
-
};
|
|
62
|
-
a.parse(
|
|
63
|
-
n,
|
|
64
|
-
(i) => {
|
|
65
|
-
const f = b(e.name);
|
|
66
|
-
i instanceof ArrayBuffer ? (u(new Blob([i]), `${f}.glb`), t && t()) : (A(i, f), t && t());
|
|
67
|
-
},
|
|
68
|
-
(i) => {
|
|
69
|
-
console.error(i), r && r(i);
|
|
70
|
-
},
|
|
71
|
-
c
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
export {
|
|
76
|
-
$ as u
|
|
77
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const y=require("jszip"),u=require("file-saver"),d=require("./GLTFExporter-BmLF430n.cjs"),l=t=>{const r=atob(t.split(",")[1]),s=t.split(",")[0].split(":")[1].split(";")[0],n=new ArrayBuffer(r.length),o=new Uint8Array(n);for(let e=0;e<r.length;e++)o[e]=r.charCodeAt(e);return new Blob([n],{type:s})};function g(t,r){var n;const s=(n=t.split(".").pop())==null?void 0:n.split("?")[0];return s&&s!==t?s:r&&r.split("/").pop()||"png"}function b(t){return(t==null?void 0:t.split(".").shift())||"scene"}const x=(t,r,s)=>{r.forEach((n,o)=>{const e=t[n.source];if(!e.uri)return;const a=g(e.uri,e.mimeType),i=`${n.name||`texture_${o}`}.${a}`;if(e.data instanceof ArrayBuffer||e.data instanceof Uint8Array){const c=e.data instanceof Uint8Array?e.data.buffer:e.data,p=new Uint8Array(c);s.file(i,p,{binary:!0})}else if(e.uri.startsWith("data:")){const c=l(e.uri);s.file(i,c,{binary:!0})}else console.warn(`Image at index ${o} has unsupported data format`)})},A=(t,r,s)=>{t.forEach((n,o)=>{const e=`${s}.bin`;if(n instanceof ArrayBuffer){const a=new Uint8Array(n);r.file(e,a,{binary:!0})}else if(n.uri){const a=l(n.uri);r.file(e,a,{binary:!0})}else console.warn(`Buffer at index ${o} is not an ArrayBuffer or does not have a URI`)})},h=(t,r)=>{const s=new y;s.file(`${r}.gltf`,JSON.stringify(t,null,2)),t.buffers&&A(t.buffers,s,r),t.images&&t.textures&&x(t.images,t.textures,s),s.generateAsync({type:"blob"}).then(n=>{u.saveAs(n,`${r}.zip`)})},m=(t,r)=>({handleGltfExport:(n,o)=>{const e=n==null?void 0:n.model;if(!e){console.error("Scene not initialized");return}const a=new d.GLTFExporter,f={binary:o,includeCustomExtensions:!0};a.parse(e,i=>{const c=b(n.name);i instanceof ArrayBuffer?(u.saveAs(new Blob([i]),`${c}.glb`),t&&t()):(h(i,c),t&&t())},i=>{console.error(i),r&&r(i)},f)}});exports.useExportModel=m;
|
package/use-export-model.cjs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./use-export-model-CWcKp4A3.cjs");exports.useExportModel=e.useExportModel;
|
package/use-export-model.es.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { EventData, EventHandler, EventTypes } from './types';
|
|
2
|
-
declare const eventSystem: {
|
|
3
|
-
emit<T extends EventTypes>(event: T, data?: EventData[T]): void;
|
|
4
|
-
on<T extends EventTypes>(event: T, handler?: EventHandler<T>): void;
|
|
5
|
-
off<T extends EventTypes>(event: T, handler?: EventHandler<T>): void;
|
|
6
|
-
};
|
|
7
|
-
export default eventSystem;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Action, ModelFileTypes } from '../types';
|
|
2
|
-
/**
|
|
3
|
-
* Hook to load binary files
|
|
4
|
-
*
|
|
5
|
-
* @param dispatch - The dispatch function from the useReducer hook
|
|
6
|
-
* @returns An object containing the loadBinary function
|
|
7
|
-
*/
|
|
8
|
-
declare function useLoadBinary(dispatch: React.Dispatch<Action>): {
|
|
9
|
-
loadBinary: (file: File, fileType: ModelFileTypes, onProgress: () => void) => void;
|
|
10
|
-
};
|
|
11
|
-
export default useLoadBinary;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { useOptimizeModel } from '../use-optimize-model';
|
|
2
|
-
interface ModelProviderProps extends React.PropsWithChildren {
|
|
3
|
-
optimizer?: ReturnType<typeof useOptimizeModel>;
|
|
4
|
-
}
|
|
5
|
-
declare const ModelContext: import('react').Context<{
|
|
6
|
-
on: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
7
|
-
off: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
8
|
-
load: (filesOrDirectories: import('./types').InputFileOrDirectory) => Promise<void>;
|
|
9
|
-
reset: () => void;
|
|
10
|
-
optimize: {
|
|
11
|
-
simplifyOptimization: () => void;
|
|
12
|
-
dedupOptimization: () => void;
|
|
13
|
-
quantizeOptimization: () => void;
|
|
14
|
-
normalsOptimization: () => void;
|
|
15
|
-
texturesCompressOptimization: () => void;
|
|
16
|
-
getSize: () => void;
|
|
17
|
-
reset: () => void;
|
|
18
|
-
report: null;
|
|
19
|
-
error: null;
|
|
20
|
-
loading: boolean;
|
|
21
|
-
} | {
|
|
22
|
-
simplifyOptimization: (options?: {
|
|
23
|
-
ratio?: number;
|
|
24
|
-
error?: number;
|
|
25
|
-
} | undefined) => Promise<void>;
|
|
26
|
-
dedupOptimization: (options?: import('@gltf-transform/functions').DedupOptions | undefined) => Promise<void>;
|
|
27
|
-
quantizeOptimization: (options?: import('@gltf-transform/functions').QuantizeOptions | undefined) => Promise<void>;
|
|
28
|
-
normalsOptimization: (options?: import('@gltf-transform/functions').NormalsOptions | undefined) => Promise<void>;
|
|
29
|
-
texturesCompressOptimization: (options?: import('@gltf-transform/functions').TextureCompressOptions | undefined) => Promise<void>;
|
|
30
|
-
getSize: () => import('../use-optimize-model/types').ModelSize | null;
|
|
31
|
-
reset: () => void;
|
|
32
|
-
report: import('@gltf-transform/functions').InspectReport | null;
|
|
33
|
-
error: Error | null;
|
|
34
|
-
loading: boolean;
|
|
35
|
-
};
|
|
36
|
-
file: import('./types').ModelFile | null;
|
|
37
|
-
isFileLoading: boolean;
|
|
38
|
-
progress: number;
|
|
39
|
-
supportedFileTypes: import('./types').ModelFileTypes[];
|
|
40
|
-
}>;
|
|
41
|
-
declare const ModelProvider: ({ children, optimizer }: ModelProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
42
|
-
declare const useModelContext: () => {
|
|
43
|
-
on: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
44
|
-
off: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
45
|
-
load: (filesOrDirectories: import('./types').InputFileOrDirectory) => Promise<void>;
|
|
46
|
-
reset: () => void;
|
|
47
|
-
optimize: {
|
|
48
|
-
simplifyOptimization: () => void;
|
|
49
|
-
dedupOptimization: () => void;
|
|
50
|
-
quantizeOptimization: () => void;
|
|
51
|
-
normalsOptimization: () => void;
|
|
52
|
-
texturesCompressOptimization: () => void;
|
|
53
|
-
getSize: () => void;
|
|
54
|
-
reset: () => void;
|
|
55
|
-
report: null;
|
|
56
|
-
error: null;
|
|
57
|
-
loading: boolean;
|
|
58
|
-
} | {
|
|
59
|
-
simplifyOptimization: (options?: {
|
|
60
|
-
ratio?: number;
|
|
61
|
-
error?: number;
|
|
62
|
-
} | undefined) => Promise<void>;
|
|
63
|
-
dedupOptimization: (options?: import('@gltf-transform/functions').DedupOptions | undefined) => Promise<void>;
|
|
64
|
-
quantizeOptimization: (options?: import('@gltf-transform/functions').QuantizeOptions | undefined) => Promise<void>;
|
|
65
|
-
normalsOptimization: (options?: import('@gltf-transform/functions').NormalsOptions | undefined) => Promise<void>;
|
|
66
|
-
texturesCompressOptimization: (options?: import('@gltf-transform/functions').TextureCompressOptions | undefined) => Promise<void>;
|
|
67
|
-
getSize: () => import('../use-optimize-model/types').ModelSize | null;
|
|
68
|
-
reset: () => void;
|
|
69
|
-
report: import('@gltf-transform/functions').InspectReport | null;
|
|
70
|
-
error: Error | null;
|
|
71
|
-
loading: boolean;
|
|
72
|
-
};
|
|
73
|
-
file: import('./types').ModelFile | null;
|
|
74
|
-
isFileLoading: boolean;
|
|
75
|
-
progress: number;
|
|
76
|
-
supportedFileTypes: import('./types').ModelFileTypes[];
|
|
77
|
-
};
|
|
78
|
-
export { ModelContext, ModelProvider, useModelContext };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Action, State } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Initial state for the useReadModelFiles hook
|
|
4
|
-
*
|
|
5
|
-
* @property file - Currently loaded file
|
|
6
|
-
* @property isFileLoading - Flag indicating if a file is currently being loaded
|
|
7
|
-
* @property progress - Current loading progress (0-100)
|
|
8
|
-
* @property supportedFileTypes - List of supported file types
|
|
9
|
-
*/
|
|
10
|
-
export declare const initialState: State;
|
|
11
|
-
/**
|
|
12
|
-
* Reducer function for the useReadModelFiles hook
|
|
13
|
-
*
|
|
14
|
-
* @param state - The current state of the reducer
|
|
15
|
-
* @param action - The action to be performed
|
|
16
|
-
* @returns The updated state
|
|
17
|
-
*/
|
|
18
|
-
declare function reducer(state: State, action: Action): State;
|
|
19
|
-
export default reducer;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Object3D } from 'three';
|
|
2
|
-
export declare enum ModelFileTypes {
|
|
3
|
-
gltf = "gltf",
|
|
4
|
-
glb = "glb",
|
|
5
|
-
usdz = "usdz"
|
|
6
|
-
}
|
|
7
|
-
export type InputFileOrDirectory = (File | FileSystemDirectoryHandle)[];
|
|
8
|
-
export interface ReducedGltf {
|
|
9
|
-
images: {
|
|
10
|
-
name: string;
|
|
11
|
-
uri: string;
|
|
12
|
-
}[];
|
|
13
|
-
buffers: {
|
|
14
|
-
bytelength: string;
|
|
15
|
-
uri: string;
|
|
16
|
-
}[];
|
|
17
|
-
}
|
|
18
|
-
export interface ModelFile {
|
|
19
|
-
model: Object3D;
|
|
20
|
-
type: ModelFileTypes;
|
|
21
|
-
name: string;
|
|
22
|
-
}
|
|
23
|
-
export interface State {
|
|
24
|
-
file: ModelFile | null;
|
|
25
|
-
isFileLoading: boolean;
|
|
26
|
-
progress: number;
|
|
27
|
-
supportedFileTypes: ModelFileTypes[];
|
|
28
|
-
}
|
|
29
|
-
export type Action = {
|
|
30
|
-
type: 'set-file';
|
|
31
|
-
payload: ModelFile;
|
|
32
|
-
} | {
|
|
33
|
-
type: 'set-file-loading';
|
|
34
|
-
payload: boolean;
|
|
35
|
-
} | {
|
|
36
|
-
type: 'set-progress';
|
|
37
|
-
payload: number;
|
|
38
|
-
} | {
|
|
39
|
-
type: 'reset-state';
|
|
40
|
-
};
|
|
41
|
-
export type EventTypes = 'multiple-models' | 'not-loaded-files' | 'load-start' | 'load-progress' | 'load-complete' | 'load-reset' | 'load-error';
|
|
42
|
-
export type EventData = {
|
|
43
|
-
'multiple-models': File[];
|
|
44
|
-
'not-loaded-files': File[];
|
|
45
|
-
'load-start': null;
|
|
46
|
-
'load-progress': number;
|
|
47
|
-
'load-complete': State['file'];
|
|
48
|
-
'load-reset': null;
|
|
49
|
-
'load-error': Error | unknown;
|
|
50
|
-
};
|
|
51
|
-
export type EventHandler<T extends EventTypes> = (data?: EventData[T]) => void;
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { useOptimizeModel } from '../use-optimize-model';
|
|
2
|
-
import { ModelFileTypes, InputFileOrDirectory, ModelFile } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Custom hook to load and manage 3D models, integrating optimization functionalities.
|
|
5
|
-
*
|
|
6
|
-
* @param optimizer - Optional optimizer hook returned from useOptimizeModel.
|
|
7
|
-
* @returns An object containing the state, event handlers, and optimization functions.
|
|
8
|
-
*/
|
|
9
|
-
declare function useLoadModel(optimizer?: ReturnType<typeof useOptimizeModel>): {
|
|
10
|
-
/**
|
|
11
|
-
* Add an event listener to the event system.
|
|
12
|
-
*/
|
|
13
|
-
on: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
14
|
-
/**
|
|
15
|
-
* Remove an event listener from the event system.
|
|
16
|
-
*/
|
|
17
|
-
off: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
18
|
-
/**
|
|
19
|
-
* Load a model and integrate it with the optimizer if available.
|
|
20
|
-
* This also makes the model available in the use-model-context hook.
|
|
21
|
-
*/
|
|
22
|
-
load: (filesOrDirectories: InputFileOrDirectory) => Promise<void>;
|
|
23
|
-
/**
|
|
24
|
-
* Reset the model and optimizer state to the initial state.
|
|
25
|
-
*/
|
|
26
|
-
reset: () => void;
|
|
27
|
-
/**
|
|
28
|
-
* Integration of the optimizer
|
|
29
|
-
* An object containing functions to interact with the optimizer.
|
|
30
|
-
*
|
|
31
|
-
* It exposes the following functions:
|
|
32
|
-
*
|
|
33
|
-
* - `simplifyOptimization(options?: SimplifyOptions)`: Runs the simplification optimization with the given options.
|
|
34
|
-
* - `dedupOptimization(options?: DedupOptions)`: Runs the deduplication optimization with the given options.
|
|
35
|
-
* - `quantizeOptimization(options?: QuantizeOptions)`: Runs the quantization optimization with the given options.
|
|
36
|
-
* - `texturesCompressionOptimization(options?: TextureCompressionOptions)`: Runs the texture compression optimization with the given options.
|
|
37
|
-
* - `getSize()`: Returns the size of the optimized model.
|
|
38
|
-
* - `reset()`: Resets the optimizer.
|
|
39
|
-
* - `report`: The report state of the optimizer.
|
|
40
|
-
* - `error`: The error state of the optimizer.
|
|
41
|
-
* - `loading`: The loading state of the optimizer.
|
|
42
|
-
*/
|
|
43
|
-
optimize: {
|
|
44
|
-
simplifyOptimization: () => void;
|
|
45
|
-
dedupOptimization: () => void;
|
|
46
|
-
quantizeOptimization: () => void;
|
|
47
|
-
normalsOptimization: () => void;
|
|
48
|
-
texturesCompressOptimization: () => void;
|
|
49
|
-
getSize: () => void;
|
|
50
|
-
reset: () => void;
|
|
51
|
-
report: null;
|
|
52
|
-
error: null;
|
|
53
|
-
loading: boolean;
|
|
54
|
-
} | {
|
|
55
|
-
simplifyOptimization: (options?: {
|
|
56
|
-
ratio?: number;
|
|
57
|
-
error?: number;
|
|
58
|
-
} | undefined) => Promise<void>;
|
|
59
|
-
dedupOptimization: (options?: import('@gltf-transform/functions').DedupOptions | undefined) => Promise<void>;
|
|
60
|
-
quantizeOptimization: (options?: import('@gltf-transform/functions').QuantizeOptions | undefined) => Promise<void>;
|
|
61
|
-
normalsOptimization: (options?: import('@gltf-transform/functions').NormalsOptions | undefined) => Promise<void>;
|
|
62
|
-
texturesCompressOptimization: (options?: import('@gltf-transform/functions').TextureCompressOptions | undefined) => Promise<void>;
|
|
63
|
-
getSize: () => import('../use-optimize-model/types').ModelSize | null;
|
|
64
|
-
reset: () => void;
|
|
65
|
-
report: import('@gltf-transform/functions').InspectReport | null;
|
|
66
|
-
error: Error | null;
|
|
67
|
-
loading: boolean;
|
|
68
|
-
};
|
|
69
|
-
file: ModelFile | null;
|
|
70
|
-
isFileLoading: boolean;
|
|
71
|
-
progress: number;
|
|
72
|
-
supportedFileTypes: ModelFileTypes[];
|
|
73
|
-
};
|
|
74
|
-
export default useLoadModel;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Recursively reads a directory
|
|
3
|
-
*
|
|
4
|
-
* @param directoryHandle - The directory handle to read
|
|
5
|
-
* @returns - An array of file objects
|
|
6
|
-
*/
|
|
7
|
-
declare function readDirectory(directoryHandle: FileSystemDirectoryHandle): Promise<File[]>;
|
|
8
|
-
export default readDirectory;
|
package/use-load-model.cjs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./model-context-CQJta_vi.cjs");exports.ModelContext=e.ModelContext;exports.ModelFileTypes=e.ModelFileTypes;exports.ModelProvider=e.ModelProvider;exports.useLoadModel=e.useLoadModel;exports.useModelContext=e.useModelContext;
|
package/use-load-model.es.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Action, State } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Initial state for the reducer.
|
|
4
|
-
*/
|
|
5
|
-
export declare const initialState: State;
|
|
6
|
-
/**
|
|
7
|
-
* Reducer function to manage state transitions.
|
|
8
|
-
*
|
|
9
|
-
* @param state - Current state.
|
|
10
|
-
* @param action - Action to perform.
|
|
11
|
-
* @returns New state after applying the action.
|
|
12
|
-
*/
|
|
13
|
-
export declare const reducer: (state: State, action: Action) => State;
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { Document } from '@gltf-transform/core';
|
|
2
|
-
import { InspectReport } from '@gltf-transform/functions';
|
|
3
|
-
/**
|
|
4
|
-
* Interface representing the size details of the model.
|
|
5
|
-
*/
|
|
6
|
-
export interface ModelSize {
|
|
7
|
-
/** The file size in bytes. */
|
|
8
|
-
fileSize: number;
|
|
9
|
-
/** The file size formatted as a human-readable string. */
|
|
10
|
-
displayFileSize: string;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Interface representing the state of the model optimizer.
|
|
14
|
-
*/
|
|
15
|
-
export interface State {
|
|
16
|
-
modelDoc: Document | null;
|
|
17
|
-
modelReport: InspectReport | null;
|
|
18
|
-
error: Error | null;
|
|
19
|
-
loading: boolean;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Types of actions for the reducer.
|
|
23
|
-
*/
|
|
24
|
-
export type Action = {
|
|
25
|
-
type: 'LOAD_START';
|
|
26
|
-
} | {
|
|
27
|
-
type: 'LOAD_SUCCESS';
|
|
28
|
-
payload: {
|
|
29
|
-
modelDoc: Document;
|
|
30
|
-
modelReport: InspectReport;
|
|
31
|
-
};
|
|
32
|
-
} | {
|
|
33
|
-
type: 'LOAD_ERROR';
|
|
34
|
-
payload: Error;
|
|
35
|
-
} | {
|
|
36
|
-
type: 'RESET';
|
|
37
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Object3D } from 'three';
|
|
2
|
-
import { dedup, quantize, normals, textureCompress } from '@gltf-transform/functions';
|
|
3
|
-
import { ModelSize } from './types';
|
|
4
|
-
/**
|
|
5
|
-
* Custom React hook for optimizing Three.js models using glTF-Transform.
|
|
6
|
-
*
|
|
7
|
-
* This hook provides functions to load, optimize, and retrieve Three.js models.
|
|
8
|
-
* Optimizations include deduplication, quantization, and simplification of model meshes and textures.
|
|
9
|
-
*
|
|
10
|
-
* @returns An object containing functions to interact with the model optimizer.
|
|
11
|
-
*/
|
|
12
|
-
declare const useOptimizeModel: () => {
|
|
13
|
-
load: (model: Object3D) => Promise<void>;
|
|
14
|
-
getModel: () => Promise<Uint8Array | null>;
|
|
15
|
-
getSize: () => ModelSize | null;
|
|
16
|
-
report: import('@gltf-transform/functions').InspectReport | null;
|
|
17
|
-
simplifyOptimization: (options?: {
|
|
18
|
-
ratio?: number;
|
|
19
|
-
error?: number;
|
|
20
|
-
}) => Promise<void>;
|
|
21
|
-
dedupOptimization: (options?: Parameters<typeof dedup>[0]) => Promise<void>;
|
|
22
|
-
quantizeOptimization: (options?: Parameters<typeof quantize>[0]) => Promise<void>;
|
|
23
|
-
normalsOptimization: (options?: Parameters<typeof normals>[0]) => Promise<void>;
|
|
24
|
-
texturesOptimization: (options?: Parameters<typeof textureCompress>[0]) => Promise<void>;
|
|
25
|
-
reset: () => void;
|
|
26
|
-
error: Error | null;
|
|
27
|
-
loading: boolean;
|
|
28
|
-
};
|
|
29
|
-
export default useOptimizeModel;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const r=require("react"),T=require("./GLTFExporter-BmLF430n.cjs"),L=require("@gltf-transform/core"),a=require("@gltf-transform/functions"),m=require("@gltf-transform/extensions"),q=require("meshoptimizer"),R={modelDoc:null,modelReport:null,error:null,loading:!1},M=(i,o)=>{switch(o.type){case"LOAD_START":return{...i,loading:!0,error:null};case"LOAD_SUCCESS":return{...i,loading:!1,modelDoc:o.payload.modelDoc,modelReport:o.payload.modelReport};case"LOAD_ERROR":return{...i,loading:!1,error:o.payload};case"RESET":return{...R};default:return i}},_=()=>{const[i,o]=r.useReducer(M,R),{modelDoc:n,modelReport:l,error:f,loading:S}=i,z=r.useRef(new T.GLTFExporter),y=r.useRef(new L.WebIO().registerExtensions([m.KHRMeshQuantization,m.KHRMaterialsVolume,m.KHRMaterialsTransmission])),O=r.useCallback(async e=>{o({type:"LOAD_START"});try{const t={binary:!0},c=await z.current.parseAsync(e,t),u=new Uint8Array(c),p=await y.current.readBinary(u),d=a.inspect(p);o({type:"LOAD_SUCCESS",payload:{modelDoc:p,modelReport:d}})}catch(t){o({type:"LOAD_ERROR",payload:t}),console.error("Error loading model:",t)}},[]),s=r.useCallback(async e=>{if(n)try{await n.transform(...e);const t=a.inspect(n);o({type:"LOAD_SUCCESS",payload:{modelDoc:n,modelReport:t}})}catch(t){console.error("Error applying transforms:",t)}},[n]),C=r.useCallback(async e=>{const{ratio:t=.5,error:c=.001}=e||{};await s([a.weld(),a.simplify({simplifier:q.MeshoptSimplifier,ratio:t,error:c})])},[s]),E=r.useCallback(async e=>{await s([a.dedup(e)])},[s]),w=r.useCallback(async e=>{await s([a.quantize(e)])},[s]),b=r.useCallback(async e=>{await s([a.normals(e)])},[s]),g=r.useCallback(async e=>{e&&await s([a.textureCompress(e)])},[s]),D=r.useCallback(async()=>{if(!n)return null;try{return await y.current.writeBinary(n)}catch(e){return console.error("Error getting model binary:",e),null}},[n]),A=r.useCallback(()=>{if(!l)return null;const e=d=>d.reduce((k,x)=>k+x.size,0),t=e(l.meshes.properties),c=e(l.textures.properties),u=t+c,p=`${(u/(1024*1024)).toFixed(2)} MB`;return{fileSize:u,displayFileSize:p}},[l]),h=r.useCallback(()=>{o({type:"RESET"})},[]);return{load:O,getModel:D,getSize:A,report:l,simplifyOptimization:C,dedupOptimization:E,quantizeOptimization:w,normalsOptimization:b,texturesOptimization:g,reset:h,error:f,loading:S}};exports.useOptimizeModel=_;
|