Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

When editing skeleton with mirror mode, hide the right side to make more clean

Scott Petrovica53e3ac932e8
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 1d8924f..5c7b8d0 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -246,6 +246,19 @@ export class Mesh2MotionEngine {
this.scene.add(this.skeleton_helper)
}
+ public sync_skeleton_helper_joint_visibility (): void {
+ if (!(this.skeleton_helper instanceof CustomSkeletonHelper)) {
+ return
+ }
+
+ const is_edit_skeleton_step = this.process_step === ProcessStep.EditSkeleton
+
+ this.skeleton_helper.setJointsVisible(is_edit_skeleton_step)
+ this.skeleton_helper.setHideRightSideJoints(
+ is_edit_skeleton_step && this.edit_skeleton_step.is_mirror_mode_enabled()
+ )
+ }
+
public update_a_pose_options_visibility (): void {
if (this.ui.dom_a_pose_correction_options != null) {
if (this.load_skeleton_step.skeleton_type() === SkeletonType.Human) {
@@ -372,7 +385,7 @@ export class Mesh2MotionEngine {
this.transform_controls.enabled = true
this.transform_controls.setMode(this.transform_controls_type) // 'translate', 'rotate'
- this.skeleton_helper?.setJointsVisible(true)
+ this.sync_skeleton_helper_joint_visibility()
this.mesh_preview_display_type = ModelPreviewDisplay.WeightPainted
this.changed_model_preview_display(this.mesh_preview_display_type) // show weight painted mesh by default
@@ -393,7 +406,7 @@ export class Mesh2MotionEngine {
// update reference of skeleton helper to use the final skinned mesh
this.regenerate_skeleton_helper(this.weight_skin_step.skeleton())
- this.skeleton_helper?.setJointsVisible(false) // no need to show joints during
+ this.sync_skeleton_helper_joint_visibility()
// hide skeleton by default in animations listing step
if (this.ui.dom_show_skeleton_checkbox !== null) {
@@ -510,6 +523,10 @@ export class Mesh2MotionEngine {
return
}
+ if (!this.edit_skeleton_step.is_bone_selectable(closest_bone)) {
+ return
+ }
+
// only do selection if we are close
// the orbit controls also have panning with alt-click, so we don't want to interfere with that
if (closest_distance === null || closest_distance > this.transform_controls_hover_distance) {
diff --git a/src/lib/CustomSkeletonHelper.ts b/src/lib/CustomSkeletonHelper.ts
index f025902..d3991a8 100644
--- a/src/lib/CustomSkeletonHelper.ts
+++ b/src/lib/CustomSkeletonHelper.ts
@@ -3,7 +3,7 @@
// and ideas from
// https://discourse.threejs.org/t/extend-skeletonhelper-to-accommodate-fat-lines-perhaps-with-linesegments2/59436/2
-import { Color, Matrix4, Vector3, Points, PointsMaterial, BufferGeometry, Float32BufferAttribute, TextureLoader, LineSegments, LineBasicMaterial } from 'three'
+import { Color, Matrix4, Vector3, Points, PointsMaterial, BufferGeometry, Float32BufferAttribute, TextureLoader, LineSegments, LineBasicMaterial, type Bone } from 'three'
const _vector = /*@__PURE__*/ new Vector3()
const _boneMatrix = /*@__PURE__*/ new Matrix4()
@@ -12,6 +12,7 @@ const _matrixWorldInv = /*@__PURE__*/ new Matrix4()
class CustomSkeletonHelper extends LineSegments {
private readonly joint_points: Points
private readonly jointTexture = new TextureLoader().load('/images/skeleton-joint-point.png')
+ private hide_right_side_joints: boolean = false
constructor (object: any, options = {}) {
const bones = getBoneList(object)
@@ -87,7 +88,12 @@ class CustomSkeletonHelper extends LineSegments {
const bone = bones[i]
_boneMatrix.multiplyMatrices(_matrixWorldInv, bone.matrixWorld)
_vector.setFromMatrixPosition(_boneMatrix)
- pointPositions.setXYZ(i, _vector.x, _vector.y, _vector.z) // Update point position
+
+ if (this.hide_right_side_joints && is_right_side_bone(bone)) {
+ pointPositions.setXYZ(i, Number.NaN, Number.NaN, Number.NaN)
+ } else {
+ pointPositions.setXYZ(i, _vector.x, _vector.y, _vector.z) // Update point position
+ }
if (bone.parent && bone.parent.isBone) {
_boneMatrix.multiplyMatrices(_matrixWorldInv, bone.parent.matrixWorld)
@@ -130,6 +136,10 @@ class CustomSkeletonHelper extends LineSegments {
public setJointsVisible (visible: boolean): void {
this.joint_points.visible = visible
}
+
+ public setHideRightSideJoints (value: boolean): void {
+ this.hide_right_side_joints = value
+ }
}
function getBoneList (object: any): any[] {
@@ -146,4 +156,10 @@ function getBoneList (object: any): any[] {
return boneList
}
+function is_right_side_bone (bone: Bone): boolean {
+ const normalized_bone_name = bone.name.toLowerCase()
+
+ return /(^right_|^r_|_right$|_r$|\.right$|\.r$|-right$|-r$)/.test(normalized_bone_name)
+}
+
export { CustomSkeletonHelper }
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 11b89f7..b3bce9d 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -26,6 +26,7 @@ export class EventListeners {
// Update skeleton helper if it exists
if (this.bootstrap.skeleton_helper !== undefined) {
this.bootstrap.regenerate_skeleton_helper(this.bootstrap.edit_skeleton_step.skeleton(), 'Skeleton Helper')
+ this.bootstrap.sync_skeleton_helper_joint_visibility()
}
// Refresh weight painting if in weight painted mode
@@ -34,6 +35,10 @@ export class EventListeners {
}
})
+ this.bootstrap.edit_skeleton_step.addEventListener('mirrorModeChanged', () => {
+ this.bootstrap.sync_skeleton_helper_joint_visibility()
+ })
+
// attribution link clicking brings up contributors dialog
this.bootstrap.ui.dom_attribution_link?.addEventListener('click', (event: MouseEvent) => {
event.preventDefault()
diff --git a/src/lib/UI.ts b/src/lib/UI.ts
index 1b70455..adbd3fd 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -24,7 +24,7 @@ export class UI {
dom_skeleton_drop_type: HTMLSelectElement | null = null
dom_hand_skeleton_options: HTMLElement | null = null
dom_hand_skeleton_selection: HTMLSelectElement | null = null
- dom_mirror_skeleton_checkbox: HTMLElement | null = null
+ dom_mirror_skeleton_checkbox: HTMLInputElement | null = null
dom_independent_bone_movement_checkbox: HTMLInputElement | null = null
dom_scale_skeleton_button: HTMLButtonElement | null = null
dom_undo_button: HTMLButtonElement | null = null
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index a692030..27839af 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -136,6 +136,14 @@ export class StepEditSkeleton extends EventTarget {
this.show_debug = false
}
+ let mirror_mode_enabled: boolean = this.mirror_mode_enabled
+
+ if (this.ui.dom_mirror_skeleton_checkbox !== null) {
+ mirror_mode_enabled = this.ui.dom_mirror_skeleton_checkbox.checked
+ }
+
+ this.set_mirror_mode_enabled(mirror_mode_enabled)
+
this.update_bind_button_text()
// Don't add event listeners again if we are navigating back to this step
@@ -202,12 +210,32 @@ export class StepEditSkeleton extends EventTarget {
public set_mirror_mode_enabled (value: boolean): void {
this.mirror_mode_enabled = value
+ this.dispatchEvent(new CustomEvent('mirrorModeChanged', {
+ detail: { enabled: value }
+ }))
}
public is_mirror_mode_enabled (): boolean {
return this.mirror_mode_enabled
}
+ public is_bone_selectable (bone: Bone | null): boolean {
+ if (bone === null) {
+ return false
+ }
+
+ if (!this.mirror_mode_enabled) {
+ return true
+ }
+
+ return !this.is_right_side_bone(bone)
+ }
+
+ private is_right_side_bone (bone: Bone): boolean {
+ const normalized_bone_name = bone.name.toLowerCase()
+ return /(^right_|^r_|_right$|_r$|\.right$|\.r$|-right$|-r$)/.test(normalized_bone_name)
+ }
+
/**
* Find the mirrored counterpart of a bone by stripping side suffixes and
* matching against the rest of the skeleton. Returns undefined for centre-line
@@ -269,19 +297,37 @@ export class StepEditSkeleton extends EventTarget {
if (this.ui.dom_mirror_skeleton_checkbox !== null) {
this.ui.dom_mirror_skeleton_checkbox.addEventListener('change', (event) => {
+ const target = event.target as HTMLInputElement | null
+
+ if (target === null) {
+ return
+ }
+
// mirror skeleton movements along the X axis
- this.set_mirror_mode_enabled(event.target.checked)
+ this.set_mirror_mode_enabled(target.checked)
})
}
if (this.ui.dom_independent_bone_movement_checkbox !== null) {
this.ui.dom_independent_bone_movement_checkbox.addEventListener('change', (event) => {
- this.independent_bone_movement.set_enabled(event.target.checked)
+ const target = event.target as HTMLInputElement | null
+
+ if (target === null) {
+ return
+ }
+
+ this.independent_bone_movement.set_enabled(target.checked)
})
}
this.ui.dom_enable_skin_debugging?.addEventListener('change', (event) => {
- this.show_debug = event.target.checked
+ const target = event.target as HTMLInputElement | null
+
+ if (target === null) {
+ return
+ }
+
+ this.show_debug = target.checked
this.update_bind_button_text()
})
@@ -474,6 +520,11 @@ export class StepEditSkeleton extends EventTarget {
return
}
+ if (!this.is_bone_selectable(closest_bone)) {
+ this.update_bone_hover_point_position(null)
+ return
+ }
+
this.update_bone_hover_point_position(closest_bone)
}