misc_sound.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js misc - sound</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background-color: #000000;
  9. margin: 0px;
  10. overflow: hidden;
  11. font-family:Monospace;
  12. font-size:13px;
  13. text-align:center;
  14. font-weight: bold;
  15. text-align:center;
  16. }
  17. a {
  18. color:#0078ff;
  19. }
  20. #info {
  21. color:#fff;
  22. position: absolute;
  23. top: 0px; width: 100%;
  24. padding: 5px;
  25. z-index:100;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl 3d sounds example -
  32. music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank">larrylarrybb</a> and
  33. <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank">skullbeatz</a> <br/><br/>
  34. navigate with WASD / arrows / mouse
  35. </div>
  36. <div id="container"></div>
  37. <script type="text/javascript" src="../build/Three.js"></script>
  38. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  39. <script type="text/javascript" src="js/Detector.js"></script>
  40. <script type="text/javascript">
  41. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  42. var container;
  43. var camera, scene, renderer;
  44. var light, pointLight;
  45. var mesh;
  46. var material_sphere1, material_sphere2;
  47. var sound1, sound2;
  48. var Sound = function ( sources, radius, volume ) {
  49. var audio = document.createElement( 'audio' );
  50. for ( var i = 0; i < sources.length; i ++ ) {
  51. var source = document.createElement( 'source' );
  52. source.src = sources[ i ];
  53. audio.appendChild( source );
  54. }
  55. this.position = new THREE.Vector3();
  56. this.play = function () {
  57. audio.play();
  58. }
  59. this.update = function ( camera ) {
  60. var distance = this.position.distanceTo( camera.position );
  61. if ( distance <= radius ) {
  62. audio.volume = volume * ( 1 - distance / radius );
  63. } else {
  64. audio.volume = 0;
  65. }
  66. }
  67. }
  68. init();
  69. animate();
  70. function init() {
  71. container = document.getElementById( 'container' );
  72. scene = new THREE.Scene();
  73. scene.fog = new THREE.FogExp2( 0x000000, 0.0035 );
  74. camera = new THREE.FirstPersonCamera( {
  75. fov: 50, aspect: window.innerWidth / window.innerHeight, near: 1, far: 10000,
  76. movementSpeed: 70, lookSpeed: 0.05, noFly: true, lookVertical: false
  77. } );
  78. camera.position.set( 0, 25, 0 );
  79. light = new THREE.DirectionalLight( 0xffffff );
  80. light.position.set( 0, 0.5, 1 );
  81. light.position.normalize();
  82. scene.add( light );
  83. var sphere = new THREE.SphereGeometry( 20, 32, 16 );
  84. material_sphere1 = new THREE.MeshLambertMaterial( { color: 0xffaa00, shading: THREE.FlatShading } );
  85. material_sphere2 = new THREE.MeshLambertMaterial( { color: 0xff2200, shading: THREE.FlatShading } );
  86. var cube = new THREE.CubeGeometry( 5, 40, 5 );
  87. var material_cube = new THREE.MeshLambertMaterial( { color: 0xffff00, shading: THREE.FlatShading } );
  88. material_cube.color.setHSV( 0.1, 0.7, 1 );
  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_wireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, wireframeLinewidth: 1 } );
  108. material_wireframe.color.setHSV( 0.1, 0.2, 0.25 );
  109. var plane = new THREE.PlaneGeometry( 1000, 1000, 100, 100 );
  110. mesh = new THREE.Mesh( plane, material_wireframe );
  111. mesh.position.y = 0.1;
  112. mesh.rotation.x = -1.57;
  113. scene.add( mesh );
  114. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: false } );
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. container.innerHTML = "";
  117. container.appendChild( renderer.domElement );
  118. renderer.autoClear = false;
  119. }
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. render();
  123. }
  124. function render() {
  125. var time = new Date().getTime() * 0.005;
  126. material_sphere1.color.setHSV( 0.0, 0.3 + 0.7 * ( 1 + Math.cos(time) ) / 2, 1 );
  127. material_sphere2.color.setHSV( 0.1, 0.3 + 0.7 * ( 1 + Math.sin(time) ) / 2, 1 );
  128. renderer.clear();
  129. renderer.render( scene, camera );
  130. sound1.update( camera );
  131. sound2.update( camera );
  132. }
  133. </script>
  134. </body>
  135. </html>