Import a 3D Model and automatically assign and export animations
TypeScript
human skeleton finger variations
Scott Petrovice7696fdb1519
diff --git a/src/index.html b/src/index.htmlindex 381d99c..d986ab7 100644--- a/src/index.html+++ b/src/index.html@@ -57,7 +57,7 @@<div style="text-align: center; font-size: 1.5rem">or</div>- <div style="display: flex; flex-direction: column; gap: 1rem; background-color: #0f2f3d; padding: 1rem;">+ <div class="alternate-background-section"><p style="margin: 0">Reference model:</p><select id="model-selection"><option value="models/human-mannequin.glb">Human</option>@@ -104,18 +104,38 @@<hr />- <label>Skeleton Type to use:</label>- <select id="skeleton-selection">- <option value="human">Human</option>- <option value="quadraped">4 Leg Creature</option>- <option value="bird">Bird</option>- </select>- <!-- <hr /> -->+ <div style="display: flex; flex-direction: column; gap: 1rem;">+ <span>Skeleton Type to use:</span>+ <select id="skeleton-selection">+ <option value="human">Human</option>+ <option value="quadraped">4 Leg Creature</option>+ <option value="bird">Bird</option>+ </select>+ </div>- <button id="load-skeleton-button">Load Skeleton ›</button>+ <!-- Hand skeleton options for human skeleton -->+ <div id="hand-skeleton-options" class="alternate-background-section" >++ <span>+ Hand Options:+ <div class="tooltip" style="display: inline-block; margin-left: 0.5rem;">+ <span class="material-symbols-outlined" style="font-size: 16px;">help</span>+ <span class="tooltiptext">Variations on which finger bones you want to keep. </span>+ </div>+ </span>+ <select id="hand-skeleton-selection">+ <option value="all-fingers">All Fingers</option>+ <option value="thumb-and-index">Thumb + Main Finger</option>+ <option value="simplified-hand">Simplified Hand</option>+ </select>+ </div>++ <!-- <hr /> -->++ <button id="load-skeleton-button">Load Skeleton ›</button></div>diff --git a/src/lib/UI.ts b/src/lib/UI.tsindex e2dae65..092c86a 100644--- a/src/lib/UI.ts+++ b/src/lib/UI.ts@@ -18,6 +18,8 @@ export class UI {dom_load_skeleton_button: HTMLButtonElement | null = nulldom_skeleton_edit_tools: HTMLElement | null = nulldom_skeleton_drop_type: HTMLSelectElement | null = null+ dom_hand_skeleton_options: HTMLElement | null = null+ dom_hand_skeleton_selection: HTMLSelectElement | null = nulldom_mirror_skeleton_checkbox: HTMLElement | null = nulldom_scale_skeleton_button: HTMLButtonElement | null = nulldom_undo_button: HTMLButtonElement | null = null@@ -86,6 +88,8 @@ export class UI {this.dom_load_skeleton_button = document.querySelector('#load-skeleton-button')this.dom_skeleton_edit_tools = document.querySelector('#skeleton-step-actions')this.dom_skeleton_drop_type = document.querySelector('#skeleton-selection')+ this.dom_hand_skeleton_options = document.querySelector('#hand-skeleton-options')+ this.dom_hand_skeleton_selection = document.querySelector('#hand-skeleton-selection')this.dom_mirror_skeleton_checkbox = document.querySelector('#mirror-skeleton')this.dom_scale_skeleton_button = document.querySelector('#scale-skeleton-button')this.dom_undo_button = document.querySelector('#undo-button')diff --git a/src/lib/enums/SkeletonType.ts b/src/lib/enums/SkeletonType.tsindex c64749e..322d8b2 100644--- a/src/lib/enums/SkeletonType.ts+++ b/src/lib/enums/SkeletonType.ts@@ -5,3 +5,10 @@ export enum SkeletonType {Human = 'skeletons/human-skeleton.glb',Bird = 'skeletons/skeleton-bird.glb',}++// Hand skeleton variation types for human skeleton+export enum HandSkeletonType {+ AllFingers = 'all-fingers',+ ThumbAndIndex = 'thumb-and-index',+ SimplifiedHand = 'simplified-hand'+}diff --git a/src/lib/processes/StepLoadSkeleton.ts b/src/lib/processes/StepLoadSkeleton.tsindex 07a5eb2..03a4573 100644--- a/src/lib/processes/StepLoadSkeleton.ts+++ b/src/lib/processes/StepLoadSkeleton.ts@@ -1,7 +1,7 @@import { UI } from '../UI.ts'import { Object3D, type Object3DEventMap } from 'three'import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'-import { SkeletonType } from '../enums/SkeletonType.js'+import { SkeletonType, HandSkeletonType } from '../enums/SkeletonType.js'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepLoadSkeleton extends EventTarget {@@ -9,6 +9,7 @@ export class StepLoadSkeleton extends EventTarget {private readonly ui: UI = new UI()private loaded_armature: Object3D = new Object3D()private skeleton_t: SkeletonType = SkeletonType.Human+ private hand_skeleton_t: HandSkeletonType = HandSkeletonType.AllFingerspublic skeleton_type (): SkeletonType {return this.skeleton_t@@ -28,9 +29,19 @@ export class StepLoadSkeleton extends EventTarget {}this.add_event_listeners()++ // Initialize hand skeleton options visibility+ this.toggle_hand_skeleton_options()}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', () => {+ this.toggle_hand_skeleton_options()+ })+ }+if (this.ui.dom_load_skeleton_button !== null) {this.ui.dom_load_skeleton_button.addEventListener('click', () => {if (this.ui.dom_skeleton_drop_type === null) {@@ -43,6 +54,14 @@ export class StepLoadSkeleton extends EventTarget {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 loadswitch (skeleton_file) {@@ -59,7 +78,7 @@ export class StepLoadSkeleton extends EventTarget {// load skeleton from GLB fileconsole.log('trying to load skeleton', this.skeleton_t)- this.loader.load(this.skeleton_t, (gltf) => {+ this.loader.load(this.skeleton_t, (gltf: any) => {// traverse scene and find first bone object// we will go to the parent and mark that as the original armaturelet armature_found = false@@ -85,6 +104,11 @@ export class StepLoadSkeleton extends EventTarget {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)+ }+// reset the armature to 0,0,0 in case it is off for some reasonthis.loaded_armature.position.set(0, 0, 0)this.loaded_armature.updateWorldMatrix(true, true)@@ -98,4 +122,87 @@ export class StepLoadSkeleton extends EventTarget {public armature (): Object3D<Object3DEventMap> {return this.loaded_armature}++ private toggle_hand_skeleton_options (): void {+ if (this.ui.dom_skeleton_drop_type === null || this.ui.dom_hand_skeleton_options === null) {+ return+ }++ const skeleton_selection = this.ui.dom_skeleton_drop_type.options+ const skeleton_file: string = skeleton_selection[skeleton_selection.selectedIndex].value++ if (skeleton_file === 'human') {+ this.ui.dom_hand_skeleton_options.style.display = 'flex'+ } else {+ this.ui.dom_hand_skeleton_options.style.display = 'none'+ }+ }++ private modify_hand_skeleton (armature: Object3D, hand_type: HandSkeletonType): void {+ const bones_to_remove: any[] = []++ armature.traverse((child: any) => {+ if (child.type === 'Bone') {+ const bone = child+ const bone_name = (bone.name ?? '').toLowerCase() as string++ // 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_finger_bone(bone_name) &&+ !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+ }+ }+ })++ // 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_finger_bone (bone_name: string): boolean {+ const finger_patterns = ['finger', 'thumb', 'index', 'middle', 'ring', 'pinky']+ return finger_patterns.some(pattern => bone_name.includes(pattern))+ }++ private is_thumb_bone (bone_name: string): boolean {+ return bone_name.includes('thumb')+ }++ private is_index_finger_bone (bone_name: string): boolean {+ return bone_name.includes('index')+ }++ 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/styles.css b/src/styles.cssindex a411f22..09fec10 100644--- a/src/styles.css+++ b/src/styles.css@@ -373,7 +373,7 @@ input[type="file"] {/* Tooltip text */.tooltip .tooltiptext {visibility: hidden;- width: 200px;+ width: 150px;background-color: #0b1822;color: #fff;text-align: center;@@ -427,4 +427,12 @@ input[type="file"] {border-radius: 3px;flex-direction: column;+}++.alternate-background-section {+ display: flex;+ flex-direction: column;+ gap: 1rem;+ background-color: #0f2f3d;+ padding: 1rem;}\ No newline at end of file