Sfoglia il codice sorgente

add optimization for consecutive keys that are identical -- no linear interoplation required.

Ben Houston 10 anni fa
parent
commit
eb64f9e824
1 ha cambiato i file con 16 aggiunte e 5 eliminazioni
  1. 16 5
      src/animation/KeyframeTrack.js

+ 16 - 5
src/animation/KeyframeTrack.js

@@ -48,7 +48,7 @@ THREE.KeyframeTrack.prototype = {
 		//if( /morph/i.test( this.name ) ) {
 		//	console.log( "lastIndex: ", this.lastIndex );
 		//}
-		
+
 		if( this.lastIndex >= this.keys.length ) {
 
 			this.setResult( this.keys[ this.keys.length - 1 ].value );
@@ -63,11 +63,19 @@ THREE.KeyframeTrack.prototype = {
 
 		}
 
-		// linear interpolation to start with
-		var alpha = ( time - this.keys[ this.lastIndex - 1 ].time ) / ( this.keys[ this.lastIndex ].time - this.keys[ this.lastIndex - 1 ].time );
+		var prevKey = this.keys[ this.lastIndex - 1 ];
+		this.setResult( prevKey.value );
+
+		if( prevKey.constantToNext ) {
+
+			return this.result;
+
+		}
 
-		this.setResult( this.keys[ this.lastIndex - 1 ].value );
-		this.result = this.lerp( this.result, this.keys[ this.lastIndex ].value, alpha );
+		// linear interpolation to start with
+		var currentKey = this.keys[ this.lastIndex ];
+		var alpha = ( time - prevKey.time ) / ( currentKey.time - prevKey.time );
+		this.result = this.lerp( this.result, currentKey.value, alpha );
 		//console.log( 'lerp result', this.result )
 		/*if( /morph/i.test( this.name ) ) {
 			console.log( '   interpolated: ', {
@@ -189,6 +197,9 @@ THREE.KeyframeTrack.prototype = {
 
 			// TODO:add here a check for linear interpolation optimization.
 
+			// determine if interpolation is required
+			prevKey.constantToNext = equalsFunc( prevKey.value, currKey.value );
+
 			newKeys.push( currKey );
 			prevKey = currKey;
 		}