Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

remove some code to help simplify

Scott Petrovice07bd4c0bb84
diff --git a/src/lib/Utilities.ts b/src/lib/Utilities.ts
index 9fa4c00..fb42acd 100644
--- a/src/lib/Utilities.ts
+++ b/src/lib/Utilities.ts
@@ -287,7 +287,6 @@ export class Utility {
}
})
- bones[closest_bone_index].assigned_vertices.push(vertex_index)
return closest_bone_index
}
}
diff --git a/src/lib/models/BoneCalculationData.ts b/src/lib/models/BoneCalculationData.ts
index 0d0a1b1..4cc3674 100644
--- a/src/lib/models/BoneCalculationData.ts
+++ b/src/lib/models/BoneCalculationData.ts
@@ -2,19 +2,13 @@ import { type Bone } from 'three'
export default class BoneCalculationData
{
- public name: string
+ public name: string = ''
public bone_object: Bone
- public supports_envelope_calculation: boolean
- public has_child_bone: boolean
- public assigned_vertices: number[]
- public use_minimum_envelope_expand: boolean
+ public has_child_bone: boolean = false
- constructor (name = '', bone: Bone, supports_envelope = false, has_child_bone = false, assigned_vertices = [], use_minimum_distance = false) {
- this.name = name
+ constructor (bone: Bone) {
+ this.name = bone.name || ''
this.bone_object = bone
- this.supports_envelope_calculation = supports_envelope
- this.has_child_bone = has_child_bone
- this.assigned_vertices = assigned_vertices
- this.use_minimum_envelope_expand = use_minimum_distance
+ this.has_child_bone = bone.children.length > 0
}
}
diff --git a/src/lib/solvers/AbstractAutoSkinSolver.ts b/src/lib/solvers/AbstractAutoSkinSolver.ts
index a20f095..4ea6cb7 100644
--- a/src/lib/solvers/AbstractAutoSkinSolver.ts
+++ b/src/lib/solvers/AbstractAutoSkinSolver.ts
@@ -64,10 +64,7 @@ export abstract class AbstractAutoSkinSolver {
protected init_bone_weights_data_structure (bone_hier: Object3D): void {
const bones_list: Bone[] = Utility.bone_list_from_hierarchy(bone_hier)
bones_list.forEach((bone: Bone) => {
- const has_child_bone: boolean = bone.children.length > 0
- const supports_envelope: boolean = false
- const new_bone_object: BoneCalculationData =
- new BoneCalculationData(bone.name, bone, supports_envelope, has_child_bone)
+ const new_bone_object: BoneCalculationData = new BoneCalculationData(bone)
this.bones_master_data.push(new_bone_object)
})
}
diff --git a/src/lib/solvers/BoneWeightsByDistance.ts b/src/lib/solvers/BoneWeightsByDistance.ts
index fb7064d..3a62c49 100644
--- a/src/lib/solvers/BoneWeightsByDistance.ts
+++ b/src/lib/solvers/BoneWeightsByDistance.ts
@@ -36,7 +36,6 @@ export default class BoneWeightsByDistance extends AbstractAutoSkinSolver {
}
})
- this.get_bone_master_data()[closest_bone_index].assigned_vertices.push(i)
// assign to final weights. closest bone is always 100% weight
skin_indices.push(closest_bone_index, 0, 0, 0)
@@ -53,16 +52,6 @@ export default class BoneWeightsByDistance extends AbstractAutoSkinSolver {
private objects_to_show_for_debugging (): Group {
const vertex_points_to_show: Vector3[] = []
- // get a list of all the vertices assigned to bones
- this.get_bone_master_data().forEach(bone => {
- const assigned_vertices: number[] = bone.assigned_vertices
-
- assigned_vertices.forEach(vertex_index => {
- const vertex_position: Vector3 = new Vector3().fromBufferAttribute(this.geometry.attributes.position, vertex_index)
- vertex_points_to_show.push(vertex_position)
- })
- })
-
const debug_color = 0xff0000 // vertices that are part of envelope
const debug_assigned_points: Group = Generators.create_spheres_for_points(vertex_points_to_show,
debug_color, 'Vertices assigned to bone')
diff --git a/src/lib/solvers/SolverDistanceChildTargeting.ts b/src/lib/solvers/SolverDistanceChildTargeting.ts
index 3385775..f51cbbf 100644
--- a/src/lib/solvers/SolverDistanceChildTargeting.ts
+++ b/src/lib/solvers/SolverDistanceChildTargeting.ts
@@ -39,15 +39,9 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolver
// mutates (assigns) skin_indices and skin_weights
console.time('calculate_closest_bone_weights')
- this.calculate_closest_bone_weights(skin_indices, skin_weights)
+ this.calculate_median_bone_weights(skin_indices, skin_weights)
console.timeEnd('calculate_closest_bone_weights')
- // this is the second pass, where we look at the parent bone and assign weights
- // to it if the direction is similar. This will help bones mostly affect their children
- console.time('calculate_parent_bone_weights')
- this.calculate_parent_bone_weights(skin_indices, skin_weights)
- console.timeEnd('calculate_parent_bone_weights')
-
if (this.show_debug) {
this.debugging_scene_object.add(this.objects_to_show_for_debugging(skin_indices))
this.points_to_show_for_debugging.length = 0 // Clear the points after adding to the scene
@@ -86,48 +80,6 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolver
return distance_to_bottom_of_hip
}
- private calculate_parent_bone_weights (skin_indices: number[], skin_weights: number[]): void {
- for (let i = 0; i < this.geometry_vertex_count(); i++) {
- const vertex_position: Vector3 = new Vector3().fromBufferAttribute(this.geometry.attributes.position, i)
- const current_bone_index = skin_indices[i * 4] // Get the currently assigned bone index
- const current_bone = this.get_bone_master_data()[current_bone_index].bone_object
-
- // keep the original weights for hips. the parent is the root which never rotates
- // and shouldn't be affected. A better solution would be to assign to upper legs somehow.
- if (current_bone.name.includes('hips') === true) {
- // skip the hip bone because the parent is the root which never rotates/moves
- continue
- }
-
- const parent_bone = current_bone.parent
- if (parent_bone === null) {
- continue // Skip if there is no parent bone
- }
-
- // get length of the bone to its child
- const child_bone: Bone | null = current_bone.children[0] as Bone
- if (child_bone === undefined) {
- continue // Skip if there is no child bone
- }
-
- const current_bone_position: Vector3 = this.cached_bone_positions[current_bone_index]
- const child_bone_position: Vector3 = Utility.world_position_from_object(child_bone)
- const distance_to_child_bone = current_bone_position.distanceTo(child_bone_position)
-
-
- const distance_to_vertex: number = current_bone_position.distanceTo(vertex_position) // This is the distance from the bone to the vertex
- // If the vertex is within 10% of the distance to the child bone, assign 50% weight to the parent and 50% to the bone
- if (Math.abs(distance_to_vertex) < Math.abs(distance_to_child_bone * 0.2)) {
- const parent_bone_index = this.bone_object_to_index.get(parent_bone)
- if (parent_bone_index !== undefined) {
- skin_indices[i * 4 + 1] = parent_bone_index // Assign parent bone index
- skin_weights[i * 4 + 1] = 0.5 // Assign 50% weight to parent
- skin_weights[i * 4] = 0.5 // Assign 50% weight to current bone
- }
- }
- }
- }
-
/**
* This function will assign the closest bone to each vertex
* It returns void, but it will modify the skin_indices and skin_weights arrays
@@ -135,11 +87,10 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolver
* @param skin_indices
* @param skin_weights
*/
- private calculate_closest_bone_weights (skin_indices: number[], skin_weights: number[]): void {
+ private calculate_median_bone_weights (skin_indices: number[], skin_weights: number[]): void {
for (let i = 0; i < this.geometry_vertex_count(); i++) {
const vertex_position: Vector3 = new Vector3().fromBufferAttribute(this.geometry.attributes.position, i)
- // let closest_bone: Bone = this.bones_master_data[0].bone_object
- let closest_bone_distance: number = 10000
+ let closest_bone_distance: number = 1000 // arbitrary large number to start with
let closest_bone_index: number = 0
this.get_bone_master_data().forEach((bone, idx) => {
@@ -166,8 +117,6 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolver
}
})
- this.get_bone_master_data()[closest_bone_index].assigned_vertices.push(i)
-
// assign to final weights. closest bone is always 100% weight
skin_indices.push(closest_bone_index, 0, 0, 0)
skin_weights.push(1.0, 0, 0, 0)