Sidebar.Animation.js 2.3 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4
/**
 * @author mrdoob / http://mrdoob.com/
 */

5 6 7
Sidebar.Animation = function ( editor ) {

	var signals = editor.signals;
8

T
Temdog007 已提交
9 10 11
	var renderer = null;

	signals.rendererChanged.add( function ( newRenderer ) {
T
Temdog007 已提交
12

T
Temdog007 已提交
13 14
		renderer = newRenderer;

T
Temdog007 已提交
15 16 17 18
	} );

	signals.objectSelected.add( function ( object ) {

T
Temdog007 已提交
19
		var uuid = object !== null ? object.uuid : '';
T
Temdog007 已提交
20 21 22 23
		var animations = editor.animations[ uuid ];
		if ( animations !== undefined ) {

			container.setDisplay( '' );
T
Temdog007 已提交
24

T
Temdog007 已提交
25
			mixer = new THREE.AnimationMixer( object );
T
Temdog007 已提交
26
			var options = {};
T
Temdog007 已提交
27 28 29 30 31 32
			for ( var animation of animations ) {

				options[ animation.name ] = animation.name;

				var action = mixer.clipAction( animation );
				actions[ animation.name ] = action;
T
Temdog007 已提交
33 34

			}
T
Temdog007 已提交
35 36 37 38 39 40
			animationsSelect.setOptions( options );

		} else {

			container.setDisplay( 'none' );

T
Temdog007 已提交
41
		}
T
Temdog007 已提交
42 43

	} );
T
Temdog007 已提交
44 45 46 47

	var mixer, currentAnimation, actions = {};

	var clock = new THREE.Clock();
T
Temdog007 已提交
48 49 50 51
	function updateAnimation() {

		if ( mixer !== undefined && renderer !== null ) {

T
Temdog007 已提交
52
			var dt = clock.getDelta();
T
Temdog007 已提交
53
			mixer.update( dt * speed.getValue() );
T
Temdog007 已提交
54 55 56 57 58 59 60
			if ( currentAnimation !== undefined && currentAnimation.isRunning() ) {

				requestAnimationFrame( updateAnimation );
				renderer.render( editor.scene, editor.camera );

			} else {

T
Temdog007 已提交
61
				currentAnimation = undefined;
T
Temdog007 已提交
62

T
Temdog007 已提交
63
			}
T
Temdog007 已提交
64

T
Temdog007 已提交
65
		}
T
Temdog007 已提交
66

T
Temdog007 已提交
67 68
	}

T
Temdog007 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
	function playAnimation() {

		currentAnimation = actions[ animationsSelect.getValue() ];
		if ( currentAnimation !== undefined ) {

			stopAnimations();
			currentAnimation.play();
			updateAnimation();

		}

	}

T
Temdog007 已提交
82 83 84 85
	function stopAnimations() {

		if ( mixer !== undefined ) {

T
Temdog007 已提交
86
			mixer.stopAllAction();
T
Temdog007 已提交
87

T
Temdog007 已提交
88
		}
T
Temdog007 已提交
89

T
Temdog007 已提交
90
	}
91

M
Mr.doob 已提交
92
	var container = new UI.Panel();
93
	container.setDisplay( 'none' );
94

M
Mr.doob 已提交
95
	container.add( new UI.Text( 'Animation' ).setTextTransform( 'uppercase' ) );
96

T
Temdog007 已提交
97 98 99
	var div = new UI.Div().setMarginLeft( '90px' );
	container.add( div );

T
Temdog007 已提交
100
	div.add( new UI.Button( "Play" ).setFontSize( '12px' ).onClick( playAnimation ).setMarginRight( '10px' ) );
T
Temdog007 已提交
101

T
Temdog007 已提交
102
	div.add( new UI.Button( "Stop" ).setFontSize( '12px' ).onClick( stopAnimations ), new UI.Break() );
T
Temdog007 已提交
103

T
Temdog007 已提交
104 105 106 107 108 109 110 111
	var animationsSelect = new UI.Select().setFontSize( '12px' ).setMarginTop( '10px' ).setMarginBottom( '10px' );
	div.add( animationsSelect, new UI.Break() );

	var row = new UI.Row();
	div.add( row );

	var speed = new UI.Number( 1 ).setRange( 0.25, 2 ).setStep( 0.5 ).setMarginLeft( '10px' );
	row.add( new UI.Text( "Speed" ), speed );
M
Mr.doob 已提交
112

113 114
	return container;

T
Tristan VALCKE 已提交
115
};