Browse Source

optimize Mixer (lazy updates to cache, use map instead of linear search) so that it can handle 9000 tracks.

Ben Houston 10 years ago
parent
commit
922a9dc9ad
2 changed files with 25 additions and 26 deletions
  1. 2 6
      examples/webgl_shadowmap_performance.html
  2. 23 20
      src/animation/AnimationMixer.js

+ 2 - 6
examples/webgl_shadowmap_performance.html

@@ -237,7 +237,7 @@
 
 				scene.add( mesh );
 
-				//mixer = new THREE.AnimationMixer( scene );
+				mixer = new THREE.AnimationMixer( scene );
 					
 				// MORPHS
 
@@ -254,10 +254,8 @@
 					var mesh = new THREE.Mesh( geometry, material );
 					mesh.speed = speed;
 
-					var mixer = new THREE.AnimationMixer( this.mesh );
 					mixer.addAction( new THREE.AnimationAction( geometry.clips[0], Math.random() ).warpToDuration( duration ).setLocalRoot( mesh ) );
 				
-					mesh.mixer = mixer;
 					mesh.position.set( x, y, z );
 					mesh.rotation.y = Math.PI/2;
 
@@ -368,14 +366,12 @@
 
 				var delta = clock.getDelta();
 
-				//if( mixer ) mixer.update( delta );
+				if( mixer ) mixer.update( delta );
 
 				for ( var i = 0; i < morphs.length; i ++ ) {
 
 					morph = morphs[ i ];
 
-			    	morph.mixer.update( delta );
-
 					morph.position.x += morph.speed * delta;
 
 					if ( morph.position.x  > 2000 )  {

+ 23 - 20
src/animation/AnimationMixer.js

@@ -14,6 +14,7 @@ THREE.AnimationMixer = function( root ) {
 	this.timeScale = 1.0;
 	this.actions = [];
 	this.propertyBindings = [];
+	this.propertyBindingNamesToIndex = {};
 
 };
 
@@ -45,6 +46,7 @@ THREE.AnimationMixer.prototype = {
 			
 				propertyBinding = new THREE.PropertyBinding( root, track.name );
 				this.propertyBindings.push( propertyBinding );
+				this.propertyBindingNamesToIndex[ root.uuid + '-' + track.name ] = this.propertyBindings.length - 1;
 			
 			}
 			else {
@@ -56,43 +58,42 @@ THREE.AnimationMixer.prototype = {
 
 		}
 
-		this.updatePropertyBindingIndices();
+		this.propertyBindingIndicesDirty = true;
 
 	},
 
 	getPropertyBindingIndex: function( rootNode, trackName ) {
 		
-		for( var k = 0; k < this.propertyBindings.length; k ++ ) {
-			if( this.propertyBindings[k].trackName === trackName &&
-				this.propertyBindings[k].rootNode === rootNode ) {
-				return k;
-			}
-		}	
-
-		return -1;
+		var index = this.propertyBindingNamesToIndex[ rootNode.uuid + '-' + trackName ];
+		return ( index !== undefined ) ? index : -1;
 
 	},
 
 	updatePropertyBindingIndices: function() {
 
-		for( var i = 0; i < this.actions.length; i++ ) {
+		if( this.propertyBindingIndicesDirty ) {
 
-			var action = this.actions[i];
+			for( var i = 0; i < this.actions.length; i++ ) {
 
-			var root = action.localRoot || this.root;
+				var action = this.actions[i];
 
-			var propertyBindingIndices = [];
+				var root = action.localRoot || this.root;
 
-			for( var j = 0; j < action.clip.tracks.length; j ++ ) {
+				var propertyBindingIndices = [];
 
-				var trackName = action.clip.tracks[j].name;
-				propertyBindingIndices.push( this.getPropertyBindingIndex( root, trackName ) );
-			
+				for( var j = 0; j < action.clip.tracks.length; j ++ ) {
+
+					var trackName = action.clip.tracks[j].name;
+					propertyBindingIndices.push( this.getPropertyBindingIndex( root, trackName ) );
+				
+				}
+
+				action.propertyBindingIndices = propertyBindingIndices;
 			}
 
-			action.propertyBindingIndices = propertyBindingIndices;
-		}
+			this.propertyBindingIndicesDirty = false;
 
+		}
 	},
 
 	removeAllActions: function() {
@@ -150,7 +151,7 @@ THREE.AnimationMixer.prototype = {
 			}
 		}
 
-		this.updatePropertyBindingIndices();
+		this.propertyBindingIndicesDirty = true;
 
 		return this;
 
@@ -238,6 +239,8 @@ THREE.AnimationMixer.prototype = {
 
 	update: function( deltaTime ) {
 
+		this.updatePropertyBindingIndices();
+
 		var mixerDeltaTime = deltaTime * this.timeScale;
 		this.time += mixerDeltaTime;