Browse Source

Merge pull request #14345 from donmccurdy/animation-optimize-optout

AnimationMixer: Move validate/optimization call, allow opt-out.
Mr.doob 7 years ago
parent
commit
533ab8aa07

+ 19 - 1
docs/api/animation/AnimationClip.html

@@ -46,6 +46,16 @@
 			array via [page:.resetDuration resetDuration].
 		</p>
 
+		<h3>[property:Boolean isOptimized]</h3>
+		<p>
+			Whether the clip has been optimized with .optimize().
+		</p>
+
+		<h3>[property:Boolean isValidated]</h3>
+		<p>
+			Whether the clip has been validated with .validate().
+		</p>
+
 		<h3>[property:String name]</h3>
 		<p>
 			A name for this clip. A certain clip can be searched via [page:.findByName findByName].
@@ -69,7 +79,8 @@
 		<h3>[method:AnimationClip optimize]()</h3>
 		<p>
 			Optimizes each track by removing equivalent sequential keys (which are common in morph target
-			sequences).
+			sequences). Called automatically by [page:AnimationMixer]'s clipAction() method. Calling this
+			method will set [page:.isValidated isValidated] to true, and additional calls will do nothing.
 		</p>
 
 		<h3>[method:null resetDuration]()</h3>
@@ -83,6 +94,13 @@
 			Trims all tracks to the clip's duration.
 		</p>
 
+		<h3>[method:AnimationClip validate]()</h3>
+		<p>
+			Performs minimal validation on each track in the clip. Called automatically by
+			[page:AnimationMixer]'s clipAction() method. Calling this method will set
+			[page:.isValidated isValidated] to true, and additional calls will do nothing.
+		</p>
+
 
 		<h2>Static Methods</h2>
 

+ 2 - 0
docs/api/animation/AnimationMixer.html

@@ -55,6 +55,8 @@
 			from the mixer's default root. The first parameter can be either an [page:AnimationClip] object
 			or the name of an AnimationClip.<br /><br />
 
+			Automatically calls .validate() and .optimize() on the clip.<br /><br />
+
 			If an action fitting these parameters doesn't yet exist, it will be created by this method.<br /><br />
 
 			Note: Calling this method several times with the same parameters returns always the same clip

+ 3 - 3
docs/api/animation/KeyframeTrack.html

@@ -204,8 +204,8 @@
 
 		<h3>[method:null optimize]()</h3>
 		<p>
-			Removes equivalent sequential keys, which are common in morph target sequences. Called
-			automatically by the constructor.
+			Removes equivalent sequential keys, which are common in morph target sequences. Calling this
+			method will set [page:.isOptimized isOptimized] to true, and additional calls will do nothing.
 		</p>
 
 		<h3>[method:null scale]()</h3>
@@ -236,7 +236,7 @@
 
 		<h3>[method:null validate]()</h3>
 		<p>
-			Performs minimal validation on the tracks. Called automatically by the constructor.<br /><br />
+			Performs minimal validation on the tracks.<br /><br />
 			This method logs errors to the console, if a track is empty, if the [page:.valueSize value size] is not valid, if an item
 			in the [page:.times times] or [page:.values values] array is not a valid number or if the items in the *times* array are out of order.
 		</p>

+ 23 - 2
src/animation/AnimationClip.js

@@ -19,6 +19,9 @@ function AnimationClip( name, duration, tracks ) {
 	this.tracks = tracks;
 	this.duration = ( duration !== undefined ) ? duration : - 1;
 
+	this.isValidated = false;
+	this.isOptimized = false;
+
 	this.uuid = _Math.generateUUID();
 
 	// this means it should figure out its duration by scanning the tracks
@@ -28,8 +31,6 @@ function AnimationClip( name, duration, tracks ) {
 
 	}
 
-	this.optimize();
-
 }
 
 Object.assign( AnimationClip, {
@@ -341,14 +342,34 @@ Object.assign( AnimationClip.prototype, {
 
 	},
 
+	validate: function () {
+
+		if ( this.isValidated ) return this;
+
+		for ( var i = 0; i < this.tracks.length; i ++ ) {
+
+			this.tracks[ i ].validate();
+
+		}
+
+		this.isValidated = true;
+
+		return this;
+
+	},
+
 	optimize: function () {
 
+		if ( this.isOptimized ) return this;
+
 		for ( var i = 0; i < this.tracks.length; i ++ ) {
 
 			this.tracks[ i ].optimize();
 
 		}
 
+		this.isOptimized = true;
+
 		return this;
 
 	}

+ 3 - 0
src/animation/AnimationMixer.js

@@ -555,6 +555,9 @@ AnimationMixer.prototype = Object.assign( Object.create( EventDispatcher.prototy
 		// clip must be known when specified via string
 		if ( clipObject === null ) return null;
 
+		clipObject.validate();
+		clipObject.optimize();
+
 		// allocate all resources required to run it
 		var newAction = new AnimationAction( this, clipObject, optionalRoot );
 

+ 0 - 3
src/animation/KeyframeTrack.js

@@ -36,9 +36,6 @@ function KeyframeTrack( name, times, values, interpolation ) {
 
 	this.setInterpolation( interpolation || this.DefaultInterpolation );
 
-	this.validate();
-	this.optimize();
-
 }
 
 // Static methods:

+ 32 - 2
test/unit/src/animation/AnimationClip.tests.js

@@ -4,6 +4,7 @@
 /* global QUnit */
 
 import { AnimationClip } from '../../../../src/animation/AnimationClip';
+import { NumberKeyframeTrack } from '../../../../src/animation/tracks/NumberKeyframeTrack';
 
 export default QUnit.module( 'Animation', () => {
 
@@ -66,9 +67,38 @@ export default QUnit.module( 'Animation', () => {
 
 		} );
 
-		QUnit.todo( "optimize", ( assert ) => {
+		QUnit.test( 'optimize', ( assert ) => {
 
-			assert.ok( false, "everything's gonna be alright" );
+			var track = new NumberKeyframeTrack( '.material.opacity', [ 0, 1, 2, 3, 4 ], [ 0, 0, 0, 0, 1 ] );
+			var clip = new AnimationClip( 'fadeIn', 4, [ track ] );
+
+			assert.equal( clip.tracks[0].values.length, 5 );
+
+			clip.isOptimized = true;
+			clip.optimize();
+
+			assert.equal( clip.tracks[0].values.length, 5 );
+
+			clip.isOptimized = false;
+			clip.optimize();
+
+			assert.equal( clip.tracks[0].values.length, 3 );
+
+		} );
+
+		QUnit.test( 'validate', ( assert ) => {
+
+			var track = new NumberKeyframeTrack( '.material.opacity', [ 0, 1 ], [ 0, NaN ] );
+			var clip = new AnimationClip( 'fadeIn', 1, [ track ] );
+
+			track.validate = () => { throw new Error('Validation should not be called.') };
+			clip.isValidated = true;
+			clip.validate();
+
+			delete track.validate;
+			clip.isValidated = false;
+			clip.validate();
+			assert.ok( clip.isValidated );
 
 		} );