Import a 3D Model and automatically assign and export animations
TypeScript
First shot at failing to play and loop through each animation
Scott Petrovic79b3519cef9e
diff --git a/package-lock.json b/package-lock.jsonindex 995e6f1..1618b8f 100644--- a/package-lock.json+++ b/package-lock.json@@ -9,6 +9,7 @@"version": "0.0.0","dependencies": {"file-saver": "^2.0.5",+ "jszip": "^3.10.1","three": "0.175.0","vite-plugin-glsl": "^1.1.2"},@@ -718,6 +719,12 @@"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==","dev": true},+ "node_modules/core-util-is": {+ "version": "1.0.3",+ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",+ "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==",+ "license": "MIT"+ },"node_modules/cross-spawn": {"version": "7.0.3","resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",@@ -1728,6 +1735,12 @@"node": ">= 4"}},+ "node_modules/immediate": {+ "version": "3.0.6",+ "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz",+ "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==",+ "license": "MIT"+ },"node_modules/import-fresh": {"version": "3.3.0","resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz",@@ -1766,8 +1779,7 @@"node_modules/inherits": {"version": "2.0.4","resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",- "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",- "dev": true+ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="},"node_modules/internal-slot": {"version": "1.0.6",@@ -2084,6 +2096,18 @@"json5": "lib/cli.js"}},+ "node_modules/jszip": {+ "version": "3.10.1",+ "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.1.tgz",+ "integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==",+ "license": "(MIT OR GPL-3.0-or-later)",+ "dependencies": {+ "lie": "~3.3.0",+ "pako": "~1.0.2",+ "readable-stream": "~2.3.6",+ "setimmediate": "^1.0.5"+ }+ },"node_modules/keyv": {"version": "4.5.4","resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",@@ -2106,6 +2130,15 @@"node": ">= 0.8.0"}},+ "node_modules/lie": {+ "version": "3.3.0",+ "resolved": "https://registry.npmjs.org/lie/-/lie-3.3.0.tgz",+ "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==",+ "license": "MIT",+ "dependencies": {+ "immediate": "~3.0.5"+ }+ },"node_modules/locate-path": {"version": "6.0.0","resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz",@@ -2349,6 +2382,12 @@"url": "https://github.com/sponsors/sindresorhus"}},+ "node_modules/pako": {+ "version": "1.0.11",+ "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",+ "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==",+ "license": "(MIT AND Zlib)"+ },"node_modules/parent-module": {"version": "1.0.1","resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",@@ -2455,6 +2494,12 @@"node": ">= 0.8.0"}},+ "node_modules/process-nextick-args": {+ "version": "2.0.1",+ "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",+ "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==",+ "license": "MIT"+ },"node_modules/punycode": {"version": "2.3.1","resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",@@ -2484,6 +2529,27 @@}]},+ "node_modules/readable-stream": {+ "version": "2.3.8",+ "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz",+ "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==",+ "license": "MIT",+ "dependencies": {+ "core-util-is": "~1.0.0",+ "inherits": "~2.0.3",+ "isarray": "~1.0.0",+ "process-nextick-args": "~2.0.0",+ "safe-buffer": "~5.1.1",+ "string_decoder": "~1.1.1",+ "util-deprecate": "~1.0.1"+ }+ },+ "node_modules/readable-stream/node_modules/isarray": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",+ "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",+ "license": "MIT"+ },"node_modules/regexp.prototype.flags": {"version": "1.5.1","resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.5.1.tgz",@@ -2617,6 +2683,12 @@"url": "https://github.com/sponsors/ljharb"}},+ "node_modules/safe-buffer": {+ "version": "5.1.2",+ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",+ "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",+ "license": "MIT"+ },"node_modules/safe-regex-test": {"version": "1.0.0","resolved": "https://registry.npmjs.org/safe-regex-test/-/safe-regex-test-1.0.0.tgz",@@ -2675,6 +2747,12 @@"node": ">= 0.4"}},+ "node_modules/setimmediate": {+ "version": "1.0.5",+ "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz",+ "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==",+ "license": "MIT"+ },"node_modules/shebang-command": {"version": "2.0.0","resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",@@ -2727,6 +2805,15 @@"node": ">=0.10.0"}},+ "node_modules/string_decoder": {+ "version": "1.1.1",+ "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",+ "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",+ "license": "MIT",+ "dependencies": {+ "safe-buffer": "~5.1.0"+ }+ },"node_modules/string.prototype.trim": {"version": "1.2.8","resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.2.8.tgz",@@ -3002,6 +3089,12 @@"punycode": "^2.1.0"}},+ "node_modules/util-deprecate": {+ "version": "1.0.2",+ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",+ "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",+ "license": "MIT"+ },"node_modules/vite": {"version": "4.5.0","resolved": "https://registry.npmjs.org/vite/-/vite-4.5.0.tgz",diff --git a/package.json b/package.jsonindex cc8c70f..ccca255 100644--- a/package.json+++ b/package.json@@ -19,6 +19,7 @@},"dependencies": {"file-saver": "^2.0.5",+ "jszip": "^3.10.1","three": "0.175.0","vite-plugin-glsl": "^1.1.2"}diff --git a/src/preview-generator/preview-generator.ts b/src/preview-generator/preview-generator.tsindex 8c7969e..9373cf6 100644--- a/src/preview-generator/preview-generator.ts+++ b/src/preview-generator/preview-generator.ts@@ -7,7 +7,9 @@ import { ThemeManager } from '../lib/ThemeManager.ts'import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import { WebMRecorder } from './webm-recorder.ts'-import { saveAs } from 'file-saver';++import { saveAs } from 'file-saver'+import JSZip from 'jszip'export class PreviewGeneratorBootstrap {public readonly camera = Generators.create_camera()@@ -16,10 +18,19 @@ export class PreviewGeneratorBootstrap {public readonly scene: THREE.Scene = new THREE.Scene()public readonly theme_manager = new ThemeManager()+ private readonly recorder: WebMRecorder = new WebMRecorder(this.renderer)+ private readonly zip = new JSZip()+private environment_container: THREE.Group = new THREE.Group()// loading model and animation stuff- private gltf_animation_loader: GLTFLoader = new GLTFLoader()++ private readonly gltf_animation_loader: GLTFLoader = new GLTFLoader()+ private animation_clips: THREE.AnimationClip[] = []+ private readonly mixers: THREE.AnimationMixer[] = []+ private readonly skinned_meshes: THREE.SkinnedMesh[] = []++ private current_animation_index_processing: number = 0constructor () {this.animate = this.animate.bind(this)@@ -35,34 +46,109 @@ export class PreviewGeneratorBootstrap {// setup event listener for record buttonconst record_button = document.getElementById('record-button')record_button?.addEventListener('click', () => {- this.start_recording()+ void this.start_recording()})this.animate()}private async start_recording (): Promise<void> {- const recorder: WebMRecorder = new WebMRecorder(this.renderer)+ if (this.skinned_meshes.length === 0 || this.animation_clips.length === 0) {+ alert('Animation or animations not loaded yet.')+ return+ }- // TODO: play through each animation and do a recording- // after all the animations are done zip up the results and download+ await this.process_animation_clip()+ }- await recorder.record_webm(1, 'preview.webm').then(file => {- console.log(file)- saveAs(file, 'preview.webm')+ private async process_animation_clip (): Promise<void> {+ // if we have processed all animations, we can stop+ const temp_limit = 4 // this.animation_clips.length+ if (this.current_animation_index_processing >= temp_limit) {+ console.log('All animations processed.')+ return+ }++ const clip = this.animation_clips[this.current_animation_index_processing]++ console.log('processing', clip.name)++ // Stop all previous actions+ this.mixers.forEach(mixer => {+ mixer.stopAllAction()})++ // go through each mixer and play clip+ this.mixers.forEach(mixer => {+ const action = mixer.clipAction(clip)+ action.reset()+ action.setLoop(THREE.LoopOnce, 1) // Play only once+ action.clampWhenFinished = true // Hold the last frame+ action.play()+ })++ // Wait for a short moment to ensure animation is visible+ // before we start recording+ // await new Promise(resolve => setTimeout(resolve, 200))++ // wait for animation to complete+ await new Promise(resolve => {+ this.mixers[0].addEventListener('finished', resolve)+ })++ // Record for the duration of the clip (or a max duration)+ // const duration = Math.min(clip.duration, 10) // max 10s per clip+ // const file = await recorder.record_webm(duration, `${clip.name}.webm`)+ // zip.file(`${clip.name}.webm`, file)++ // stop each mixer+ this.mixers.forEach(mixer => {+ mixer.stopAllAction()+ })++ // increment animation index+ this.current_animation_index_processing += 1+ await this.process_animation_clip()+ }++ // Generate zip and trigger download+ private async generate_zip (): Promise<void> {+ const blob = await this.zip.generateAsync({ type: 'blob' })+ saveAs(blob, 'animation-previews.zip')}private load_human_model_and_animations (): void {// Load the human model hereconst filepath: string = '../animations/human-base-animations.glb'this.gltf_animation_loader.load(filepath, (gltf: any) => {- // load the skinned mesh- this.scene.add(...gltf.scene.children)-- // grab a list of the animations following logic of main Mesh2Motion app with cleanup- const cloned_anims: THREE.AnimationClip[] = Utility.deep_clone_animation_clips(gltf.animations)- console.log(cloned_anims)+ // grabbed all the skinned meshes in the scene+ gltf.scene.traverse((child: any) => {+ if (child.isSkinnedMesh) {+ this.skinned_meshes.push(child)+ }+ })++ // load the skinned mesh into the scene to see+ // some models have multiple skinned meshes+ this.scene.add(...this.skinned_meshes)++ console.log(this.skinned_meshes)++ // grab a list of the animations from file+ // these are stored outside the mesh at the root level on a GLTF file+ this.animation_clips = gltf.animations++ if (this.skinned_meshes.length === 0) {+ console.warn('No SkinnedMesh found. Cannot proceed with animations.')+ return+ }++ // create a mixer for each skinned mesh+ // each mixer can only control one skinned mesh+ this.skinned_meshes.forEach(skin_mesh => {+ const mixer = new THREE.AnimationMixer(skin_mesh)+ this.mixers.push(mixer)+ })})}@@ -93,6 +179,12 @@ export class PreviewGeneratorBootstrap {private animate (): void {requestAnimationFrame(this.animate)++ // update each mixer+ this.mixers.forEach(mixer => {+ mixer.update(1 / 30)+ })+this.renderer.render(this.scene, this.camera)}