@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.
78 lines (77 loc) • 2.38 kB
JavaScript
import y from "jszip";
import { saveAs as u } from "file-saver";
import { G as m } from "./GLTFExporter-C4hCeb4H.js";
const l = (t) => {
const r = atob(t.split(",")[1]), o = t.split(",")[0].split(":")[1].split(";")[0], e = new ArrayBuffer(r.length), s = new Uint8Array(e);
for (let n = 0; n < r.length; n++)
s[n] = r.charCodeAt(n);
return new Blob([e], { type: o });
};
function g(t, r) {
var e;
const o = (e = t.split(".").pop()) == null ? void 0 : e.split("?")[0];
return o && o !== t ? o : r && r.split("/").pop() || "png";
}
function b(t) {
return (t == null ? void 0 : t.split(".").shift()) || "scene";
}
const d = (t, r, o) => {
r.forEach((e, s) => {
const n = t[e.source];
if (!n.uri) return;
const a = g(n.uri, n.mimeType), i = `${e.name || `texture_${s}`}.${a}`;
if (n.data instanceof ArrayBuffer || n.data instanceof Uint8Array) {
const f = n.data instanceof Uint8Array ? n.data.buffer : n.data, p = new Uint8Array(f);
o.file(i, p, { binary: !0 });
} else if (n.uri.startsWith("data:")) {
const f = l(n.uri);
o.file(i, f, { binary: !0 });
} else
console.warn(`Image at index ${s} has unsupported data format`);
});
}, x = (t, r, o) => {
t.forEach((e, s) => {
const n = `${o}.bin`;
if (e instanceof ArrayBuffer) {
const a = new Uint8Array(e);
r.file(n, a, { binary: !0 });
} else if (e.uri) {
const a = l(e.uri);
r.file(n, a, { binary: !0 });
} else
console.warn(
`Buffer at index ${s} is not an ArrayBuffer or does not have a URI`
);
});
}, A = (t, r) => {
const o = new y();
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) => {
u(e, `${r}.zip`);
});
}, $ = (t, r) => ({
handleGltfExport: (e, s) => {
const n = e == null ? void 0 : e.model;
if (!n) {
console.error("Scene not initialized");
return;
}
const a = new m(), c = {
binary: s,
includeCustomExtensions: !0
};
a.parse(
n,
(i) => {
const f = b(e.name);
i instanceof ArrayBuffer ? (u(new Blob([i]), `${f}.glb`), t && t()) : (A(i, f), t && t());
},
(i) => {
console.error(i), r && r(i);
},
c
);
}
});
export {
$ as u
};