Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

make the a-pose adjustment non-destructive and real-time

Nathan Fenner2eca5561fb6f
diff --git a/package-lock.json b/package-lock.json
index c400cc8..cc5466f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -14,6 +14,7 @@
"vite-plugin-glsl": "^1.1.2"
},
"devDependencies": {
+ "@types/three": "^0.179.0",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"eslint": "^8.56.0",
"eslint-config-standard-with-typescript": "^43.0.0",
@@ -33,6 +34,13 @@
"node": ">=0.10.0"
}
},
+ "node_modules/@dimforge/rapier3d-compat": {
+ "version": "0.12.0",
+ "resolved": "https://registry.npmjs.org/@dimforge/rapier3d-compat/-/rapier3d-compat-0.12.0.tgz",
+ "integrity": "sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow==",
+ "dev": true,
+ "license": "Apache-2.0"
+ },
"node_modules/@esbuild/win32-x64": {
"version": "0.18.20",
"resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.18.20.tgz",
@@ -193,6 +201,13 @@
}
}
},
+ "node_modules/@tweenjs/tween.js": {
+ "version": "23.1.3",
+ "resolved": "https://registry.npmjs.org/@tweenjs/tween.js/-/tween.js-23.1.3.tgz",
+ "integrity": "sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/@types/estree": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz",
@@ -216,6 +231,36 @@
"integrity": "sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==",
"dev": true
},
+ "node_modules/@types/stats.js": {
+ "version": "0.17.4",
+ "resolved": "https://registry.npmjs.org/@types/stats.js/-/stats.js-0.17.4.tgz",
+ "integrity": "sha512-jIBvWWShCvlBqBNIZt0KAshWpvSjhkwkEu4ZUcASoAvhmrgAUI2t1dXrjSL4xXVLB4FznPrIsX3nKXFl/Dt4vA==",
+ "dev": true,
+ "license": "MIT"
+ },
+ "node_modules/@types/three": {
+ "version": "0.179.0",
+ "resolved": "https://registry.npmjs.org/@types/three/-/three-0.179.0.tgz",
+ "integrity": "sha512-VgbFG2Pgsm84BqdegZzr7w2aKbQxmgzIu4Dy7/75ygiD/0P68LKmp5ie08KMPNqGTQwIge8s6D1guZf1RnZE0A==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@dimforge/rapier3d-compat": "~0.12.0",
+ "@tweenjs/tween.js": "~23.1.3",
+ "@types/stats.js": "*",
+ "@types/webxr": "*",
+ "@webgpu/types": "*",
+ "fflate": "~0.8.2",
+ "meshoptimizer": "~0.22.0"
+ }
+ },
+ "node_modules/@types/webxr": {
+ "version": "0.5.22",
+ "resolved": "https://registry.npmjs.org/@types/webxr/-/webxr-0.5.22.tgz",
+ "integrity": "sha512-Vr6Stjv5jPRqH690f5I5GLjVk8GSsoQSYJ2FVd/3jJF7KaqfwPi3ehfBS96mlQ2kPCwZaX6U0rG2+NGHBKkA/A==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "6.15.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.15.0.tgz",
@@ -411,6 +456,13 @@
"integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==",
"dev": true
},
+ "node_modules/@webgpu/types": {
+ "version": "0.1.64",
+ "resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.64.tgz",
+ "integrity": "sha512-84kRIAGV46LJTlJZWxShiOrNL30A+9KokD7RB3dRCIqODFjodS5tCD5yyiZ8kIReGVZSDfA3XkkwyyOIF6K62A==",
+ "dev": true,
+ "license": "BSD-3-Clause"
+ },
"node_modules/acorn": {
"version": "8.11.2",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.2.tgz",
@@ -1385,6 +1437,13 @@
"reusify": "^1.0.4"
}
},
+ "node_modules/fflate": {
+ "version": "0.8.2",
+ "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz",
+ "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/file-entry-cache": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
@@ -2181,6 +2240,13 @@
"node": ">= 8"
}
},
+ "node_modules/meshoptimizer": {
+ "version": "0.22.0",
+ "resolved": "https://registry.npmjs.org/meshoptimizer/-/meshoptimizer-0.22.0.tgz",
+ "integrity": "sha512-IebiK79sqIy+E4EgOr+CAw+Ke8hAspXKzBd0JdgEmPHiAwmvEj2S4h1rfvo+o/BnfEYd/jAOg5IeeIjzlzSnDg==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/micromatch": {
"version": "4.0.5",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz",
diff --git a/package.json b/package.json
index 6e57243..34b8dca 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,7 @@
"build": "vite build"
},
"devDependencies": {
+ "@types/three": "^0.179.0",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"eslint": "^8.56.0",
"eslint-config-standard-with-typescript": "^43.0.0",
diff --git a/src/index.html b/src/index.html
index a0b4f48..9aad2af 100644
--- a/src/index.html
+++ b/src/index.html
@@ -270,21 +270,21 @@
</div>
<div style="display: flex; flex-direction: row; gap: 1rem; justify-content: flex-start; align-items: center;">
- <input type="number" id="extend-arm-input" name="arm-extend-input" value="3" min="-25" max="25" step="1">
+ <input type="number" id="extend-arm-numeric-input" name="arm-extend-input" value="0" min="-50" max="50" step="1">
<span class="suffix-unit">%</span>
- <button id="extend-arm-button" class="secondary-button" >Open Arms</button>
-
- <!-- checkbox option to show skeleton -->
- <div class="styled-checkbox icon-toggle">
- <input type="checkbox" id="show-skeleton-checkbox" name="show-skeleton" value="show" style="display:none;">
- <label for="show-skeleton-checkbox" title="Show skeleton" tabindex="0">
- <img src="images/icons/bone-display.svg" width="30" height="30" alt="Show skeleton" />
- </label>
- </div>
+ <label><input id="extend-arm-range-input" type="range" min="-50" max="50" value="0" /> Open Arms</label>
</div>
-
- <hr />
-
+
+ <hr />
+
+ </div>
+
+ <!-- checkbox option to show skeleton -->
+ <div class="styled-checkbox icon-toggle">
+ <input type="checkbox" id="show-skeleton-checkbox" name="show-skeleton" value="show" style="display:none;">
+ <label for="show-skeleton-checkbox" title="Show skeleton" tabindex="0">
+ <img src="images/icons/bone-display.svg" width="30" height="30" alt="Show skeleton" />
+ </label>
</div>
diff --git a/src/lib/UI.ts b/src/lib/UI.ts
index 3da1fcf..821c326 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -54,8 +54,8 @@ export class UI {
dom_total_time: HTMLElement | null = null
dom_import_animations_button: HTMLButtonElement | null = null
- dom_extend_arm_input: HTMLElement | null = null
- dom_extend_arm_button: HTMLButtonElement | null = null
+ dom_extend_arm_range_input: HTMLInputElement | null = null
+ dom_extend_arm_numeric_input: HTMLInputElement | null = null
dom_a_pose_correction_options: HTMLElement | null = null
dom_export_button_hidden_link: HTMLElement | null = null
dom_animation_count: HTMLElement | null = null
@@ -131,8 +131,8 @@ export class UI {
this.dom_current_time = document.querySelector('#current-time')
this.dom_total_time = document.querySelector('#total-time')
- this.dom_extend_arm_input = document.querySelector('#extend-arm-input')
- this.dom_extend_arm_button = document.querySelector('#extend-arm-button')
+ this.dom_extend_arm_range_input = document.querySelector('#extend-arm-range-input')
+ this.dom_extend_arm_numeric_input = document.querySelector('#extend-arm-numeric-input')
this.dom_a_pose_correction_options = document.querySelector('#a-pose-correction-options')
this.dom_build_version = document.querySelector('#build-version')
diff --git a/src/lib/Utilities.ts b/src/lib/Utilities.ts
index 4716a9c..ea9b5f9 100644
--- a/src/lib/Utilities.ts
+++ b/src/lib/Utilities.ts
@@ -290,10 +290,14 @@ export class Utility {
return closest_bone_index
}
+ static deep_clone_animation_clip (clip: AnimationClip): AnimationClip {
+ const tracks = clip.tracks.map((track: KeyframeTrack) => track.clone())
+ return new AnimationClip(clip.name, clip.duration, tracks)
+ }
+
static deep_clone_animation_clips (animation_clips: AnimationClip[]): AnimationClip[] {
return animation_clips.map((clip: AnimationClip) => {
- const tracks = clip.tracks.map((track: KeyframeTrack) => track.clone())
- return new AnimationClip(clip.name, clip.duration, tracks)
+ return Utility.deep_clone_animation_clip(clip)
})
}
@@ -315,4 +319,15 @@ export class Utility {
// console.log(animation_clip.tracks) // UNUSED DEBUG CODE
})
}
+
+ static parse_input_number (value: string | undefined): number {
+ if (value === '' || value === undefined) {
+ return 0
+ }
+ const value_numeric = parseFloat(value);
+ if (typeof value_numeric !== 'number' || !isFinite(value_numeric)) {
+ return 0
+ }
+ return value_numeric;
+ }
}
diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.ts
index 196b05d..a0fbcc2 100644
--- a/src/lib/processes/StepAnimationsListing.ts
+++ b/src/lib/processes/StepAnimationsListing.ts
@@ -12,13 +12,29 @@ import { Utility } from '../Utilities.ts'
import { ThemeManager } from '../ThemeManager.ts'
import { AnimationSearch, AnimationWithState } from '../AnimationSearch.ts'
+export interface WarpedAnimationClip {
+ /**
+ * The original version of the animation clip, without any transformations
+ * applied to it.
+ *
+ * This allows for simple non-destructive modification of the animation,
+ * since we can always reset to the original.
+ */
+ original_animation_clip: AnimationClip
+ /**
+ * The warped version of the animation clip, which is what will be displayed
+ * and downloaded by the user.
+ */
+ display_animation_clip: AnimationClip
+}
+
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepAnimationsListing extends EventTarget {
private readonly theme_manager: ThemeManager
private readonly ui: UI
private readonly animation_player: AnimationPlayer
- private animation_clips_loaded: AnimationClip[] = []
+ private animation_clips_loaded: WarpedAnimationClip[] = []
private gltf_animation_loader: GLTFLoader = new GLTFLoader()
private readonly fbx_animation_loader: FBXLoader = new FBXLoader()
@@ -36,6 +52,11 @@ export class StepAnimationsListing extends EventTarget {
private hip_bone_offset: Vector3 = new Vector3(0, 0, 0) // -z will bring hip bone down. Helps set new base hip position
private hip_bone_scale_factor_z: number = 1.0 // this is used to scale the hip bone position for animations
+ /**
+ * The amount to raise the arms.
+ */
+ private warp_arm_amount: number = 0.0
+
// the human model has a hip bone that needs position changes applied
// This will be for animations like falling. We need to capture the offset between
// the original position and the new position from our edited armature
@@ -127,8 +148,11 @@ export class StepAnimationsListing extends EventTarget {
this.animation_player.update(delta_time)
}
+ /**
+ * Returns a list of all of the currently-displayed animation clips.
+ */
public animation_clips (): AnimationClip[] {
- return this.animation_clips_loaded
+ return this.animation_clips_loaded.map(clip => clip.original_animation_clip)
}
public load_and_apply_default_animation_to_skinned_mesh (final_skinned_meshes: SkinnedMesh[], skeleton_type: SkeletonType): void {
@@ -174,7 +198,10 @@ export class StepAnimationsListing extends EventTarget {
// we did all the processing needed, so add them
// to the full list of animation clips
- this.animation_clips_loaded.push(...cloned_anims)
+ this.animation_clips_loaded.push(...cloned_anims.map(clip => ({
+ original_animation_clip: clip,
+ display_animation_clip: Utility.deep_clone_animation_clip(clip),
+ })))
remaining_loads--
if (remaining_loads === 0) {
@@ -186,14 +213,18 @@ export class StepAnimationsListing extends EventTarget {
private onAllAnimationsLoaded (): void {
// sort all animation names alphabetically
- this.animation_clips_loaded.sort((a: AnimationClip, b: AnimationClip) => {
- if (a.name < b.name) { return -1 }
- if (a.name > b.name) { return 1 }
+ this.animation_clips_loaded.sort((a: WarpedAnimationClip, b: WarpedAnimationClip) => {
+ if (a.display_animation_clip.name < b.display_animation_clip.name) { return -1 }
+ if (a.display_animation_clip.name > b.display_animation_clip.name) { return 1 }
return 0
})
// create user interface with all available animation clips
- this.build_animation_clip_ui(this.animation_clips_loaded, this.theme_manager, this.skeleton_type)
+ this.build_animation_clip_ui(
+ this.animation_clips_loaded.map(clip => clip.display_animation_clip),
+ this.theme_manager,
+ this.skeleton_type,
+ )
// add event listener to listem for checkbox changes when we change
// the amount of animations to export
@@ -233,11 +264,22 @@ export class StepAnimationsListing extends EventTarget {
})
}
- private extend_arm_animations_by_percentage (percentage: number): void {
+ /**
+ * Rebuilds all of the warped animations by applying the specified warps.
+ */
+ private rebuild_warped_animations (): void {
+ // Reset all of the warped clips to the corresponding original clip.
+ this.animation_clips_loaded.forEach((warped_clip: WarpedAnimationClip) => {
+ warped_clip.display_animation_clip = Utility.deep_clone_animation_clip(warped_clip.original_animation_clip)
+ })
+ /// Apply the arm extension warp:
+ this.apply_arm_extension_warp(this.warp_arm_amount)
+ }
+ private apply_arm_extension_warp (percentage: number): void {
// loop through each animation clip to update the tracks
- this.animation_clips_loaded.forEach((animation_clip: AnimationClip) => {
- animation_clip.tracks.forEach((track: KeyframeTrack) => {
+ this.animation_clips_loaded.forEach((warped_clip: WarpedAnimationClip) => {
+ warped_clip.display_animation_clip.tracks.forEach((track: KeyframeTrack) => {
// if our name does not contain 'quaternion', we need to exit
// since we are only modifying the quaternion tracks (e.g. L_Arm.quaternion )
if (track.name.indexOf('quaternion') < 0) {
@@ -301,7 +343,7 @@ export class StepAnimationsListing extends EventTarget {
const all_animation_actions: AnimationAction[] = []
this.skinned_meshes_to_animate.forEach((skinned_mesh) => {
- const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index]
+ const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index].display_animation_clip
const anim_action: AnimationAction = this.animation_mixer.clipAction(clip_to_play, skinned_mesh)
anim_action.stop()
@@ -313,7 +355,7 @@ export class StepAnimationsListing extends EventTarget {
// Update the animation player with the current animation and all actions
if (all_animation_actions.length > 0) {
- const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index]
+ const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index].display_animation_clip
this.animation_player.set_animation(clip_to_play, all_animation_actions)
}
}
@@ -348,10 +390,27 @@ export class StepAnimationsListing extends EventTarget {
})
}
- // A-Pose arm extension event listner
- this.ui.dom_extend_arm_button?.addEventListener('click', (event) => {
- const extend_arm_value: number = this.ui.dom_extend_arm_input?.value
- this.extend_arm_animations_by_percentage(extend_arm_value)
+ // A-Pose arm extension event listener
+ this.ui.dom_extend_arm_numeric_input?.addEventListener('input', (event) => {
+ const extend_arm_value: number = Utility.parse_input_number(this.ui.dom_extend_arm_numeric_input?.value)
+ if (this.ui.dom_extend_arm_range_input !== null) {
+ // Copy the value from the numeric onto the slider input.
+ this.ui.dom_extend_arm_range_input.value = extend_arm_value.toString()
+ }
+ this.warp_arm_amount = extend_arm_value
+
+ this.rebuild_warped_animations()
+ this.play_animation(this.current_playing_index)
+ })
+ this.ui.dom_extend_arm_range_input?.addEventListener('input', (event) => {
+ const extend_arm_value: number = Utility.parse_input_number(this.ui.dom_extend_arm_range_input?.value)
+ if (this.ui.dom_extend_arm_numeric_input !== null) {
+ // Copy the value from the slider onto the numeric input.
+ this.ui.dom_extend_arm_numeric_input.value = extend_arm_value.toString()
+ }
+ this.warp_arm_amount = extend_arm_value
+
+ this.rebuild_warped_animations()
this.play_animation(this.current_playing_index)
})