Browse Source

Merge pull request #17173 from Mugen87/dev31

Audio: Remove all IIFEs.
Mr.doob 6 years ago
parent
commit
f159660419
3 changed files with 71 additions and 65 deletions
  1. 5 5
      src/audio/AudioContext.js
  2. 37 32
      src/audio/AudioListener.js
  3. 29 28
      src/audio/PositionalAudio.js

+ 5 - 5
src/audio/AudioContext.js

@@ -2,25 +2,25 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-var context;
+var _context;
 
 var AudioContext = {
 
 	getContext: function () {
 
-		if ( context === undefined ) {
+		if ( _context === undefined ) {
 
-			context = new ( window.AudioContext || window.webkitAudioContext )();
+			_context = new ( window.AudioContext || window.webkitAudioContext )();
 
 		}
 
-		return context;
+		return _context;
 
 	},
 
 	setContext: function ( value ) {
 
-		context = value;
+		_context = value;
 
 	}
 

+ 37 - 32
src/audio/AudioListener.js

@@ -8,6 +8,9 @@ import { Clock } from '../core/Clock.js';
 import { Object3D } from '../core/Object3D.js';
 import { AudioContext } from './AudioContext.js';
 
+var _position, _quaternion, _scale;
+var _orientation;
+
 function AudioListener() {
 
 	Object3D.call( this );
@@ -23,6 +26,10 @@ function AudioListener() {
 
 	this.timeDelta = 0;
 
+	// private
+
+	this._clock = new Clock();
+
 }
 
 AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
@@ -91,54 +98,52 @@ AudioListener.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 	},
 
-	updateMatrixWorld: ( function () {
-
-		var position = new Vector3();
-		var quaternion = new Quaternion();
-		var scale = new Vector3();
+	updateMatrixWorld: function ( force ) {
 
-		var orientation = new Vector3();
-		var clock = new Clock();
+		Object3D.prototype.updateMatrixWorld.call( this, force );
 
-		return function updateMatrixWorld( force ) {
+		if ( _position === undefined ) {
 
-			Object3D.prototype.updateMatrixWorld.call( this, force );
+			_position = new Vector3();
+			_quaternion = new Quaternion();
+			_scale = new Vector3();
+			_orientation = new Vector3();
 
-			var listener = this.context.listener;
-			var up = this.up;
+		}
 
-			this.timeDelta = clock.getDelta();
+		var listener = this.context.listener;
+		var up = this.up;
 
-			this.matrixWorld.decompose( position, quaternion, scale );
+		this.timeDelta = this._clock.getDelta();
 
-			orientation.set( 0, 0, - 1 ).applyQuaternion( quaternion );
+		this.matrixWorld.decompose( _position, _quaternion, _scale );
 
-			if ( listener.positionX ) {
+		_orientation.set( 0, 0, - 1 ).applyQuaternion( _quaternion );
 
-				// code path for Chrome (see #14393)
+		if ( listener.positionX ) {
 
-				var endTime = this.context.currentTime + this.timeDelta;
+			// code path for Chrome (see #14393)
 
-				listener.positionX.linearRampToValueAtTime( position.x, endTime );
-				listener.positionY.linearRampToValueAtTime( position.y, endTime );
-				listener.positionZ.linearRampToValueAtTime( position.z, endTime );
-				listener.forwardX.linearRampToValueAtTime( orientation.x, endTime );
-				listener.forwardY.linearRampToValueAtTime( orientation.y, endTime );
-				listener.forwardZ.linearRampToValueAtTime( orientation.z, endTime );
-				listener.upX.linearRampToValueAtTime( up.x, endTime );
-				listener.upY.linearRampToValueAtTime( up.y, endTime );
-				listener.upZ.linearRampToValueAtTime( up.z, endTime );
+			var endTime = this.context.currentTime + this.timeDelta;
 
-			} else {
+			listener.positionX.linearRampToValueAtTime( _position.x, endTime );
+			listener.positionY.linearRampToValueAtTime( _position.y, endTime );
+			listener.positionZ.linearRampToValueAtTime( _position.z, endTime );
+			listener.forwardX.linearRampToValueAtTime( _orientation.x, endTime );
+			listener.forwardY.linearRampToValueAtTime( _orientation.y, endTime );
+			listener.forwardZ.linearRampToValueAtTime( _orientation.z, endTime );
+			listener.upX.linearRampToValueAtTime( up.x, endTime );
+			listener.upY.linearRampToValueAtTime( up.y, endTime );
+			listener.upZ.linearRampToValueAtTime( up.z, endTime );
 
-				listener.setPosition( position.x, position.y, position.z );
-				listener.setOrientation( orientation.x, orientation.y, orientation.z, up.x, up.y, up.z );
+		} else {
 
-			}
+			listener.setPosition( _position.x, _position.y, _position.z );
+			listener.setOrientation( _orientation.x, _orientation.y, _orientation.z, up.x, up.y, up.z );
 
-		};
+		}
 
-	} )()
+	}
 
 } );
 

+ 29 - 28
src/audio/PositionalAudio.js

@@ -7,6 +7,9 @@ import { Quaternion } from '../math/Quaternion.js';
 import { Audio } from './Audio.js';
 import { Object3D } from '../core/Object3D.js';
 
+var _position, _quaternion, _scale;
+var _orientation;
+
 function PositionalAudio( listener ) {
 
 	Audio.call( this, listener );
@@ -93,50 +96,48 @@ PositionalAudio.prototype = Object.assign( Object.create( Audio.prototype ), {
 
 	},
 
-	updateMatrixWorld: ( function () {
-
-		var position = new Vector3();
-		var quaternion = new Quaternion();
-		var scale = new Vector3();
-
-		var orientation = new Vector3();
+	updateMatrixWorld: function ( force ) {
 
-		return function updateMatrixWorld( force ) {
+		Object3D.prototype.updateMatrixWorld.call( this, force );
 
-			Object3D.prototype.updateMatrixWorld.call( this, force );
+		if ( _position === undefined ) {
 
-			if ( this.hasPlaybackControl === true && this.isPlaying === false ) return;
+			_position = new Vector3();
+			_quaternion = new Quaternion();
+			_scale = new Vector3();
+			_orientation = new Vector3();
 
-			this.matrixWorld.decompose( position, quaternion, scale );
+		}
 
-			orientation.set( 0, 0, 1 ).applyQuaternion( quaternion );
+		if ( this.hasPlaybackControl === true && this.isPlaying === false ) return;
 
-			var panner = this.panner;
+		this.matrixWorld.decompose( _position, _quaternion, _scale );
 
-			if ( panner.positionX ) {
+		_orientation.set( 0, 0, 1 ).applyQuaternion( _quaternion );
 
-				// code path for Chrome and Firefox (see #14393)
+		var panner = this.panner;
 
-				var endTime = this.context.currentTime + this.listener.timeDelta;
+		if ( panner.positionX ) {
 
-				panner.positionX.linearRampToValueAtTime( position.x, endTime );
-				panner.positionY.linearRampToValueAtTime( position.y, endTime );
-				panner.positionZ.linearRampToValueAtTime( position.z, endTime );
-				panner.orientationX.linearRampToValueAtTime( orientation.x, endTime );
-				panner.orientationY.linearRampToValueAtTime( orientation.y, endTime );
-				panner.orientationZ.linearRampToValueAtTime( orientation.z, endTime );
+			// code path for Chrome and Firefox (see #14393)
 
-			} else {
+			var endTime = this.context.currentTime + this.listener.timeDelta;
 
-				panner.setPosition( position.x, position.y, position.z );
-				panner.setOrientation( orientation.x, orientation.y, orientation.z );
+			panner.positionX.linearRampToValueAtTime( _position.x, endTime );
+			panner.positionY.linearRampToValueAtTime( _position.y, endTime );
+			panner.positionZ.linearRampToValueAtTime( _position.z, endTime );
+			panner.orientationX.linearRampToValueAtTime( _orientation.x, endTime );
+			panner.orientationY.linearRampToValueAtTime( _orientation.y, endTime );
+			panner.orientationZ.linearRampToValueAtTime( _orientation.z, endTime );
 
-			}
+		} else {
 
-		};
+			panner.setPosition( _position.x, _position.y, _position.z );
+			panner.setOrientation( _orientation.x, _orientation.y, _orientation.z );
 
-	} )()
+		}
 
+	}
 
 } );