Преглед изворни кода

Editor: Improved SkinnedMesh animations support.

Mr.doob пре 11 година
родитељ
комит
fe1fc9ba42
3 измењених фајлова са 59 додато и 57 уклоњено
  1. 3 1
      editor/js/Editor.js
  2. 34 45
      editor/js/Sidebar.Animation.js
  3. 22 11
      editor/js/Viewport.js

+ 3 - 1
editor/js/Editor.js

@@ -6,7 +6,8 @@ var Editor = function () {
 
 		// actions
 
-		playAnimations: new SIGNALS.Signal(),
+		playAnimation: new SIGNALS.Signal(),
+		stopAnimation: new SIGNALS.Signal(),
 
 		// notifications
 
@@ -321,6 +322,7 @@ Editor.prototype = {
 			'HemisphereLight': THREE.HemisphereLight,
 			'PointLight': THREE.PointLight,
 			'SpotLight': THREE.SpotLight,
+			'SkinnedMesh': THREE.SkinnedMesh,
 			'Mesh': THREE.Mesh,
 			'Sprite': THREE.Sprite,
 			'Object3D': THREE.Object3D

+ 34 - 45
editor/js/Sidebar.Animation.js

@@ -11,84 +11,73 @@ Sidebar.Animation = function ( editor ) {
 	container.addStatic( new UI.Text( 'ANIMATION' ) );
 	container.add( new UI.Break() );
 
-	var AnimationsRow = new UI.Panel();
-	var Animations = new UI.Select().setOptions( options ).setWidth( '130px' ).setColor( '#444' ).setFontSize( '12px' );
-	AnimationsRow.add( new UI.Text( 'animations' ).setWidth( '90px' ) );
-	AnimationsRow.add( Animations );
+	var animationsRow = new UI.Panel();
+	container.add( animationsRow );
 
-	container.add( AnimationsRow );
-	container.add( new UI.Break() );
+	var animations = {};
 
-	var PlayRow = new UI.Panel();
-	var playButton = new UI.Button().setLabel( 'Play' ).onClick( play );
-	PlayRow.add( playButton );
+	signals.objectAdded.add( function ( object ) {
 
-	container.add( PlayRow );
-	container.add( new UI.Break() );
+		object.traverse( function ( child ) {
 
-	function play() {
+			if ( child instanceof THREE.SkinnedMesh ) {
 
-		var value = Animations.getValue();
+				var material = child.material;
 
-		if ( possibleAnimations[ value ] ) {
+				if ( material instanceof THREE.MeshFaceMaterial ) {
 
-			var anims = possibleAnimations[value]
+					for ( var i = 0; i < material.materials.length; i ++ ) {
 
-			for ( var i = 0; i < anims.length; i ++ ) {
+						material.materials[ i ].skinning = true;
 
-				anims[ i ].play();
+					}
 
-			}
+				} else {
 
-			signals.playAnimations.dispatch( anims );
+					child.material.skinning = true;
 
-		};
+				}
 
-	}
+				console.log( child.geometry );
 
-	signals.objectAdded.add( function ( object ) {
+				animations[ child.id ] = new THREE.Animation( child );
 
-		if ( object instanceof THREE.SkinnedMesh ) {
-
-			var geometry = object.geometry;
-			var material = object.material;
+			}
 
-			material.skinning = true;
+		} );
 
-			var name = geometry.animation.name;
+	} );
 
-			options[ name ] = name
+	signals.objectSelected.add( function ( object ) {
 
-			Animations.setOptions( options );
+		container.setDisplay( 'none' );
 
-			THREE.AnimationHandler.add( geometry.animation );
+		if ( object instanceof THREE.SkinnedMesh ) {
 
-			var animation = new THREE.Animation( object, name );
+			animationsRow.clear();
 
-			if ( possibleAnimations[ name ] ){
+			var animation = animations[ object.id ];
 
-				possibleAnimations[ name ].push( animation );
+			var playButton = new UI.Button().setLabel( 'Play' ).onClick( function () {
 
-			} else {
+				animation.play();
 
-				possibleAnimations[ name ] = [ animation ];
+				signals.playAnimation.dispatch( animation );
 
-			}
+			} );
+			animationsRow.add( playButton );
 
-		}
+			var pauseButton = new UI.Button().setLabel( 'Stop' ).onClick( function () {
 
-	} );
+				animation.stop();
 
-	signals.objectSelected.add( function ( object ) {
+				signals.stopAnimation.dispatch( animation );
 
-		if ( object && object.geometry && object.geometry.animation ) {
+			} );
+			animationsRow.add( pauseButton );
 
 			container.setDisplay( 'block' );
 
-		} else {
-
-			container.setDisplay( 'none' );
-
 		}
 
 	} );

+ 22 - 11
editor/js/Viewport.js

@@ -429,22 +429,23 @@ var Viewport = function ( editor ) {
 
 	} );
 
-	signals.playAnimations.add( function (animations) {
-		
-		function animate() {
+	var animations = [];
 
-			requestAnimationFrame( animate );
-			
-			for ( var i = 0; i < animations.length ; i ++ ) {
+	signals.playAnimation.add( function ( animation ) {
 
-				animations[i].update(0.016);
+		animations.push( animation );
 
-			} 
+	} );
 
-			render();
-		}
+	signals.stopAnimation.add( function ( animation ) {
 
-		animate();
+		var index = animations.indexOf( animation );
+
+		if ( index !== -1 ) {
+
+			animations.splice( index, 1 );
+
+		}
 
 	} );
 
@@ -563,6 +564,16 @@ var Viewport = function ( editor ) {
 
 		requestAnimationFrame( animate );
 
+		// animations
+
+		if ( THREE.AnimationHandler.animations.length > 0 ) {
+
+			THREE.AnimationHandler.update( 0.016 );
+
+			render();
+
+		}
+
 	}
 
 	function render() {