Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

don't make webm recorder async so we can better record actions

Scott Petrovicf0f912a0e0eb
diff --git a/src/preview-generator/preview-generator.ts b/src/preview-generator/preview-generator.ts
index 6d55f65..b8a655a 100644
--- a/src/preview-generator/preview-generator.ts
+++ b/src/preview-generator/preview-generator.ts
@@ -132,31 +132,22 @@ class PreviewGenerator {
action.reset()
// wait for the animation to finish
- // this includes cleanup from the last event listener finish
await new Promise<void>(resolve => {
- // Define handler in the same scope as resolve
- const handler = (e: any) => {
+ const handler = async (e: any) => {
this.mixer_.removeEventListener('finished', handler)
+ // Stop recording after animation ends
+ const file = await this.recorder.stop()
+ this.zip.file(`${clip.name}.webm`, file)
resolve()
}
-
- // Remove any previous instance of this handler (safe even if not present)
this.mixer_.removeEventListener('finished', handler)
-
- // the "real" event listener after the previous cleanup work happened
this.mixer_.addEventListener('finished', handler)
+
+ // Start recording before playing the animation
+ this.recorder.start(`${clip.name}.webm`)
action.play()
})
- // Wait for a short moment to ensure animation is visible
- // before we start recording
- // await new Promise(resolve => setTimeout(resolve, 200))
-
- // Record for the duration of the clip (or a max duration)
- const duration = Math.min(clip.duration, 5) // max 5s per clip
- const file = await this.recorder.record_webm(duration, `${clip.name}.webm`)
- this.zip.file(`${clip.name}.webm`, file)
-
// stop all animations in mixer
this.mixer_.stopAllAction()
diff --git a/src/preview-generator/webm-recorder.ts b/src/preview-generator/webm-recorder.ts
index b894d33..54a0a13 100644
--- a/src/preview-generator/webm-recorder.ts
+++ b/src/preview-generator/webm-recorder.ts
@@ -2,50 +2,63 @@ import * as THREE from 'three'
export class WebMRecorder {
public width: number = 160
- public height: number = 120
+ public height: number = 140
public fps: number = 24
- public kbps: number = 800
+ public kbps: number = 1000
public readonly recorded_files: File[] = []
+ private readonly renderer_ref: THREE.WebGLRenderer
- renderer_ref: THREE.WebGLRenderer
+ private old_size: THREE.Vector2 | null = null
+ private old_pr: number | null = null
+ private recorder: MediaRecorder | null = null
+ private chunks: Blob[] = []
+ private file_name: string = ''
+ private stopped_promise: Promise<void> | null = null
+ private stopped_resolve: (() => void) | null = null
constructor (renderer: THREE.WebGLRenderer) {
this.renderer_ref = renderer
}
- public async record_webm (duration_in_sec: number, fileName: string): Promise<File> {
- // store original size of renderer temporarily to capture a certain resolution
- const old_size = this.renderer_ref.getSize(new THREE.Vector2())
- const old_pr = this.renderer_ref.getPixelRatio()
-
+ public start(fileName: string): void {
+ this.file_name = fileName
+ this.old_size = this.renderer_ref.getSize(new THREE.Vector2())
+ this.old_pr = this.renderer_ref.getPixelRatio()
this.renderer_ref.setPixelRatio(1)
this.renderer_ref.setSize(this.width, this.height, false)
const stream: MediaStream = this.renderer_ref.domElement.captureStream(this.fps)
- const recorder = new MediaRecorder(stream, {
+ this.recorder = new MediaRecorder(stream, {
mimeType: this.supported_webm_mime_type(),
videoBitsPerSecond: this.kbps * 1000
})
+ this.chunks = []
+ this.recorder.ondataavailable = e => (e.data.size !== 0) && this.chunks.push(e.data)
+ this.stopped_promise = new Promise<void>(resolve => {
+ this.stopped_resolve = resolve
+ this.recorder!.onstop = () => resolve()
+ })
+ this.recorder.start()
+ }
- const chunks: Blob[] = []
- recorder.ondataavailable = e => (e.data.size !== 0) && chunks.push(e.data)
-
- const stopped = new Promise(r => recorder.onstop = r)
-
- recorder.start()
-
- await new Promise(res => setTimeout(res, duration_in_sec * 1000))
-
- recorder.stop()
-
- await stopped
-
+ public async stop(): Promise<File> {
+ if (!this.recorder) throw new Error('Recorder not started')
+ this.recorder.stop()
+ if (this.stopped_promise) {
+ await this.stopped_promise
+ }
// restore the renderer size and pixel ratio
- this.renderer_ref.setSize(old_size.x, old_size.y, false)
- this.renderer_ref.setPixelRatio(old_pr)
-
- return new File(chunks, fileName, { type: this.supported_webm_mime_type() })
+ if (this.old_size && this.old_pr !== null) {
+ this.renderer_ref.setSize(this.old_size.x, this.old_size.y, false)
+ this.renderer_ref.setPixelRatio(this.old_pr)
+ }
+ const file = new File(this.chunks, this.file_name, { type: this.supported_webm_mime_type() })
+ this.recorder = null
+ this.stopped_promise = null
+ this.stopped_resolve = null
+ this.chunks = []
+ return file
}
private supported_webm_mime_type (): string {