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