Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Use Abstract class instead of interface for solvers

Scott Petrovicb09b7f3077c8
diff --git a/src/lib/interfaces/IAutoSkinSolver.ts b/src/lib/interfaces/IAutoSkinSolver.ts
deleted file mode 100644
index 872abf1..0000000
--- a/src/lib/interfaces/IAutoSkinSolver.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { type Object3D, type BufferGeometry } from 'three'
-import type BoneTesterData from '../models/BoneTesterData'
-import { type SkeletonType } from '../enums/SkeletonType'
-
-export interface IAutoSkinSolver {
- set_geometry: (geom: BufferGeometry) => void
- set_debugging_scene_object: (scene_object: Object3D) => void
- set_show_debug: (debug_value: boolean) => void
- set_bone_index_to_test: (bone_idx: number) => void
- calculate_indexes_and_weights: () => number[][]
- test_bones_outside_in_mesh: () => BoneTesterData
- set_skeleton_type: (skinning_type: SkeletonType) => void
-}
diff --git a/src/lib/models/BoneCalculationData.ts b/src/lib/models/BoneCalculationData.ts
index 41e8d4b..0d0a1b1 100644
--- a/src/lib/models/BoneCalculationData.ts
+++ b/src/lib/models/BoneCalculationData.ts
@@ -9,8 +9,7 @@ export default class BoneCalculationData
public assigned_vertices: number[]
public use_minimum_envelope_expand: boolean
- constructor(name = '', bone: Bone, supports_envelope = false, has_child_bone = false, assigned_vertices = [], use_minimum_distance = false)
- {
+ constructor (name = '', bone: Bone, supports_envelope = false, has_child_bone = false, assigned_vertices = [], use_minimum_distance = false) {
this.name = name
this.bone_object = bone
this.supports_envelope_calculation = supports_envelope
diff --git a/src/lib/processes/StepWeightSkin.ts b/src/lib/processes/StepWeightSkin.ts
index a18379d..212c0c1 100644
--- a/src/lib/processes/StepWeightSkin.ts
+++ b/src/lib/processes/StepWeightSkin.ts
@@ -9,16 +9,16 @@ 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 { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.ts'
import BoneTesterData from '../models/BoneTesterData.ts'
import { type SkeletonType } from '../enums/SkeletonType.ts'
import BoneWeightsByDistanceChild from '../solvers/BoneWeightsByDistanceChild.ts'
+import { type AbstractAutoSkinSolver } from '../solvers/AbstractAutoSkinSolver.ts'
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepWeightSkin extends EventTarget {
private readonly ui: UI = new UI()
private skinning_armature: Object3D | undefined
- private bone_skinning_formula: IAutoSkinSolver | undefined
+ private bone_skinning_formula: AbstractAutoSkinSolver | undefined
private binding_skeleton: Skeleton | undefined
private skinned_meshes: SkinnedMesh[] = []
@@ -41,22 +41,23 @@ export class StepWeightSkin extends EventTarget {
}
}
-
public create_bone_formula_object (editable_armature: Object3D, skinning_formula: string, skeleton_type: SkeletonType): 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) {
- this.bone_skinning_formula = new BoneWeightsByEnvelope(this.skinning_armature.children[0], skeleton_type)
- } else if (skinning_formula === SkinningFormula.Distance) {
+ if (skinning_formula === SkinningFormula.Distance) {
this.bone_skinning_formula = new BoneWeightsByDistance(this.skinning_armature.children[0], skeleton_type)
- } else if (skinning_formula === SkinningFormula.MedianDistance) {
- this.bone_skinning_formula = new BoneWeightsByMedianDistance(this.skinning_armature.children[0], skeleton_type)
- } else if (skinning_formula === SkinningFormula.DistanceChild) {
+ }
+
+ if (skinning_formula === SkinningFormula.DistanceChild) {
this.bone_skinning_formula = new BoneWeightsByDistanceChild(this.skinning_armature.children[0], skeleton_type)
}
+ // if (skinning_formula === SkinningFormula.Envelope) {
+ // this.bone_skinning_formula = new BoneWeightsByEnvelope(this.skinning_armature.children[0], skeleton_type)
+ // }
+
return this.bone_skinning_formula
}
diff --git a/src/lib/solvers/AbstractAutoSkinSolver.ts b/src/lib/solvers/AbstractAutoSkinSolver.ts
new file mode 100644
index 0000000..cfe185d
--- /dev/null
+++ b/src/lib/solvers/AbstractAutoSkinSolver.ts
@@ -0,0 +1,70 @@
+import { BufferGeometry, Object3D, type Bone } from 'three'
+import { type SkeletonType } from '../enums/SkeletonType.js'
+import BoneTesterData from '../models/BoneTesterData.js'
+import BoneCalculationData from '../models/BoneCalculationData.js'
+import { Utility } from '../Utilities.js'
+
+export abstract class AbstractAutoSkinSolver {
+ protected readonly bones_master_data: BoneCalculationData[] = []
+ protected geometry: BufferGeometry = new BufferGeometry()
+ protected show_debug: boolean = false
+ protected bone_idx_test: number = -1
+ protected skeleton_type: SkeletonType | null = null
+ protected debugging_scene_object: Object3D = new Object3D()
+
+ constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {
+ this.set_skeleton_type(skeleton_type)
+ this.init_bone_weights_data_structure(bone_hier)
+ }
+
+ // calculation that will bring back the skin indices and weights
+ public abstract calculate_indexes_and_weights (): number[][]
+
+ public set_geometry (geom: BufferGeometry): void {
+ this.geometry = geom
+ }
+
+ public set_show_debug (debug_value: boolean): void {
+ this.show_debug = debug_value
+ }
+
+ public set_bone_index_to_test (bone_idx: number): void {
+ this.bone_idx_test = bone_idx
+ }
+
+ public set_debugging_scene_object (scene_object: Object3D): void {
+ this.debugging_scene_object = scene_object
+ }
+
+ public set_skeleton_type (skinning_type: SkeletonType): void {
+ this.skeleton_type = skinning_type
+ }
+
+ public get_bone_master_data (): BoneCalculationData[] {
+ return this.bones_master_data
+ }
+
+ public test_bones_outside_in_mesh (): BoneTesterData {
+ return new BoneTesterData([], [])
+ }
+
+ protected geometry_vertex_count (): number {
+ if (this.geometry === null) {
+ console.error('Geometry is null. cannot get vertex count')
+ return -1
+ }
+
+ return this.geometry.attributes.position.array.length / 3
+ }
+
+ 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)
+ this.bones_master_data.push(new_bone_object)
+ })
+ }
+}
diff --git a/src/lib/solvers/BoneWeightsByDistance.ts b/src/lib/solvers/BoneWeightsByDistance.ts
index f3aedae..c16f4a5 100644
--- a/src/lib/solvers/BoneWeightsByDistance.ts
+++ b/src/lib/solvers/BoneWeightsByDistance.ts
@@ -1,56 +1,24 @@
-import { type Bone, BufferGeometry, Object3D, Vector3 } from 'three'
+import { Vector3 } from 'three'
import { Utility } from '../Utilities.js'
-import BoneCalculationData from '../models/BoneCalculationData.js'
-import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.js'
-import BoneTesterData from '../models/BoneTesterData.js'
import { SkeletonType } from '../enums/SkeletonType.js'
+import { AbstractAutoSkinSolver } from './AbstractAutoSkinSolver.js'
-export default class BoneWeightsByDistance implements IAutoSkinSolver {
- private readonly bones_master_data: BoneCalculationData[] = []
- private geometry: BufferGeometry = new BufferGeometry()
- private show_debug: boolean = false
- private bone_idx_test: number = -1
- private debugging_scene_object: Object3D = new Object3D()
- private skeleton_type: SkeletonType | null = null
-
- constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {
- this.set_skeleton_type(skeleton_type)
- this.init_bone_weights_data_structure(bone_hier)
- }
-
- public set_debugging_scene_object (scene_object: Object3D): void {
- this.debugging_scene_object = scene_object
- }
-
- public set_geometry (geom: BufferGeometry): void {
- this.geometry = geom
- }
-
- public set_skeleton_type (skinning_type: SkeletonType): void {
- this.skeleton_type = skinning_type
- }
-
- public set_show_debug (debug_value: boolean): void {
- this.show_debug = debug_value
- }
-
- public set_bone_index_to_test (bone_idx: number): void {
- this.bone_idx_test = bone_idx
- }
-
+export default class BoneWeightsByDistance extends AbstractAutoSkinSolver {
public calculate_indexes_and_weights (): number[][] {
+ // 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
- for (let i = 0; i < this.get_vertex_count(); i++) {
+ 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_index: number = 0
- this.bones_master_data.forEach((bone, idx) => {
+ this.get_bone_master_data().forEach((bone, idx) => {
// our human skeleton has a root controller bone that isn't in the mesh. Don't assign weights to this?
if (this.skeleton_type === SkeletonType.Human) {
if (bone.bone_object.name === 'root') {
@@ -67,7 +35,7 @@ export default class BoneWeightsByDistance implements IAutoSkinSolver {
}
})
- this.bones_master_data[closest_bone_index].assigned_vertices.push(i)
+ 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)
@@ -75,33 +43,9 @@ export default class BoneWeightsByDistance implements IAutoSkinSolver {
}
if (this.show_debug) {
- console.log('Assigned all the bones to vertices: ', this.bones_master_data)
+ console.log('Assigned all the bones to vertices: ', this.get_bone_master_data())
}
return [skin_indices, skin_weights]
}
-
- public test_bones_outside_in_mesh (): BoneTesterData {
- // don't do test for now and just return success
- return new BoneTesterData([], [])
- }
-
- private init_bone_weights_data_structure (bone_hier: Object3D): void {
- const bones_list: Bone[] = Utility.bone_list_from_hierarchy(bone_hier)
- bones_list.forEach((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)
- this.bones_master_data.push(new_bone_object)
- })
- }
-
- private get_vertex_count (): number {
- if (this.geometry === null) {
- return -1
- }
-
- return this.geometry.attributes.position.array.length / 3
- }
}
diff --git a/src/lib/solvers/BoneWeightsByDistanceChild.ts b/src/lib/solvers/BoneWeightsByDistanceChild.ts
index 5fc2b0d..d59a5ba 100644
--- a/src/lib/solvers/BoneWeightsByDistanceChild.ts
+++ b/src/lib/solvers/BoneWeightsByDistanceChild.ts
@@ -1,9 +1,7 @@
-import { type Bone, BufferGeometry, Object3D, Vector3 } from 'three'
+import { Vector3 } from 'three'
import { Utility } from '../Utilities.js'
-import BoneCalculationData from '../models/BoneCalculationData.js'
-import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.js'
-import BoneTesterData from '../models/BoneTesterData.js'
import { SkeletonType } from '../enums/SkeletonType.js'
+import { AbstractAutoSkinSolver } from './AbstractAutoSkinSolver.js'
/**
* BoneWeightsByDistanceChild
@@ -12,51 +10,22 @@ import { SkeletonType } from '../enums/SkeletonType.js'
* 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 implements IAutoSkinSolver {
- private readonly bones_master_data: BoneCalculationData[] = []
- private geometry: BufferGeometry = new BufferGeometry()
- private show_debug: boolean = false
- private skeleton_type: SkeletonType | null = null
-
- constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {
- this.set_skeleton_type(skeleton_type)
- this.init_bone_weights_data_structure(bone_hier)
- console.log('Using the Bone weights by distance child solver')
- }
-
- public set_debugging_scene_object (scene_object: Object3D): void {
- this.debugging_scene_object = scene_object
- }
-
- public set_geometry (geom: BufferGeometry): void {
- this.geometry = geom
- }
-
- public set_skeleton_type (skinning_type: SkeletonType): void {
- this.skeleton_type = skinning_type
- }
-
- public set_show_debug (debug_value: boolean): void {
- this.show_debug = debug_value
- }
-
- public set_bone_index_to_test (bone_idx: number): void {
- this.bone_idx_test = bone_idx
- }
-
+export default class BoneWeightsByDistanceChild extends AbstractAutoSkinSolver {
public calculate_indexes_and_weights (): number[][] {
+ // 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
- for (let i = 0; i < this.get_vertex_count(); i++) {
+ 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_index: number = 0
- this.bones_master_data.forEach((bone, idx) => {
+ this.get_bone_master_data().forEach((bone, idx) => {
// our human skeleton has a root controller bone that isn't in the mesh. Don't assign weights to this?
if (this.skeleton_type === SkeletonType.Human) {
if (bone.bone_object.name === 'root') {
@@ -73,7 +42,7 @@ export default class BoneWeightsByDistanceChild implements IAutoSkinSolver {
}
})
- this.bones_master_data[closest_bone_index].assigned_vertices.push(i)
+ 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)
@@ -81,33 +50,9 @@ export default class BoneWeightsByDistanceChild implements IAutoSkinSolver {
}
if (this.show_debug) {
- console.log('Assigned all the bones to vertices: ', this.bones_master_data)
+ console.log('Assigned all the bones to vertices: ', this.get_bone_master_data())
}
return [skin_indices, skin_weights]
}
-
- public test_bones_outside_in_mesh (): BoneTesterData {
- // don't do test for now and just return success
- return new BoneTesterData([], [])
- }
-
- private init_bone_weights_data_structure (bone_hier: Object3D): void {
- const bones_list: Bone[] = Utility.bone_list_from_hierarchy(bone_hier)
- bones_list.forEach((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)
- this.bones_master_data.push(new_bone_object)
- })
- }
-
- private get_vertex_count (): number {
- if (this.geometry === null) {
- return -1
- }
-
- return this.geometry.attributes.position.array.length / 3
- }
}
diff --git a/src/lib/solvers/BoneWeightsByEnvelope.ts b/src/lib/solvers/BoneWeightsByEnvelope.ts
index 2b03aa0..38d5732 100644
--- a/src/lib/solvers/BoneWeightsByEnvelope.ts
+++ b/src/lib/solvers/BoneWeightsByEnvelope.ts
@@ -4,7 +4,7 @@ import { Generators } from '../Generators.js'
import { Utility } from '../Utilities.ts'
import BoneCalculationData from '../models/BoneCalculationData.ts'
import { BufferGeometry, type Bone, Object3D, Mesh, Vector3, Raycaster, type Intersection } from 'three'
-import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.ts'
+import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.ts' // unused...delete
import type IntersectionPointData from '../models/IntersectionPointData.ts'
import IntersectionGeometryData from '../models/IntersectionGeometryData.ts'
import BoneTesterData from '../models/BoneTesterData.ts'
diff --git a/src/lib/solvers/BoneWeightsByMedianDistance.ts b/src/lib/solvers/BoneWeightsByMedianDistance.ts
index 7ebebaf..93d4499 100644
--- a/src/lib/solvers/BoneWeightsByMedianDistance.ts
+++ b/src/lib/solvers/BoneWeightsByMedianDistance.ts
@@ -1,7 +1,7 @@
import { Utility } from '../Utilities.js'
import { type Bone, BufferGeometry, Object3D, type Scene, Vector3 } from 'three'
import BoneCalculationData from '../models/BoneCalculationData.js'
-import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.js'
+import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.js' //unused...delete to abstract class
import BoneTesterData from '../models/BoneTesterData.js'
import { SkeletonType } from '../enums/SkeletonType.js'