Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Create better fully working distance child solver and show debug output

Scott Petrovic300d8c973ce7
diff --git a/src/lib/Generators.ts b/src/lib/Generators.ts
index 6f7c608..97ac7f6 100644
--- a/src/lib/Generators.ts
+++ b/src/lib/Generators.ts
@@ -116,8 +116,8 @@ export class Generators {
return group
}
- static create_spheres_for_points (points: Vector3[], sphere_size = 0.01, color = 0x00ffff, name = ''): Points {
- const points_material: PointsMaterial = new PointsMaterial({ size: sphere_size, color })
+ static create_spheres_for_points (points: Vector3[], sphere_size = 0.005, color = 0x00ffff, name = ''): Points {
+ const points_material: PointsMaterial = new PointsMaterial({ size: sphere_size, color, depthTest: false })
const points_geometry = new BufferGeometry().setFromPoints(points)
const point_objects: Points = new Points(points_geometry, points_material)
point_objects.name = `Point display: ${name}`
diff --git a/src/lib/processes/StepEditSkeleton.ts b/src/lib/processes/StepEditSkeleton.ts
index 047d665..97cddfa 100644
--- a/src/lib/processes/StepEditSkeleton.ts
+++ b/src/lib/processes/StepEditSkeleton.ts
@@ -131,8 +131,6 @@ export class StepEditSkeleton extends EventTarget {
switch (value) {
case 'bone-envelope':
return SkinningFormula.Envelope
- case 'closest-bone-middle':
- return SkinningFormula.MedianDistance
case 'closest-bone-child':
return SkinningFormula.DistanceChild
}
diff --git a/src/lib/solvers/BoneWeightsByDistanceChild.ts b/src/lib/solvers/BoneWeightsByDistanceChild.ts
index d0b4adf..4e19a4f 100644
--- a/src/lib/solvers/BoneWeightsByDistanceChild.ts
+++ b/src/lib/solvers/BoneWeightsByDistanceChild.ts
@@ -6,20 +6,91 @@ import { Generators } from '../Generators.js'
/**
* BoneWeightsByDistanceChild
- * This works very similar to the normal distance solver.
+ * This works very similar to the normal distance solver.
* 1. First pass is to assign closest bone to each vertex like before
* 2. Second pass is to look through each vertex and look at bone assigned and direction
* - if bone to vertex direction points to a similar direction to parent bone, assign to the parent bone
*/
export default class BoneWeightsByDistanceChild extends AbstractAutoSkinSolver {
+ private readonly points_to_show_for_debugging: Vector3[] = []
+
public calculate_indexes_and_weights (): number[][] {
- // There can be multiple objects that need skinning, so
+ // There can be multiple objects that need skinning, so
// this will make sure we have a clean slate by putting it in function
const skin_indices: number[] = []
const skin_weights: number[] = []
- // loop through each vertex and find the closest bone
- // then assign the closest vertices to that bone in the assigned_vertices property
+ // mutates (assigns) skin_indices and skin_weights
+ this.calculate_closest_bone_weights(skin_indices, skin_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
+ this.calculate_parent_bone_weights(skin_indices, skin_weights)
+
+ if (this.show_debug) {
+ this.debugging_scene_object.add(this.objects_to_show_for_debugging())
+ // console.log('Debugging points:', this.get_bone_master_data())
+ this.points_to_show_for_debugging.length = 0 // Clear the points after adding to the scene
+ }
+
+ return [skin_indices, skin_weights]
+ }
+
+ private calculate_parent_bone_weights (skin_indices: number[], skin_weights: number[]): void {
+ const reassigned_vertex_indices: number[] = [] // For debugging, track reassigned vertices
+
+ 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 === 'DEF-hips' && this.skeleton_type === SkeletonType.Human) {
+ // 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) {
+ console.log(`WARNING: No parent bone for vertex on ${current_bone.name}`)
+ continue // Skip if there is no parent bone
+ }
+
+ // Calculate direction vectors
+ const current_bone_position = Utility.world_position_from_object(current_bone)
+ const parent_bone_position = Utility.world_position_from_object(parent_bone)
+
+ const direction_to_current_bone = new Vector3().subVectors(vertex_position, current_bone_position).normalize()
+ const direction_to_parent_bone = new Vector3().subVectors(vertex_position, parent_bone_position).normalize()
+
+ // Compare directions using dot product
+ const similarity = direction_to_current_bone.dot(direction_to_parent_bone)
+
+ // If the directions are similar (e.g., dot product > 0.8), reassign to the parent bone
+ if (similarity < -0.3) {
+ const parent_bone_index = this.get_bone_master_data().findIndex(b => b.bone_object === parent_bone)
+ if (parent_bone_index !== -1) {
+ // Update skin indices and weights
+ skin_indices[i * 4] = parent_bone_index
+ skin_weights[i * 4] = 1.0 // Assign full weight to the parent bone
+
+ reassigned_vertex_indices.push(i) // Track reassigned vertex for debugging
+
+ this.points_to_show_for_debugging.push(vertex_position) // Add to points to show for debugging
+ }
+ }
+ }
+ }
+
+ /**
+ * This function will assign the closest bone to each vertex
+ * It returns void, but it will modify the skin_indices and skin_weights arrays
+ * This function mutates the arrays passed in as arguments
+ * @param skin_indices
+ * @param skin_weights
+ */
+ private calculate_closest_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
@@ -49,32 +120,13 @@ export default class BoneWeightsByDistanceChild extends AbstractAutoSkinSolver {
skin_indices.push(closest_bone_index, 0, 0, 0)
skin_weights.push(1.0, 0, 0, 0)
}
-
- if (this.show_debug) {
- this.debugging_scene_object.add(this.objects_to_show_for_debugging())
- }
-
- return [skin_indices, skin_weights]
}
private objects_to_show_for_debugging (): Points {
- const debug_sphere_size: number = 0.2
+ const debug_sphere_size: number = 0.07
const debug_color = 0xff00ff // vertices that are part of envelope
- const vertex_points_to_show: Vector3[] = []
-
- // Create a 10 random points to test that are hard-coded
- for (let i = 0; i < 10; i++) {
- const random_point: Vector3 = new Vector3(
- Math.random() * 2 - 1,
- Math.random() * 2 - 1,
- Math.random() * 2 - 1
- ).normalize().multiplyScalar(0.5)
-
- vertex_points_to_show.push(random_point)
- }
-
- const debug_assigned_points: Points = Generators.create_spheres_for_points(vertex_points_to_show,
+ const debug_assigned_points: Points = Generators.create_spheres_for_points(this.points_to_show_for_debugging,
debug_sphere_size, debug_color, 'Vertices assigned to bone')
return debug_assigned_points