Sidebar.Animation.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 * speed.getValue() );
  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 playAnimation() {
  42. currentAnimation = actions[ animationsSelect.getValue() ];
  43. if ( currentAnimation !== undefined ) {
  44. stopAnimations();
  45. currentAnimation.play();
  46. updateAnimation();
  47. }
  48. }
  49. function stopAnimations() {
  50. if ( mixer !== undefined ) {
  51. mixer.stopAllAction();
  52. }
  53. }
  54. var container = new UI.Panel();
  55. container.setDisplay( 'none' );
  56. container.add( new UI.Text( 'Animation' ).setTextTransform( 'uppercase' ) );
  57. var div = new UI.Div().setMarginLeft( '90px' );
  58. container.add( div );
  59. div.add( new UI.Button( "Play" ).setFontSize( '12px' ).onClick( playAnimation ).setMarginRight( '10px' ) );
  60. div.add( new UI.Button( "Stop" ).setFontSize( '12px' ).onClick( stopAnimations ), new UI.Break() );
  61. var animationsSelect = new UI.Select().setFontSize( '12px' ).setMarginTop( '10px' ).setMarginBottom( '10px' );
  62. div.add( animationsSelect, new UI.Break() );
  63. var row = new UI.Row();
  64. div.add( row );
  65. var speed = new UI.Number( 1 ).setRange( 0.25, 2 ).setStep( 0.5 ).setMarginLeft( '10px' );
  66. row.add( new UI.Text( "Speed" ), speed );
  67. return container;
  68. };