webaudio_orientation.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/WebGL.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 ( WEBGL.isWebGLAvailable() === false ) {
  83. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  84. }
  85. var scene, camera, renderer;
  86. var startButton = document.getElementById( 'startButton' );
  87. startButton.addEventListener( 'click', init );
  88. function init() {
  89. var overlay = document.getElementById( 'overlay' );
  90. overlay.remove();
  91. var container = document.getElementById( 'container' );
  92. //
  93. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  94. camera.position.set( 0, 1, 2 );
  95. var reflectionCube = new THREE.CubeTextureLoader()
  96. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  97. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  98. reflectionCube.format = THREE.RGBFormat;
  99. scene = new THREE.Scene();
  100. scene.background = new THREE.Color( 0xa0a0a0 );
  101. scene.fog = new THREE.Fog( 0xa0a0a0, 2, 20 );
  102. //
  103. var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  104. light.position.set( 0, 200, 0 );
  105. scene.add( light );
  106. light = new THREE.DirectionalLight( 0xffffff );
  107. light.position.set( 0, 200, 100 );
  108. light.castShadow = true;
  109. light.shadow.camera.top = 180;
  110. light.shadow.camera.bottom = -100;
  111. light.shadow.camera.left = -120;
  112. light.shadow.camera.right = 120;
  113. scene.add( light );
  114. //
  115. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 20, 20 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  116. mesh.rotation.x = - Math.PI / 2;
  117. mesh.receiveShadow = true;
  118. scene.add( mesh );
  119. var grid = new THREE.GridHelper( 20, 20, 0x000000, 0x000000 );
  120. grid.material.opacity = 0.2;
  121. grid.material.transparent = true;
  122. scene.add( grid );
  123. //
  124. var listener = new THREE.AudioListener();
  125. camera.add( listener );
  126. var audioElement = document.getElementById( 'music' );
  127. var positionalAudio = new THREE.PositionalAudio( listener );
  128. positionalAudio.setMediaElementSource( audioElement );
  129. positionalAudio.setRefDistance( 1 );
  130. positionalAudio.setDirectionalCone( 210, 230, 0.1 );
  131. //
  132. var gltfLoader = new THREE.GLTFLoader();
  133. gltfLoader.load( 'models/gltf/BoomBox/glTF-Binary/BoomBox.glb', function( gltf ) {
  134. var boomBox = gltf.scene;
  135. boomBox.position.set( 0, 0.2, 0 );
  136. boomBox.scale.set( 20, 20, 20 );
  137. boomBox.traverse( function( object ) {
  138. if ( object.isMesh ) {
  139. object.material.envMap = reflectionCube;
  140. object.geometry.rotateY( - Math.PI );
  141. }
  142. } );
  143. audioElement.play();
  144. boomBox.add( positionalAudio );
  145. scene.add( boomBox );
  146. animate();
  147. } );
  148. // sound is damped behind this wall
  149. var wallGeometry = new THREE.BoxBufferGeometry( 2, 1, 0.1 );
  150. var wallMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
  151. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  152. wall.position.set( 0, 0.5, - 0.5 );
  153. scene.add( wall );
  154. //
  155. renderer = new THREE.WebGLRenderer( { antialias: true } );
  156. renderer.setSize( window.innerWidth, window.innerHeight );
  157. renderer.setPixelRatio( window.devicePixelRatio );
  158. container.appendChild( renderer.domElement );
  159. renderer.gammaOutput = true;
  160. //
  161. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  162. controls.target.set( 0, 0.1, 0 );
  163. controls.update();
  164. controls.minDistance = 0.5;
  165. controls.maxDistance = 10;
  166. controls.maxPolarAngle = 0.5 * Math.PI;
  167. //
  168. window.addEventListener( 'resize', onWindowResize, false );
  169. }
  170. function onWindowResize( event ) {
  171. camera.aspect = window.innerWidth / window.innerHeight;
  172. camera.updateProjectionMatrix();
  173. renderer.setSize( window.innerWidth, window.innerHeight );
  174. }
  175. function animate() {
  176. requestAnimationFrame( animate );
  177. renderer.render( scene, camera );
  178. }
  179. </script>
  180. </body>
  181. </html>