Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Give the skeleton helper balls for joints

Scott Petrovic2c035cf19c8d
diff --git a/src/lib/CustomSkeletonHelper.ts b/src/lib/CustomSkeletonHelper.ts
index 083e9a1..4f98f29 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, Vector2, Vector3 } from 'three'
+import { Color, Matrix4, Vector2, Vector3, SphereGeometry, MeshBasicMaterial, Mesh } from 'three'
import { Line2, LineGeometry, LineMaterial } from 'three/examples/jsm/Addons.js'
const _vector = /*@__PURE__*/ new Vector3()
@@ -11,9 +11,10 @@ const _boneMatrix = /*@__PURE__*/ new Matrix4()
const _matrixWorldInv = /*@__PURE__*/ new Matrix4()
class CustomSkeletonHelper extends Line2 {
+ private readonly joint_spheres: Mesh[] = []
+
constructor (object: any, options = {}) {
const bones = getBoneList(object)
- // const geometry = new BufferGeometry();
const geometry = new LineGeometry()
const vertices = []
@@ -30,11 +31,6 @@ class CustomSkeletonHelper extends Line2 {
colors.push(color.r, color.g, color.b)
}
}
-
- // geometry.setAttribute(
- // 'instanceStart', new Float32BufferAttribute(lineStart, 3));
- // geometry.setAttribute(
- // 'instanceEnd', new Float32BufferAttribute(lineStop, 3));
geometry.setPositions(vertices)
geometry.setColors(colors)
@@ -60,23 +56,17 @@ class CustomSkeletonHelper extends Line2 {
this.matrixAutoUpdate = false
window.addEventListener('resize', () => {this.updateResolution()})
- }
-
- // Method to dynamically update the line width
- setLineWidth(newWidth: number): void {
- this.material.linewidth = newWidth
- this.material.needsUpdate = true // Trigger material update
- }
- // Method to dynamically update dashed property
- setDashed(isDashed: boolean): void {
- this.material.dashed = isDashed
- this.material.needsUpdate = true // Trigger material update
- }
+ // add sphere geometry for joints
+ const sphereGeometry = new SphereGeometry(0.01, 16, 16)
+ const joint_color: Color = new Color(options.jointColor || 0x0000ff)
+ const sphereMaterial = new MeshBasicMaterial({ color: joint_color, depthTest: false })
- updateResolution (): void {
- this.material.resolution.set(window.innerWidth, window.innerHeight);
- console.log('Resolution updated:', this.material.resolution);
+ for (let i = 0; i < bones.length; i++) {
+ const sphere = new Mesh(sphereGeometry, sphereMaterial)
+ this.add(sphere)
+ this.joint_spheres.push(sphere)
+ }
}
updateMatrixWorld (force: boolean): void {
@@ -88,15 +78,22 @@ class CustomSkeletonHelper extends Line2 {
_matrixWorldInv.copy(this.root.matrixWorld).invert()
- for (const [index, bone] of bones.entries()) {
+ let lineIndex = 0
+ for (let i = 0; i < bones.length; i++) {
+ const bone = bones[i];
+ _boneMatrix.multiplyMatrices(_matrixWorldInv, bone.matrixWorld)
+ _vector.setFromMatrixPosition(_boneMatrix)
+ this.joint_spheres[i].position.copy(_vector)
+
if (bone.parent && bone.parent.isBone) {
- _boneMatrix.multiplyMatrices(_matrixWorldInv, bone.matrixWorld)
- _vector.setFromMatrixPosition(_boneMatrix)
- lineStart.setXYZ(index, _vector.x, _vector.y, _vector.z)
-
_boneMatrix.multiplyMatrices(_matrixWorldInv, bone.parent.matrixWorld)
_vector.setFromMatrixPosition(_boneMatrix)
- lineEnd.setXYZ(index, _vector.x, _vector.y, _vector.z)
+ lineStart.setXYZ(lineIndex, _vector.x, _vector.y, _vector.z)
+
+ _boneMatrix.multiplyMatrices(_matrixWorldInv, bone.matrixWorld)
+ _vector.setFromMatrixPosition(_boneMatrix)
+ lineEnd.setXYZ(lineIndex, _vector.x, _vector.y, _vector.z)
+ lineIndex++
}
}
@@ -108,6 +105,11 @@ class CustomSkeletonHelper extends Line2 {
dispose (): void {
this.geometry.dispose()
this.material.dispose()
+
+ this.joint_spheres.forEach(sphere => {
+ sphere.geometry.dispose()
+ sphere.material.dispose()
+ })
}
}
diff --git a/src/lib/Generators.ts b/src/lib/Generators.ts
index b290d9f..72b2a4b 100644
--- a/src/lib/Generators.ts
+++ b/src/lib/Generators.ts
@@ -30,15 +30,16 @@ export class Generators {
floor_mesh.receiveShadow = true
// xyz axes helper display
- const axes_helper = new AxesHelper(0.15)
+ const axes_helper = new AxesHelper(0.3)
axes_helper.name = 'Axes Helper'
- axes_helper.position.copy(new Vector3(0, 0.002, 0)) // offset a bit to avoid z-fighting
+ axes_helper.position.copy(new Vector3(0, 0.008, 0)) // offset a bit to avoid z-fighting
// grid display on floor
const size: number = 10
const divisions: number = 10
const grid_helper: GridHelper = new GridHelper(size, divisions, color, color)
+
return [grid_helper, floor_mesh, axes_helper]
}
diff --git a/src/script.ts b/src/script.ts
index 93016be..5dcdf27 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -90,7 +90,7 @@ export class Bootstrap {
// basic things in another group, to better isolate what we are working on
this.environment_container.name = 'Setup objects'
this.environment_container.add(...Generators.create_default_lights())
- this.environment_container.add(...Generators.create_grid_helper())
+ this.environment_container.add(...Generators.create_grid_helper(0x4496a8))
this.scene.add(this.environment_container)
const fog_near = 0
@@ -105,7 +105,7 @@ export class Bootstrap {
this.scene.remove(this.skeleton_helper)
}
- this.skeleton_helper = new CustomSkeletonHelper(new_skeleton.bones[0], { dashed: true, linewidth: 0.005, color: 0x00ff00 })
+ this.skeleton_helper = new CustomSkeletonHelper(new_skeleton.bones[0], { dashed: true, linewidth: 0.01, color: 0x4e7d58, jointColor: 0x00ff00 })
this.skeleton_helper.name = helper_name
this.scene.add(this.skeleton_helper)
}
diff --git a/src/styles.css b/src/styles.css
index 17c30b4..dc867d8 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -166,7 +166,7 @@ option {
#info-panel
{
- background: #0000003d;
+ background: #171d2d;
overflow-y: auto;
height: fit-content;
bottom: 20px;
@@ -177,6 +177,7 @@ option {
border-radius: 3px;
border: 1px solid #3e4e51;
max-height: calc(100% - 50px);
+ box-shadow: 3px 3px 9px #0c1016;
}
#info-messaging {
padding: 0.5rem;