webaudio_orientation.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webaudio - orientation</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. background:#777;
  10. padding:0;
  11. margin:0;
  12. overflow:hidden;
  13. font-family: Monospace;
  14. }
  15. #info {
  16. position: absolute;
  17. z-index: 2;
  18. top: 0px;
  19. width: 100%;
  20. color: #ffffff;
  21. padding: 5px;
  22. font-size:13px;
  23. text-align:center;
  24. font-weight: bold;
  25. }
  26. a {
  27. color: #ffffff;
  28. }
  29. #overlay {
  30. position: absolute;
  31. z-index: 1;
  32. top: 0;
  33. left: 0;
  34. width: 100%;
  35. height:100%;
  36. display: flex;
  37. align-items: center;
  38. justify-content: center;
  39. opacity: 1;
  40. background-color: #000000;
  41. color: #ffffff;
  42. }
  43. #overlay > div {
  44. text-align: center;
  45. }
  46. #overlay > div > button {
  47. height: 20px;
  48. width: 100px;
  49. background: transparent;
  50. color: #ffffff;
  51. outline: 1px solid #ffffff;
  52. border: 0px;
  53. cursor: pointer;
  54. }
  55. #overlay > div > p {
  56. color: #777777;
  57. font-size: 12px;
  58. }
  59. </style>
  60. <script src="../build/three.js"></script>
  61. <script src="js/Detector.js"></script>
  62. <script src="js/controls/OrbitControls.js"></script>
  63. <script src="js/loaders/GLTFLoader.js"></script>
  64. </head>
  65. <body>
  66. <audio loop id="music" preload="auto" style="display: none">
  67. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" type="audio/ogg">
  68. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3" type="audio/mpeg">
  69. </audio>
  70. <div id="overlay">
  71. <div>
  72. <button id="startButton">Click to Play</button>
  73. <p>Automatic audio playback requires a user interaction.</p>
  74. </div>
  75. </div>
  76. <div id="container"></div>
  77. <div id="info">
  78. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio orientation |
  79. music by <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener noreferrer">skullbeatz</a>
  80. </div>
  81. <script>
  82. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  83. var scene, camera, renderer;
  84. var startButton = document.getElementById( 'startButton' );
  85. startButton.addEventListener( 'click', init );
  86. function init() {
  87. var overlay = document.getElementById( 'overlay' );
  88. overlay.remove();
  89. var container = document.getElementById( 'container' );
  90. //
  91. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  92. camera.position.set( 0, 1, 2 );
  93. var reflectionCube = new THREE.CubeTextureLoader()
  94. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  95. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  96. reflectionCube.format = THREE.RGBFormat;
  97. scene = new THREE.Scene();
  98. scene.background = new THREE.Color( 0xa0a0a0 );
  99. scene.fog = new THREE.Fog( 0xa0a0a0, 2, 20 );
  100. //
  101. var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  102. light.position.set( 0, 200, 0 );
  103. scene.add( light );
  104. light = new THREE.DirectionalLight( 0xffffff );
  105. light.position.set( 0, 200, 100 );
  106. light.castShadow = true;
  107. light.shadow.camera.top = 180;
  108. light.shadow.camera.bottom = -100;
  109. light.shadow.camera.left = -120;
  110. light.shadow.camera.right = 120;
  111. scene.add( light );
  112. //
  113. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20, 20 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  114. mesh.rotation.x = - Math.PI / 2;
  115. mesh.receiveShadow = true;
  116. scene.add( mesh );
  117. var grid = new THREE.GridHelper( 20, 20, 0x000000, 0x000000 );
  118. grid.material.opacity = 0.2;
  119. grid.material.transparent = true;
  120. scene.add( grid );
  121. //
  122. var audioLoader = new THREE.AudioLoader();
  123. var listener = new THREE.AudioListener();
  124. camera.add( listener );
  125. var audioElement = document.getElementById( 'music' );
  126. var positionalAudio = new THREE.PositionalAudio( listener );
  127. positionalAudio.setMediaElementSource( audioElement );
  128. positionalAudio.setRefDistance( 1 );
  129. // configure directionality cone, see https://developer.mozilla.org/en-US/docs/Web/API/PannerNode
  130. positionalAudio.panner.coneInnerAngle = 210;
  131. positionalAudio.panner.coneOuterAngle = 230;
  132. positionalAudio.panner.coneOuterGain = 0.1;
  133. //
  134. var gltfLoader = new THREE.GLTFLoader();
  135. gltfLoader.load( 'models/gltf/BoomBox/glTF-Binary/BoomBox.glb', function( gltf ) {
  136. var boomBox = gltf.scene;
  137. boomBox.position.set( 0, 0.2, 0 );
  138. boomBox.scale.set( 20, 20, 20 );
  139. boomBox.traverse( function( object ) {
  140. if ( object.isMesh ) {
  141. object.material.envMap = reflectionCube;
  142. object.geometry.rotateY( - Math.PI );
  143. }
  144. } );
  145. audioElement.play();
  146. boomBox.add( positionalAudio );
  147. scene.add( boomBox );
  148. animate();
  149. } );
  150. // sound is damped behind this wall
  151. var wallGeometry = new THREE.BoxBufferGeometry( 2, 1, 0.1 );
  152. var wallMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
  153. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  154. wall.position.set( 0, 0.5, - 0.5 );
  155. scene.add( wall );
  156. //
  157. renderer = new THREE.WebGLRenderer( { antialias: true } );
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. renderer.setPixelRatio( window.devicePixelRatio );
  160. container.appendChild( renderer.domElement );
  161. renderer.gammaOutput = true;
  162. //
  163. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  164. controls.target.set( 0, 0.1, 0 );
  165. controls.update();
  166. controls.minDistance = 0.5;
  167. controls.maxDistance = 10;
  168. controls.maxPolarAngle = 0.5 * Math.PI;
  169. //
  170. window.addEventListener( 'resize', onWindowResize, false );
  171. }
  172. function onWindowResize( event ) {
  173. camera.aspect = window.innerWidth / window.innerHeight;
  174. camera.updateProjectionMatrix();
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. }
  177. function animate() {
  178. requestAnimationFrame( animate );
  179. renderer.render( scene, camera );
  180. }
  181. </script>
  182. </body>
  183. </html>