You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WebCAD/src/Add-on/loadfbx.ts

55 lines
1.9 KiB

import { LoadingManager, Mesh, MeshNormalMaterial } from "three";
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
import { app } from "../ApplicationServices/Application";
import { FileSystem } from "../Common/FileSystem";
import { Command, CommandWrap } from "../Editor/CommandMachine";
import { GetBox, MoveMatrix } from "../Geometry/GeUtils";
export class Fbx implements Command
{
async exec()
{
FileSystem.ChooseFile({
filter: ".fbx", callback: async (files) =>
{
CommandWrap(() =>
{
let m = new MeshNormalMaterial();
for (let i = 0; i < files.length; i++)
{
let f = files.item(i);
let reader = new FileReader();
// Closure to capture the file information.
reader.onload = (ev) =>
{
let manager = new LoadingManager();
let loader = new FBXLoader(manager);
let obj = loader.parse(reader.result as string, "");
obj.traverse(o =>
{
if (o.type === "Mesh")
{
(<Mesh>o).material = m;
}
})
let box = GetBox(obj);
let mp = box.min.clone().negate();
obj.applyMatrix(MoveMatrix(mp));
obj.matrixWorldNeedsUpdate = true;
app.Viewer.Scene.add(obj);
}
// Read in the image file as a data URL.
reader.readAsArrayBuffer(f);
}
}, "fbx");
}
});
}
}