123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- /**
- * @author mikael emtinger / http://gomo.se/
- */
- THREE.AnimationHandler = {
- LINEAR: 0,
- CATMULLROM: 1,
- CATMULLROM_FORWARD: 2,
- //
- add: function () {
- console.warn( 'THREE.AnimationHandler.add() has been deprecated.' );
- },
- get: function () {
- console.warn( 'THREE.AnimationHandler.get() has been deprecated.' );
- },
- remove: function () {
- console.warn( 'THREE.AnimationHandler.remove() has been deprecated.' );
- },
- //
- animations: [],
- init: function ( data ) {
- if ( data.initialized === true ) return data;
- // loop through all keys
- for ( var h = 0; h < data.hierarchy.length; h ++ ) {
- for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
- // remove minus times
- if ( data.hierarchy[ h ].keys[ k ].time < 0 ) {
- data.hierarchy[ h ].keys[ k ].time = 0;
- }
- // create quaternions
- if ( data.hierarchy[ h ].keys[ k ].rot !== undefined &&
- ! ( data.hierarchy[ h ].keys[ k ].rot instanceof THREE.Quaternion ) ) {
- var quat = data.hierarchy[ h ].keys[ k ].rot;
- data.hierarchy[ h ].keys[ k ].rot = new THREE.Quaternion().fromArray( quat );
- }
- }
- // prepare morph target keys
- if ( data.hierarchy[ h ].keys.length && data.hierarchy[ h ].keys[ 0 ].morphTargets !== undefined ) {
- // get all used
- var usedMorphTargets = {};
- for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
- for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) {
- var morphTargetName = data.hierarchy[ h ].keys[ k ].morphTargets[ m ];
- usedMorphTargets[ morphTargetName ] = - 1;
- }
- }
- data.hierarchy[ h ].usedMorphTargets = usedMorphTargets;
- // set all used on all frames
- for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
- var influences = {};
- for ( var morphTargetName in usedMorphTargets ) {
- for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) {
- if ( data.hierarchy[ h ].keys[ k ].morphTargets[ m ] === morphTargetName ) {
- influences[ morphTargetName ] = data.hierarchy[ h ].keys[ k ].morphTargetsInfluences[ m ];
- break;
- }
- }
- if ( m === data.hierarchy[ h ].keys[ k ].morphTargets.length ) {
- influences[ morphTargetName ] = 0;
- }
- }
- data.hierarchy[ h ].keys[ k ].morphTargetsInfluences = influences;
- }
- }
- // remove all keys that are on the same time
- for ( var k = 1; k < data.hierarchy[ h ].keys.length; k ++ ) {
- if ( data.hierarchy[ h ].keys[ k ].time === data.hierarchy[ h ].keys[ k - 1 ].time ) {
- data.hierarchy[ h ].keys.splice( k, 1 );
- k --;
- }
- }
- // set index
- for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
- data.hierarchy[ h ].keys[ k ].index = k;
- }
- }
- data.initialized = true;
- return data;
- },
- parse: function ( root ) {
- var parseRecurseHierarchy = function ( root, hierarchy ) {
- hierarchy.push( root );
- for ( var c = 0; c < root.children.length; c ++ )
- parseRecurseHierarchy( root.children[ c ], hierarchy );
- };
- // setup hierarchy
- var hierarchy = [];
- if ( root instanceof THREE.SkinnedMesh ) {
- for ( var b = 0; b < root.skeleton.bones.length; b ++ ) {
- hierarchy.push( root.skeleton.bones[ b ] );
- }
- } else {
- parseRecurseHierarchy( root, hierarchy );
- }
- return hierarchy;
- },
- play: function ( animation ) {
- if ( this.animations.indexOf( animation ) === - 1 ) {
- this.animations.push( animation );
- }
- },
- stop: function ( animation ) {
- var index = this.animations.indexOf( animation );
- if ( index !== - 1 ) {
- this.animations.splice( index, 1 );
- }
- },
- update: function ( deltaTimeMS ) {
- for ( var i = 0; i < this.animations.length; i ++ ) {
- this.animations[ i ].resetBlendWeights();
- }
- for ( var i = 0; i < this.animations.length; i ++ ) {
- this.animations[ i ].update( deltaTimeMS );
- }
- }
- };
|