Import a 3D Model and automatically assign and export animations
TypeScript
Switch to custom skeleton helper to try to get better display
Scott Petrovic7d7ef843cf20
diff --git a/src/lib/CustomSkeletonHelper.ts b/src/lib/CustomSkeletonHelper.tsnew file mode 100644index 0000000..083e9a1--- /dev/null+++ b/src/lib/CustomSkeletonHelper.ts@@ -0,0 +1,128 @@+// Original code from+// https://github.com/mrdoob/three.js/blob/master/src/helpers/SkeletonHelper.js+// 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 { Line2, LineGeometry, LineMaterial } from 'three/examples/jsm/Addons.js'++const _vector = /*@__PURE__*/ new Vector3()+const _boneMatrix = /*@__PURE__*/ new Matrix4()+const _matrixWorldInv = /*@__PURE__*/ new Matrix4()++class CustomSkeletonHelper extends Line2 {+ constructor (object: any, options = {}) {+ const bones = getBoneList(object)+ // const geometry = new BufferGeometry();+ const geometry = new LineGeometry()++ const vertices = []+ const colors = []+ const color = new Color(options.color || 0x0000ff) // Default color blue++ for (let i = 0; i < bones.length; i++) {+ const bone = bones[i]++ if (bone.parent && bone.parent.isBone) {+ vertices.push(0, 0, 0) // Start+ vertices.push(0, 0, 0) // End+ colors.push(color.r, color.g, color.b)+ 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)++ const material = new LineMaterial({+ linewidth: options.linewidth || 0.005,+ worldUnits: true,+ vertexColors: true,+ dashed: options.dashed || false, // this doesn't seem to do anything+ alphaToCoverage: true,+ resolution: new Vector2(window.innerWidth, window.innerHeight),+ depthTest: false+ })++ super(geometry, material)++ this.isSkeletonHelper = true+ this.type = 'CustomSkeletonHelper'++ this.root = object+ this.bones = bones++ this.matrix = object.matrixWorld+ 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+ }++ updateResolution (): void {+ this.material.resolution.set(window.innerWidth, window.innerHeight);+ console.log('Resolution updated:', this.material.resolution);+ }++ updateMatrixWorld (force: boolean): void {+ const bones = this.bones;++ const geometry = this.geometry+ const lineStart = geometry.getAttribute('instanceStart')+ const lineEnd = geometry.getAttribute('instanceEnd')++ _matrixWorldInv.copy(this.root.matrixWorld).invert()++ for (const [index, bone] of bones.entries()) {+ 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)+ }+ }++ geometry.getAttribute('instanceStart').needsUpdate = true+ geometry.getAttribute('instanceEnd').needsUpdate = true+ super.updateMatrixWorld(force)+ }++ dispose (): void {+ this.geometry.dispose()+ this.material.dispose()+ }+}++function getBoneList (object: any): any[] {+ const boneList: any[] = []++ if (object.isBone === true) {+ boneList.push(object)+ }++ for (let i = 0; i < object.children.length; i++) {+ boneList.push.apply(boneList, getBoneList(object.children[i]))+ }++ return boneList+}++export { CustomSkeletonHelper }diff --git a/src/lib/Generators.ts b/src/lib/Generators.tsindex 396bb12..b290d9f 100644--- a/src/lib/Generators.ts+++ b/src/lib/Generators.ts@@ -1,10 +1,12 @@import {PerspectiveCamera, DoubleSide, DirectionalLight, GridHelper,Bone, Skeleton, AmbientLight, PlaneGeometry, Mesh,- SphereGeometry, SkeletonHelper, type MeshBasicMaterial, MeshPhongMaterial, AxesHelper,+ SphereGeometry, type MeshBasicMaterial, MeshPhongMaterial, AxesHelper,Vector3, PointsMaterial, BufferGeometry, Points, type Object3D, WebGLRenderer} from 'three'+import { CustomSkeletonHelper } from './CustomSkeletonHelper'+import { Utility } from './Utilities'// eslint-disable-next-line @typescript-eslint/no-extraneous-class@@ -74,9 +76,9 @@ export class Generators {return skeleton}- static create_skeleton_helper(skeleton: Skeleton): SkeletonHelper- {- const skeleton_helper = new SkeletonHelper(skeleton.bones[0])+ static create_skeleton_helper (skeleton: Skeleton): CustomSkeletonHelper {+ const skeleton_helper = new CustomSkeletonHelper(skeleton.bones[0], { lineWidth: 5, dashed: true })+ skeleton_helper.setLineWidth(30)return skeleton_helper}diff --git a/src/script.ts b/src/script.tsindex 140ff05..93016be 100644--- a/src/script.ts+++ b/src/script.ts@@ -16,12 +16,14 @@ import { StepExportToFile } from './lib/processes/StepExportToFile.ts'import { StepWeightSkin } from './lib/processes/StepWeightSkin.ts'import { ProcessStep } from './lib/enums/ProcessStep.ts'-import { type Bone, Group, Scene, type Skeleton, type Vector3, type SkeletonHelper, type Points } from 'three'+import { type Bone, Group, Scene, type Skeleton, type Vector3, type Points } from 'three'import type BoneTesterData from './lib/models/BoneTesterData.ts'import { build_version } from './environment.js'import { SkeletonType } from './lib/enums/SkeletonType.ts'+import { CustomSkeletonHelper } from './lib/CustomSkeletonHelper.ts'+export class Bootstrap {private readonly camera = Generators.create_camera()private readonly renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })@@ -41,7 +43,7 @@ export class Bootstrap {private readonly scene: Scene = new Scene()// for looking at specific bones- private skeleton_helper: SkeletonHelper | undefined = undefined+ private skeleton_helper: CustomSkeletonHelper | undefined = undefinedprivate debugging_visual_object: Group = new Group()private process_step: ProcessStep = ProcessStep.LoadModel@@ -103,7 +105,7 @@ export class Bootstrap {this.scene.remove(this.skeleton_helper)}- this.skeleton_helper = new THREE.SkeletonHelper(new_skeleton.bones[0])+ this.skeleton_helper = new CustomSkeletonHelper(new_skeleton.bones[0], { dashed: true, linewidth: 0.005, color: 0x00ff00 })this.skeleton_helper.name = helper_namethis.scene.add(this.skeleton_helper)}