webaudio_orientation.html 5.1 KB

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