1
0

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