Browse Source

Rewrote KeyframeTrack.prototype.trim.

tschw 9 years ago
parent
commit
953014decf
1 changed files with 12 additions and 23 deletions
  1. 12 23
      src/animation/KeyframeTrack.js

+ 12 - 23
src/animation/KeyframeTrack.js

@@ -183,36 +183,25 @@ THREE.KeyframeTrack.prototype = {
 	// IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
 	trim: function( startTime, endTime ) {
 
-		var times = this.times;
-		var nKeys = times.length;
-
-		var firstKeysToRemove = 0;
-		for ( var i = 1; i !== nKeys; ++ i ) {
-
-			if ( times[i] <= startTime ) ++ firstKeysToRemove;
-
-		}
-
-		var lastKeysToRemove = 0;
-		for ( var i = nKeys - 2; i !== 0; -- i ) {
+		var times = this.times,
+			nKeys = times.length,
+			from = 0,
+			to = nKeys - 1;
 
-			if ( times[i] >= endTime ) ++ lastKeysToRemove;
-			else break;
+		while ( from !== nKeys && times[ from ] < startTime ) ++ from;
+		while ( to !== -1 && times[ to ] > endTime ) -- to;
 
-		}
+		++ to; // inclusive -> exclusive bound
 
-		// remove last keys first because it doesn't affect the position of the first keys (the otherway around doesn't work as easily)
-		if( ( firstKeysToRemove + lastKeysToRemove ) !== 0 ) {
+		if( from !== 0 || to !== nKeys ) {
 
-			var from = firstKeysToRemove;
-			var to = nKeys - lastKeysToRemove - firstKeysToRemove;
+			// empty tracks are forbidden, so keep at least one keyframe
+			if ( from >= to ) to = Math.max( to , 1 ), from = to - 1;
 
 			var stride = this.getValueSize();
-
 			this.times = THREE.AnimationUtils.arraySlice( times, from, to );
-
-			var values = this.values;
-			this.values = THREE.AnimationUtils.arraySlice( values, from * stride, to * stride );
+			this.values = THREE.AnimationUtils.
+					arraySlice( this.values, from * stride, to * stride );
 
 		}