PositionalAudio.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { Vector3 } from '../math/Vector3.js';
  5. import { Quaternion } from '../math/Quaternion.js';
  6. import { Audio } from './Audio.js';
  7. import { Object3D } from '../core/Object3D.js';
  8. function PositionalAudio( listener ) {
  9. Audio.call( this, listener );
  10. this.panner = this.context.createPanner();
  11. this.panner.connect( this.gain );
  12. }
  13. PositionalAudio.prototype = Object.assign( Object.create( Audio.prototype ), {
  14. constructor: PositionalAudio,
  15. getOutput: function () {
  16. return this.panner;
  17. },
  18. getRefDistance: function () {
  19. return this.panner.refDistance;
  20. },
  21. setRefDistance: function ( value ) {
  22. this.panner.refDistance = value;
  23. return this;
  24. },
  25. getRolloffFactor: function () {
  26. return this.panner.rolloffFactor;
  27. },
  28. setRolloffFactor: function ( value ) {
  29. this.panner.rolloffFactor = value;
  30. return this;
  31. },
  32. getDistanceModel: function () {
  33. return this.panner.distanceModel;
  34. },
  35. setDistanceModel: function ( value ) {
  36. this.panner.distanceModel = value;
  37. return this;
  38. },
  39. getMaxDistance: function () {
  40. return this.panner.maxDistance;
  41. },
  42. setMaxDistance: function ( value ) {
  43. this.panner.maxDistance = value;
  44. return this;
  45. },
  46. setDirectionalCone: function ( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
  47. this.panner.coneInnerAngle = coneInnerAngle;
  48. this.panner.coneOuterAngle = coneOuterAngle;
  49. this.panner.coneOuterGain = coneOuterGain;
  50. return this;
  51. },
  52. updateMatrixWorld: ( function () {
  53. var position = new Vector3();
  54. var quaternion = new Quaternion();
  55. var scale = new Vector3();
  56. var orientation = new Vector3();
  57. return function updateMatrixWorld( force ) {
  58. Object3D.prototype.updateMatrixWorld.call( this, force );
  59. var panner = this.panner;
  60. this.matrixWorld.decompose( position, quaternion, scale );
  61. orientation.set( 0, 0, 1 ).applyQuaternion( quaternion );
  62. if ( panner.positionX ) {
  63. // code path for Chrome and Firefox (see #14393)
  64. var endTime = this.context.currentTime + this.listener.timeDelta;
  65. panner.positionX.linearRampToValueAtTime( position.x, endTime );
  66. panner.positionY.linearRampToValueAtTime( position.y, endTime );
  67. panner.positionZ.linearRampToValueAtTime( position.z, endTime );
  68. panner.orientationX.linearRampToValueAtTime( orientation.x, endTime );
  69. panner.orientationY.linearRampToValueAtTime( orientation.y, endTime );
  70. panner.orientationZ.linearRampToValueAtTime( orientation.z, endTime );
  71. } else {
  72. panner.setPosition( position.x, position.y, position.z );
  73. panner.setOrientation( orientation.x, orientation.y, orientation.z );
  74. }
  75. };
  76. } )()
  77. } );
  78. export { PositionalAudio };