PositionalAudio.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.panningModel = 'HRTF';
  12. this.panner.connect( this.gain );
  13. }
  14. PositionalAudio.prototype = Object.assign( Object.create( Audio.prototype ), {
  15. constructor: PositionalAudio,
  16. getOutput: function () {
  17. return this.panner;
  18. },
  19. getRefDistance: function () {
  20. return this.panner.refDistance;
  21. },
  22. setRefDistance: function ( value ) {
  23. this.panner.refDistance = value;
  24. return this;
  25. },
  26. getRolloffFactor: function () {
  27. return this.panner.rolloffFactor;
  28. },
  29. setRolloffFactor: function ( value ) {
  30. this.panner.rolloffFactor = value;
  31. return this;
  32. },
  33. getDistanceModel: function () {
  34. return this.panner.distanceModel;
  35. },
  36. setDistanceModel: function ( value ) {
  37. this.panner.distanceModel = value;
  38. return this;
  39. },
  40. getMaxDistance: function () {
  41. return this.panner.maxDistance;
  42. },
  43. setMaxDistance: function ( value ) {
  44. this.panner.maxDistance = value;
  45. return this;
  46. },
  47. setDirectionalCone: function ( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
  48. this.panner.coneInnerAngle = coneInnerAngle;
  49. this.panner.coneOuterAngle = coneOuterAngle;
  50. this.panner.coneOuterGain = coneOuterGain;
  51. return this;
  52. },
  53. updateMatrixWorld: ( function () {
  54. var position = new Vector3();
  55. var quaternion = new Quaternion();
  56. var scale = new Vector3();
  57. var orientation = new Vector3();
  58. return function updateMatrixWorld( force ) {
  59. Object3D.prototype.updateMatrixWorld.call( this, force );
  60. if ( this.hasPlaybackControl === true && this.isPlaying === false ) return;
  61. this.matrixWorld.decompose( position, quaternion, scale );
  62. orientation.set( 0, 0, 1 ).applyQuaternion( quaternion );
  63. var panner = this.panner;
  64. if ( panner.positionX ) {
  65. // code path for Chrome and Firefox (see #14393)
  66. var endTime = this.context.currentTime + this.listener.timeDelta;
  67. panner.positionX.linearRampToValueAtTime( position.x, endTime );
  68. panner.positionY.linearRampToValueAtTime( position.y, endTime );
  69. panner.positionZ.linearRampToValueAtTime( position.z, endTime );
  70. panner.orientationX.linearRampToValueAtTime( orientation.x, endTime );
  71. panner.orientationY.linearRampToValueAtTime( orientation.y, endTime );
  72. panner.orientationZ.linearRampToValueAtTime( orientation.z, endTime );
  73. } else {
  74. panner.setPosition( position.x, position.y, position.z );
  75. panner.setOrientation( orientation.x, orientation.y, orientation.z );
  76. }
  77. };
  78. } )()
  79. } );
  80. export { PositionalAudio };
粤ICP备19079148号