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.

71 lines (70 loc) 2.28 kB
import { useCallback as e, useRef as t } from "react"; import { ModelExporter as n } from "@vctrl/core/model-exporter"; import r from "file-saver"; //#region packages/hooks/src/use-export-model/use-export-model.ts var i = (i, a) => { let o = t(new n()), s = e(async (e, t) => { let n = e?.model; if (!n) { console.error("Scene not initialized"); return; } try { let a = o.current, s = e.name.replace(/\.[^/.]+$/, ""); if (t) { let e = await a.exportThreeJSGLB(n, {}); if (!(e.data instanceof Uint8Array)) throw Error("Expected GLB export to return Uint8Array"); r.saveAs(new Blob([new Uint8Array(e.data)]), `${s}.glb`); } else { let e = await a.exportThreeJSGLTF(n), t = await a.createZIPArchive(e, s); r.saveAs(new Blob([new Uint8Array(t)]), `${s}.zip`); } i && i(); } catch (e) { console.error("Export failed:", e), a && a(e); } }, [i, a]), c = e(async (e) => { let t = e?.model; if (!t) { console.error("Scene not initialized"); return; } try { let n = o.current, a = e.name.replace(/\.[^/.]+$/, ""), s = await n.exportThreeJSUSDZ(t); r.saveAs(new Blob([new Uint8Array(s.data)]), `${a}.usdz`), i && i(); } catch (e) { console.error("USDZ export failed:", e), a && a(e); } }, [i, a]), l = e(async (e, t) => { try { let n = o.current, a = t?.name.replace(/\.[^/.]+$/, "") || "model", s = await n.exportDocumentGLBDraco(e); r.saveAs(new Blob([new Uint8Array(s.data)]), `${a}.glb`), i && i(); } catch (e) { console.error("Draco GLB export failed:", e), a && a(e); } }, [i, a]); return { handleThreeGltfExport: s, handleDocumentGltfExport: e(async function(e, t, n = !1, s = !0) { try { let a = o.current, c = t?.name.replace(/\.[^/.]+$/, "") || "model"; if (n) { let t = await a.exportDocumentGLB(e); r.saveAs(new Blob([new Uint8Array(t.data)]), `${c}.glb`); } else { let t = await a.exportDocumentGLTF(e); if (!s) return t; let n = await a.createZIPArchive(t, c); r.saveAs(new Blob([new Uint8Array(n)]), `${c}.zip`); } i && i(); } catch (e) { console.error("Export failed:", e), a && a(e); } }, [a, i]), handleThreeUsdzExport: c, handleDocumentGlbDracoExport: l }; }; //#endregion export { i as t };