Sidebar.Animation.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Sidebar.Animation = function ( editor ) {
  5. var signals = editor.signals;
  6. var renderer = null;
  7. signals.rendererChanged.add( function ( newRenderer ) {
  8. renderer = newRenderer;
  9. } );
  10. signals.objectSelected.add( function ( object ) {
  11. var uuid = object !== null ? object.uuid : '';
  12. var animations = editor.animations[ uuid ];
  13. if ( animations !== undefined ) {
  14. container.setDisplay( '' );
  15. mixer = new THREE.AnimationMixer( object );
  16. var options = {};
  17. for ( var animation of animations ) {
  18. options[ animation.name ] = animation.name;
  19. var action = mixer.clipAction( animation );
  20. actions[ animation.name ] = action;
  21. }
  22. animationsSelect.setOptions( options );
  23. } else {
  24. container.setDisplay( 'none' );
  25. }
  26. } );
  27. var mixer, currentAnimation, actions = {};
  28. var clock = new THREE.Clock();
  29. function updateAnimation() {
  30. if ( mixer !== undefined && renderer !== null ) {
  31. var dt = clock.getDelta();
  32. mixer.update( dt );
  33. if ( currentAnimation !== undefined && currentAnimation.isRunning() ) {
  34. requestAnimationFrame( updateAnimation );
  35. renderer.render( editor.scene, editor.camera );
  36. } else {
  37. currentAnimation = undefined;
  38. }
  39. }
  40. }
  41. function stopAnimations() {
  42. if ( mixer !== undefined ) {
  43. mixer.stopAllAction();
  44. }
  45. }
  46. var container = new UI.Panel();
  47. container.setDisplay( 'none' );
  48. container.add( new UI.Text( 'Animation' ).setTextTransform( 'uppercase' ) );
  49. var div = new UI.Div().setMarginLeft( '90px' );
  50. container.add( div );
  51. div.add( new UI.Button( "Stop" ).setFontSize( '12px' ).onClick( stopAnimations ), new UI.Break() );
  52. var animationsSelect = new UI.Select().setFontSize( '12px' ).setMarginTop( '10px' ).onChange( function () {
  53. currentAnimation = actions[ animationsSelect.getValue() ];
  54. if ( currentAnimation !== undefined ) {
  55. stopAnimations();
  56. currentAnimation.play();
  57. updateAnimation();
  58. }
  59. } );
  60. div.add( animationsSelect );
  61. return container;
  62. };