Import a 3D Model and automatically assign and export animations
TypeScript
Create a custom FBX loader to help monitor when there are broken images being loaded
Scott Petrovicb9053da90619
diff --git a/src/lib/processes/load-model/CustomFBXLoader.ts b/src/lib/processes/load-model/CustomFBXLoader.tsnew file mode 100644index 0000000..2389b12--- /dev/null+++ b/src/lib/processes/load-model/CustomFBXLoader.ts@@ -0,0 +1,68 @@+import { EventDispatcher, type Object3DEventMap, type Group } from 'three'+import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader'+import { ModalDialog } from '../../ModalDialog'++export interface FBXResults {+ fbx_scene: Group<Object3DEventMap>+ is_missing_dependencies: boolean+}++export class CustomFBXLoader extends EventDispatcher {+ private readonly loader: FBXLoader++ constructor () {+ super()+ this.loader = new FBXLoader()+ }++ public async loadFBX (url: string): Promise<FBXResults> {+ return await new Promise<FBXResults>((resolve, reject) => {+ let has_finished_loading_fbx = false+ let has_finished_loading_all_dependencies = false+ let is_missing_dependencies = false+ let loaded_fbx: Group++ const check_if_complete = (): void => {+ const is_complete = has_finished_loading_fbx && has_finished_loading_all_dependencies+ if (is_complete) {+ const fbx_results: FBXResults = {+ fbx_scene: loaded_fbx,+ is_missing_dependencies+ } satisfies FBXResults+ resolve(fbx_results)+ }+ }++ // check for errors while loading dependencies like images+ this.loader.manager.itemError = (url: string) => {+ is_missing_dependencies = true+ new ModalDialog(+ 'Error loading resource associated with FBX.',+ `The material will be replaced with a basic material for now. You will need to update this outside the application: ${url+ .split('/')+ .pop()}`+ ).show()+ }++ this.loader.load(+ url,+ (fbx) => {+ loaded_fbx = fbx+ has_finished_loading_fbx = true+ check_if_complete()+ },+ undefined, // onProgress callback+ (error) => {+ reject(error)+ }+ )++ // check for image loading errors+ this.loader.manager.onLoad = () => {+ has_finished_loading_all_dependencies = true+ check_if_complete()+ }+ })+ }+}+diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.tsindex 96ae15b..c9a78d9 100644--- a/src/lib/processes/load-model/StepLoadModel.ts+++ b/src/lib/processes/load-model/StepLoadModel.ts@@ -1,8 +1,8 @@import { UI } from '../../UI.ts'import { ModelZipLoader } from './ModelZipLoader.ts'+import { CustomFBXLoader, type FBXResults } from './CustomFBXLoader.ts'import { Box3 } from 'three/src/math/Box3.js'import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'-import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js'import { Scene } from 'three/src/scenes/Scene.js'import { Mesh } from 'three/src/objects/Mesh.js'@@ -15,7 +15,7 @@ import { ModalDialog } from '../../ModalDialog.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepLoadModel extends EventTarget {private readonly gltf_loader = new GLTFLoader()- private readonly fbx_loader = new FBXLoader()+ private readonly custom_fbx_loader: CustomFBXLoader = new CustomFBXLoader()private readonly ui: UI = UI.getInstance()private original_model_data: Scene = new Scene()private final_mesh_data: Scene = new Scene()@@ -27,6 +27,10 @@ export class StepLoadModel extends EventTarget {private _added_event_listeners: boolean = false+ // this can happen when images are not loading. this+ // will mess up model and need to just replace the entire material+ private mesh_has_broken_material: boolean = false+// for debugging, let's count these to help us test performance things bettervertex_count = 0triangle_count = 0@@ -64,6 +68,13 @@ export class StepLoadModel extends EventTarget {geometry_to_add.name = child.namethis.geometry_list.push(geometry_to_add)+ // material is broken somehow, so just use a normal material to help communicate this+ if (this.mesh_has_broken_material) {+ const new_material: Material = new MeshNormalMaterial()+ this.material_list.push(new_material)+ return+ }+const new_material: Material = (child as Mesh).material.clone()this.material_list.push(new_material)}@@ -150,12 +161,7 @@ export class StepLoadModel extends EventTarget {public load_model_file (model_file_path: string | ArrayBuffer | null, file_extension: string): void {if (file_extension === 'fbx') {- console.log('Loading FBX model:', model_file_path)- this.fbx_loader.load(model_file_path as string, (fbx) => {- const loaded_scene: Scene = new Scene()- loaded_scene.add(fbx)- this.process_loaded_scene(loaded_scene)- })+ this.load_fbx_file(model_file_path)} else if (file_extension === 'glb') {this.gltf_loader.load(model_file_path as string, (gltf) => {const loaded_scene: Scene = gltf.scene@@ -169,6 +175,26 @@ export class StepLoadModel extends EventTarget {}}+ private load_fbx_file (model_file_path: string | ArrayBuffer | null): void {+ if (typeof model_file_path !== 'string') {+ console.warn('something weird happened and FBX is being loaded that is not a filepath for a string:', model_file_path)+ return+ }++ // console.log('Loading FBX model:', model_file_path)+ this.custom_fbx_loader.loadFBX(model_file_path).then((results: FBXResults) => {+ console.log('loaded the FBX file', results)+ this.mesh_has_broken_material = results.is_missing_dependencies+ const loaded_scene: Scene = new Scene()+ this.process_loaded_scene(loaded_scene)++ // TODO: add the processed, cleaned up data instead of the fbx_scene directly+ loaded_scene.add(results.fbx_scene)+ }).catch((error) => {+ console.warn('some type of error', error)+ })+ }+/*** Handles loading a model from a ZIP file with GLTF data* supporting both data URLs and ArrayBuffer input.