UNPKG

@vctrl/hooks

Version:

vctrl/hooks is a React hooks package designed to simplify 3D model loading and management within React applications. It's part of the vectreal-core ecosystem and is primarily used in the vctrl/viewer React component and the official website application.

128 lines (127 loc) 3.28 kB
import { useReducer as M, useRef as d, useCallback as n } from "react"; import { G as _ } from "./GLTFExporter-C4hCeb4H.js"; import { WebIO as b } from "@gltf-transform/core"; import { inspect as y, weld as B, simplify as U, dedup as F, quantize as H, normals as K, textureCompress as q } from "@gltf-transform/functions"; import { KHRMeshQuantization as G, KHRMaterialsVolume as k, KHRMaterialsTransmission as I } from "@gltf-transform/extensions"; import { MeshoptSimplifier as Q } from "meshoptimizer"; const f = { modelDoc: null, modelReport: null, error: null, loading: !1 }, V = (i, t) => { switch (t.type) { case "LOAD_START": return { ...i, loading: !0, error: null }; case "LOAD_SUCCESS": return { ...i, loading: !1, modelDoc: t.payload.modelDoc, modelReport: t.payload.modelReport }; case "LOAD_ERROR": return { ...i, loading: !1, error: t.payload }; case "RESET": return { ...f }; default: return i; } }, P = () => { const [i, t] = M(V, f), { modelDoc: s, modelReport: a, error: R, loading: S } = i, O = d(new _()), u = d( new b().registerExtensions([ G, k, I ]) ), z = n(async (r) => { t({ type: "LOAD_START" }); try { const e = { binary: !0 }, l = await O.current.parseAsync(r, e), c = new Uint8Array(l), p = await u.current.readBinary(c), m = y(p); t({ type: "LOAD_SUCCESS", payload: { modelDoc: p, modelReport: m } }); } catch (e) { t({ type: "LOAD_ERROR", payload: e }), console.error("Error loading model:", e); } }, []), o = n( async (r) => { if (s) try { await s.transform(...r); const e = y(s); t({ type: "LOAD_SUCCESS", payload: { modelDoc: s, modelReport: e } }); } catch (e) { console.error("Error applying transforms:", e); } }, [s] ), w = n( async (r) => { const { ratio: e = 0.5, error: l = 1e-3 } = r || {}; await o([ B(), U({ simplifier: Q, ratio: e, error: l }) ]); }, [o] ), E = n( async (r) => { await o([F(r)]); }, [o] ), g = n( async (r) => { await o([H(r)]); }, [o] ), D = n( async (r) => { await o([K(r)]); }, [o] ), A = n( async (r) => { r && await o([q(r)]); }, [o] ), h = n(async () => { if (!s) return null; try { return await u.current.writeBinary(s); } catch (r) { return console.error("Error getting model binary:", r), null; } }, [s]), x = n(() => { if (!a) return null; const r = (m) => m.reduce((C, L) => C + L.size, 0), e = r(a.meshes.properties), l = r(a.textures.properties), c = e + l, p = `${(c / (1024 * 1024)).toFixed(2)} MB`; return { fileSize: c, displayFileSize: p }; }, [a]), T = n(() => { t({ type: "RESET" }); }, []); return { load: z, getModel: h, getSize: x, report: a, simplifyOptimization: w, dedupOptimization: E, quantizeOptimization: g, normalsOptimization: D, texturesOptimization: A, reset: T, error: R, loading: S }; }; export { P as u };