Browse Source

Editor: Support animation export with GLTFExporter.

Mugen87 4 years ago
parent
commit
f966357679
3 changed files with 33 additions and 11 deletions
  1. 1 1
      editor/js/Editor.js
  2. 7 7
      editor/js/Loader.js
  3. 25 3
      editor/js/Menubar.File.js

+ 1 - 1
editor/js/Editor.js

@@ -357,7 +357,7 @@ Editor.prototype = {
 
 	},
 
-	addAnimation: function ( object, animations ) {
+	addAnimations: function ( object, animations ) {
 
 		if ( animations.length > 0 ) {
 

+ 7 - 7
editor/js/Loader.js

@@ -169,7 +169,7 @@ function Loader( editor ) {
 
 					collada.scene.name = filename;
 
-					editor.addAnimation( collada.scene, collada.animations );
+					editor.addAnimations( collada.scene, collada.animations );
 					editor.execute( new AddObjectCommand( editor, collada.scene ) );
 
 				}, false );
@@ -210,7 +210,7 @@ function Loader( editor ) {
 					var loader = new FBXLoader( manager );
 					var object = loader.parse( contents );
 
-					editor.addAnimation( object, object.animations );
+					editor.addAnimations( object, object.animations );
 					editor.execute( new AddObjectCommand( editor, object ) );
 
 				}, false );
@@ -234,7 +234,7 @@ function Loader( editor ) {
 						var scene = result.scene;
 						scene.name = filename;
 
-						editor.addAnimation( scene, result.animations );
+						editor.addAnimations( scene, result.animations );
 						editor.execute( new AddObjectCommand( editor, scene ) );
 
 					} );
@@ -271,7 +271,7 @@ function Loader( editor ) {
 						var scene = result.scene;
 						scene.name = filename;
 
-						editor.addAnimation( scene, result.animations );
+						editor.addAnimations( scene, result.animations );
 						editor.execute( new AddObjectCommand( editor, scene ) );
 
 					} );
@@ -370,7 +370,7 @@ function Loader( editor ) {
 					mesh.mixer = new THREE.AnimationMixer( mesh );
 					mesh.name = filename;
 
-					editor.addAnimation( mesh, geometry.animations );
+					editor.addAnimations( mesh, geometry.animations );
 					editor.execute( new AddObjectCommand( editor, mesh ) );
 
 				}, false );
@@ -682,7 +682,7 @@ function Loader( editor ) {
 
 						var scene = result.scene;
 
-						editor.addAnimation( scene, result.animations );
+						editor.addAnimations( scene, result.animations );
 						editor.execute( new AddObjectCommand( editor, scene ) );
 
 					} );
@@ -700,7 +700,7 @@ function Loader( editor ) {
 
 						var scene = result.scene;
 
-						editor.addAnimation( scene, result.animations );
+						editor.addAnimations( scene, result.animations );
 						editor.execute( new AddObjectCommand( editor, scene ) );
 
 					} );

+ 25 - 3
editor/js/Menubar.File.js

@@ -245,13 +245,16 @@ function MenubarFile( editor ) {
 	option.setTextContent( strings.getKey( 'menubar/file/export/glb' ) );
 	option.onClick( function () {
 
+		var scene = editor.scene;
+		var animations = getAnimations( scene );
+
 		var exporter = new GLTFExporter();
 
-		exporter.parse( editor.scene, function ( result ) {
+		exporter.parse( scene, function ( result ) {
 
 			saveArrayBuffer( result, 'scene.glb' );
 
-		}, { binary: true } );
+		}, { binary: true, animations: animations } );
 
 	} );
 	options.add( option );
@@ -263,13 +266,16 @@ function MenubarFile( editor ) {
 	option.setTextContent( strings.getKey( 'menubar/file/export/gltf' ) );
 	option.onClick( function () {
 
+		var scene = editor.scene;
+		var animations = getAnimations( scene );
+
 		var exporter = new GLTFExporter();
 
 		exporter.parse( editor.scene, function ( result ) {
 
 			saveString( JSON.stringify( result, null, 2 ), 'scene.gltf' );
 
-		} );
+		}, { animations: animations } );
 
 
 	} );
@@ -471,6 +477,22 @@ function MenubarFile( editor ) {
 
 	}
 
+	function getAnimations( scene ) {
+
+		var animations = [];
+
+		scene.traverse( function ( object ) {
+
+			var objectAnimations = editor.animations[ object.uuid ];
+
+			if ( objectAnimations !== undefined ) animations.push( ... objectAnimations );
+
+		} );
+
+		return animations;
+
+	}
+
 	return container;
 
 }