Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Try to make placing joints easier by color grouping and cleaner circle graphic

Scott Petrovic331bda3846e5
diff --git a/src/lib/CustomSkeletonHelper.ts b/src/lib/CustomSkeletonHelper.ts
index d3991a8..5d68bb3 100644
--- a/src/lib/CustomSkeletonHelper.ts
+++ b/src/lib/CustomSkeletonHelper.ts
@@ -3,25 +3,38 @@
// 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, type Bone } from 'three'
+import { Color, Matrix4, Vector3, Points, PointsMaterial, BufferGeometry, Float32BufferAttribute, TextureLoader, LineSegments, LineBasicMaterial, type Bone, type ColorRepresentation } from 'three'
+import { BoneCategory, BoneClassifier } from './solvers/BoneClassifier'
const _vector = /*@__PURE__*/ new Vector3()
const _boneMatrix = /*@__PURE__*/ new Matrix4()
const _matrixWorldInv = /*@__PURE__*/ new Matrix4()
+interface CustomSkeletonHelperOptions {
+ color?: ColorRepresentation
+ jointColor?: ColorRepresentation
+}
+
+const bone_category_colors: Record<BoneCategory, number> = {
+ [BoneCategory.Torso]: 0xff9f1c,
+ [BoneCategory.Limb]: 0x3a86ff,
+ [BoneCategory.Extremity]: 0x8338ec,
+ [BoneCategory.Other]: 0xadb5bd
+}
+
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 = {}) {
+ constructor (object: any, options: CustomSkeletonHelperOptions = {}) {
const bones = getBoneList(object)
const geometry = new BufferGeometry()
const vertices = []
const colors = []
const color = new Color(options.color || 0x0000ff) // Default color blue
- const joint_color = new Color(options.jointColor || 0x0000ff) // Default joint color blue
+ const joint_color = new Color(options.jointColor ?? 0x0000ff) // Default joint color blue
for (let i = 0; i < bones.length; i++) {
const bone = bones[i]
@@ -58,18 +71,33 @@ class CustomSkeletonHelper extends LineSegments {
// Add points for joints
const pointsGeometry = new BufferGeometry()
const pointsMaterial = new PointsMaterial({
- size: 25, // Size of the joint circles on skeleton
- color: joint_color,
+ size: 30, // Size of the joint circles on skeleton
+ color: options.jointColor !== undefined ? joint_color : 0xffffff,
depthTest: false,
sizeAttenuation: false, // Disable size attenuation to keep size constant in screen space
map: this.jointTexture,
transparent: true, // Enable transparency for the circular texture
- opacity: 0.3
+ opacity: 0.8,
+ vertexColors: options.jointColor === undefined
})
const pointPositions = new Float32BufferAttribute(bones.length * 3, 3)
pointsGeometry.setAttribute('position', pointPositions)
+ // use bone category to color joints to help with seeing a bunch of them
+ if (options.jointColor === undefined) {
+ const bone_classifier = new BoneClassifier(bones as Bone[])
+ const point_colors: number[] = []
+
+ bones.forEach((bone, idx) => {
+ const bone_category = bone_classifier.get_category(idx)
+ const category_color = new Color(bone_category_colors[bone_category])
+ point_colors.push(category_color.r, category_color.g, category_color.b)
+ })
+
+ pointsGeometry.setAttribute('color', new Float32BufferAttribute(point_colors, 3))
+ }
+
this.joint_points = new Points(pointsGeometry, pointsMaterial)
this.add(this.joint_points)
}
diff --git a/src/lib/solvers/BoneClassifier.ts b/src/lib/solvers/BoneClassifier.ts
index 8694316..e7649c8 100644
--- a/src/lib/solvers/BoneClassifier.ts
+++ b/src/lib/solvers/BoneClassifier.ts
@@ -58,22 +58,12 @@ export class BoneClassifier {
private classify_bone (bone: Bone): BoneCategory {
const name = bone.name.toLowerCase()
- // Torso bones: spine, chest, hips, pelvis, neck, wings, tails
- // tails and feathers aren't technically torso, but we want
- // to give them more smoothing since they aren't as rigid
- const torso_keywords = [
- 'spine', 'chest', 'hips', 'pelvis', 'neck', 'torso', 'abdomen', 'body',
- 'wing', 'feather', 'tail'
- ]
- if (torso_keywords.some(kw => name.includes(kw))) {
- return BoneCategory.Torso
- }
// Extremity bones: hands, feet, fingers, toes
const extremity_keywords = [
'hand', 'foot', 'toe', 'ball',
'thumb', 'index', 'middle', 'ring', 'pinky', 'finger',
- 'mouth', 'jaw', 'eye', 'tongue'
+ 'eye', 'tongue', 'wing', 'feather'
]
if (extremity_keywords.some(kw => name.includes(kw))) {
return BoneCategory.Extremity
@@ -82,13 +72,24 @@ export class BoneClassifier {
// Limb bones: upper/lower arms, thighs, calves, shoulders
const limb_keywords = [
'arm', 'upperarm', 'lowerarm', 'forearm', 'elbow', 'wrist',
- 'shoulder', 'clavicle',
+ 'shoulder', 'clavicle', 'ankle', 'fin',
'thigh', 'calf', 'shin', 'knee', 'leg', 'upleg', 'lowleg'
]
if (limb_keywords.some(kw => name.includes(kw))) {
return BoneCategory.Limb
}
+ // Torso bones: spine, chest, hips, pelvis, neck, wings, tails
+ // tails and feathers aren't technically torso, but we want
+ // to give them more smoothing since they aren't as rigid
+ const torso_keywords = [
+ 'spine', 'chest', 'hips', 'pelvis', 'neck', 'torso', 'abdomen', 'body',
+ 'tail', 'head', 'mouth', 'stomach', 'chin', 'teeth'
+ ]
+ if (torso_keywords.some(kw => name.includes(kw))) {
+ return BoneCategory.Torso
+ }
+
return BoneCategory.Other
}
}
diff --git a/static/images/skeleton-joint-point.png b/static/images/skeleton-joint-point.png
index 7c00620..9edf2e3 100644
Binary files a/static/images/skeleton-joint-point.png and b/static/images/skeleton-joint-point.png differ