Import a 3D Model and automatically assign and export animations
TypeScript
Move hand bone removal logic to separate class
Scott Petrovicf3e4b559502e
diff --git a/src/lib/Utilities.ts b/src/lib/Utilities.tsindex a053d01..981ab90 100644--- a/src/lib/Utilities.ts+++ b/src/lib/Utilities.ts@@ -1,5 +1,5 @@import {- Vector3, Vector2, type Object3D, Mesh, Group, Bone, type Skeleton, Euler, Raycaster, AnimationClip, KeyframeTrack,+ Vector3, Vector2, type Object3D, Mesh, Group, Bone, type Skeleton, Euler, Raycaster, AnimationClip, type KeyframeTrack,type PerspectiveCamera, type Scene, type Object3DEventMap, type BufferAttribute, type BufferGeometry, type InterleavedBufferAttribute} from 'three'import BoneTransformState from './interfaces/BoneTransformState'diff --git a/src/lib/processes/load-skeleton/HandHelper.ts b/src/lib/processes/load-skeleton/HandHelper.tsnew file mode 100644index 0000000..16d2c01--- /dev/null+++ b/src/lib/processes/load-skeleton/HandHelper.ts@@ -0,0 +1,92 @@+import { type Object3D } from 'three'+import { HandSkeletonType } from '../../enums/SkeletonType'++export class HandHelper {+ public modify_hand_skeleton (armature: Object3D, hand_type: HandSkeletonType): void {+ const bones_to_remove: Object3D[] = []++ armature.traverse((child: Object3D) => {+ if (child.type === 'Bone') {+ const bone = child+ const bone_name: string = (bone.name ?? '').toLowerCase()++ // bone is not part of hand, we can just skip it+ // we also always keep the palm of the hand+ if (!this.is_hand_bone(bone_name) || this.is_palm_bone(bone_name)) {+ return+ }++ // remove bones on mesh based on hand type selected on UI+ switch (hand_type) {+ case HandSkeletonType.ThumbAndIndex:+ // Remove all finger bones except thumb and index finger+ if (!this.is_thumb_bone(bone_name) &&+ !this.is_middle_finger_bone(bone_name)) {+ bones_to_remove.push(bone)+ }++ // also remove tip bones since we want something simplified+ if (this.is_end_tip_bone(bone_name)) {+ bones_to_remove.push(bone)+ }+ break++ case HandSkeletonType.SimplifiedHand:+ // Remove all finger/feet tip bones (bones ending with 'tip')+ if (this.is_end_tip_bone(bone_name)) {+ bones_to_remove.push(bone)+ }+ break++ case HandSkeletonType.SingleBone:+ // Remove all non-middle finger bones, keeping one line of bones+ if (!this.is_middle_finger_bone(bone_name)) {+ bones_to_remove.push(bone)+ break+ }++ // remove all tip bones+ if (this.is_end_tip_bone(bone_name) || bone_name.includes('03') || bone_name.includes('04') || bone_name.includes('02')) {+ bones_to_remove.push(bone)+ }++ break+ }+ }+ })++ // Remove the identified bones+ bones_to_remove.forEach(bone => {+ if (bone.parent != null) {+ bone.parent.remove(bone)+ }+ })++ console.log(`Modified hand skeleton: ${hand_type}, removed ${bones_to_remove.length} bones`)+ }++ private is_hand_bone (bone_name: string): boolean {+ const hand_patterns = ['hand', 'finger', 'thumb', 'index', 'middle', 'ring', 'pinky']+ return hand_patterns.some(pattern => bone_name.includes(pattern))+ }++ private is_palm_bone (bone_name: string): boolean {+ const hand_patterns = ['hand']+ return hand_patterns.some(pattern => bone_name.includes(pattern))+ }++ private is_thumb_bone (bone_name: string): boolean {+ return bone_name.includes('thumb')+ }++ private is_middle_finger_bone (bone_name: string): boolean {+ return bone_name.includes('middle')+ }++ // "tip" bones are the last bone at the end of the fingers and feet+ // they have the word 'tip' in the bone name+ private is_end_tip_bone (bone_name: string): boolean {+ // Look for bones that are finger tips - usually end with 'tip', 'end', or numbers like '3'+ return bone_name.toLowerCase().includes('tip')+ }+}diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.tsindex 658bb50..d1d9f2d 100644--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts@@ -1,18 +1,17 @@import { UI } from '../../UI.ts'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 { SkeletonType, type 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, remove_preview_skeleton } from './PreviewSkeletonManager.ts'+import { HandHelper } from './HandHelper.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport 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.None- private hand_skeleton_t: HandSkeletonType = HandSkeletonType.AllFingersprivate _added_event_listeners: boolean = falseprivate readonly _main_scene: Scene@@ -156,8 +155,9 @@ export class StepLoadSkeleton extends EventTarget {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, this.hand_skeleton_type())+ if (this.skeleton_file_path() === SkeletonType.Human) {+ const helper = new HandHelper()+ helper.modify_hand_skeleton(this.loaded_armature, this.hand_skeleton_type())}// reset the armature to 0,0,0 in case it is off for some reason@@ -203,92 +203,4 @@ export class StepLoadSkeleton extends EventTarget {this.ui.dom_hand_skeleton_options.style.display = 'none'}}-- private modify_hand_skeleton (armature: Object3D, hand_type: HandSkeletonType): void {- const bones_to_remove: Object3D[] = []-- armature.traverse((child: Object3D) => {- if (child.type === 'Bone') {- const bone = child- const bone_name = (bone.name ?? '').toLowerCase() as string-- // bone is not part of hand, we can just skip it- // we also always keep the palm of the hand- if (!this.is_hand_bone(bone_name) || this.is_palm_bone(bone_name)) {- return- }-- // remove bones on mesh based on hand type selected on UI- switch (hand_type) {- case HandSkeletonType.ThumbAndIndex:- // Remove all finger bones except thumb and index finger- if (!this.is_thumb_bone(bone_name) &&- !this.is_middle_finger_bone(bone_name)) {- bones_to_remove.push(bone)- }-- // also remove tip bones since we want something simplified- if (this.is_end_tip_bone(bone_name)) {- bones_to_remove.push(bone)- }- break-- case HandSkeletonType.SimplifiedHand:- // Remove all finger/feet tip bones (bones ending with 'tip')- if (this.is_end_tip_bone(bone_name)) {- bones_to_remove.push(bone)- }- break-- case HandSkeletonType.SingleBone:- // Remove all non-middle finger bones, keeping one line of bones- if (!this.is_middle_finger_bone(bone_name)) {- bones_to_remove.push(bone)- break- }-- // remove all tip bones- if (this.is_end_tip_bone(bone_name) || bone_name.includes('03') || bone_name.includes('04') || bone_name.includes('02')) {- bones_to_remove.push(bone)- }-- break- }- }- })-- // Remove the identified bones- bones_to_remove.forEach(bone => {- if (bone.parent != null) {- bone.parent.remove(bone)- }- })-- console.log(`Modified hand skeleton: ${hand_type}, removed ${bones_to_remove.length} bones`)- }-- private is_hand_bone (bone_name: string): boolean {- const hand_patterns = ['hand', 'finger', 'thumb', 'index', 'middle', 'ring', 'pinky']- return hand_patterns.some(pattern => bone_name.includes(pattern))- }-- private is_palm_bone (bone_name: string): boolean {- const hand_patterns = ['hand']- return hand_patterns.some(pattern => bone_name.includes(pattern))- }-- private is_thumb_bone (bone_name: string): boolean {- return bone_name.includes('thumb')- }-- private is_middle_finger_bone (bone_name: string): boolean {- return bone_name.includes('middle')- }-- // "tip" bones are the last bone at the end of the fingers and feet- // they have the word 'tip' in the bone name- private is_end_tip_bone (bone_name: string): boolean {- // Look for bones that are finger tips - usually end with 'tip', 'end', or numbers like '3'- return bone_name.toLowerCase().includes('tip')- }}