@vctrl/hooks 0.18.0 → 0.23.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/CHANGELOG.md +85 -0
- package/README.md +9 -9
- package/chunk-CMqjfN_6.cjs +1 -0
- package/index.cjs.js +1 -1
- package/index.es.js +3 -3
- package/package.json +73 -67
- package/use-export-model-8JbUalyf.cjs +1 -0
- package/{use-export-model-DzVKdqnY.js → use-export-model-BXg6L_W5.js} +5 -5
- package/use-export-model.cjs.js +1 -1
- package/use-export-model.es.js +1 -1
- package/use-load-model/error-helpers.d.ts +5 -0
- package/use-load-model/file-loaders.d.ts +12 -0
- package/use-load-model/index.d.ts +2 -1
- package/use-load-model/optimizer-integration.d.ts +16 -0
- package/use-load-model/scene-data-builder.d.ts +3 -0
- package/use-load-model/types.d.ts +17 -4
- package/use-load-model/use-load-model.d.ts +5 -16
- package/use-load-model/utils/fetch-manifest-assets.d.ts +10 -0
- package/use-load-model/utils/index.d.ts +1 -0
- package/use-load-model-B8oqou4I.cjs +1 -0
- package/use-load-model-C88r8-HE.js +585 -0
- package/use-load-model.cjs.js +1 -1
- package/use-load-model.es.js +11 -11
- package/use-optimize-model/use-optimize-model.d.ts +14 -12
- package/use-optimize-model/utils/browser-texture-encoder.d.ts +41 -0
- package/use-optimize-model/utils/index.d.ts +2 -2
- package/use-optimize-model/utils/texture-optimization.d.ts +3 -13
- package/use-optimize-model-CweunXiv.js +296 -0
- package/use-optimize-model-Jie2FUHc.cjs +1 -0
- package/use-optimize-model.cjs.js +1 -1
- package/use-optimize-model.es.js +1 -1
- package/GLTFLoader-CygFefRr.cjs +0 -1
- package/GLTFLoader-NiJ7qzh1.js +0 -1381
- package/__vite-browser-external-Bn_csQuM.js +0 -7
- package/__vite-browser-external-CVmj8TrW.cjs +0 -1
- package/chunk-foFoljWS.js +0 -23
- package/chunk-gGpyby2o.cjs +0 -1
- package/lib-DKn2MJ9X.js +0 -4111
- package/lib-DZ-MihdJ.cjs +0 -2
- package/model-exporter-SMSAY90N.cjs +0 -1
- package/model-exporter-cvguDGnn.js +0 -6512
- package/server-communication-Bj6ZxFw2.js +0 -4659
- package/server-communication-yXZR8eso.cjs +0 -18
- package/use-export-model-CaijtW1c.cjs +0 -1
- package/use-load-model-B6UXiFWh.js +0 -517
- package/use-load-model-CH-WPQ_s.cjs +0 -1
- package/use-optimize-model/utils/validation.d.ts +0 -8
- package/use-optimize-model-C-fzzKh6.cjs +0 -1
- package/use-optimize-model-Cb3WMvDZ.js +0 -299
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import { c as e, o as t, t as n } from "./server-communication-Bj6ZxFw2.js";
|
|
2
|
-
import { useCallback as r, useEffect as i, useMemo as a, useReducer as o, useRef as s, useState as c } from "react";
|
|
3
|
-
//#region packages/hooks/src/use-optimize-model/state.ts
|
|
4
|
-
var l = {
|
|
5
|
-
info: null,
|
|
6
|
-
error: null,
|
|
7
|
-
loading: !1,
|
|
8
|
-
report: null
|
|
9
|
-
}, u = (e, t) => {
|
|
10
|
-
switch (t.type) {
|
|
11
|
-
case "LOAD_START": return {
|
|
12
|
-
...e,
|
|
13
|
-
loading: !0,
|
|
14
|
-
error: null
|
|
15
|
-
};
|
|
16
|
-
case "LOAD_SUCCESS": return {
|
|
17
|
-
...e,
|
|
18
|
-
loading: !1,
|
|
19
|
-
report: t.payload.report
|
|
20
|
-
};
|
|
21
|
-
case "LOAD_ERROR": return {
|
|
22
|
-
...e,
|
|
23
|
-
loading: !1,
|
|
24
|
-
error: t.payload
|
|
25
|
-
};
|
|
26
|
-
case "RESET": return { ...l };
|
|
27
|
-
default: return e;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
//#endregion
|
|
31
|
-
//#region packages/hooks/src/use-optimize-model/use-calc-optimization-info.ts
|
|
32
|
-
function d(e) {
|
|
33
|
-
return e ? {
|
|
34
|
-
verticesCount: e.stats.vertices.after ?? 0,
|
|
35
|
-
primitivesCount: e.stats.triangles.after ?? 0,
|
|
36
|
-
meshesCount: e.stats.meshes.after ?? 0,
|
|
37
|
-
textureAssetCount: e.stats.texturesCount.after ?? 0,
|
|
38
|
-
materialsCount: e.stats.materials.after ?? 0,
|
|
39
|
-
sceneBytes: e.optimizedSize ?? 0
|
|
40
|
-
} : {
|
|
41
|
-
verticesCount: 0,
|
|
42
|
-
primitivesCount: 0,
|
|
43
|
-
meshesCount: 0,
|
|
44
|
-
textureAssetCount: 0,
|
|
45
|
-
materialsCount: 0,
|
|
46
|
-
sceneBytes: 0
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
var f = (e) => {
|
|
50
|
-
let t = s(null), [n, o] = c(!1), l = e.report;
|
|
51
|
-
return i(() => {
|
|
52
|
-
!n && l && (t.current = l, o(!0));
|
|
53
|
-
}, [n, l]), {
|
|
54
|
-
info: a(() => {
|
|
55
|
-
let e = d(t.current), n = d(l);
|
|
56
|
-
return {
|
|
57
|
-
initial: {
|
|
58
|
-
verticesCount: e.verticesCount,
|
|
59
|
-
primitivesCount: e.primitivesCount,
|
|
60
|
-
meshesCount: e.meshesCount,
|
|
61
|
-
texturesCount: e.textureAssetCount,
|
|
62
|
-
sceneBytes: e.sceneBytes
|
|
63
|
-
},
|
|
64
|
-
optimized: {
|
|
65
|
-
verticesCount: n.verticesCount,
|
|
66
|
-
primitivesCount: n.primitivesCount,
|
|
67
|
-
meshesCount: n.meshesCount,
|
|
68
|
-
texturesCount: n.textureAssetCount,
|
|
69
|
-
sceneBytes: n.sceneBytes
|
|
70
|
-
},
|
|
71
|
-
improvement: {
|
|
72
|
-
verticesCount: e.verticesCount - n.verticesCount,
|
|
73
|
-
primitivesCount: e.primitivesCount - n.primitivesCount,
|
|
74
|
-
meshesCount: e.meshesCount - n.meshesCount,
|
|
75
|
-
texturesCount: e.textureAssetCount - n.textureAssetCount,
|
|
76
|
-
sceneBytes: e.sceneBytes - n.sceneBytes
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}, [l]),
|
|
80
|
-
reset: r(() => {
|
|
81
|
-
t.current = null, o(!1);
|
|
82
|
-
}, [])
|
|
83
|
-
};
|
|
84
|
-
}, p = (e) => {
|
|
85
|
-
let t = e.headers.get("content-type");
|
|
86
|
-
if (!t) throw Error("Missing content type from server response");
|
|
87
|
-
if (t.includes("application/json")) throw Error("Unexpected JSON success response from texture optimizer");
|
|
88
|
-
if (!t.startsWith("image/") && !t.includes("application/octet-stream")) throw Error(`Unexpected content type from server: ${t}`);
|
|
89
|
-
}, m = async (e) => {
|
|
90
|
-
if (e.ok) {
|
|
91
|
-
if (e.headers.get("content-type")?.includes("application/json")) {
|
|
92
|
-
let t = await e.json();
|
|
93
|
-
throw Error(t.error || t.details || "Server returned error");
|
|
94
|
-
}
|
|
95
|
-
p(e);
|
|
96
|
-
}
|
|
97
|
-
}, h = 2e4, g = 2, _ = 50 * 1024 * 1024, v = 4, y = new Set([
|
|
98
|
-
408,
|
|
99
|
-
425,
|
|
100
|
-
429,
|
|
101
|
-
500,
|
|
102
|
-
502,
|
|
103
|
-
503,
|
|
104
|
-
504
|
|
105
|
-
]), b = async (e) => {
|
|
106
|
-
await new Promise((t) => setTimeout(t, e));
|
|
107
|
-
}, x = (e) => {
|
|
108
|
-
let t = e.headers.get("content-type");
|
|
109
|
-
return t && t.split(";")[0]?.trim() || "application/octet-stream";
|
|
110
|
-
}, S = (e) => {
|
|
111
|
-
let t = e.headers.get("X-Texture-File-Name") || e.headers.get("X-Texture-Name");
|
|
112
|
-
if (!t || t.trim().length === 0) throw Error("Optimized texture response is missing a canonical file name");
|
|
113
|
-
return t.trim();
|
|
114
|
-
}, C = async (e, t, n, r, i) => {
|
|
115
|
-
let a = 0;
|
|
116
|
-
for (;;) {
|
|
117
|
-
let o = new AbortController(), s = setTimeout(() => o.abort(), r);
|
|
118
|
-
try {
|
|
119
|
-
let r = await fetch(e, {
|
|
120
|
-
method: "POST",
|
|
121
|
-
headers: t,
|
|
122
|
-
body: n,
|
|
123
|
-
signal: o.signal
|
|
124
|
-
});
|
|
125
|
-
if (!r.ok && y.has(r.status) && a < i) {
|
|
126
|
-
a += 1, await b(200 * a + Math.floor(Math.random() * 120));
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
return r;
|
|
130
|
-
} catch (e) {
|
|
131
|
-
if (a >= i) throw e;
|
|
132
|
-
a += 1, await b(200 * a + Math.floor(Math.random() * 120));
|
|
133
|
-
} finally {
|
|
134
|
-
clearTimeout(s);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}, w = async (e, t, r, i) => {
|
|
138
|
-
let a = e.getTexturePayload(t.index), o = r.maxTextureUploadBytes ?? _;
|
|
139
|
-
if (a.image.byteLength > o) throw Error(`Texture ${t.index} payload (${a.image.byteLength} bytes) exceeds maxTextureUploadBytes (${o} bytes). Increase maxTextureUploadBytes to allow optimizing larger source textures.`);
|
|
140
|
-
let s = new Uint8Array(a.image.byteLength);
|
|
141
|
-
s.set(a.image);
|
|
142
|
-
let c = s.buffer, { serverOptions: l, ...u } = r, d = n.createRequestHeaders(i, {
|
|
143
|
-
"Content-Type": "application/octet-stream",
|
|
144
|
-
"X-Texture-Index": String(t.index),
|
|
145
|
-
"X-Texture-Name": a.fileName,
|
|
146
|
-
"X-Texture-File-Name": a.fileName,
|
|
147
|
-
"X-Texture-Mime-Type": a.mimeType,
|
|
148
|
-
"X-Optimize-Options": JSON.stringify(u)
|
|
149
|
-
}), f = await C(i.endpoint, d, c, r.requestTimeoutMs ?? h, r.maxRetries ?? g);
|
|
150
|
-
f.ok || await n.handleServerResponseError(f), await m(f);
|
|
151
|
-
let p = Number.parseInt(f.headers.get("X-Texture-Index") || "", 10);
|
|
152
|
-
if (!Number.isFinite(p) || p !== t.index) throw Error(`Texture identity mismatch. Expected ${t.index}, received ${f.headers.get("X-Texture-Index")}`);
|
|
153
|
-
let v = new Uint8Array(await f.arrayBuffer());
|
|
154
|
-
if (v.byteLength === 0) throw Error(`Empty optimized payload for texture ${t.index}`);
|
|
155
|
-
let y = S(f);
|
|
156
|
-
e.replaceTexturePayload(t.index, v, x(f), y);
|
|
157
|
-
}, T = async (e, t) => {
|
|
158
|
-
let r = n.createDefaultServerOptions(t.serverOptions), i = e.listTextureDescriptors(), a = [], o = 0, s = t.maxConcurrentRequests ?? v;
|
|
159
|
-
try {
|
|
160
|
-
for (let n = 0; n < i.length; n += s) {
|
|
161
|
-
let c = i.slice(n, n + s), l = await Promise.allSettled(c.map(async (n) => (await w(e, n, t, r), n.index)));
|
|
162
|
-
for (let [e, t] of l.entries()) {
|
|
163
|
-
if (t.status === "fulfilled") {
|
|
164
|
-
o += 1;
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
a.push({
|
|
168
|
-
index: c[e].index,
|
|
169
|
-
reason: t.reason instanceof Error ? t.reason.message : String(t.reason)
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (o > 0 && e.addAppliedOptimization("texture compression"), a.length > 0) {
|
|
174
|
-
let e = a.map((e) => `#${e.index}: ${e.reason}`).join("; "), t = o === 0 ? "Texture optimization failed for all textures." : `Texture optimization failed for ${a.length} of ${i.length} textures.`;
|
|
175
|
-
throw Error(`${t} ${e}`);
|
|
176
|
-
}
|
|
177
|
-
} catch (e) {
|
|
178
|
-
throw console.error("Server-side texture compression failed:", e), Error(`Server-side texture compression failed: ${e}. Client-side texture compression is not supported in browser environments.`, { cause: e });
|
|
179
|
-
}
|
|
180
|
-
}, E = () => {
|
|
181
|
-
throw console.warn("Client-side texture compression is not supported in browser environments. Please enable server-side compression by setting serverOptions.enabled = true."), Error("Texture compression requires server-side processing. Enable serverOptions.enabled = true in your options.");
|
|
182
|
-
}, D = (e) => e?.serverOptions?.enabled ?? !1, O = async (e, t) => {
|
|
183
|
-
e.hasModel() && (D(t) && t ? await T(e, t) : E());
|
|
184
|
-
}, k = () => {
|
|
185
|
-
let [n, a] = o(u, l), { report: c, error: d, loading: p } = n, { info: m, reset: h } = f(n), g = s(new e());
|
|
186
|
-
i(() => {
|
|
187
|
-
let e = g.current;
|
|
188
|
-
return () => {
|
|
189
|
-
e.reset();
|
|
190
|
-
};
|
|
191
|
-
}, []);
|
|
192
|
-
let _ = r(async (e) => {
|
|
193
|
-
a({ type: "LOAD_START" });
|
|
194
|
-
try {
|
|
195
|
-
let t = g.current;
|
|
196
|
-
await t.loadFromThreeJS(e), a({
|
|
197
|
-
type: "LOAD_SUCCESS",
|
|
198
|
-
payload: { report: await t.getReport() }
|
|
199
|
-
});
|
|
200
|
-
} catch (e) {
|
|
201
|
-
a({
|
|
202
|
-
type: "LOAD_ERROR",
|
|
203
|
-
payload: e
|
|
204
|
-
}), console.error("Error loading model:", e);
|
|
205
|
-
}
|
|
206
|
-
}, []), v = r(async (e) => {
|
|
207
|
-
a({ type: "LOAD_START" });
|
|
208
|
-
try {
|
|
209
|
-
let n = g.current, r = {};
|
|
210
|
-
for (let n of Object.values(e.assetData ?? {})) r[n.fileName] = t(n);
|
|
211
|
-
let i = {
|
|
212
|
-
json: e.gltfJson,
|
|
213
|
-
resources: r
|
|
214
|
-
};
|
|
215
|
-
await n.loadFromJSON(i), a({
|
|
216
|
-
type: "LOAD_SUCCESS",
|
|
217
|
-
payload: { report: await n.getReport() }
|
|
218
|
-
});
|
|
219
|
-
} catch (e) {
|
|
220
|
-
throw a({
|
|
221
|
-
type: "LOAD_ERROR",
|
|
222
|
-
payload: e
|
|
223
|
-
}), console.error("Error loading server scene into optimizer:", e), e;
|
|
224
|
-
}
|
|
225
|
-
}, []), y = r(async (e) => {
|
|
226
|
-
if (g.current.hasModel()) try {
|
|
227
|
-
await g.current.simplify(e), a({
|
|
228
|
-
type: "LOAD_SUCCESS",
|
|
229
|
-
payload: { report: await g.current.getReport() }
|
|
230
|
-
});
|
|
231
|
-
} catch (e) {
|
|
232
|
-
console.error("Simplification failed:", e);
|
|
233
|
-
}
|
|
234
|
-
}, []), b = r(async (e) => {
|
|
235
|
-
if (g.current.hasModel()) try {
|
|
236
|
-
await g.current.deduplicate(e), a({
|
|
237
|
-
type: "LOAD_SUCCESS",
|
|
238
|
-
payload: { report: await g.current.getReport() }
|
|
239
|
-
});
|
|
240
|
-
} catch (e) {
|
|
241
|
-
console.error("Deduplication failed:", e);
|
|
242
|
-
}
|
|
243
|
-
}, []), x = r(async (e) => {
|
|
244
|
-
if (g.current.hasModel()) try {
|
|
245
|
-
await g.current.quantize(e), a({
|
|
246
|
-
type: "LOAD_SUCCESS",
|
|
247
|
-
payload: { report: await g.current.getReport() }
|
|
248
|
-
});
|
|
249
|
-
} catch (e) {
|
|
250
|
-
console.error("Quantization failed:", e);
|
|
251
|
-
}
|
|
252
|
-
}, []), S = r(async (e) => {
|
|
253
|
-
if (g.current.hasModel()) try {
|
|
254
|
-
await g.current.optimizeNormals(e), a({
|
|
255
|
-
type: "LOAD_SUCCESS",
|
|
256
|
-
payload: { report: await g.current.getReport() }
|
|
257
|
-
});
|
|
258
|
-
} catch (e) {
|
|
259
|
-
console.error("Normals optimization failed:", e);
|
|
260
|
-
}
|
|
261
|
-
}, []), C = r(async (e) => {
|
|
262
|
-
try {
|
|
263
|
-
await O(g.current, e), a({
|
|
264
|
-
type: "LOAD_SUCCESS",
|
|
265
|
-
payload: { report: await g.current.getReport() }
|
|
266
|
-
});
|
|
267
|
-
} catch (e) {
|
|
268
|
-
throw console.error("Texture optimization failed:", e), e;
|
|
269
|
-
}
|
|
270
|
-
}, []), w = r(async () => {
|
|
271
|
-
if (!g.current.hasModel()) return null;
|
|
272
|
-
try {
|
|
273
|
-
return await g.current.export();
|
|
274
|
-
} catch (e) {
|
|
275
|
-
return console.error("Error getting model binary:", e), null;
|
|
276
|
-
}
|
|
277
|
-
}, []), T = r(() => g.current.hasModel() ? g.current.document : null, []);
|
|
278
|
-
return {
|
|
279
|
-
load: _,
|
|
280
|
-
loadFromServerSceneData: v,
|
|
281
|
-
getModel: w,
|
|
282
|
-
isReady: !!T() && !p,
|
|
283
|
-
_getDocument: T,
|
|
284
|
-
reset: r(() => {
|
|
285
|
-
g.current.reset(), a({ type: "RESET" }), h();
|
|
286
|
-
}, [h]),
|
|
287
|
-
error: d,
|
|
288
|
-
loading: p,
|
|
289
|
-
report: c,
|
|
290
|
-
info: m,
|
|
291
|
-
simplifyOptimization: y,
|
|
292
|
-
dedupOptimization: b,
|
|
293
|
-
quantizeOptimization: x,
|
|
294
|
-
normalsOptimization: S,
|
|
295
|
-
texturesOptimization: C
|
|
296
|
-
};
|
|
297
|
-
};
|
|
298
|
-
//#endregion
|
|
299
|
-
export { k as t };
|