webaudio_orientation.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webaudio - orientation</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <audio loop id="music" preload="auto" style="display: none">
  11. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg" type="audio/ogg">
  12. <source src="sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3" type="audio/mpeg">
  13. </audio>
  14. <div id="overlay">
  15. <button id="startButton">Play</button>
  16. </div>
  17. <div id="container"></div>
  18. <div id="info">
  19. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio - orientation<br/>
  20. music by <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener noreferrer">skullbeatz</a>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { PositionalAudioHelper } from 'three/addons/helpers/PositionalAudioHelper.js';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. let scene, camera, renderer;
  36. const startButton = document.getElementById( 'startButton' );
  37. startButton.addEventListener( 'click', init );
  38. function init() {
  39. const overlay = document.getElementById( 'overlay' );
  40. overlay.remove();
  41. const container = document.getElementById( 'container' );
  42. //
  43. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  44. camera.position.set( 3, 2, 3 );
  45. const reflectionCube = new THREE.CubeTextureLoader()
  46. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  47. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0xa0a0a0 );
  50. scene.fog = new THREE.Fog( 0xa0a0a0, 2, 20 );
  51. //
  52. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
  53. hemiLight.position.set( 0, 20, 0 );
  54. scene.add( hemiLight );
  55. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  56. dirLight.position.set( 5, 5, 0 );
  57. dirLight.castShadow = true;
  58. dirLight.shadow.camera.top = 1;
  59. dirLight.shadow.camera.bottom = - 1;
  60. dirLight.shadow.camera.left = - 1;
  61. dirLight.shadow.camera.right = 1;
  62. dirLight.shadow.camera.near = 0.1;
  63. dirLight.shadow.camera.far = 20;
  64. scene.add( dirLight );
  65. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  66. //
  67. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 50, 50 ), new THREE.MeshPhongMaterial( { color: 0xcbcbcb, depthWrite: false } ) );
  68. mesh.rotation.x = - Math.PI / 2;
  69. mesh.receiveShadow = true;
  70. scene.add( mesh );
  71. const grid = new THREE.GridHelper( 50, 50, 0xc1c1c1, 0xc1c1c1 );
  72. scene.add( grid );
  73. //
  74. const listener = new THREE.AudioListener();
  75. camera.add( listener );
  76. const audioElement = document.getElementById( 'music' );
  77. audioElement.play();
  78. const positionalAudio = new THREE.PositionalAudio( listener );
  79. positionalAudio.setMediaElementSource( audioElement );
  80. positionalAudio.setRefDistance( 1 );
  81. positionalAudio.setDirectionalCone( 180, 230, 0.1 );
  82. const helper = new PositionalAudioHelper( positionalAudio, 0.1 );
  83. positionalAudio.add( helper );
  84. //
  85. const gltfLoader = new GLTFLoader();
  86. gltfLoader.load( 'models/gltf/BoomBox.glb', function ( gltf ) {
  87. const boomBox = gltf.scene;
  88. boomBox.position.set( 0, 0.2, 0 );
  89. boomBox.scale.set( 20, 20, 20 );
  90. boomBox.traverse( function ( object ) {
  91. if ( object.isMesh ) {
  92. object.material.envMap = reflectionCube;
  93. object.geometry.rotateY( - Math.PI );
  94. object.castShadow = true;
  95. }
  96. } );
  97. boomBox.add( positionalAudio );
  98. scene.add( boomBox );
  99. renderer.setAnimationLoop( animate );
  100. } );
  101. // sound is damped behind this wall
  102. const wallGeometry = new THREE.BoxGeometry( 2, 1, 0.1 );
  103. const wallMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, transparent: true, opacity: 0.5 } );
  104. const wall = new THREE.Mesh( wallGeometry, wallMaterial );
  105. wall.position.set( 0, 0.5, - 0.5 );
  106. scene.add( wall );
  107. //
  108. renderer = new THREE.WebGLRenderer( { antialias: true } );
  109. renderer.setPixelRatio( window.devicePixelRatio );
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. renderer.shadowMap.enabled = true;
  112. container.appendChild( renderer.domElement );
  113. //
  114. const controls = new OrbitControls( camera, renderer.domElement );
  115. controls.target.set( 0, 0.1, 0 );
  116. controls.update();
  117. controls.minDistance = 0.5;
  118. controls.maxDistance = 10;
  119. controls.maxPolarAngle = 0.5 * Math.PI;
  120. //
  121. window.addEventListener( 'resize', onWindowResize );
  122. }
  123. function onWindowResize() {
  124. camera.aspect = window.innerWidth / window.innerHeight;
  125. camera.updateProjectionMatrix();
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. }
  128. function animate() {
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>