Ver Fonte

Merge pull request #20013 from ianpurvis/animation-tracks-es6-classes

Animation Tracks: Convert to es6 classes
Mr.doob há 4 anos atrás
pai
commit
582df680fd

+ 43 - 47
src/animation/KeyframeTrack.js

@@ -8,28 +8,26 @@ import { LinearInterpolant } from '../math/interpolants/LinearInterpolant.js';
 import { DiscreteInterpolant } from '../math/interpolants/DiscreteInterpolant.js';
 import { AnimationUtils } from './AnimationUtils.js';
 
-function KeyframeTrack( name, times, values, interpolation ) {
+class KeyframeTrack {
 
-	if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' );
-	if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name );
+	constructor( name, times, values, interpolation ) {
 
-	this.name = name;
+		if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' );
+		if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name );
 
-	this.times = AnimationUtils.convertArray( times, this.TimeBufferType );
-	this.values = AnimationUtils.convertArray( values, this.ValueBufferType );
+		this.name = name;
 
-	this.setInterpolation( interpolation || this.DefaultInterpolation );
+		this.times = AnimationUtils.convertArray( times, this.TimeBufferType );
+		this.values = AnimationUtils.convertArray( values, this.ValueBufferType );
 
-}
-
-// Static methods
+		this.setInterpolation( interpolation || this.DefaultInterpolation );
 
-Object.assign( KeyframeTrack, {
+	}
 
 	// Serialization (in static context, because of constructor invocation
 	// and automatic invocation of .toJSON):
 
-	toJSON: function ( track ) {
+	static toJSON( track ) {
 
 		const trackType = track.constructor;
 
@@ -67,37 +65,25 @@ Object.assign( KeyframeTrack, {
 
 	}
 
-} );
-
-Object.assign( KeyframeTrack.prototype, {
-
-	constructor: KeyframeTrack,
-
-	TimeBufferType: Float32Array,
-
-	ValueBufferType: Float32Array,
-
-	DefaultInterpolation: InterpolateLinear,
-
-	InterpolantFactoryMethodDiscrete: function ( result ) {
+	InterpolantFactoryMethodDiscrete( result ) {
 
 		return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result );
 
-	},
+	}
 
-	InterpolantFactoryMethodLinear: function ( result ) {
+	InterpolantFactoryMethodLinear( result ) {
 
 		return new LinearInterpolant( this.times, this.values, this.getValueSize(), result );
 
-	},
+	}
 
-	InterpolantFactoryMethodSmooth: function ( result ) {
+	InterpolantFactoryMethodSmooth( result ) {
 
 		return new CubicInterpolant( this.times, this.values, this.getValueSize(), result );
 
-	},
+	}
 
-	setInterpolation: function ( interpolation ) {
+	setInterpolation( interpolation ) {
 
 		let factoryMethod;
 
@@ -152,9 +138,9 @@ Object.assign( KeyframeTrack.prototype, {
 
 		return this;
 
-	},
+	}
 
-	getInterpolation: function () {
+	getInterpolation() {
 
 		switch ( this.createInterpolant ) {
 
@@ -172,16 +158,16 @@ Object.assign( KeyframeTrack.prototype, {
 
 		}
 
-	},
+	}
 
-	getValueSize: function () {
+	getValueSize() {
 
 		return this.values.length / this.times.length;
 
-	},
+	}
 
 	// move all keyframes either forwards or backwards in time
-	shift: function ( timeOffset ) {
+	shift( timeOffset ) {
 
 		if ( timeOffset !== 0.0 ) {
 
@@ -197,10 +183,10 @@ Object.assign( KeyframeTrack.prototype, {
 
 		return this;
 
-	},
+	}
 
 	// scale all keyframe times by a factor (useful for frame <-> seconds conversions)
-	scale: function ( timeScale ) {
+	scale( timeScale ) {
 
 		if ( timeScale !== 1.0 ) {
 
@@ -216,11 +202,11 @@ Object.assign( KeyframeTrack.prototype, {
 
 		return this;
 
-	},
+	}
 
 	// removes keyframes before and after animation without changing any values within the range [startTime, endTime].
 	// 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 ) {
+	trim( startTime, endTime ) {
 
 		const times = this.times,
 			nKeys = times.length;
@@ -260,10 +246,10 @@ Object.assign( KeyframeTrack.prototype, {
 
 		return this;
 
-	},
+	}
 
 	// ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
-	validate: function () {
+	validate() {
 
 		let valid = true;
 
@@ -337,11 +323,11 @@ Object.assign( KeyframeTrack.prototype, {
 
 		return valid;
 
-	},
+	}
 
 	// removes equivalent sequential keys as common in morph target sequences
 	// (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
-	optimize: function () {
+	optimize() {
 
 		// times or values may be shared with other tracks, so overwriting is unsafe
 		const times = AnimationUtils.arraySlice( this.times ),
@@ -450,9 +436,9 @@ Object.assign( KeyframeTrack.prototype, {
 
 		return this;
 
-	},
+	}
 
-	clone: function () {
+	clone() {
 
 		const times = AnimationUtils.arraySlice( this.times, 0 );
 		const values = AnimationUtils.arraySlice( this.values, 0 );
@@ -467,6 +453,16 @@ Object.assign( KeyframeTrack.prototype, {
 
 	}
 
+}
+
+Object.assign( KeyframeTrack.prototype, {
+
+	TimeBufferType: Float32Array,
+
+	ValueBufferType: Float32Array,
+
+	DefaultInterpolation: InterpolateLinear,
+
 } );
 
 export { KeyframeTrack };

+ 2 - 9
src/animation/tracks/BooleanKeyframeTrack.js

@@ -4,16 +4,9 @@ import { KeyframeTrack } from '../KeyframeTrack.js';
 /**
  * A Track of Boolean keyframe values.
  */
+class BooleanKeyframeTrack extends KeyframeTrack {}
 
-function BooleanKeyframeTrack( name, times, values ) {
-
-	KeyframeTrack.call( this, name, times, values );
-
-}
-
-BooleanKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
-
-	constructor: BooleanKeyframeTrack,
+Object.assign( BooleanKeyframeTrack.prototype, {
 
 	ValueTypeName: 'bool',
 	ValueBufferType: Array,

+ 2 - 9
src/animation/tracks/ColorKeyframeTrack.js

@@ -3,16 +3,9 @@ import { KeyframeTrack } from '../KeyframeTrack.js';
 /**
  * A Track of keyframe values that represent color.
  */
+class ColorKeyframeTrack extends KeyframeTrack {}
 
-function ColorKeyframeTrack( name, times, values, interpolation ) {
-
-	KeyframeTrack.call( this, name, times, values, interpolation );
-
-}
-
-ColorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
-
-	constructor: ColorKeyframeTrack,
+Object.assign( ColorKeyframeTrack.prototype, {
 
 	ValueTypeName: 'color'
 

+ 2 - 9
src/animation/tracks/NumberKeyframeTrack.js

@@ -3,16 +3,9 @@ import { KeyframeTrack } from '../KeyframeTrack.js';
 /**
  * A Track of numeric keyframe values.
  */
+class NumberKeyframeTrack extends KeyframeTrack {}
 
-function NumberKeyframeTrack( name, times, values, interpolation ) {
-
-	KeyframeTrack.call( this, name, times, values, interpolation );
-
-}
-
-NumberKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
-
-	constructor: NumberKeyframeTrack,
+Object.assign( NumberKeyframeTrack.prototype, {
 
 	ValueTypeName: 'number'
 

+ 6 - 11
src/animation/tracks/QuaternionKeyframeTrack.js

@@ -5,16 +5,17 @@ import { QuaternionLinearInterpolant } from '../../math/interpolants/QuaternionL
 /**
  * A Track of quaternion keyframe values.
  */
+class QuaternionKeyframeTrack extends KeyframeTrack {
 
-function QuaternionKeyframeTrack( name, times, values, interpolation ) {
+	InterpolantFactoryMethodLinear( result ) {
 
-	KeyframeTrack.call( this, name, times, values, interpolation );
+		return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result );
 
-}
+	}
 
-QuaternionKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
+}
 
-	constructor: QuaternionKeyframeTrack,
+Object.assign( QuaternionKeyframeTrack.prototype, {
 
 	ValueTypeName: 'quaternion',
 
@@ -22,12 +23,6 @@ QuaternionKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.
 
 	DefaultInterpolation: InterpolateLinear,
 
-	InterpolantFactoryMethodLinear: function ( result ) {
-
-		return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result );
-
-	},
-
 	InterpolantFactoryMethodSmooth: undefined // not yet implemented
 
 } );

+ 2 - 9
src/animation/tracks/StringKeyframeTrack.js

@@ -4,16 +4,9 @@ import { KeyframeTrack } from '../KeyframeTrack.js';
 /**
  * A Track that interpolates Strings
  */
+class StringKeyframeTrack extends KeyframeTrack {}
 
-function StringKeyframeTrack( name, times, values, interpolation ) {
-
-	KeyframeTrack.call( this, name, times, values, interpolation );
-
-}
-
-StringKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
-
-	constructor: StringKeyframeTrack,
+Object.assign( StringKeyframeTrack.prototype, {
 
 	ValueTypeName: 'string',
 	ValueBufferType: Array,

+ 2 - 9
src/animation/tracks/VectorKeyframeTrack.js

@@ -3,16 +3,9 @@ import { KeyframeTrack } from '../KeyframeTrack.js';
 /**
  * A Track of vectored keyframe values.
  */
+class VectorKeyframeTrack extends KeyframeTrack {}
 
-function VectorKeyframeTrack( name, times, values, interpolation ) {
-
-	KeyframeTrack.call( this, name, times, values, interpolation );
-
-}
-
-VectorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), {
-
-	constructor: VectorKeyframeTrack,
+Object.assign( VectorKeyframeTrack.prototype, {
 
 	ValueTypeName: 'vector'