webaudio_timing.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. <div>
  12. <button id="startButton">Click to Play</button>
  13. <p>Audio playback requires user interaction.</p>
  14. </div>
  15. </div>
  16. <div id="container"></div>
  17. <div id="info">
  18. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio - timing<br/>
  19. sound effect by <a href="https://freesound.org/people/michorvath/sounds/269718/" target="_blank" rel="noopener noreferrer">michorvath</a>
  20. </div>
  21. <script type="module">
  22. import * as THREE from '../build/three.module.js';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. var scene, camera, renderer, clock;
  25. var objects = [];
  26. var speed = 2.5;
  27. var height = 3;
  28. var offset = 0.5;
  29. var startButton = document.getElementById( 'startButton' );
  30. startButton.addEventListener( 'click', init );
  31. function init() {
  32. var overlay = document.getElementById( 'overlay' );
  33. overlay.remove();
  34. var container = document.getElementById( 'container' );
  35. scene = new THREE.Scene();
  36. clock = new THREE.Clock();
  37. //
  38. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  39. camera.position.set( 7, 3, 7 );
  40. // lights
  41. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  42. scene.add( ambientLight );
  43. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.7 );
  44. directionalLight.position.set( 0, 5, 5 );
  45. scene.add( directionalLight );
  46. var d = 5;
  47. directionalLight.castShadow = true;
  48. directionalLight.shadow.camera.left = - d;
  49. directionalLight.shadow.camera.right = d;
  50. directionalLight.shadow.camera.top = d;
  51. directionalLight.shadow.camera.bottom = - d;
  52. directionalLight.shadow.camera.near = 1;
  53. directionalLight.shadow.camera.far = 20;
  54. directionalLight.shadow.mapSize.x = 1024;
  55. directionalLight.shadow.mapSize.y = 1024;
  56. // audio
  57. var audioLoader = new THREE.AudioLoader();
  58. var listener = new THREE.AudioListener();
  59. camera.add( listener );
  60. // floor
  61. var floorGeometry = new THREE.PlaneBufferGeometry( 10, 10 );
  62. var floorMaterial = new THREE.MeshLambertMaterial( { color: 0x4676b6 } );
  63. var floor = new THREE.Mesh( floorGeometry, floorMaterial );
  64. floor.rotation.x = Math.PI * - 0.5;
  65. floor.receiveShadow = true;
  66. scene.add( floor );
  67. // objects
  68. var count = 5;
  69. var radius = 3;
  70. var ballGeometry = new THREE.SphereBufferGeometry( 0.3, 32, 16 );
  71. ballGeometry.translate( 0, 0.3, 0 );
  72. var ballMaterial = new THREE.MeshLambertMaterial( { color: 0xcccccc } );
  73. // create objects when audio buffer is loaded
  74. audioLoader.load( 'sounds/ping_pong.mp3', function ( buffer ) {
  75. for ( var i = 0; i < count; i ++ ) {
  76. var s = i / count * Math.PI * 2;
  77. var ball = new THREE.Mesh( ballGeometry, ballMaterial );
  78. ball.castShadow = true;
  79. ball.userData.down = false;
  80. ball.position.x = radius * Math.cos( s );
  81. ball.position.z = radius * Math.sin( s );
  82. var audio = new THREE.PositionalAudio( listener );
  83. audio.setBuffer( buffer );
  84. ball.add( audio );
  85. scene.add( ball );
  86. objects.push( ball );
  87. }
  88. animate();
  89. } );
  90. //
  91. renderer = new THREE.WebGLRenderer( { antialias: true } );
  92. renderer.shadowMap.enabled = true;
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.setClearColor( 0x000000 );
  95. renderer.setPixelRatio( window.devicePixelRatio );
  96. container.appendChild( renderer.domElement );
  97. //
  98. var controls = new OrbitControls( camera, renderer.domElement );
  99. controls.minDistance = 1;
  100. controls.maxDistance = 25;
  101. //
  102. window.addEventListener( 'resize', onWindowResize, false );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. render();
  112. }
  113. function render() {
  114. var time = clock.getElapsedTime();
  115. for ( var i = 0; i < objects.length; i ++ ) {
  116. var ball = objects[ i ];
  117. var 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. var 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>