webaudio_orientation.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <audio loop id="music" preload="auto" style="display: none">
  11. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" type="audio/ogg">
  12. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3" type="audio/mpeg">
  13. </audio>
  14. <div id="overlay">
  15. <div>
  16. <button id="startButton">Click to Play</button>
  17. <p>Audio playback requires user interaction.</p>
  18. </div>
  19. </div>
  20. <div id="container"></div>
  21. <div id="info">
  22. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio - orientation<br/>
  23. music by <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener noreferrer">skullbeatz</a>
  24. </div>
  25. <script type="module">
  26. import * as THREE from '../build/three.module.js';
  27. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  28. import { PositionalAudioHelper } from './jsm/helpers/PositionalAudioHelper.js';
  29. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  30. var scene, camera, renderer;
  31. var startButton = document.getElementById( 'startButton' );
  32. startButton.addEventListener( 'click', init );
  33. function init() {
  34. var overlay = document.getElementById( 'overlay' );
  35. overlay.remove();
  36. var container = document.getElementById( 'container' );
  37. //
  38. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  39. camera.position.set( 3, 2, 3 );
  40. var reflectionCube = new THREE.CubeTextureLoader()
  41. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  42. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  43. reflectionCube.format = THREE.RGBFormat;
  44. scene = new THREE.Scene();
  45. scene.background = new THREE.Color( 0xa0a0a0 );
  46. scene.fog = new THREE.Fog( 0xa0a0a0, 2, 20 );
  47. //
  48. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  49. hemiLight.position.set( 0, 20, 0 );
  50. scene.add( hemiLight );
  51. var dirLight = new THREE.DirectionalLight( 0xffffff );
  52. dirLight.position.set( 5, 5, 0 );
  53. dirLight.castShadow = true;
  54. dirLight.shadow.camera.top = 1;
  55. dirLight.shadow.camera.bottom = - 1;
  56. dirLight.shadow.camera.left = - 1;
  57. dirLight.shadow.camera.right = 1;
  58. dirLight.shadow.camera.near = 0.1;
  59. dirLight.shadow.camera.far = 20;
  60. scene.add( dirLight );
  61. // scene.add( new CameraHelper( dirLight.shadow.camera ) );
  62. //
  63. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 50, 50 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  64. mesh.rotation.x = - Math.PI / 2;
  65. mesh.receiveShadow = true;
  66. scene.add( mesh );
  67. var grid = new THREE.GridHelper( 50, 50, 0x888888, 0x888888 );
  68. scene.add( grid );
  69. //
  70. var listener = new THREE.AudioListener();
  71. camera.add( listener );
  72. var audioElement = document.getElementById( 'music' );
  73. audioElement.play();
  74. var positionalAudio = new THREE.PositionalAudio( listener );
  75. positionalAudio.setMediaElementSource( audioElement );
  76. positionalAudio.setRefDistance( 1 );
  77. positionalAudio.setDirectionalCone( 180, 230, 0.1 );
  78. var helper = new PositionalAudioHelper( positionalAudio, 0.1 );
  79. positionalAudio.add( helper );
  80. //
  81. var gltfLoader = new GLTFLoader();
  82. gltfLoader.load( 'models/gltf/BoomBox/glTF-Binary/BoomBox.glb', function ( gltf ) {
  83. var boomBox = gltf.scene;
  84. boomBox.position.set( 0, 0.2, 0 );
  85. boomBox.scale.set( 20, 20, 20 );
  86. boomBox.traverse( function ( object ) {
  87. if ( object.isMesh ) {
  88. object.material.envMap = reflectionCube;
  89. object.geometry.rotateY( - Math.PI );
  90. object.castShadow = true;
  91. }
  92. } );
  93. boomBox.add( positionalAudio );
  94. scene.add( boomBox );
  95. animate();
  96. } );
  97. // sound is damped behind this wall
  98. var wallGeometry = new THREE.BoxBufferGeometry( 2, 1, 0.1 );
  99. var wallMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, transparent: true, opacity: 0.5 } );
  100. var wall = new THREE.Mesh( wallGeometry, wallMaterial );
  101. wall.position.set( 0, 0.5, - 0.5 );
  102. scene.add( wall );
  103. //
  104. renderer = new THREE.WebGLRenderer( { antialias: true } );
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.outputEncoding = THREE.sRGBEncoding;
  108. renderer.shadowMap.enabled = true;
  109. container.appendChild( renderer.domElement );
  110. //
  111. var controls = new OrbitControls( camera, renderer.domElement );
  112. controls.target.set( 0, 0.1, 0 );
  113. controls.update();
  114. controls.minDistance = 0.5;
  115. controls.maxDistance = 10;
  116. controls.maxPolarAngle = 0.5 * Math.PI;
  117. //
  118. window.addEventListener( 'resize', onWindowResize, false );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>