webaudio_sandbox.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webaudio - sandbox</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. <audio id="song" preload="auto" style="display: none">
  11. <source src="sounds/358232_j_s_song.ogg" type="audio/ogg">
  12. <source src="sounds/358232_j_s_song.mp3" type="audio/mpeg">
  13. </audio>
  14. <audio id="skullbeatz" preload="auto" style="display: none">
  15. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" type="audio/ogg">
  16. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3" type="audio/mpeg">
  17. </audio>
  18. <audio id="utopia" loop preload="auto" style="display: none">
  19. <source src="sounds/Project_Utopia.ogg" type="audio/ogg">
  20. <source src="sounds/Project_Utopia.mp3" type="audio/mpeg">
  21. </audio>
  22. <div id="overlay">
  23. <button id="startButton">Play</button>
  24. </div>
  25. <div id="info">
  26. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webaudio - sandbox<br/>
  27. music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank" rel="noopener">larrylarrybb</a>,
  28. <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener">skullbeatz</a> and
  29. <a href="http://opengameart.org/content/project-utopia-seamless-loop" target="_blank" rel="noopener">congusbongus</a><br/><br/>
  30. navigate with WASD / arrows / mouse
  31. </div>
  32. <script type="importmap">
  33. {
  34. "imports": {
  35. "three": "../build/three.module.js",
  36. "three/addons/": "./jsm/"
  37. }
  38. }
  39. </script>
  40. <script type="module">
  41. import * as THREE from 'three';
  42. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  43. import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
  44. let camera, controls, scene, renderer, light;
  45. let material1, material2, material3;
  46. let analyser1, analyser2, analyser3;
  47. const timer = new THREE.Timer();
  48. timer.connect( document );
  49. const startButton = document.getElementById( 'startButton' );
  50. startButton.addEventListener( 'click', init );
  51. function init() {
  52. const overlay = document.getElementById( 'overlay' );
  53. overlay.remove();
  54. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  55. camera.position.set( 0, 25, 0 );
  56. const listener = new THREE.AudioListener();
  57. camera.add( listener );
  58. scene = new THREE.Scene();
  59. scene.fog = new THREE.FogExp2( 0x000000, 0.0025 );
  60. light = new THREE.DirectionalLight( 0xffffff, 3 );
  61. light.position.set( 0, 0.5, 1 ).normalize();
  62. scene.add( light );
  63. const sphere = new THREE.SphereGeometry( 20, 32, 16 );
  64. material1 = new THREE.MeshPhongMaterial( { color: 0xffaa00, flatShading: true, shininess: 0 } );
  65. material2 = new THREE.MeshPhongMaterial( { color: 0xff2200, flatShading: true, shininess: 0 } );
  66. material3 = new THREE.MeshPhongMaterial( { color: 0x6622aa, flatShading: true, shininess: 0 } );
  67. // sound spheres
  68. const mesh1 = new THREE.Mesh( sphere, material1 );
  69. mesh1.position.set( - 250, 30, 0 );
  70. scene.add( mesh1 );
  71. const sound1 = new THREE.PositionalAudio( listener );
  72. const songElement = document.getElementById( 'song' );
  73. sound1.setMediaElementSource( songElement );
  74. sound1.setRefDistance( 20 );
  75. songElement.play();
  76. mesh1.add( sound1 );
  77. //
  78. const mesh2 = new THREE.Mesh( sphere, material2 );
  79. mesh2.position.set( 250, 30, 0 );
  80. scene.add( mesh2 );
  81. const sound2 = new THREE.PositionalAudio( listener );
  82. const skullbeatzElement = document.getElementById( 'skullbeatz' );
  83. sound2.setMediaElementSource( skullbeatzElement );
  84. sound2.setRefDistance( 20 );
  85. skullbeatzElement.play();
  86. mesh2.add( sound2 );
  87. //
  88. const mesh3 = new THREE.Mesh( sphere, material3 );
  89. mesh3.position.set( 0, 30, - 250 );
  90. scene.add( mesh3 );
  91. const sound3 = new THREE.PositionalAudio( listener );
  92. const oscillator = listener.context.createOscillator();
  93. oscillator.type = 'sine';
  94. oscillator.frequency.setValueAtTime( 144, sound3.context.currentTime );
  95. oscillator.start( 0 );
  96. sound3.setNodeSource( oscillator );
  97. sound3.setRefDistance( 20 );
  98. sound3.setVolume( 0.5 );
  99. mesh3.add( sound3 );
  100. // analysers
  101. analyser1 = new THREE.AudioAnalyser( sound1, 32 );
  102. analyser2 = new THREE.AudioAnalyser( sound2, 32 );
  103. analyser3 = new THREE.AudioAnalyser( sound3, 32 );
  104. // global ambient audio
  105. const sound4 = new THREE.Audio( listener );
  106. const utopiaElement = document.getElementById( 'utopia' );
  107. sound4.setMediaElementSource( utopiaElement );
  108. sound4.setVolume( 0.5 );
  109. utopiaElement.play();
  110. // ground
  111. const helper = new THREE.GridHelper( 1000, 10, 0x444444, 0x444444 );
  112. helper.position.y = 0.1;
  113. scene.add( helper );
  114. //
  115. const SoundControls = function () {
  116. this.master = listener.getMasterVolume();
  117. this.firstSphere = sound1.getVolume();
  118. this.secondSphere = sound2.getVolume();
  119. this.thirdSphere = sound3.getVolume();
  120. this.Ambient = sound4.getVolume();
  121. };
  122. const GeneratorControls = function () {
  123. this.frequency = oscillator.frequency.value;
  124. this.wavetype = oscillator.type;
  125. };
  126. const gui = new GUI();
  127. const soundControls = new SoundControls();
  128. const generatorControls = new GeneratorControls();
  129. const volumeFolder = gui.addFolder( 'sound volume' );
  130. const generatorFolder = gui.addFolder( 'sound generator' );
  131. volumeFolder.add( soundControls, 'master' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  132. listener.setMasterVolume( soundControls.master );
  133. } );
  134. volumeFolder.add( soundControls, 'firstSphere' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  135. sound1.setVolume( soundControls.firstSphere );
  136. } );
  137. volumeFolder.add( soundControls, 'secondSphere' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  138. sound2.setVolume( soundControls.secondSphere );
  139. } );
  140. volumeFolder.add( soundControls, 'thirdSphere' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  141. sound3.setVolume( soundControls.thirdSphere );
  142. } );
  143. volumeFolder.add( soundControls, 'Ambient' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( function () {
  144. sound4.setVolume( soundControls.Ambient );
  145. } );
  146. volumeFolder.open();
  147. generatorFolder.add( generatorControls, 'frequency' ).min( 50.0 ).max( 5000.0 ).step( 1.0 ).onChange( function () {
  148. oscillator.frequency.setValueAtTime( generatorControls.frequency, listener.context.currentTime );
  149. } );
  150. generatorFolder.add( generatorControls, 'wavetype', [ 'sine', 'square', 'sawtooth', 'triangle' ] ).onChange( function () {
  151. oscillator.type = generatorControls.wavetype;
  152. } );
  153. generatorFolder.open();
  154. //
  155. renderer = new THREE.WebGLRenderer( { antialias: true } );
  156. renderer.setPixelRatio( window.devicePixelRatio );
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. renderer.setAnimationLoop( animate );
  159. document.body.appendChild( renderer.domElement );
  160. //
  161. controls = new FirstPersonControls( camera, renderer.domElement );
  162. controls.movementSpeed = 70;
  163. controls.lookSpeed = 0.05;
  164. controls.lookVertical = false;
  165. //
  166. window.addEventListener( 'resize', onWindowResize );
  167. }
  168. function onWindowResize() {
  169. camera.aspect = window.innerWidth / window.innerHeight;
  170. camera.updateProjectionMatrix();
  171. renderer.setSize( window.innerWidth, window.innerHeight );
  172. controls.handleResize();
  173. }
  174. function animate() {
  175. timer.update();
  176. const delta = timer.getDelta();
  177. controls.update( delta );
  178. material1.emissive.b = analyser1.getAverageFrequency() / 256;
  179. material2.emissive.b = analyser2.getAverageFrequency() / 256;
  180. material3.emissive.b = analyser3.getAverageFrequency() / 256;
  181. renderer.render( scene, camera );
  182. }
  183. </script>
  184. </body>
  185. </html>
粤ICP备19079148号