misc_sound.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js misc - sound</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. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. font-weight: bold;
  16. text-align:center;
  17. }
  18. a {
  19. color:#0078ff;
  20. }
  21. #info {
  22. color:#fff;
  23. position: absolute;
  24. top: 0px; width: 100%;
  25. padding: 5px;
  26. z-index:100;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank">three.js</a> - webgl 3d sounds example -
  33. music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank">larrylarrybb</a> and
  34. <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank">skullbeatz</a> <br/><br/>
  35. navigate with WASD / arrows / mouse
  36. </div>
  37. <div id="container"></div>
  38. <script src="../build/three.min.js"></script>
  39. <script src="js/controls/FirstPersonControls.js"></script>
  40. <script src="js/Detector.js"></script>
  41. <script>
  42. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  43. var container;
  44. var camera, controls, scene, renderer;
  45. var light, pointLight;
  46. var mesh;
  47. var material_sphere1, material_sphere2;
  48. var sound1, sound2;
  49. var clock = new THREE.Clock();
  50. var Sound = function ( sources, radius, volume ) {
  51. var audio = document.createElement( 'audio' );
  52. for ( var i = 0; i < sources.length; i ++ ) {
  53. var source = document.createElement( 'source' );
  54. source.src = sources[ i ];
  55. audio.appendChild( source );
  56. }
  57. this.position = new THREE.Vector3();
  58. this.play = function () {
  59. audio.play();
  60. }
  61. this.update = function ( camera ) {
  62. var distance = this.position.distanceTo( camera.position );
  63. if ( distance <= radius ) {
  64. audio.volume = volume * ( 1 - distance / radius );
  65. } else {
  66. audio.volume = 0;
  67. }
  68. }
  69. }
  70. init();
  71. animate();
  72. function init() {
  73. container = document.getElementById( 'container' );
  74. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  75. camera.position.set( 0, 25, 0 );
  76. controls = new THREE.FirstPersonControls( camera );
  77. controls.movementSpeed = 70;
  78. controls.lookSpeed = 0.05;
  79. controls.noFly = true;
  80. controls.lookVertical = false;
  81. scene = new THREE.Scene();
  82. scene.fog = new THREE.FogExp2( 0x000000, 0.0035 );
  83. light = new THREE.DirectionalLight( 0xffffff );
  84. light.position.set( 0, 0.5, 1 ).normalize();
  85. scene.add( light );
  86. var sphere = new THREE.SphereGeometry( 20, 32, 16 );
  87. material_sphere1 = new THREE.MeshLambertMaterial( { color: 0xffaa00, shading: THREE.FlatShading } );
  88. material_sphere2 = new THREE.MeshLambertMaterial( { color: 0xff2200, shading: THREE.FlatShading } );
  89. // sound spheres
  90. var s = 1;
  91. var mesh1 = new THREE.Mesh( sphere, material_sphere1 );
  92. mesh1.position.set( -250, 30, 0 );
  93. mesh1.scale.set( s, s, s );
  94. scene.add( mesh1 );
  95. sound1 = new Sound( [ 'sounds/358232_j_s_song.mp3', 'sounds/358232_j_s_song.ogg' ], 275, 1 );
  96. sound1.position.copy( mesh1.position );
  97. sound1.play();
  98. //
  99. var mesh2 = new THREE.Mesh( sphere, material_sphere2 );
  100. mesh2.position.set( 250, 30, 0 );
  101. mesh2.scale.set( s, s, s );
  102. scene.add( mesh2 );
  103. sound2 = new Sound( [ 'sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3', 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg' ], 275, 1 );
  104. sound2.position.copy( mesh2.position );
  105. sound2.play();
  106. // ground
  107. var material = new THREE.MeshLambertMaterial( { color: 0x7f7566, wireframe: true, wireframeLinewidth: 1 } );
  108. mesh = new THREE.Mesh( new THREE.PlaneGeometry( 1000, 1000, 100, 100 ), material );
  109. mesh.position.y = 0.1;
  110. mesh.rotation.x = - Math.PI / 2;
  111. scene.add( mesh );
  112. //
  113. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: true } );
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. container.innerHTML = "";
  116. container.appendChild( renderer.domElement );
  117. //
  118. window.addEventListener( 'resize', onWindowResize, false );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. controls.handleResize();
  125. }
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. }
  130. function render() {
  131. var delta = clock.getDelta(),
  132. time = clock.getElapsedTime() * 5;
  133. controls.update( delta );
  134. material_sphere1.color.setHSL( 0.0, 0.3 + 0.7 * ( 1 + Math.cos( time ) ) / 2, 0.5 );
  135. material_sphere2.color.setHSL( 0.1, 0.3 + 0.7 * ( 1 + Math.sin( time ) ) / 2, 0.5 );
  136. renderer.render( scene, camera );
  137. sound1.update( camera );
  138. sound2.update( camera );
  139. }
  140. </script>
  141. </body>
  142. </html>