Skip to content

Instantly share code, notes, and snippets.

@yushimatenjin
Created April 14, 2023 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yushimatenjin/2d67b739bd5a33604337b4b20d4172ad to your computer and use it in GitHub Desktop.
Save yushimatenjin/2d67b739bd5a33604337b4b20d4172ad to your computer and use it in GitHub Desktop.
class VRMLoader {
constructor(editor) {
this.editor = editor;
this.editor.on("assets:add", (asset) => {
const type = asset.get("type");
const fileName = asset.get("name");
const fileExtension = fileName.split(".").pop();
if (type === "binary" && fileExtension === "vrm") {
setTimeout(async () => {
const name = this.replaceExtension(fileName, "glb");
const assetUrl = asset.get("file").url;
const res = await fetch(assetUrl);
const glbData = await res.arrayBuffer();
const blob = new Blob([glbData], { type: "model/gltf-binary" });
const file = new File([blob], name, { type: "model/gltf-binary" });
const dt = new DataTransfer();
dt.items.add(file);
const list = dt.files;
this.editor.call("assets:upload:files", list);
}, 1000);
}
});
}
replaceExtension(fileName, newExtension) {
const parts = fileName.split(".");
parts.pop();
parts.push(newExtension);
return parts.join(".");
}
}
new VRMLoader(editor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment