Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Only load texture images once and store. small code cleanup

Scott Petrovicfaf5453be995
diff --git a/src/lib/CustomSkeletonHelper.ts b/src/lib/CustomSkeletonHelper.ts
index c569d42..676e467 100644
--- a/src/lib/CustomSkeletonHelper.ts
+++ b/src/lib/CustomSkeletonHelper.ts
@@ -12,6 +12,7 @@ const _matrixWorldInv = /*@__PURE__*/ new Matrix4()
class CustomSkeletonHelper extends Line2 {
private readonly joint_points: Points
+ private readonly jointTexture = new TextureLoader().load('images/skeleton-joint-point.png')
constructor (object: any, options = {}) {
const bones = getBoneList(object)
@@ -62,7 +63,7 @@ class CustomSkeletonHelper extends Line2 {
color: options.jointColor || 0xffffff,
depthTest: false,
sizeAttenuation: false, // Disable size attenuation to keep size constant in screen space
- map: new TextureLoader().load('images/skeleton-joint-point.png'), // Use a circular texture
+ map: this.jointTexture,
transparent: true // Enable transparency for the circular texture
})
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 981e706..269a443 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -29,12 +29,10 @@ export class EventListeners {
}
}, false)
- if (this.bootstrap.transform_controls !== undefined) {
- this.bootstrap.transform_controls.addEventListener('dragging-changed', (event: any) => {
- this.bootstrap.is_transform_controls_dragging = event.value
- this.bootstrap.controls.enabled = !event.value
- })
- }
+ this.bootstrap.transform_controls?.addEventListener('dragging-changed', (event: any) => {
+ this.bootstrap.is_transform_controls_dragging = event.value
+ this.bootstrap.controls.enabled = !event.value
+ })
this.bootstrap.load_model_step.addEventListener('modelLoaded', () => {
this.bootstrap.scene.add(this.bootstrap.load_model_step.model_meshes())
@@ -47,14 +45,13 @@ export class EventListeners {
this.bootstrap.process_step = this.bootstrap.process_step_changed(ProcessStep.EditSkeleton)
})
- if (this.bootstrap.ui.dom_bind_pose_button !== null) {
- this.bootstrap.ui.dom_bind_pose_button.addEventListener('click', () => {
- const passed_bone_skinning_test = this.bootstrap.test_bone_weighting_success()
- if (passed_bone_skinning_test) {
- this.bootstrap.process_step_changed(ProcessStep.BindPose)
- }
- })
- }
+ this.bootstrap.ui.dom_bind_pose_button?.addEventListener('click', () => {
+ const passed_bone_skinning_test = this.bootstrap.test_bone_weighting_success()
+ if (passed_bone_skinning_test) {
+ this.bootstrap.process_step_changed(ProcessStep.BindPose)
+ }
+ })
+
this.bootstrap.ui.dom_rotate_model_x_button?.addEventListener('click', () => {
this.bootstrap.load_model_step.rotate_model_by_axis('x', 90)
@@ -78,10 +75,12 @@ export class EventListeners {
this.bootstrap.file_export_step.export(this.bootstrap.weight_skin_step.final_skinned_meshes(), 'exported-model')
})
- this.bootstrap.ui.dom_back_to_edit_skeleton_button.addEventListener('click', () => {
+ // going back to edit skeleton step after skinning
+ // this will do a lot of resetting
+ this.bootstrap.ui.dom_back_to_edit_skeleton_button?.addEventListener('click', () => {
const existing_skinned_meshes = this.bootstrap.scene.children.filter((child: Object3D) => child.name === 'Skinned Mesh')
console.log(existing_skinned_meshes)
- existing_skinned_meshes.forEach((existing_skinned_mesh) => {
+ existing_skinned_meshes.forEach((existing_skinned_mesh: Object3D) => {
Utility.remove_object_with_children(existing_skinned_mesh)
})
@@ -92,7 +91,10 @@ export class EventListeners {
// reset current bone selection for edit skeleton step
this.bootstrap.edit_skeleton_step.set_currently_selected_bone(null)
- this.bootstrap.ui.dom_selected_bone_label.innerHTML = 'None'
+
+ if (this.bootstrap.ui.dom_selected_bone_label !== null) {
+ this.bootstrap.ui.dom_selected_bone_label.innerHTML = 'None'
+ }
})
// change view event listeners when configuring skeleton
@@ -103,12 +105,10 @@ export class EventListeners {
// listen for transform mode changes form edit skeleton step
// change transform type for controls
this.bootstrap.ui.dom_transform_translate_button?.addEventListener('click', () => {
- // this.current_transform_type = 'translate'
this.bootstrap.transform_controls.setMode('translate')
})
this.bootstrap.ui.dom_transform_rotate_button?.addEventListener('click', () => {
- // this.current_transform_type = 'rotate'
this.bootstrap.transform_controls.setMode('rotate')
})
}
diff --git a/src/lib/processes/StepEditSkeleton.ts b/src/lib/processes/StepEditSkeleton.ts
index 48e9982..bf4f6c7 100644
--- a/src/lib/processes/StepEditSkeleton.ts
+++ b/src/lib/processes/StepEditSkeleton.ts
@@ -31,6 +31,9 @@ export class StepEditSkeleton extends EventTarget {
private joint_hover_point: Object3D | null = null
private _main_scene_ref: Scene | null = null
+ private readonly joint_texture = new TextureLoader().load('images/skeleton-joint-point.png')
+
+
constructor () {
super()
this.ui = new UI()
@@ -64,10 +67,9 @@ export class StepEditSkeleton extends EventTarget {
this.update_bind_button_text()
// set default skinning algorithm based on first option
- if (this.ui.dom_skinning_algorithm_selection != null) {
- const selection = this.ui.dom_skinning_algorithm_selection.value
- this.skinning_algorithm = this.convert_skinning_algorithm_to_enum(selection)
- }
+ const selection = this.ui.dom_skinning_algorithm_selection?.value
+ this.skinning_algorithm = this.convert_skinning_algorithm_to_enum(selection)
+
this.add_event_listeners()
}
@@ -293,7 +295,7 @@ export class StepEditSkeleton extends EventTarget {
size: 20, // Size of the point in pixels
sizeAttenuation: false, // Disable size attenuation
depthTest: false, // always render on top
- map: new TextureLoader().load('images/skeleton-joint-point.png'), // Use a circular texture
+ map: this.joint_texture, // Use a circular texture
transparent: true // Enable transparency for the circular texture
})