Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Start adding a skeleton preview manager to preview skeleton

Scott Petrovic02a2c7c7687a
diff --git a/src/lib/enums/SkeletonType.ts b/src/lib/enums/SkeletonType.ts
index 3409e3c..0fc8fd7 100644
--- a/src/lib/enums/SkeletonType.ts
+++ b/src/lib/enums/SkeletonType.ts
@@ -4,6 +4,8 @@ export enum SkeletonType {
Quadraped = 'skeletons/skeleton-quadraped.glb',
Human = 'skeletons/human-skeleton.glb',
Bird = 'skeletons/skeleton-bird.glb',
+ Error = 'ERROR',
+ None = 'None'
}
// Hand skeleton variation types for human skeleton
diff --git a/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts b/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
new file mode 100644
index 0000000..21a8e49
--- /dev/null
+++ b/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
@@ -0,0 +1,45 @@
+import { Group, Object3D, Object3DEventMap, SkeletonHelper, type Scene } from 'three'
+import { GLTF, GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
+import type GLTFResult from './interfaces/GLTFResult'
+import { type SkeletonType } from '../../enums/SkeletonType'
+
+const skeleton_group_name: string = 'preview_skeleton_group'
+
+// need a function that will add a preview skeleton to the scene
+// this will remove any existing preview skeleton first
+// and then add the new one based on the selected type
+export async function add_preview_skeleton (root: Scene, skeleton_file_path: SkeletonType): Promise<Object3D<Object3DEventMap>> {
+ remove_preview_skeleton(root)
+
+ // create new group for the preview skeleton
+ let preview_skeleton_group = root.getObjectByName(skeleton_group_name) as Group
+ if (!preview_skeleton_group) {
+ preview_skeleton_group = new Group()
+ preview_skeleton_group.name = skeleton_group_name
+ root.add(preview_skeleton_group)
+ }
+
+ // need some logic to load the actual skeleton based on the type
+ // we can only see the skeleton helper, so jwe will add that to the group
+ const loaded_scene: Object3D<Object3DEventMap> = await load_skeleton(skeleton_file_path)
+ loaded_scene.name = 'preview_skeleton'
+ preview_skeleton_group.add(loaded_scene)
+ root.add(preview_skeleton_group)
+ console.log('loaded skeleton: ', loaded_scene)
+ return loaded_scene
+}
+
+async function load_skeleton (file_path: string): Promise<Object3D<Object3DEventMap>> {
+ const loader = new GLTFLoader()
+ const gltf: GLTF | GLTFResult = await loader.loadAsync(file_path)
+ // If your GLTFResult extends GLTF and has `.scene`, this is fine:
+ return gltf.scene as Object3D<Object3DEventMap>
+}
+
+// need a function that will remove the preview skeleton from the scene
+export function remove_preview_skeleton (root: Scene): void {
+ const skeleton_group = root.getObjectByName(skeleton_group_name)
+ if (skeleton_group !== undefined && skeleton_group.parent) {
+ skeleton_group.parent.remove(skeleton_group)
+ }
+}
diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
index 4072c9f..b71a005 100644
--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
@@ -1,23 +1,24 @@
import { UI } from '../../UI.ts'
-import { Object3D, type Scene, type Object3DEventMap } from 'three'
+import { Object3D, type Scene, type Object3DEventMap, Skeleton } from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { SkeletonType, HandSkeletonType } from '../../enums/SkeletonType.js'
import type GLTFResult from './interfaces/GLTFResult.ts'
import { add_origin_markers, remove_origin_markers } from './OriginMarkerManager'
+import { add_preview_skeleton } from './PreviewSkeletonManager.ts'
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepLoadSkeleton extends EventTarget {
private readonly loader: GLTFLoader = new GLTFLoader()
private readonly ui: UI = UI.getInstance()
private loaded_armature: Object3D = new Object3D()
- private skeleton_t: SkeletonType = SkeletonType.Human
+ private skeleton_t: SkeletonType = SkeletonType.None
private hand_skeleton_t: HandSkeletonType = HandSkeletonType.AllFingers
private _added_event_listeners: boolean = false
private readonly _main_scene: Scene
public skeleton_type (): SkeletonType {
- return this.skeleton_t
+ return this.skeleton_file_path() // this is actually the type/filepath combo
}
constructor (main_scene: Scene) {
@@ -44,7 +45,7 @@ export class StepLoadSkeleton extends EventTarget {
this._added_event_listeners = true
}
- // Initialize hand skeleton options visibility
+ // Initialize hand skeleton hand options visibility
this.toggle_hand_skeleton_options()
// add origin markers for debugging model loading issues
@@ -67,23 +68,49 @@ export class StepLoadSkeleton extends EventTarget {
remove_origin_markers(this._main_scene)
}
+ private skeleton_file_path (): SkeletonType {
+ // get currently selected option out of the model-selection drop-down
+ const skeleton_selection = this.ui.dom_skeleton_drop_type.options
+ const skeleton_file: string = skeleton_selection[skeleton_selection.selectedIndex].value
+
+ // set the skeleton type. This will be used for the animations listing later
+ // so it knows what animations to load
+ switch (skeleton_file) {
+ case 'quadraped':
+ return SkeletonType.Quadraped
+ case 'human':
+ return SkeletonType.Human
+ case 'bird':
+ return SkeletonType.Bird
+ default:
+ console.error('unknown skeleton type selected: ', skeleton_file)
+ return SkeletonType.Error
+ }
+ }
+
private add_event_listeners (): void {
// Add event listener for skeleton type changes to show/hide hand options
if (this.ui.dom_skeleton_drop_type !== null) {
this.ui.dom_skeleton_drop_type.addEventListener('change', () => {
// get selected value from skeleton options
const skeleton_selection = this.ui.dom_skeleton_drop_type.options
- const skeleton_selected_option: string = skeleton_selection[skeleton_selection.selectedIndex].value
+ this.skeleton_t = skeleton_selection[skeleton_selection.selectedIndex].value
- this.toggle_hand_skeleton_options(skeleton_selected_option)
+ this.toggle_hand_skeleton_options()
// remove the "select a skeleton" option if we picked something else
if (this.has_select_skeleton_ui_option()) {
this.ui.dom_skeleton_drop_type?.options.remove(0)
}
- // enable the ability to progress to next step
- this.allow_proceeding_to_next_step(true)
+ // load the preview skeleton
+ // need to get the file name for the correct skeleton
+ add_preview_skeleton(this._main_scene, this.skeleton_file_path()).then(() => {
+ // enable the ability to progress to next step
+ this.allow_proceeding_to_next_step(true)
+ }).catch((err) => {
+ console.error('error loading preview skeleton: ', err)
+ })
})
}
@@ -94,31 +121,10 @@ export class StepLoadSkeleton extends EventTarget {
return
}
- // get currently selected option out of the model-selection drop-down
- const skeleton_selection = this.ui.dom_skeleton_drop_type.options
-
- const skeleton_file: string = skeleton_selection[skeleton_selection.selectedIndex].value
-
// Get hand skeleton selection for human skeletons
- let hand_skeleton_selection = HandSkeletonType.AllFingers
- if (skeleton_file === 'human' && this.ui.dom_hand_skeleton_selection !== null) {
- const hand_selection = this.ui.dom_hand_skeleton_selection.options
- hand_skeleton_selection = hand_selection[hand_selection.selectedIndex].value as HandSkeletonType
- this.hand_skeleton_t = hand_skeleton_selection
- }
-
- // set the skeleton type. This will be used for the animations listing later
- // so it knows what animations to load
- switch (skeleton_file) {
- case 'quadraped':
- this.skeleton_t = SkeletonType.Quadraped
- break
- case 'human':
- this.skeleton_t = SkeletonType.Human
- break
- case 'bird':
- this.skeleton_t = SkeletonType.Bird
- break
+ if (this.skeleton_file_path() === SkeletonType.Human) {
+ const hand_selection = this.ui.dom_hand_skeleton_selection?.options
+ this.hand_skeleton_t = hand_selection[hand_selection.selectedIndex].value as HandSkeletonType
}
// load skeleton from GLB file
@@ -144,14 +150,12 @@ export class StepLoadSkeleton extends EventTarget {
}
})
- console.log('loaded GLTF file with data: ', gltf)
-
this.loaded_armature = original_armature.clone()
this.loaded_armature.name = 'Loaded Armature'
// Apply hand skeleton modifications for human skeletons
if (this.skeleton_t === SkeletonType.Human) {
- this.modify_hand_skeleton(this.loaded_armature, hand_skeleton_selection)
+ this.modify_hand_skeleton(this.loaded_armature, this.hand_skeleton_t)
}
// reset the armature to 0,0,0 in case it is off for some reason
@@ -178,12 +182,12 @@ export class StepLoadSkeleton extends EventTarget {
return this.loaded_armature
}
- private toggle_hand_skeleton_options (skeleton_value: string): void {
+ private toggle_hand_skeleton_options (): void {
if (this.ui.dom_skeleton_drop_type === null || this.ui.dom_hand_skeleton_options === null) {
return
}
- if (skeleton_value === 'human') {
+ if (this.skeleton_file_path() === SkeletonType.Human) {
this.ui.dom_hand_skeleton_options.style.display = 'flex'
} else {
this.ui.dom_hand_skeleton_options.style.display = 'none'