misc_sound.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, 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.FirstPersonCamera( {
  76. fov: 50, aspect: window.innerWidth / window.innerHeight, near: 1, far: 10000,
  77. movementSpeed: 70, lookSpeed: 0.05, noFly: true, lookVertical: false
  78. } );
  79. camera.position.set( 0, 25, 0 );
  80. light = new THREE.DirectionalLight( 0xffffff );
  81. light.position.set( 0, 0.5, 1 );
  82. light.position.normalize();
  83. scene.add( light );
  84. var sphere = new THREE.SphereGeometry( 20, 32, 16 );
  85. material_sphere1 = new THREE.MeshLambertMaterial( { color: 0xffaa00, shading: THREE.FlatShading } );
  86. material_sphere2 = new THREE.MeshLambertMaterial( { color: 0xff2200, shading: THREE.FlatShading } );
  87. var cube = new THREE.CubeGeometry( 5, 40, 5 );
  88. var material_cube = new THREE.MeshLambertMaterial( { color: 0xffff00, shading: THREE.FlatShading } );
  89. material_cube.color.setHSV( 0.1, 0.7, 1 );
  90. // sound spheres
  91. var s = 1;
  92. var mesh1 = new THREE.Mesh( sphere, material_sphere1 );
  93. mesh1.position.set( -250, 30, 0 );
  94. mesh1.scale.set( s, s, s );
  95. scene.add( mesh1 );
  96. sound1 = new Sound( [ 'sounds/358232_j_s_song.mp3', 'sounds/358232_j_s_song.ogg' ], 275, 1 );
  97. sound1.position.copy( mesh1.position );
  98. sound1.play();
  99. //
  100. var mesh2 = new THREE.Mesh( sphere, material_sphere2 );
  101. mesh2.position.set( 250, 30, 0 );
  102. mesh2.scale.set( s, s, s );
  103. scene.add( mesh2 );
  104. sound2 = new Sound( [ 'sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3', 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg' ], 275, 1 );
  105. sound2.position.copy( mesh2.position );
  106. sound2.play();
  107. // ground
  108. var material_wireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, wireframeLinewidth: 1 } );
  109. material_wireframe.color.setHSV( 0.1, 0.2, 0.25 );
  110. var plane = new THREE.PlaneGeometry( 1000, 1000, 100, 100 );
  111. mesh = new THREE.Mesh( plane, material_wireframe );
  112. mesh.position.y = 0.1;
  113. mesh.rotation.x = -1.57;
  114. scene.add( mesh );
  115. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: false } );
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. container.innerHTML = "";
  118. container.appendChild( renderer.domElement );
  119. renderer.autoClear = false;
  120. }
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. render();
  124. }
  125. function render() {
  126. var time = new Date().getTime() * 0.005;
  127. material_sphere1.color.setHSV( 0.0, 0.3 + 0.7 * ( 1 + Math.cos(time) ) / 2, 1 );
  128. material_sphere2.color.setHSV( 0.1, 0.3 + 0.7 * ( 1 + Math.sin(time) ) / 2, 1 );
  129. renderer.clear();
  130. renderer.render( scene, camera );
  131. sound1.update( camera );
  132. sound2.update( camera );
  133. }
  134. </script>
  135. </body>
  136. </html>