123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webaudio - visualizer</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <script id="vertexShader" type="x-shader/x-vertex">
- varying vec2 vUv;
- void main() {
- vUv = uv;
- gl_Position = vec4( position, 1.0 );
- }
- </script>
- <script id="fragmentShader" type="x-shader/x-fragment">
- uniform sampler2D tAudioData;
- varying vec2 vUv;
- void main() {
- vec3 backgroundColor = vec3( 0.125, 0.125, 0.125 );
- vec3 color = vec3( 1.0, 1.0, 0.0 );
- float f = texture2D( tAudioData, vec2( vUv.x, 0.0 ) ).r;
- float i = step( vUv.y, f ) * step( f - 0.0125, vUv.y );
- gl_FragColor = vec4( mix( backgroundColor, color, i ), 1.0 );
- }
- </script>
- </head>
- <body>
- <div id="overlay">
- <div>
- <button id="startButton">Click to Play</button>
- <p>Audio playback requires user interaction.</p>
- </div>
- </div>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> webaudio - visualizer<br/>
- music by <a href="http://www.newgrounds.com/audio/listen/376737" target="_blank" rel="noopener">skullbeatz</a>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- var scene, camera, renderer, analyser, uniforms;
- var startButton = document.getElementById( 'startButton' );
- startButton.addEventListener( 'click', init );
- function init() {
- var fftSize = 128;
- //
- var overlay = document.getElementById( 'overlay' );
- overlay.remove();
- //
- var container = document.getElementById( 'container' );
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setClearColor( 0x000000 );
- renderer.setPixelRatio( window.devicePixelRatio );
- container.appendChild( renderer.domElement );
- scene = new THREE.Scene();
- camera = new THREE.Camera();
- //
- var listener = new THREE.AudioListener();
- var audio = new THREE.Audio( listener );
- var mediaElement = new Audio( 'sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3' );
- mediaElement.loop = true;
- mediaElement.play();
- audio.setMediaElementSource( mediaElement );
- analyser = new THREE.AudioAnalyser( audio, fftSize );
- //
- uniforms = {
- tAudioData: { value: new THREE.DataTexture( analyser.data, fftSize / 2, 1, THREE.LuminanceFormat ) }
- };
- var material = new THREE.ShaderMaterial( {
- uniforms: uniforms,
- vertexShader: document.getElementById( 'vertexShader' ).textContent,
- fragmentShader: document.getElementById( 'fragmentShader' ).textContent
- } );
- var geometry = new THREE.PlaneBufferGeometry( 1, 1 );
- var mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- //
- window.addEventListener( 'resize', onResize, false );
- animate();
- }
- function onResize() {
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- analyser.getFrequencyData();
- uniforms.tAudioData.value.needsUpdate = true;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|