Import a 3D Model and automatically assign and export animations
TypeScript
Convert edit skin and weight skin steps to typescript
Scott Petrovica03fe072e7e9
diff --git a/.eslintrc.json b/.eslintrc.jsonindex d89f260..8491e50 100644--- a/.eslintrc.json+++ b/.eslintrc.json@@ -18,6 +18,10 @@{"selector": "class","format": ["PascalCase"]+ },+ {+ "selector": "import",+ "format": ["PascalCase", "snake_case"]}],"indent": "off",diff --git a/src/lib/processes/StepEditSkeleton.ts b/src/lib/processes/StepEditSkeleton.tsindex ff0d19d..c8111b5 100644--- a/src/lib/processes/StepEditSkeleton.ts+++ b/src/lib/processes/StepEditSkeleton.ts@@ -1,16 +1,16 @@import { UI } from '../UI.ts'import { Generators } from '../Generators.ts'import { Utility } from '../Utilities.ts'-import { Vector3, Euler } from 'three'+import { Vector3, Euler, Object3D, Skeleton, Bone } from 'three'import { SkinningFormula } from '../enums/SkinningFormula.js'export class StepEditSkeleton extends EventTarget {private readonly ui: UI- private edited_armature = null- private edited_skeleton = null- private mirror_mode_enabled = true- private skinning_algorithm = SkinningFormula.Envelope- private show_debug = true+ private edited_armature: Object3D = new Object3D()+ private edited_skeleton: Skeleton = new Skeleton()+ private mirror_mode_enabled: boolean = true+ private skinning_algorithm: string = SkinningFormula.Envelope+ private show_debug: boolean = trueconstructor () {super()@@ -39,12 +39,12 @@ export class StepEditSkeleton extends EventTarget {this.show_debug = this.ui.dom_enable_skin_debugging.checked}- this._update_bind_button_text()+ this.update_bind_button_text()- this.addEventListeners()+ this.add_event_listeners()}- instructions_text () {+ public instructions_text (): string {return `<div>Instructions</div><div>Position skeleton into correct postion</div><ol>@@ -56,106 +56,127 @@ export class StepEditSkeleton extends EventTarget {</ol>`}- _update_bind_button_text () {- if(this.show_debug) {+ private update_bind_button_text (): void {+ if (this.show_debug && this.ui.dom_bind_pose_button !== null) {this.ui.dom_bind_pose_button.innerHTML = 'Test Skinning'return}- this.ui.dom_bind_pose_button.innerHTML = 'Skin Model'+ if (this.ui.dom_bind_pose_button !== null) {+ this.ui.dom_bind_pose_button.innerHTML = 'Skin Model'+ }}- show_debugging () {+ public show_debugging (): boolean {return this.show_debug}- set_mirror_mode_enabled (value) {+ public set_mirror_mode_enabled (value: boolean): void {this.mirror_mode_enabled = value}- is_mirror_mode_enabled () {+ public is_mirror_mode_enabled (): boolean {return this.mirror_mode_enabled}- algorithm () {+ public algorithm (): string {return this.skinning_algorithm}- addEventListeners () {- this.ui.dom_move_to_origin_button.addEventListener('click', () => {- // the base bone itself is not at the origin, but the parent is the armature object- this.edited_skeleton.bones[0].position.set(0, 0, 0)- this.edited_skeleton.bones[0].updateWorldMatrix(true, true) // update on renderer- })-- this.ui.dom_scale_skeleton_button.addEventListener('click', () => {- const modify_scale = 1.0 + (this.ui.dom_scale_skeleton_input_box.value/100.0)- Utility.scale_armature_by_scalar(this.edited_armature, modify_scale)- this.edited_armature.updateWorldMatrix(true, true)- })+ public add_event_listeners (): void {+ if (this.ui.dom_move_to_origin_button !== null) {+ this.ui.dom_move_to_origin_button.addEventListener('click', () => {+ // the base bone itself is not at the origin, but the parent is the armature object+ this.edited_skeleton.bones[0].position.set(0, 0, 0)+ this.edited_skeleton.bones[0].updateWorldMatrix(true, true) // update on renderer+ })+ }- this.ui.dom_mirror_skeleton_checkbox.addEventListener('change', (event) => {- // mirror skeleton movements along the X axis- this.set_mirror_mode_enabled(event.target.checked)- })+ if (this.ui.dom_scale_skeleton_button !== null && this.ui.dom_scale_skeleton_input_box !== null) {+ this.ui.dom_scale_skeleton_button.addEventListener('click', () => {+ const modify_scale = 1.0 + (this.ui.dom_scale_skeleton_input_box.value / 100.0)+ Utility.scale_armature_by_scalar(this.edited_armature, modify_scale)+ this.edited_armature.updateWorldMatrix(true, true)+ })+ }- this.ui.dom_skinning_algorithm_selection.addEventListener('change', (event) => {- const selection = event.target.value+ if (this.ui.dom_mirror_skeleton_checkbox !== null) {+ this.ui.dom_mirror_skeleton_checkbox.addEventListener('change', (event) => {+ // mirror skeleton movements along the X axis+ this.set_mirror_mode_enabled(event.target.checked)+ })+ }- if(selection === 'bone-envelope') {- this.skinning_algorithm = SkinningFormula.Envelope- } else if(selection === 'closest-bone') {- this.skinning_algorithm = SkinningFormula.Distance- } else if(selection === 'closest-bone-middle') {- this.skinning_algorithm = SkinningFormula.MedianDistance- }- })+ if (this.ui.dom_skinning_algorithm_selection !== null) {+ this.ui.dom_skinning_algorithm_selection.addEventListener('change', (event) => {+ const selection = event.target.value++ if (selection === 'bone-envelope') {+ this.skinning_algorithm = SkinningFormula.Envelope+ } else if (selection === 'closest-bone') {+ this.skinning_algorithm = SkinningFormula.Distance+ } else if (selection === 'closest-bone-middle') {+ this.skinning_algorithm = SkinningFormula.MedianDistance+ }+ })+ }- this.ui.dom_enable_skin_debugging.addEventListener('change', (event) => {- this.show_debug = event.target.checked- this._update_bind_button_text()- })+ if (this.ui.dom_enable_skin_debugging !== null) {+ this.ui.dom_enable_skin_debugging.addEventListener('change', (event) => {+ this.show_debug = event.target.checked+ this.update_bind_button_text()+ })+ }}- removeEventListeners () {- this.ui.dom_move_to_origin_button.removeEventListener('click', () => {})- this.ui.dom_scale_skeleton_button.removeEventListener('click', () => {})- this.ui.dom_mirror_skeleton_checkbox.removeEventListener('change', () => {})- this.ui.dom_skinning_algorithm_selection.removeEventListener('change', () => {})- this.ui.dom_enable_skin_debugging.removeEventListener('change', () => {})- }+ public remove_event_listeners (): void {+ if (this.ui.dom_move_to_origin_button !== null) {+ this.ui.dom_move_to_origin_button.removeEventListener('click', () => {})+ }- set_armature (armature) {- if(this.edited_armature !== null) {- return this.edited_armature+ if (this.ui.dom_scale_skeleton_button !== null) {+ this.ui.dom_scale_skeleton_button.removeEventListener('click', () => {})}- this.edited_armature = armature.clone()+ if (this.ui.dom_mirror_skeleton_checkbox !== null) {+ this.ui.dom_mirror_skeleton_checkbox.removeEventListener('change', () => {})+ }- this._create_skeleton()+ if (this.ui.dom_skinning_algorithm_selection !== null) {+ this.ui.dom_skinning_algorithm_selection.removeEventListener('change', () => {})+ }++ if (this.ui.dom_enable_skin_debugging !== null) {+ this.ui.dom_enable_skin_debugging.removeEventListener('change', () => {})+ }+ }++ public set_armature (armature: Object3D): void {+ this.edited_armature = armature.clone()+ this.create_skeleton()}- _create_skeleton () {+ private create_skeleton (): Skeleton {// create skeleton and helper to visualizethis.edited_skeleton = Generators.create_skeleton(this.edited_armature.children[0])this.edited_skeleton.name = 'Editing Skeleton'// update the world matrix for the skeleton// without this the skeleton helper won't appear when the bones are first loaded- this.edited_skeleton.bones[0].updateWorldMatrix( true, true )+ this.edited_skeleton.bones[0].updateWorldMatrix(true, true)return this.edited_skeleton}- armature () {+ public armature (): Object3D {return this.edited_armature}- skeleton () {+ public skeleton (): Skeleton {return this.edited_skeleton}- apply_mirror_mode (selected_bone, transform_type) {+ public apply_mirror_mode (selected_bone: Bone, transform_type: string): void {// if we are on the positive side mirror mode is enabled// we need to change the position of the bone on the other side of the mirror@@ -166,27 +187,27 @@ export class StepEditSkeleton extends EventTarget {// Find another bone that has the same base name// that should be the mirror- let mirror_bone = null+ let mirror_bone: Bone | undefinedthis.edited_skeleton.bones.forEach((bone) => {const bone_name_to_compare = Utility.calculate_bone_base_name(bone.name)- if(bone_name_to_compare === base_bone_name && bone.name !== selected_bone.name) {+ if (bone_name_to_compare === base_bone_name && bone.name !== selected_bone.name) {mirror_bone = bone}})- if(mirror_bone === null) {+ if (mirror_bone === undefined) {return // we probably something along the axis (head, neck, spine)}- if(transform_type === 'translate') {+ if (transform_type === 'translate') {// move the mirror bone in the -X value of the transform control// this will mirror the movement of the bonemirror_bone.position.copy(new Vector3(-selected_bone.position.x,selected_bone.position.y,- selected_bone.position.z,+ selected_bone.position.z))}diff --git a/src/lib/processes/StepWeightSkin.js b/src/lib/processes/StepWeightSkin.tssimilarity index 62%rename from src/lib/processes/StepWeightSkin.jsrename to src/lib/processes/StepWeightSkin.tsindex 4668078..cd86eb6 100644--- a/src/lib/processes/StepWeightSkin.js+++ b/src/lib/processes/StepWeightSkin.ts@@ -8,16 +8,16 @@ import { SkinningFormula } from '../enums/SkinningFormula.js'import { Generators } from '../Generators.ts'-import { SkinnedMesh } from 'three'+import { type BufferGeometry, type Material, type Object3D, type Skeleton, SkinnedMesh } from 'three'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepWeightSkin extends EventTarget{- ui = null- skinning_armature = null+ private readonly ui: UI = new UI()+ skinning_armature: Object3D | undefinedbone_skinning_formula = null- binding_skeleton = null- skinned_meshes = []+ binding_skeleton: Skeleton | undefined+ skinned_meshes: SkinnedMesh[] = []// debug options for bone skinning formulaenable_debugging = false@@ -25,21 +25,13 @@ export class StepWeightSkin extends EventTargetdebug_scene_object = nullbone_index_to_test = -1- constructor()- {- super()- this.ui = new UI()- }-- begin()- {+ public begin (): void {this.ui.dom_current_step_index.innerHTML = '3.5'this.ui.dom_current_step_element.innerHTML = 'Skin Debug'this.ui.dom_skinned_mesh_tools.style.display = 'flex'}- instructions_text()- {+ public instructions_text (): string {return `<div>Instructions</div><div>Visualize the results of the skinning. Currently only supported for Bone Envelope</div><ol>@@ -49,45 +41,35 @@ export class StepWeightSkin extends EventTarget</ol>`}-- create_bone_formula_object(editable_armature, skinning_formula)- {-+ public create_bone_formula_object(editable_armature: Object3D, skinning_formula: string): any {this.skinning_armature = editable_armature.clone()this.skinning_armature.name = 'Armature for skinning'-+// Swap out formulas to see different results- if(skinning_formula === SkinningFormula.Envelope)- {+ if (skinning_formula === SkinningFormula.Envelope) {this.bone_skinning_formula = new BoneWeightsByEnvelope(this.skinning_armature.children[0])- }- else if(skinning_formula === SkinningFormula.Distance)- {+ } else if (skinning_formula === SkinningFormula.Distance) {this.bone_skinning_formula = new BoneWeightsByDistance(this.skinning_armature.children[0])- }- else if(skinning_formula === SkinningFormula.MedianDistance)- {+ } else if (skinning_formula === SkinningFormula.MedianDistance) {this.bone_skinning_formula = new BoneWeightsByMedianDistance(this.skinning_armature.children[0])}return this.bone_skinning_formula}- skeleton()+ public skeleton (): Skeleton | undefined{// gets bone hierarchy from the armaturereturn this.binding_skeleton}- set_mesh_geometry(geometry)+ public set_mesh_geometry(geometry: BufferGeometry): void{this.bone_skinning_formula.set_geometry(geometry)}- test_geometry()- {- if(this.show_debug)- {+ public test_geometry() {+ if (this.show_debug) {this.bone_skinning_formula.set_show_debug(this.show_debug)this.bone_skinning_formula.set_debugging_scene_object(this.debug_scene_object)this.bone_skinning_formula.set_bone_index_to_test(this.bone_index_to_test)@@ -96,22 +78,29 @@ export class StepWeightSkin extends EventTargetreturn this.bone_skinning_formula.test_bones_outside_in_mesh()}- create_binding_skeleton()- {- // when we copy over the armature with the bind, we will lose the reference in the variable- this.binding_skeleton = Generators.create_skeleton(this.skinning_armature.children[0])- this.binding_skeleton.name = 'Mesh Binding Skeleton'+ public create_binding_skeleton(): void {+ if (this.skinning_armature !== undefined) {+ // when we copy over the armature with the bind, we will lose the reference in the variable+ this.binding_skeleton = Generators.create_skeleton(this.skinning_armature.children[0])+ this.binding_skeleton.name = 'Mesh Binding Skeleton'+ } else {+ console.warn('Tried to create_binding_skeleton() but skinning_armature is undefined!')+ }}- clear_skinned_meshes()+ public clear_skinned_meshes(): void{this.skinned_meshes = []}- create_skinned_mesh(geometry, material)- {+ public create_skinned_mesh (geometry: BufferGeometry, material: Material): void {+ if (this.binding_skeleton === undefined) {+ console.warn('Tried to create_skinned_mesh() but binding_skeleton is undefined!')+ return+ }+// create skinned mesh- let skinned_mesh = new SkinnedMesh(geometry, material)+ const skinned_mesh: SkinnedMesh = new SkinnedMesh(geometry, material)skinned_mesh.name = 'Skinned Mesh'skinned_mesh.castShadow = true // skinned mesh won't update right if this is false@@ -123,28 +112,24 @@ export class StepWeightSkin extends EventTargetthis.skinned_meshes.push(skinned_mesh)}- final_skinned_meshes()- {+ public final_skinned_meshes(): SkinnedMesh[] {return this.skinned_meshes}- set_show_debug(value)+ public set_show_debug(value: boolean): void{this.show_debug = value}- set_debug_scene_object(scene)- {+ public set_debug_scene_object(scene): void {this.debug_scene_object = scene}- set_bone_index_to_test(index)- {+ public set_bone_index_to_test(index): void {this.bone_index_to_test = index}- calculate_weights()- {+ public calculate_weights() {return this.bone_skinning_formula.calculate_indexes_and_weights()}}diff --git a/src/lib/solvers/BoneWeightsByDistance.ts b/src/lib/solvers/BoneWeightsByDistance.tsindex 2c287c1..02c228e 100644--- a/src/lib/solvers/BoneWeightsByDistance.ts+++ b/src/lib/solvers/BoneWeightsByDistance.ts@@ -1,11 +1,11 @@-import { Bone, BufferGeometry, Object3D, Scene, Vector3 } from 'three'+import { type Bone, BufferGeometry, type Object3D, type Scene, Vector3 } from 'three'import { Utility } from '../Utilities.js'import BoneCalculationData from '../models/BoneCalculationData.js'export default class BoneWeightsByDistance{- private bones_master_data: Array<BoneCalculationData> = []- private geometry: BufferGeometry+ private readonly bones_master_data: BoneCalculationData[] = []+ private geometry: BufferGeometry = new BufferGeometry()private show_debug: boolean = falseprivate bone_idx_test: number = -1private debugging_scene_object: Scenediff --git a/src/script.js b/src/script.jsindex 2538094..932be87 100644--- a/src/script.js+++ b/src/script.js@@ -13,7 +13,7 @@ import { StepLoadSkeleton } from './lib/processes/StepLoadSkeleton.ts'import { StepEditSkeleton } from './lib/processes/StepEditSkeleton.ts'import { StepAnimationsListing } from './lib/processes/StepAnimationsListing.ts'import { StepExportToFile } from './lib/processes/StepExportToFile.ts'-import { StepWeightSkin } from './lib/processes/StepWeightSkin.js'+import { StepWeightSkin } from './lib/processes/StepWeightSkin.ts'import { ProcessStep } from './lib/enums/ProcessStep.js'@@ -180,7 +180,7 @@ function process_step_changed (process_step) {ui.hide_all_elements()// clean up any event listeners from the previous steps- edit_skeleton_step.removeEventListeners()+ edit_skeleton_step.remove_event_listeners()switch (process_step) {case ProcessStep.LoadModel: