@vctrl/hooks 0.3.2 → 0.5.0
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/{_commonjsHelpers-BbdAOH3u.js → GLTFExporter-Q5UJ5eb1.js} +64 -70
- package/{_commonjsHelpers-BKV1RZto.cjs → GLTFExporter-ltc8rmzz.cjs} +2 -2
- package/index.cjs.js +1 -1
- package/index.d.ts +1 -0
- package/index.es.js +6 -4
- package/{model-context-BmfXsk0j.cjs → model-context-ChF_IVjD.cjs} +5 -5
- package/{model-context-qjRd1gfd.js → model-context-DDCJu7oe.js} +412 -389
- package/package.json +1 -1
- package/use-export-model-BBfXwJbA.cjs +12 -0
- package/use-export-model-CUUL3YCH.js +2465 -0
- package/use-export-model.cjs.js +1 -12
- package/use-export-model.es.js +2 -2459
- package/use-load-model/model-context.d.ts +34 -0
- package/use-load-model/types.d.ts +2 -1
- package/use-load-model/use-load-model.d.ts +23 -0
- package/use-load-model.cjs.js +1 -1
- package/use-load-model.es.js +1 -1
- package/use-optimize-model/state.d.ts +13 -0
- package/use-optimize-model/types.d.ts +37 -0
- package/use-optimize-model/use-optimize-model.d.ts +22 -4
- package/use-optimize-model-BN-fP4La.cjs +1 -0
- package/use-optimize-model-C46Fr7jH.js +111 -0
- package/use-optimize-model.cjs.js +1 -1
- package/use-optimize-model.es.js +1 -1
- package/use-optimize-model-BXIlJ5Yx.cjs +0 -20
- package/use-optimize-model-BpcR9d1L.js +0 -10037
|
@@ -8,9 +8,26 @@ declare const ModelContext: import('react').Context<{
|
|
|
8
8
|
load: (filesOrDirectories: import('./types').InputFileOrDirectory) => Promise<void>;
|
|
9
9
|
reset: () => void;
|
|
10
10
|
optimize: {
|
|
11
|
+
simplifyOptimization: (options?: {
|
|
12
|
+
ratio?: number;
|
|
13
|
+
error?: number;
|
|
14
|
+
} | undefined) => Promise<void>;
|
|
15
|
+
dedupOptimization: (options?: import('@gltf-transform/functions').DedupOptions | undefined) => Promise<void>;
|
|
16
|
+
quantizeOptimization: (options?: import('@gltf-transform/functions').QuantizeOptions | undefined) => Promise<void>;
|
|
17
|
+
getSize: () => import('../use-optimize-model/types').ModelSize | null;
|
|
18
|
+
reset: () => void;
|
|
19
|
+
report: import('@gltf-transform/functions').InspectReport | null;
|
|
20
|
+
error: Error | null;
|
|
21
|
+
loading: boolean;
|
|
22
|
+
} | {
|
|
11
23
|
simplifyOptimization: () => void;
|
|
12
24
|
dedupOptimization: () => void;
|
|
13
25
|
quantizeOptimization: () => void;
|
|
26
|
+
getSize: () => void;
|
|
27
|
+
reset: () => void;
|
|
28
|
+
report: null;
|
|
29
|
+
error: null;
|
|
30
|
+
loading: boolean;
|
|
14
31
|
};
|
|
15
32
|
file: import('./types').ModelFile | null;
|
|
16
33
|
isFileLoading: boolean;
|
|
@@ -24,9 +41,26 @@ declare const useModelContext: () => {
|
|
|
24
41
|
load: (filesOrDirectories: import('./types').InputFileOrDirectory) => Promise<void>;
|
|
25
42
|
reset: () => void;
|
|
26
43
|
optimize: {
|
|
44
|
+
simplifyOptimization: (options?: {
|
|
45
|
+
ratio?: number;
|
|
46
|
+
error?: number;
|
|
47
|
+
} | undefined) => Promise<void>;
|
|
48
|
+
dedupOptimization: (options?: import('@gltf-transform/functions').DedupOptions | undefined) => Promise<void>;
|
|
49
|
+
quantizeOptimization: (options?: import('@gltf-transform/functions').QuantizeOptions | undefined) => Promise<void>;
|
|
50
|
+
getSize: () => import('../use-optimize-model/types').ModelSize | null;
|
|
51
|
+
reset: () => void;
|
|
52
|
+
report: import('@gltf-transform/functions').InspectReport | null;
|
|
53
|
+
error: Error | null;
|
|
54
|
+
loading: boolean;
|
|
55
|
+
} | {
|
|
27
56
|
simplifyOptimization: () => void;
|
|
28
57
|
dedupOptimization: () => void;
|
|
29
58
|
quantizeOptimization: () => void;
|
|
59
|
+
getSize: () => void;
|
|
60
|
+
reset: () => void;
|
|
61
|
+
report: null;
|
|
62
|
+
error: null;
|
|
63
|
+
loading: boolean;
|
|
30
64
|
};
|
|
31
65
|
file: import('./types').ModelFile | null;
|
|
32
66
|
isFileLoading: boolean;
|
|
@@ -38,10 +38,11 @@ export type Action = {
|
|
|
38
38
|
} | {
|
|
39
39
|
type: 'reset-state';
|
|
40
40
|
};
|
|
41
|
-
export type EventTypes = 'multiple-models' | 'not-loaded-files' | 'load-progress' | 'load-complete' | 'load-reset' | 'load-error';
|
|
41
|
+
export type EventTypes = 'multiple-models' | 'not-loaded-files' | 'load-start' | 'load-progress' | 'load-complete' | 'load-reset' | 'load-error';
|
|
42
42
|
export type EventData = {
|
|
43
43
|
'multiple-models': File[];
|
|
44
44
|
'not-loaded-files': File[];
|
|
45
|
+
'load-start': null;
|
|
45
46
|
'load-progress': number;
|
|
46
47
|
'load-complete': State['file'];
|
|
47
48
|
'load-reset': null;
|
|
@@ -1,14 +1,37 @@
|
|
|
1
1
|
import { useOptimizeModel } from '../use-optimize-model';
|
|
2
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
|
+
*/
|
|
3
9
|
declare function useLoadModel(optimizer?: ReturnType<typeof useOptimizeModel>): {
|
|
4
10
|
on: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
5
11
|
off: <T extends import('./types').EventTypes>(event: T, handler?: import('./types').EventHandler<T>) => void;
|
|
6
12
|
load: (filesOrDirectories: InputFileOrDirectory) => Promise<void>;
|
|
7
13
|
reset: () => void;
|
|
8
14
|
optimize: {
|
|
15
|
+
simplifyOptimization: (options?: {
|
|
16
|
+
ratio?: number;
|
|
17
|
+
error?: number;
|
|
18
|
+
} | undefined) => Promise<void>;
|
|
19
|
+
dedupOptimization: (options?: import('@gltf-transform/functions').DedupOptions | undefined) => Promise<void>;
|
|
20
|
+
quantizeOptimization: (options?: import('@gltf-transform/functions').QuantizeOptions | undefined) => Promise<void>;
|
|
21
|
+
getSize: () => import('../use-optimize-model/types').ModelSize | null;
|
|
22
|
+
reset: () => void;
|
|
23
|
+
report: import('@gltf-transform/functions').InspectReport | null;
|
|
24
|
+
error: Error | null;
|
|
25
|
+
loading: boolean;
|
|
26
|
+
} | {
|
|
9
27
|
simplifyOptimization: () => void;
|
|
10
28
|
dedupOptimization: () => void;
|
|
11
29
|
quantizeOptimization: () => void;
|
|
30
|
+
getSize: () => void;
|
|
31
|
+
reset: () => void;
|
|
32
|
+
report: null;
|
|
33
|
+
error: null;
|
|
34
|
+
loading: boolean;
|
|
12
35
|
};
|
|
13
36
|
file: ModelFile | null;
|
|
14
37
|
isFileLoading: boolean;
|
package/use-load-model.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./model-context-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./model-context-ChF_IVjD.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
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
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;
|
|
@@ -0,0 +1,37 @@
|
|
|
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,9 +1,27 @@
|
|
|
1
1
|
import { Object3D } from 'three';
|
|
2
|
+
import { dedup, quantize } 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
|
+
*/
|
|
2
12
|
declare const useOptimizeModel: () => {
|
|
3
13
|
load: (model: Object3D) => Promise<void>;
|
|
4
|
-
getModel: () => Promise<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
14
|
+
getModel: () => Promise<ArrayBuffer | 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
|
+
reset: () => void;
|
|
24
|
+
error: Error | null;
|
|
25
|
+
loading: boolean;
|
|
8
26
|
};
|
|
9
27
|
export default useOptimizeModel;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const t=require("react"),h=require("./GLTFExporter-ltc8rmzz.cjs"),L=require("@gltf-transform/core"),i=require("@gltf-transform/functions"),T=require("@gltf-transform/extensions"),k=require("meshoptimizer"),m={modelDoc:null,modelReport:null,error:null,loading:!1},q=(n,o)=>{switch(o.type){case"LOAD_START":return{...n,loading:!0,error:null};case"LOAD_SUCCESS":return{...n,loading:!1,modelDoc:o.payload.modelDoc,modelReport:o.payload.modelReport};case"LOAD_ERROR":return{...n,loading:!1,error:o.payload};case"RESET":return{...m};default:return n}},x=()=>{const[n,o]=t.useReducer(q,m),{modelDoc:s,modelReport:l,error:f,loading:R}=n,S=t.useRef(new h.GLTFExporter),y=t.useRef(new L.WebIO().registerExtensions([T.KHRMeshQuantization])),z=t.useCallback(async e=>{o({type:"LOAD_START"});try{const r={binary:!0},c=await S.current.parseAsync(e,r),u=new Uint8Array(c),p=await y.current.readBinary(u),d=i.inspect(p);o({type:"LOAD_SUCCESS",payload:{modelDoc:p,modelReport:d}})}catch(r){o({type:"LOAD_ERROR",payload:r}),console.error("Error loading model:",r)}},[]),a=t.useCallback(async e=>{if(s)try{await s.transform(...e);const r=i.inspect(s);o({type:"LOAD_SUCCESS",payload:{modelDoc:s,modelReport:r}})}catch(r){console.error("Error applying transforms:",r)}},[s]),E=t.useCallback(async e=>{const{ratio:r=.5,error:c=.001}=e||{};await a([i.weld(),i.simplify({simplifier:k.MeshoptSimplifier,ratio:r,error:c})])},[a]),O=t.useCallback(async e=>{await a([i.dedup(e)])},[a]),C=t.useCallback(async e=>{await a([i.quantize(e)])},[a]),w=t.useCallback(async()=>{if(!s)return null;try{return await y.current.writeBinary(s)}catch(e){return console.error("Error getting model binary:",e),null}},[s]),b=t.useCallback(()=>{if(!l)return null;const e=d=>d.reduce((D,A)=>D+A.size,0),r=e(l.meshes.properties),c=e(l.textures.properties),u=r+c,p=`${(u/(1024*1024)).toFixed(2)} MB`;return{fileSize:u,displayFileSize:p}},[l]),g=t.useCallback(()=>{o({type:"RESET"})},[]);return{load:z,getModel:w,getSize:b,report:l,simplifyOptimization:E,dedupOptimization:O,quantizeOptimization:C,reset:g,error:f,loading:R}};exports.useOptimizeModel=x;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { useReducer as x, useRef as m, useCallback as n } from "react";
|
|
2
|
+
import { G as C } from "./GLTFExporter-Q5UJ5eb1.js";
|
|
3
|
+
import { WebIO as _ } from "@gltf-transform/core";
|
|
4
|
+
import { inspect as y, weld as b, simplify as M, dedup as B, quantize as U } from "@gltf-transform/functions";
|
|
5
|
+
import { KHRMeshQuantization as F } from "@gltf-transform/extensions";
|
|
6
|
+
import { MeshoptSimplifier as q } from "meshoptimizer";
|
|
7
|
+
const f = {
|
|
8
|
+
modelDoc: null,
|
|
9
|
+
modelReport: null,
|
|
10
|
+
error: null,
|
|
11
|
+
loading: !1
|
|
12
|
+
}, G = (i, o) => {
|
|
13
|
+
switch (o.type) {
|
|
14
|
+
case "LOAD_START":
|
|
15
|
+
return { ...i, loading: !0, error: null };
|
|
16
|
+
case "LOAD_SUCCESS":
|
|
17
|
+
return {
|
|
18
|
+
...i,
|
|
19
|
+
loading: !1,
|
|
20
|
+
modelDoc: o.payload.modelDoc,
|
|
21
|
+
modelReport: o.payload.modelReport
|
|
22
|
+
};
|
|
23
|
+
case "LOAD_ERROR":
|
|
24
|
+
return { ...i, loading: !1, error: o.payload };
|
|
25
|
+
case "RESET":
|
|
26
|
+
return { ...f };
|
|
27
|
+
default:
|
|
28
|
+
return i;
|
|
29
|
+
}
|
|
30
|
+
}, $ = () => {
|
|
31
|
+
const [i, o] = x(G, f), { modelDoc: t, modelReport: a, error: R, loading: S } = i, E = m(new C()), u = m(
|
|
32
|
+
new _().registerExtensions([F])
|
|
33
|
+
), O = n(async (r) => {
|
|
34
|
+
o({ type: "LOAD_START" });
|
|
35
|
+
try {
|
|
36
|
+
const e = { binary: !0 }, l = await E.current.parseAsync(r, e), c = new Uint8Array(l), p = await u.current.readBinary(c), d = y(p);
|
|
37
|
+
o({
|
|
38
|
+
type: "LOAD_SUCCESS",
|
|
39
|
+
payload: { modelDoc: p, modelReport: d }
|
|
40
|
+
});
|
|
41
|
+
} catch (e) {
|
|
42
|
+
o({ type: "LOAD_ERROR", payload: e }), console.error("Error loading model:", e);
|
|
43
|
+
}
|
|
44
|
+
}, []), s = n(
|
|
45
|
+
async (r) => {
|
|
46
|
+
if (t)
|
|
47
|
+
try {
|
|
48
|
+
await t.transform(...r);
|
|
49
|
+
const e = y(t);
|
|
50
|
+
o({
|
|
51
|
+
type: "LOAD_SUCCESS",
|
|
52
|
+
payload: { modelDoc: t, modelReport: e }
|
|
53
|
+
});
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.error("Error applying transforms:", e);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
[t]
|
|
59
|
+
), z = n(
|
|
60
|
+
async (r) => {
|
|
61
|
+
const { ratio: e = 0.5, error: l = 1e-3 } = r || {};
|
|
62
|
+
await s([
|
|
63
|
+
b(),
|
|
64
|
+
M({
|
|
65
|
+
simplifier: q,
|
|
66
|
+
ratio: e,
|
|
67
|
+
error: l
|
|
68
|
+
})
|
|
69
|
+
]);
|
|
70
|
+
},
|
|
71
|
+
[s]
|
|
72
|
+
), w = n(
|
|
73
|
+
async (r) => {
|
|
74
|
+
await s([B(r)]);
|
|
75
|
+
},
|
|
76
|
+
[s]
|
|
77
|
+
), g = n(
|
|
78
|
+
async (r) => {
|
|
79
|
+
await s([U(r)]);
|
|
80
|
+
},
|
|
81
|
+
[s]
|
|
82
|
+
), D = n(async () => {
|
|
83
|
+
if (!t) return null;
|
|
84
|
+
try {
|
|
85
|
+
return await u.current.writeBinary(t);
|
|
86
|
+
} catch (r) {
|
|
87
|
+
return console.error("Error getting model binary:", r), null;
|
|
88
|
+
}
|
|
89
|
+
}, [t]), A = n(() => {
|
|
90
|
+
if (!a) return null;
|
|
91
|
+
const r = (d) => d.reduce((L, T) => L + T.size, 0), e = r(a.meshes.properties), l = r(a.textures.properties), c = e + l, p = `${(c / (1024 * 1024)).toFixed(2)} MB`;
|
|
92
|
+
return { fileSize: c, displayFileSize: p };
|
|
93
|
+
}, [a]), h = n(() => {
|
|
94
|
+
o({ type: "RESET" });
|
|
95
|
+
}, []);
|
|
96
|
+
return {
|
|
97
|
+
load: O,
|
|
98
|
+
getModel: D,
|
|
99
|
+
getSize: A,
|
|
100
|
+
report: a,
|
|
101
|
+
simplifyOptimization: z,
|
|
102
|
+
dedupOptimization: w,
|
|
103
|
+
quantizeOptimization: g,
|
|
104
|
+
reset: h,
|
|
105
|
+
error: R,
|
|
106
|
+
loading: S
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
export {
|
|
110
|
+
$ as u
|
|
111
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./use-optimize-model-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./use-optimize-model-BN-fP4La.cjs");exports.useOptimizeModel=e.useOptimizeModel;
|
package/use-optimize-model.es.js
CHANGED