1 /** 2 * Update the source with object3d. usefull for positional sounds 3 * 4 * @param {THREE.Object3D} object3d the object which originate the source 5 * @param {Number} deltaTime the number of seconds since last update 6 */ 7 WebAudio.Sound.fn.updateWithObject3d = function(object3d, deltaTime){ 8 // sanity check on parameters 9 console.assert( object3d instanceof THREE.Object3D ); 10 console.assert( typeof(deltaTime) === 'number' ); 11 12 // ensure object3d.matrixWorld is up to date 13 object3d.updateMatrixWorld(); 14 15 this.updateWithMatrix4(object3d.matrixWorld, deltaTime); 16 17 return this; // for chained API 18 } 19 20 /** 21 * Update the source with a matrixWorld. usefull for positional sounds 22 * 23 * @param {THREE.Matrix4} matrixWorld the matrixWorld describing the position of the sound 24 * @param {Number} deltaTime the number of seconds since last update 25 */ 26 WebAudio.Sound.fn.updateWithMatrix4 = function(matrixWorld, deltaTime){ 27 // sanity check on parameters 28 console.assert( matrixWorld instanceof THREE.Matrix4 ); 29 console.assert( typeof(deltaTime) === 'number' ); 30 31 //////////////////////////////////////////////////////////////////////// 32 // set position 33 var position = matrixWorld.getPosition(); 34 this._panner.setPosition(position.x, position.y, position.z); 35 36 //////////////////////////////////////////////////////////////////////// 37 // set orientation 38 var vOrientation= new THREE.Vector3(0,0,1); 39 var mOrientation= matrixWorld.clone(); 40 // zero the translation 41 mOrientation.setPosition({x : 0, y: 0, z: 0}); 42 // Multiply the 0,0,1 vector by the world matrix and normalize the result. 43 mOrientation.multiplyVector3(vOrientation); 44 vOrientation.normalize(); 45 // Set panner orientation 46 this._panner.setOrientation(vOrientation.x, vOrientation.y, vOrientation.z); 47 48 //////////////////////////////////////////////////////////////////////// 49 // set velocity 50 if( this._prevPos === undefined ){ 51 this._prevPos = matrixWorld.getPosition().clone(); 52 }else{ 53 var position = matrixWorld.getPosition(); 54 var velocity = position.clone().subSelf(this._prevPos).divideScalar(deltaTime); 55 this._prevPos = matrixWorld.getPosition().clone(); 56 this._panner.setVelocity(velocity.x, velocity.y, velocity.z); 57 } 58 }