2
0

webaudio_orientation.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. <!-- Import maps polyfill -->
  23. <!-- Remove this when import maps will be widely supported -->
  24. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { PositionalAudioHelper } from 'three/addons/helpers/PositionalAudioHelper.js';
  37. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  38. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  39. let scene, camera, renderer;
  40. const startButton = document.getElementById( 'startButton' );
  41. startButton.addEventListener( 'click', init );
  42. function init() {
  43. const overlay = document.getElementById( 'overlay' );
  44. overlay.remove();
  45. const container = document.getElementById( 'container' );
  46. //
  47. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  48. camera.position.set( 3, 2, 3 );
  49. const reflectionCube = new THREE.CubeTextureLoader()
  50. .setPath( 'textures/cube/SwedishRoyalCastle/' )
  51. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0xa0a0a0 );
  54. scene.fog = new THREE.Fog( 0xa0a0a0, 2, 20 );
  55. //
  56. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  57. hemiLight.position.set( 0, 20, 0 );
  58. scene.add( hemiLight );
  59. const dirLight = new THREE.DirectionalLight( 0xffffff );
  60. dirLight.position.set( 5, 5, 0 );
  61. dirLight.castShadow = true;
  62. dirLight.shadow.camera.top = 1;
  63. dirLight.shadow.camera.bottom = - 1;
  64. dirLight.shadow.camera.left = - 1;
  65. dirLight.shadow.camera.right = 1;
  66. dirLight.shadow.camera.near = 0.1;
  67. dirLight.shadow.camera.far = 20;
  68. scene.add( dirLight );
  69. // scene.add( new THREE.CameraHelper( dirLight.shadow.camera ) );
  70. //
  71. const mesh = new THREE.Mesh( new THREE.PlaneGeometry( 50, 50 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  72. mesh.rotation.x = - Math.PI / 2;
  73. mesh.receiveShadow = true;
  74. scene.add( mesh );
  75. const grid = new THREE.GridHelper( 50, 50, 0x888888, 0x888888 );
  76. scene.add( grid );
  77. //
  78. const listener = new THREE.AudioListener();
  79. camera.add( listener );
  80. const audioElement = document.getElementById( 'music' );
  81. audioElement.play();
  82. const positionalAudio = new THREE.PositionalAudio( listener );
  83. positionalAudio.setMediaElementSource( audioElement );
  84. positionalAudio.setRefDistance( 1 );
  85. positionalAudio.setDirectionalCone( 180, 230, 0.1 );
  86. const helper = new PositionalAudioHelper( positionalAudio, 0.1 );
  87. positionalAudio.add( helper );
  88. //
  89. const gltfLoader = new GLTFLoader();
  90. gltfLoader.load( 'models/gltf/BoomBox.glb', function ( gltf ) {
  91. const boomBox = gltf.scene;
  92. boomBox.position.set( 0, 0.2, 0 );
  93. boomBox.scale.set( 20, 20, 20 );
  94. boomBox.traverse( function ( object ) {
  95. if ( object.isMesh ) {
  96. object.material.envMap = reflectionCube;
  97. object.geometry.rotateY( - Math.PI );
  98. object.castShadow = true;
  99. }
  100. } );
  101. boomBox.add( positionalAudio );
  102. scene.add( boomBox );
  103. animate();
  104. } );
  105. // sound is damped behind this wall
  106. const wallGeometry = new THREE.BoxGeometry( 2, 1, 0.1 );
  107. const wallMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, transparent: true, opacity: 0.5 } );
  108. const wall = new THREE.Mesh( wallGeometry, wallMaterial );
  109. wall.position.set( 0, 0.5, - 0.5 );
  110. scene.add( wall );
  111. //
  112. renderer = new THREE.WebGLRenderer( { antialias: true } );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.shadowMap.enabled = true;
  116. container.appendChild( renderer.domElement );
  117. //
  118. const controls = new OrbitControls( camera, renderer.domElement );
  119. controls.target.set( 0, 0.1, 0 );
  120. controls.update();
  121. controls.minDistance = 0.5;
  122. controls.maxDistance = 10;
  123. controls.maxPolarAngle = 0.5 * Math.PI;
  124. //
  125. window.addEventListener( 'resize', onWindowResize );
  126. }
  127. function onWindowResize() {
  128. camera.aspect = window.innerWidth / window.innerHeight;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. }
  132. function animate() {
  133. requestAnimationFrame( animate );
  134. renderer.render( scene, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>