From f96635767901ef9fe6a02d3e4851e9ff5236bb7b Mon Sep 17 00:00:00 2001 From: Mugen87 Date: Sun, 4 Oct 2020 14:46:53 +0200 Subject: [PATCH] Editor: Support animation export with GLTFExporter. --- editor/js/Editor.js | 2 +- editor/js/Loader.js | 14 +++++++------- editor/js/Menubar.File.js | 28 +++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/editor/js/Editor.js b/editor/js/Editor.js index 0376cd7854f7d0..165e224ed76a35 100644 --- a/editor/js/Editor.js +++ b/editor/js/Editor.js @@ -357,7 +357,7 @@ Editor.prototype = { }, - addAnimation: function ( object, animations ) { + addAnimations: function ( object, animations ) { if ( animations.length > 0 ) { diff --git a/editor/js/Loader.js b/editor/js/Loader.js index f7d33d8a9f0d98..47aaec10b46429 100644 --- a/editor/js/Loader.js +++ b/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 ) ); } ); diff --git a/editor/js/Menubar.File.js b/editor/js/Menubar.File.js index 17e525494a8f2d..99142af85c73ed 100644 --- a/editor/js/Menubar.File.js +++ b/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; }