misc_sound.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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://github.com/mrdoob/three.js" 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.js"></script>
  39. <script src="js/RequestAnimationFrame.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 Sound = function ( sources, radius, volume ) {
  50. var audio = document.createElement( 'audio' );
  51. for ( var i = 0; i < sources.length; i ++ ) {
  52. var source = document.createElement( 'source' );
  53. source.src = sources[ i ];
  54. audio.appendChild( source );
  55. }
  56. this.position = new THREE.Vector3();
  57. this.play = function () {
  58. audio.play();
  59. }
  60. this.update = function ( camera ) {
  61. var distance = this.position.distanceTo( camera.position );
  62. if ( distance <= radius ) {
  63. audio.volume = volume * ( 1 - distance / radius );
  64. } else {
  65. audio.volume = 0;
  66. }
  67. }
  68. }
  69. init();
  70. animate();
  71. function init() {
  72. container = document.getElementById( 'container' );
  73. scene = new THREE.Scene();
  74. scene.fog = new THREE.FogExp2( 0x000000, 0.0035 );
  75. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  76. camera.position.set( 0, 25, 0 );
  77. controls = new THREE.FirstPersonControls( camera );
  78. controls.movementSpeed = 70;
  79. controls.lookSpeed = 0.05;
  80. controls.noFly = true;
  81. controls.lookVertical = false;
  82. light = new THREE.DirectionalLight( 0xffffff );
  83. light.position.set( 0, 0.5, 1 ).normalize();
  84. scene.add( light );
  85. var sphere = new THREE.SphereGeometry( 20, 32, 16 );
  86. material_sphere1 = new THREE.MeshLambertMaterial( { color: 0xffaa00, shading: THREE.FlatShading } );
  87. material_sphere2 = new THREE.MeshLambertMaterial( { color: 0xff2200, shading: THREE.FlatShading } );
  88. var cube = new THREE.CubeGeometry( 5, 40, 5 );
  89. var material_cube = new THREE.MeshLambertMaterial( { color: 0xffff00, shading: THREE.FlatShading } );
  90. material_cube.color.setHSV( 0.1, 0.7, 1 );
  91. // sound spheres
  92. var s = 1;
  93. var mesh1 = new THREE.Mesh( sphere, material_sphere1 );
  94. mesh1.position.set( -250, 30, 0 );
  95. mesh1.scale.set( s, s, s );
  96. scene.add( mesh1 );
  97. sound1 = new Sound( [ 'sounds/358232_j_s_song.mp3', 'sounds/358232_j_s_song.ogg' ], 275, 1 );
  98. sound1.position.copy( mesh1.position );
  99. sound1.play();
  100. //
  101. var mesh2 = new THREE.Mesh( sphere, material_sphere2 );
  102. mesh2.position.set( 250, 30, 0 );
  103. mesh2.scale.set( s, s, s );
  104. scene.add( mesh2 );
  105. sound2 = new Sound( [ 'sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3', 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg' ], 275, 1 );
  106. sound2.position.copy( mesh2.position );
  107. sound2.play();
  108. // ground
  109. var material_wireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, wireframeLinewidth: 1 } );
  110. material_wireframe.color.setHSV( 0.1, 0.2, 0.25 );
  111. var plane = new THREE.PlaneGeometry( 1000, 1000, 100, 100 );
  112. mesh = new THREE.Mesh( plane, material_wireframe );
  113. mesh.position.y = 0.1;
  114. mesh.rotation.x = -Math.PI/2;
  115. scene.add( mesh );
  116. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: false } );
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. container.innerHTML = "";
  119. container.appendChild( renderer.domElement );
  120. renderer.autoClear = false;
  121. }
  122. function animate() {
  123. requestAnimationFrame( animate );
  124. render();
  125. }
  126. function render() {
  127. var time = new Date().getTime() * 0.005;
  128. controls.update();
  129. material_sphere1.color.setHSV( 0.0, 0.3 + 0.7 * ( 1 + Math.cos(time) ) / 2, 1 );
  130. material_sphere2.color.setHSV( 0.1, 0.3 + 0.7 * ( 1 + Math.sin(time) ) / 2, 1 );
  131. renderer.clear();
  132. renderer.render( scene, camera );
  133. sound1.update( camera );
  134. sound2.update( camera );
  135. }
  136. </script>
  137. </body>
  138. </html>