Import a 3D Model and automatically assign and export animations
TypeScript
Convert load skeleton step to typescript
Scott Petrovicd635b1f93c95
diff --git a/src/lib/processes/StepLoadSkeleton.js b/src/lib/processes/StepLoadSkeleton.jsdeleted file mode 100644index f6651b5..0000000--- a/src/lib/processes/StepLoadSkeleton.js+++ /dev/null@@ -1,157 +0,0 @@-import { UI } from '../UI.ts'-import { Generators } from '../Generators.ts'-import { BufferGeometry, Float32BufferAttribute, Mesh, Box3, MeshBasicMaterial } from 'three'-import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'-import { Utility } from '../Utilities.ts'-import { SkeletonType } from '../enums/SkeletonType.js'--// Note: EventTarget is a built-ininterface and do not need to import it-export class StepLoadSkeleton extends EventTarget-{- loader = null- ui = null- loaded_armature = null- loaded_skeleton = null- _skeleton_type = null-- constructor()- {- super()- }-- skeleton_type()- {- return this._skeleton_type- }-- begin()- {- this.ui = new UI()-- this.ui.dom_current_step_index.innerHTML = '2'- this.ui.dom_current_step_element.innerHTML = 'Load Skeleton'- this.ui.dom_load_skeleton_tools.style.display = 'flex'-- this.addEventListeners()- }-- instructions_text()- {- return `<div>Instructions</div>- <div>Select skeleton based off model type.</div>- <ol>- <li>Mixamo human has more bones in hands</li>- <li>Four leg creature has a tail</li>- <li>You will be able to resize or move bones after importing</li>- </ol>`- }-- addEventListeners()- {- this.ui.dom_load_skeleton_button.addEventListener('click', () => {-- // get currently selected option out of the model-selection drop-down- const skeleton_selection = this.ui.dom_skeleton_drop_type.options- let skeleton_file = skeleton_selection[skeleton_selection.selectedIndex].value--- // set the skeleton type. This will be used for the animations listing later- // so it knows what animations to load- switch(skeleton_file)- {- case 'quadraped':- this._skeleton_type = SkeletonType.Quadraped- break- case 'bipedal-simple':- this._skeleton_type = SkeletonType.BipedalSimple- break- case 'bipedal-full':- this._skeleton_type = SkeletonType.BipedalFull- break- }-- // load skeleton from GLB file- this.loader = new GLTFLoader()- this.loader.load( this._skeleton_type, (gltf) => {- // traverse scene and find first bone object- // we will go to the parent and mark that as the original armature- let armature_found = false- let original_armature = null-- gltf.scene.traverse((child) => {- if(child.type === 'Bone' && armature_found === false)- {- armature_found = true- original_armature = child.parent- }- })-- this.loaded_armature = original_armature.clone()- this.loaded_armature.name = 'Loaded Armature'-- // scale geometry for skeleton- const skeleton_geometry = this._buffer_geometry_from_armature(this.loaded_armature)- const temp_skeleton_mesh = new Mesh(skeleton_geometry, new MeshBasicMaterial({color: 0x00ff00}))- const bounding_box = new Box3().setFromObject(temp_skeleton_mesh)- const height = bounding_box.max.y - bounding_box.min.y- const max_height = 1.5- const scale_factor = max_height / height-- // try to scale each bone down instead of the whole armature object- Utility.scale_armature_by_scalar(this.loaded_armature, scale_factor)-- // reset the armature to 0,0,0 in case it is off for some reason- this.loaded_armature.position.set(0, 0, 0)- this.loaded_armature.updateWorldMatrix(true, true)-- this.dispatchEvent(new CustomEvent('skeletonLoaded', { detail: this.loaded_armature } ) )- })- })- }-- armature()- {- return this.loaded_armature- }-- skeleton()- {- if(loaded_skeleton !== null)- {- this.loaded_skeleton- }-- // create skeleton and helper to visualize- this.loaded_skeleton = Generators.create_skeleton(this.loaded_armature.children[0])- this.loaded_skeleton.name = 'Mesh Editing Skeleton'-- // update the world matrix for the skeleton- // without this the skeleton helper won't appear when the bones are first loaded- skeleton.bones[0].updateWorldMatrix( true, true )-- return skeleton- }-- _buffer_geometry_from_armature(armature)- {- // create a geometry object from the skeleton- // we will use this to calculate the bone weights- const geometry = new BufferGeometry()- const vertices = []-- armature.traverse((bone) => {- if(bone.type === 'Bone')- {- const bone_world_pos = Utility.world_position_from_object(bone)- vertices.push(bone_world_pos.x)- vertices.push(bone_world_pos.y)- vertices.push(bone_world_pos.z)- }- })-- // add the vertices to the geometry- geometry.setAttribute('position', new Float32BufferAttribute(vertices, 3))-- return geometry- }-}diff --git a/src/lib/processes/StepLoadSkeleton.ts b/src/lib/processes/StepLoadSkeleton.tsnew file mode 100644index 0000000..533c773--- /dev/null+++ b/src/lib/processes/StepLoadSkeleton.ts@@ -0,0 +1,159 @@+import { UI } from '../UI.ts'+import { Generators } from '../Generators.ts'+import {+ BufferGeometry, Float32BufferAttribute, Mesh, Box3, MeshBasicMaterial,+ Object3D, type Object3DEventMap, type Skeleton+} from 'three'+import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'+import { Utility } from '../Utilities.ts'+import { SkeletonType } from '../enums/SkeletonType.js'++// Note: EventTarget is a built-ininterface and do not need to import it+export class StepLoadSkeleton extends EventTarget {+ private readonly loader: GLTFLoader = new GLTFLoader()+ private readonly ui: UI = new UI()+ private loaded_armature: Object3D = new Object3D()+ private loaded_skeleton: Skeleton | undefined+ private skeleton_ty: string = ''++ public skeleton_type (): string {+ return this.skeleton_ty+ }++ begin (): void {+ if (this.ui.dom_current_step_index !== null) {+ this.ui.dom_current_step_index.innerHTML = '2'+ }++ if (this.ui.dom_current_step_element !== null) {+ this.ui.dom_current_step_element.innerHTML = 'Load Skeleton'+ }++ if (this.ui.dom_load_skeleton_tools !== null) {+ this.ui.dom_load_skeleton_tools.style.display = 'flex'+ }++ this.add_event_listeners()+ }++ public instructions_text (): string {+ return `<div>Instructions</div>+ <div>Select skeleton based off model type.</div>+ <ol>+ <li>Mixamo human has more bones in hands</li>+ <li>Four leg creature has a tail</li>+ <li>You will be able to resize or move bones after importing</li>+ </ol>`+ }++ private add_event_listeners (): void {+ if (this.ui.dom_load_skeleton_button !== null) {+ this.ui.dom_load_skeleton_button.addEventListener('click', () => {+ if (this.ui.dom_skeleton_drop_type === null) {+ console.warn('could not find skeleton selection drop down HTML element')+ return+ }++ // get currently selected option out of the model-selection drop-down+ const skeleton_selection = this.ui.dom_skeleton_drop_type.options++ const skeleton_file: string = skeleton_selection[skeleton_selection.selectedIndex].value++ // set the skeleton type. This will be used for the animations listing later+ // so it knows what animations to load+ switch (skeleton_file) {+ case 'quadraped':+ this.skeleton_ty = SkeletonType.Quadraped+ break+ case 'bipedal-simple':+ this.skeleton_ty = SkeletonType.BipedalSimple+ break+ case 'bipedal-full':+ this.skeleton_ty = SkeletonType.BipedalFull+ break+ }++ // load skeleton from GLB file+ this.loader.load(this.skeleton_ty, (gltf) => {+ // traverse scene and find first bone object+ // we will go to the parent and mark that as the original armature+ let armature_found = false+ let original_armature: Object3D = new Object3D()++ gltf.scene.traverse((child) => {+ if (child.type === 'Bone' && !armature_found) {+ armature_found = true++ if (child.parent != null) {+ original_armature = child.parent+ } else {+ console.warn('could not find armature parent while loading skeleton')+ }+ }+ })++ this.loaded_armature = original_armature.clone()+ this.loaded_armature.name = 'Loaded Armature'++ // scale geometry for skeleton+ const skeleton_geometry = this.buffer_geometry_from_armature(this.loaded_armature)+ const temp_skeleton_mesh = new Mesh(skeleton_geometry, new MeshBasicMaterial({ color: 0x00ff00 }))+ const bounding_box = new Box3().setFromObject(temp_skeleton_mesh)+ const height = bounding_box.max.y - bounding_box.min.y+ const max_height = 1.5+ const scale_factor = max_height / height++ // try to scale each bone down instead of the whole armature object+ Utility.scale_armature_by_scalar(this.loaded_armature, scale_factor)++ // reset the armature to 0,0,0 in case it is off for some reason+ this.loaded_armature.position.set(0, 0, 0)+ this.loaded_armature.updateWorldMatrix(true, true)++ this.dispatchEvent(new CustomEvent('skeletonLoaded', { detail: this.loaded_armature }))+ })+ })+ }// end if statement+ }++ public armature (): Object3D<Object3DEventMap> {+ return this.loaded_armature+ }++ public skeleton (): any {+ if (this.loaded_skeleton !== null) {+ return this.loaded_skeleton+ }++ // create skeleton and helper to visualize+ this.loaded_skeleton = Generators.create_skeleton(this.loaded_armature.children[0])+ this.loaded_skeleton.name = 'Mesh Editing Skeleton'++ // update the world matrix for the skeleton+ // without this the skeleton helper won't appear when the bones are first loaded+ skeleton.bones[0].updateWorldMatrix(true, true)++ return skeleton+ }++ private buffer_geometry_from_armature (armature: Object3D): BufferGeometry {+ // create a geometry object from the skeleton+ // we will use this to calculate the bone weights+ const geometry: BufferGeometry = new BufferGeometry()+ const vertices: number[] = [] // array of numbers for positions++ armature.traverse((bone) => {+ if (bone.type === 'Bone') {+ const bone_world_pos = Utility.world_position_from_object(bone)+ vertices.push(bone_world_pos.x)+ vertices.push(bone_world_pos.y)+ vertices.push(bone_world_pos.z)+ }+ })++ // add the vertices to the geometry+ geometry.setAttribute('position', new Float32BufferAttribute(vertices, 3))++ return geometry+ }+}diff --git a/src/script.js b/src/script.jsindex 03d2e49..2538094 100644--- a/src/script.js+++ b/src/script.js@@ -9,7 +9,7 @@ import { TransformControls } from 'three/examples/jsm/controls/TransformControlsimport { UI } from './lib/UI.ts'import { StepLoadModel } from './lib/processes/StepLoadModel.ts'-import { StepLoadSkeleton } from './lib/processes/StepLoadSkeleton.js'+import { StepLoadSkeleton } from './lib/processes/StepLoadSkeleton.ts'import { StepEditSkeleton } from './lib/processes/StepEditSkeleton.ts'import { StepAnimationsListing } from './lib/processes/StepAnimationsListing.ts'import { StepExportToFile } from './lib/processes/StepExportToFile.ts'