webaudio_timing.html 4.8 KB

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