1
0

misc_sound.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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" src="js/Stats.js"></script>
  41. <script type="text/javascript">
  42. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  43. var container, stats;
  44. var camera, scene, renderer, soundRenderer;
  45. var light, pointLight;
  46. var cameraControl;
  47. var mesh;
  48. var material_sphere1, material_sphere2;
  49. var postprocessing = { enabled: true };
  50. init();
  51. animate();
  52. function init() {
  53. container = document.getElementById( 'container' );
  54. scene = new THREE.Scene();
  55. scene.fog = new THREE.FogExp2( 0x000000, 0.0035 );
  56. camera = new THREE.QuakeCamera( { fov: 50, aspect: window.innerWidth / window.innerHeight, near: 1, far: 10000,
  57. movement_speed: 1, look_speed: 0.002, nofly: true, look_vertical: false } );
  58. camera.position.set( 0, 25, 0 );
  59. light = new THREE.DirectionalLight( 0xffffff );
  60. light.position.set( 0, 0.5, 1 );
  61. light.position.normalize();
  62. scene.addLight( light );
  63. var sphere = new Sphere( 20, 32, 16 );
  64. material_sphere1 = new THREE.MeshLambertMaterial( { color: 0xffaa00, shading: THREE.FlatShading } );
  65. material_sphere2 = new THREE.MeshLambertMaterial( { color: 0xff2200, shading: THREE.FlatShading } );
  66. var cube = new Cube( 5, 40, 5 );
  67. var material_cube = new THREE.MeshLambertMaterial( { color: 0xffff00, shading: THREE.FlatShading } );
  68. material_cube.color.setHSV( 0.1, 0.7, 1 );
  69. // sound spheres
  70. var s = 1;
  71. var mesh1 = new THREE.Mesh( sphere, material_sphere1 );
  72. mesh1.position.set( -250, 30, 0 );
  73. mesh1.scale.set( s, s, s );
  74. var sound1 = new THREE.Sound3D( [ "sounds/358232_j_s_song.mp3", "sounds/358232_j_s_song.ogg" ] , 275, 20, true );
  75. //var sound1 = new THREE.Sound3D( "sounds/358232_j_s_song.ogg", 275, 20, true );
  76. sound1.play();
  77. var dbg = new THREE.Mesh( cube, material_cube );
  78. dbg.position.set( 0, -10 ,0 );
  79. mesh1.addChild( dbg );
  80. mesh1.addChild( sound1 );
  81. scene.addObject( mesh1 );
  82. var mesh2 = new THREE.Mesh( sphere, material_sphere2 );
  83. mesh2.position.set( 250, 30, 0 );
  84. mesh2.scale.set( s, s, s );
  85. var sound2 = new THREE.Sound3D( [ "sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3", "sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" ], 275, 20, true );
  86. //var sound2 = new THREE.Sound3D( "sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg", 275, 20, true );
  87. sound2.play();
  88. var dbg = new THREE.Mesh( cube, material_cube );
  89. dbg.position.set( 0, -10, 0 );
  90. mesh2.addChild( dbg );
  91. mesh2.addChild( sound2 );
  92. scene.addObject( mesh2 );
  93. // ground
  94. var material_wireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, wireframe_linewidth: 1 } );
  95. material_wireframe.color.setHSV( 0.1, 0.2, 0.25 );
  96. var plane = new Plane( 1000, 1000, 100, 100 );
  97. mesh = new THREE.Mesh( plane, material_wireframe );
  98. mesh.position.y = 0.1;
  99. mesh.rotation.x = -1.57;
  100. scene.addObject( mesh );
  101. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1 } );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. soundRenderer = new THREE.SoundRenderer();
  104. container.innerHTML = "";
  105. container.appendChild( renderer.domElement );
  106. container.appendChild( soundRenderer.domElement );
  107. stats = new Stats();
  108. stats.domElement.style.position = 'absolute';
  109. stats.domElement.style.top = '0px';
  110. //container.appendChild( stats.domElement );
  111. initPostprocessing();
  112. renderer.autoClear = false;
  113. }
  114. function initPostprocessing() {
  115. postprocessing.scene = new THREE.Scene();
  116. postprocessing.camera = new THREE.Camera();
  117. postprocessing.camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  118. postprocessing.camera.position.z = 100;
  119. var pars = { min_filter: THREE.LinearFilter, mag_filter: THREE.LinearFilter };
  120. postprocessing.rtTexture1 = new THREE.RenderTarget( window.innerWidth, window.innerHeight, pars );
  121. postprocessing.rtTexture2 = new THREE.RenderTarget( 512, 512, pars );
  122. postprocessing.rtTexture3 = new THREE.RenderTarget( 512, 512, pars );
  123. var screen_shader = ShaderUtils.lib["screen"];
  124. var screen_uniforms = Uniforms.clone( screen_shader.uniforms );
  125. screen_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  126. screen_uniforms["opacity"].value = 1.0;
  127. postprocessing.materialScreen = new THREE.MeshShaderMaterial( {
  128. uniforms: screen_uniforms,
  129. vertex_shader: screen_shader.vertex_shader,
  130. fragment_shader: screen_shader.fragment_shader,
  131. blending: THREE.AdditiveBlending
  132. } );
  133. var convolution_shader = ShaderUtils.lib["convolution"];
  134. var convolution_uniforms = Uniforms.clone( convolution_shader.uniforms );
  135. postprocessing.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  136. postprocessing.blury = new THREE.Vector2( 0.0, 0.001953125 );
  137. convolution_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  138. convolution_uniforms["uImageIncrement"].value = postprocessing.blurx;
  139. convolution_uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
  140. postprocessing.materialConvolution = new THREE.MeshShaderMaterial( {
  141. uniforms: convolution_uniforms,
  142. vertex_shader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertex_shader,
  143. fragment_shader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragment_shader
  144. } );
  145. postprocessing.quad = new THREE.Mesh( new Plane( window.innerWidth, window.innerHeight ), postprocessing.materialConvolution );
  146. postprocessing.quad.position.z = -500;
  147. postprocessing.scene.addObject( postprocessing.quad );
  148. }
  149. function animate() {
  150. requestAnimationFrame( animate );
  151. render();
  152. stats.update();
  153. }
  154. function render() {
  155. var time = new Date().getTime() * 0.005;
  156. material_sphere1.color.setHSV( 0.0, 0.3 + 0.7 * ( 1 + Math.cos(time) ) / 2, 1 );
  157. material_sphere2.color.setHSV( 0.1, 0.3 + 0.7 * ( 1 + Math.sin(time) ) / 2, 1 );
  158. if ( postprocessing.enabled ) {
  159. renderer.clear();
  160. // Render scene into texture
  161. renderer.render( scene, camera, postprocessing.rtTexture1 );
  162. // Render quad with blured scene into texture (convolution pass 1)
  163. postprocessing.quad.materials = [ postprocessing.materialConvolution ];
  164. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  165. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
  166. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2 );
  167. // Render quad with blured scene into texture (convolution pass 2)
  168. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
  169. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
  170. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3 );
  171. // Render original scene with superimposed blur to texture
  172. postprocessing.quad.materials = [ postprocessing.materialScreen ];
  173. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
  174. postprocessing.materialScreen.uniforms.opacity.value = 1.3;
  175. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
  176. // Render to screen
  177. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  178. renderer.render( postprocessing.scene, postprocessing.camera );
  179. } else {
  180. renderer.clear();
  181. renderer.render( scene, camera );
  182. }
  183. soundRenderer.render( scene, camera );
  184. }
  185. </script>
  186. </body>
  187. </html>
粤ICP备19079148号