@@ -241,6 +241,19 @@ document.addEventListener('DOMContentLoaded', async function() {
241
241
window . currentAnimationGroup . start ( true ) ;
242
242
}
243
243
244
+ // Update animation list and play first animation
245
+ if ( container . animationGroups . length > 0 ) {
246
+ // Store animation groups both locally and globally
247
+ animationGroups = container . animationGroups ;
248
+ window . animationGroups = animationGroups ;
249
+
250
+ // Update the animation list UI
251
+ populateAnimationList ( ) ;
252
+
253
+ // Play the first animation
254
+ playAnimation ( 0 ) ;
255
+ }
256
+
244
257
return window . currentCharacter ;
245
258
}
246
259
@@ -425,8 +438,9 @@ document.addEventListener('DOMContentLoaded', async function() {
425
438
// Rotate 90 degrees so character faces the camera from the side
426
439
character . rotation = new BABYLON . Vector3 ( 0 , Math . PI / 2 , 0 ) ;
427
440
428
- // Store animation groups
441
+ // Store animation groups both locally and globally
429
442
animationGroups = result . animationGroups ;
443
+ window . animationGroups = animationGroups ;
430
444
431
445
// Handle no animations case
432
446
if ( animationGroups . length === 0 ) {
@@ -446,6 +460,11 @@ document.addEventListener('DOMContentLoaded', async function() {
446
460
// Populate animation dropdown
447
461
populateAnimationList ( ) ;
448
462
463
+ // Make sure to play the first animation
464
+ if ( animationGroups . length > 0 ) {
465
+ playAnimation ( 0 ) ;
466
+ }
467
+
449
468
console . log ( `Loaded character with ${ animationGroups . length } animations` ) ;
450
469
}
451
470
@@ -485,55 +504,75 @@ document.addEventListener('DOMContentLoaded', async function() {
485
504
animationList . appendChild ( option ) ;
486
505
} ) ;
487
506
488
- // Select the first animation
507
+ // Select and play the first animation
489
508
if ( animationGroups . length > 0 ) {
509
+ animationList . value = "0" ;
490
510
playAnimation ( 0 ) ;
491
511
}
492
512
}
493
513
494
514
// Play selected animation
495
515
function playAnimation ( index ) {
496
- // Stop current animation if playing
497
- if ( currentAnimationGroup ) {
498
- currentAnimationGroup . stop ( ) ;
516
+ // Stop all animations first
517
+ if ( scene ) {
518
+ scene . animationGroups . forEach ( group => group . stop ( ) ) ;
499
519
}
500
520
501
521
// Play selected animation
502
522
if ( index >= 0 && index < animationGroups . length ) {
503
523
currentAnimationGroup = animationGroups [ index ] ;
504
- currentAnimationGroup . start ( true ) ; // true for looping
505
- showStatus ( `Playing animation: ${ currentAnimationGroup . name } ` ) ;
524
+ window . currentAnimationGroup = currentAnimationGroup ; // Ensure global access
525
+ if ( currentAnimationGroup ) {
526
+ currentAnimationGroup . start ( true ) ; // true for looping
527
+ showStatus ( `Playing animation: ${ currentAnimationGroup . name } ` ) ;
528
+ // Update dropdown selection to match
529
+ animationList . value = index . toString ( ) ;
530
+ }
506
531
}
507
532
}
508
533
509
534
// Recording sprite sheet functions
510
535
function startRecording ( ) {
536
+ // Get the currently selected animation
537
+ const selectedIndex = parseInt ( animationList . value ) ;
538
+
539
+ // Ensure we have a valid animation
540
+ if ( ! animationGroups || animationGroups . length === 0 ) {
541
+ showStatus ( "No animations available to record" , true ) ;
542
+ return ;
543
+ }
544
+
545
+ // Force play the selected animation if it's not already playing
546
+ if ( ! currentAnimationGroup || currentAnimationGroup !== animationGroups [ selectedIndex ] ) {
547
+ playAnimation ( selectedIndex ) ;
548
+ }
549
+
550
+ // Double check we have a valid animation now
511
551
if ( ! currentAnimationGroup ) {
512
- showStatus ( "Please play an animation first before recording" , true ) ;
552
+ showStatus ( "Failed to start animation for recording" , true ) ;
513
553
return ;
514
554
}
515
555
516
- // Get parameters from inputs
517
- totalFramesToCapture = parseInt ( framesInput . value ) || 16 ;
518
- if ( totalFramesToCapture < 1 ) totalFramesToCapture = 16 ;
556
+ // Get parameters from inputs with fallback values
557
+ totalFramesToCapture = Math . max ( 1 , parseInt ( framesInput . value ) || 16 ) ;
519
558
520
559
// Reset recording state
521
560
recordedFrames = [ ] ;
522
561
frameCount = 0 ;
523
562
isRecording = true ;
524
563
525
- // Restart the animation for a clean recording and ensure we're at the beginning
564
+ // Restart the animation from the beginning
526
565
currentAnimationGroup . stop ( ) ;
527
- currentAnimationGroup . reset ( ) ; // Reset to start position
528
- currentAnimationGroup . start ( true ) ; // Loop animation
566
+ currentAnimationGroup . reset ( ) ;
567
+ currentAnimationGroup . start ( true ) ;
529
568
530
569
showStatus ( `Recording sprite sheet: frame 0/${ totalFramesToCapture } ` ) ;
531
570
532
571
// Disable record button during recording
533
572
recordBtn . disabled = true ;
534
573
recordBtn . textContent = "Recording..." ;
535
574
536
- // Start the recording loop with a slight delay to ensure animation is ready
575
+ // Start the recording loop
537
576
setTimeout ( ( ) => {
538
577
recordingLoop ( ) ;
539
578
} , 100 ) ;
0 commit comments