webaudio_timing.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webaudio - timing</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. <div id="overlay">
  11. <button id="startButton">Play</button>
  12. </div>
  13. <div id="container"></div>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio - timing<br/>
  16. sound effect by <a href="https://freesound.org/people/michorvath/sounds/269718/" target="_blank" rel="noopener noreferrer">michorvath</a>
  17. </div>
  18. <!-- Import maps polyfill -->
  19. <!-- Remove this when import maps will be widely supported -->
  20. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three';
  30. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  31. let scene, camera, renderer, clock;
  32. const objects = [];
  33. const speed = 2.5;
  34. const height = 3;
  35. const offset = 0.5;
  36. const startButton = document.getElementById( 'startButton' );
  37. startButton.addEventListener( 'click', init );
  38. function init() {
  39. const overlay = document.getElementById( 'overlay' );
  40. overlay.remove();
  41. const container = document.getElementById( 'container' );
  42. scene = new THREE.Scene();
  43. clock = new THREE.Clock();
  44. //
  45. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  46. camera.position.set( 7, 3, 7 );
  47. // lights
  48. const ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  49. scene.add( ambientLight );
  50. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.7 );
  51. directionalLight.position.set( 0, 5, 5 );
  52. scene.add( directionalLight );
  53. const d = 5;
  54. directionalLight.castShadow = true;
  55. directionalLight.shadow.camera.left = - d;
  56. directionalLight.shadow.camera.right = d;
  57. directionalLight.shadow.camera.top = d;
  58. directionalLight.shadow.camera.bottom = - d;
  59. directionalLight.shadow.camera.near = 1;
  60. directionalLight.shadow.camera.far = 20;
  61. directionalLight.shadow.mapSize.x = 1024;
  62. directionalLight.shadow.mapSize.y = 1024;
  63. // audio
  64. const audioLoader = new THREE.AudioLoader();
  65. const listener = new THREE.AudioListener();
  66. camera.add( listener );
  67. // floor
  68. const floorGeometry = new THREE.PlaneGeometry( 10, 10 );
  69. const floorMaterial = new THREE.MeshLambertMaterial( { color: 0x4676b6 } );
  70. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  71. floor.rotation.x = Math.PI * - 0.5;
  72. floor.receiveShadow = true;
  73. scene.add( floor );
  74. // objects
  75. const count = 5;
  76. const radius = 3;
  77. const ballGeometry = new THREE.SphereGeometry( 0.3, 32, 16 );
  78. ballGeometry.translate( 0, 0.3, 0 );
  79. const ballMaterial = new THREE.MeshLambertMaterial( { color: 0xcccccc } );
  80. // create objects when audio buffer is loaded
  81. audioLoader.load( 'sounds/ping_pong.mp3', function ( buffer ) {
  82. for ( let i = 0; i < count; i ++ ) {
  83. const s = i / count * Math.PI * 2;
  84. const ball = new THREE.Mesh( ballGeometry, ballMaterial );
  85. ball.castShadow = true;
  86. ball.userData.down = false;
  87. ball.position.x = radius * Math.cos( s );
  88. ball.position.z = radius * Math.sin( s );
  89. const audio = new THREE.PositionalAudio( listener );
  90. audio.setBuffer( buffer );
  91. ball.add( audio );
  92. scene.add( ball );
  93. objects.push( ball );
  94. }
  95. animate();
  96. } );
  97. //
  98. renderer = new THREE.WebGLRenderer( { antialias: true } );
  99. renderer.shadowMap.enabled = true;
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. renderer.setClearColor( 0x000000 );
  102. renderer.setPixelRatio( window.devicePixelRatio );
  103. container.appendChild( renderer.domElement );
  104. //
  105. const controls = new OrbitControls( camera, renderer.domElement );
  106. controls.minDistance = 1;
  107. controls.maxDistance = 25;
  108. //
  109. window.addEventListener( 'resize', onWindowResize );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. render();
  119. }
  120. function render() {
  121. const time = clock.getElapsedTime();
  122. for ( let i = 0; i < objects.length; i ++ ) {
  123. const ball = objects[ i ];
  124. const previousHeight = ball.position.y;
  125. ball.position.y = Math.abs( Math.sin( i * offset + ( time * speed ) ) * height );
  126. if ( ball.position.y < previousHeight ) {
  127. ball.userData.down = true;
  128. } else {
  129. if ( ball.userData.down === true ) {
  130. // ball changed direction from down to up
  131. const audio = ball.children[ 0 ];
  132. audio.play(); // play audio with perfect timing when ball hits the surface
  133. ball.userData.down = false;
  134. }
  135. }
  136. }
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>