Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Start storing mesh geometry, material, and weight calculations in weight skin step

Scott Petrovica6da2957720a
diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.ts
index 02ab8a1..55963a1 100644
--- a/src/lib/processes/StepAnimationsListing.ts
+++ b/src/lib/processes/StepAnimationsListing.ts
@@ -85,10 +85,22 @@ export class StepAnimationsListing extends EventTarget {
this.ui.dom_skinned_mesh_animation_tools.style.display = 'flex'
}
+ this.reset_step_data()
this.add_event_listeners()
this.update_download_button_enabled()
}
+ public reset_step_data (): void {
+ // reset previous state if we are re-entering this step
+ // this will happen if we are reskinning the mesh after changes
+ this.animation_clips_loaded = []
+ this.skinned_meshes_to_animate = []
+ this.animation_mixer = new AnimationMixer()
+ this.current_playing_index = 0
+ this.hip_bone_offset = new Vector3(0, 0, 0)
+ this.hip_bone_scale_factor_z = 1.0
+ }
+
public mixer (): AnimationMixer {
return this.animation_mixer
}
@@ -107,8 +119,14 @@ export class StepAnimationsListing extends EventTarget {
this.skinned_meshes_to_animate.forEach((skinned_mesh) => {
const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index]
+
const anim_action: AnimationAction = this.animation_mixer.clipAction(clip_to_play, skinned_mesh)
+ if (anim_action === null) {
+ console.info('No anim_action found for clipAction. Aborting. Clip name trying to play: :', clip_to_play.name, this.animation_mixer)
+ return
+ }
+
const progress = Math.min(anim_action.time / clip_to_play.duration, 1)
const progress_percent: number = progress * 100 // returns 0-100
@@ -323,7 +341,11 @@ export class StepAnimationsListing extends EventTarget {
}
private add_event_listeners (): void {
+ // make sure to only add the event listeners once
+ // this could be potentially called multiple times when going back and forth
+ // between editing skeleton and this step
if (this.has_added_event_listeners) {
+ console.info('Event listeners already added to animation step. Skipping.')
return
}
diff --git a/src/lib/processes/StepLoadModel.ts b/src/lib/processes/StepLoadModel.ts
index 418d429..220bc14 100644
--- a/src/lib/processes/StepLoadModel.ts
+++ b/src/lib/processes/StepLoadModel.ts
@@ -277,7 +277,7 @@ export class StepLoadModel extends EventTarget {
return this.final_mesh_data
}
- public models_geometry_list(): BufferGeometry[] {
+ public models_geometry_list (): BufferGeometry[] {
// loop through final mesh data and return the geometeries
const geometries_to_return: BufferGeometry[] = []
this.final_mesh_data.traverse((child) => {
diff --git a/src/lib/processes/StepWeightSkin.ts b/src/lib/processes/StepWeightSkin.ts
index 8aecda4..50d7914 100644
--- a/src/lib/processes/StepWeightSkin.ts
+++ b/src/lib/processes/StepWeightSkin.ts
@@ -1,7 +1,5 @@
import { UI } from '../UI.ts'
-// import { BoneWeightsByEnvelope } from '../solvers/BoneWeightsByEnvelope.ts'
-// import BoneWeightsByMedianDistance from '../solvers/BoneWeightsByMedianDistance.ts'
import BoneWeightsByDistance from '../solvers/BoneWeightsByDistance.ts'
import BoneWeightsByDistanceChild from '../solvers/BoneWeightsByDistanceChild.ts'
import SolverDistanceChildTargeting from '../solvers/SolverDistanceChildTargeting.ts'
@@ -10,7 +8,7 @@ import { SkinningFormula } from '../enums/SkinningFormula.ts'
import { Generators } from '../Generators.ts'
-import { type BufferGeometry, type Material, type Object3D, type Skeleton, SkinnedMesh, type Scene } from 'three'
+import { BufferGeometry, type Material, type Object3D, type Skeleton, SkinnedMesh, type Scene, type Group, Uint16BufferAttribute, Float32BufferAttribute } from 'three'
import BoneTesterData from '../models/BoneTesterData.ts'
import { type SkeletonType } from '../enums/SkeletonType.ts'
@@ -24,6 +22,13 @@ export class StepWeightSkin extends EventTarget {
private binding_skeleton: Skeleton | undefined
private skinned_meshes: SkinnedMesh[] = []
+ // stores the geometry data for meshes we will skin
+ private all_mesh_geometry: BufferGeometry[] = []
+ private all_mesh_materials: Material[] = []
+
+ // weight painted mesh actually has multiple meshes that will go in a group
+ // private readonly weight_painted_mesh_preview: Group | null = null
+
// debug options for bone skinning formula
private show_debug: boolean = false
private debug_scene_object: Object3D | undefined
@@ -68,6 +73,21 @@ export class StepWeightSkin extends EventTarget {
return this.binding_skeleton
}
+ /**
+ * @param geometry Add in all mesh geometry data to be skinned.
+ */
+ public add_to_geometry_data_to_skin (geometry: BufferGeometry): void {
+ // add name to the geometry
+ geometry.name = 'Mesh ' + this.all_mesh_geometry.length
+ this.all_mesh_geometry.push(geometry)
+ }
+
+ public get_geometry_data_to_skin (): BufferGeometry[] {
+ return this.all_mesh_geometry
+ }
+
+ // This can happen multiple times, so we need a better way to handle this to store all geometries
+ // this will be useful when creating the weight painted mesh and that generation being done
public set_mesh_geometry (geometry: BufferGeometry): void {
if (this.bone_skinning_formula === undefined) {
console.warn('Tried to set_mesh_geometry() in weight skinning step, but bone_skinning_formula is undefined!')
@@ -102,8 +122,19 @@ export class StepWeightSkin extends EventTarget {
}
}
- public clear_skinned_meshes (): void {
+ /**
+ * We might need to do the skinnning process multiple times
+ * so we need to clear out the data from the previous
+ * skinned mesh process
+ */
+ public reset_all_skin_process_data (): void {
this.skinned_meshes = []
+ this.all_mesh_materials = []
+ this.all_mesh_geometry = []
+ }
+
+ public add_mesh_material (material: Material): void {
+ this.all_mesh_materials.push(material)
}
public create_skinned_mesh (geometry: BufferGeometry, material: Material): void {
@@ -147,6 +178,37 @@ export class StepWeightSkin extends EventTarget {
return [[], []]
}
- return this.bone_skinning_formula.calculate_indexes_and_weights()
+ const indices_and_weights: number[][] = this.bone_skinning_formula.calculate_indexes_and_weights()
+ return indices_and_weights
+ }
+
+ public calculate_weights_for_all_mesh_data (): void {
+ if (this.all_mesh_geometry.length === 0) {
+ console.warn('Tried to calculate_weights_for_all_mesh_data() but all_mesh_geometry is empty!')
+ return
+ }
+
+ if (this.bone_skinning_formula === undefined) {
+ console.warn('Tried to calculate_weights_for_all_mesh_data() but bone_skinning_formula is null for some reason!')
+ return
+ }
+
+ // loop through each mesh geometry and calculate the weights
+ this.all_mesh_geometry.forEach((geometry_data: BufferGeometry, idx: number) => {
+ this.bone_skinning_formula?.set_geometry(geometry_data)
+ const [final_skin_indices, final_skin_weights]: number[][] = this.calculate_weights()
+
+ geometry_data.setAttribute('skinIndex', new Uint16BufferAttribute(final_skin_indices, 4))
+ geometry_data.setAttribute('skinWeight', new Float32BufferAttribute(final_skin_weights, 4))
+
+ const associated_material: Material = this.all_mesh_materials[idx]
+
+ this.create_skinned_mesh(geometry_data, associated_material)
+ })
+
+ // private regenerate_weight_painted_mesh (indices: number[]): void {
+ // const weight_painted_mesh = Generators.create_weight_painted_mesh(indices, this.geometry)
+ // const wireframe_mesh = Generators.create_wireframe_mesh_from_geometry(this.geometry)
+ // }
}
}
diff --git a/src/lib/solvers/AbstractAutoSkinSolver.ts b/src/lib/solvers/AbstractAutoSkinSolver.ts
index cfe185d..a20f095 100644
--- a/src/lib/solvers/AbstractAutoSkinSolver.ts
+++ b/src/lib/solvers/AbstractAutoSkinSolver.ts
@@ -24,6 +24,10 @@ export abstract class AbstractAutoSkinSolver {
this.geometry = geom
}
+ public get_geometry (): BufferGeometry {
+ return this.geometry
+ }
+
public set_show_debug (debug_value: boolean): void {
this.show_debug = debug_value
}
diff --git a/src/script.ts b/src/script.ts
index a9c9b2a..3ca8e1f 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -216,12 +216,14 @@ export class Bootstrap {
this.renderer.render(this.scene, this.camera)
}
- public changed_model_preview_display (display_type: ModelPreviewDisplay): void {
+ public changed_model_preview_display (mesh_textured_display_type: ModelPreviewDisplay): void {
// show/hide loaded textured model depending on view
- this.load_model_step.model_meshes().visible = display_type === ModelPreviewDisplay.Textured
+ this.load_model_step.model_meshes().visible = mesh_textured_display_type === ModelPreviewDisplay.Textured
- if (display_type === ModelPreviewDisplay.WeightPainted) {
- console.log('TODO: Logic to show weight painted model')
+ // weight paint preview model should probably be stored in the weight skin step
+
+ if (mesh_textured_display_type === ModelPreviewDisplay.WeightPainted) {
+ this.regenerate_weight_painted_preview_mesh()
}
}
@@ -270,27 +272,31 @@ export class Bootstrap {
}
}
+ private regenerate_weight_painted_preview_mesh (): void {
+ console.log('TODO: show the weight painted mesh logic here')
+ }
+
private start_skin_weighting_step (): void {
// we only need one binding skeleton. All skinned meshes will use this.
- this.weight_skin_step.create_binding_skeleton()
-
- this.weight_skin_step.clear_skinned_meshes() // clear out any existing skinned meshes in storage
- this.load_model_step.models_geometry_list().forEach((mesh_geometry, index) => {
- // we passed the bone test, so we can do the skinning process
- this.weight_skin_step.set_mesh_geometry(mesh_geometry)
- const [final_skin_indices, final_skin_weights]: number[][] = this.weight_skin_step.calculate_weights()
+ this.weight_skin_step.reset_all_skin_process_data() // clear out any existing skinned meshes in storage
- mesh_geometry.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(final_skin_indices, 4))
- mesh_geometry.setAttribute('skinWeight', new THREE.Float32BufferAttribute(final_skin_weights, 4))
+ this.weight_skin_step.create_binding_skeleton()
- // create a separate skinned skeleton and helper for the binding pose
- // having a separate one will help us if we want to go back and edit the original skeleton
- const mesh_material = this.load_model_step.models_material_list()[index]
- this.weight_skin_step.create_skinned_mesh(mesh_geometry, mesh_material)
+ // reset geometry data to skin
+ this.load_model_step.models_geometry_list().forEach((mesh_geometry, idx) => {
+ this.weight_skin_step.add_to_geometry_data_to_skin(mesh_geometry)
+ })
- this.scene.add(...this.weight_skin_step.final_skinned_meshes())
+ // all mesh material data associated with the geometry data
+ this.load_model_step.models_material_list().forEach((mesh_material) => {
+ this.weight_skin_step.add_mesh_material(mesh_material)
})
+ // perform skinning operation
+ // this will take all the mesh geometry data we added above and create skinned meshes
+ this.weight_skin_step.calculate_weights_for_all_mesh_data()
+ this.scene.add(...this.weight_skin_step.final_skinned_meshes())
+
// remember our skeleton position before we do the skinning process
// that way if we revert to try again...we will have the original positions/rotations
this.load_model_step.model_meshes().visible = false // hide our unskinned mesh after we have done the skinning process