Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
141 lines (120 sloc) 4.76 KB
<!DOCTYPE html>
<script src='vendor/three.js/build/three.min.js'></script>
<script src='vendor/three.js/examples/js/ImprovedNoise.js'></script>
<script src='../lib/webaudiox.analyser2volume.js'></script>
<script src='../lib/webaudiox.lineout.js'></script>
<script src='../lib/webaudiox.loadbuffer.js'></script>
<script src='../lib/webaudiox.three.js'></script>
<body style='margin: 0px; background-color: #bbbbbb; overflow: hidden;'><script>
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var updateFcts = [];
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 100 );
camera.position.z = 5;
//////////////////////////////////////////////////////////////////////////////////
// to play immediatly //
//////////////////////////////////////////////////////////////////////////////////
var AudioContext= window.AudioContext || window.webkitAudioContext;
var context = new AudioContext()
// put a ListenerObject3DUpdater
var listenerUpdater = new WebAudiox.ListenerObject3DUpdater(context, camera)
updateFcts.push(function(delta, now){
listenerUpdater.update(delta, now)
})
// Create lineOut
var lineOut = new WebAudiox.LineOut(context)
lineOut.volume = 1
// add an analyser on lineOut
var analyser = context.createAnalyser()
analyser.connect(lineOut.destination);
lineOut.destination = analyser;
// load a sound and play it immediatly
WebAudiox.loadBuffer(context, 'sounds/techno.mp3', function(buffer){
var destination = lineOut.destination;
// init AudioPannerNode
var panner = context.createPanner()
// panner.coneOuterGain = 0.1
// panner.coneOuterAngle = Math.PI *180/Math.PI
// panner.coneInnerAngle = 0 *180/Math.PI
panner.connect(destination)
destination = panner
// put a PannerObject3DUpdater
var pannerUpdater = new WebAudiox.PannerObject3DUpdater(panner, mesh)
updateFcts.push(function(delta, now){
pannerUpdater.update(delta, now)
})
// init AudioBufferSourceNode
var source = context.createBufferSource();
source.buffer = buffer
source.loop = true
source.connect(destination)
destination = source
// start the sound now
source.start(0);
return source;
});
//////////////////////////////////////////////////////////////////////////////////
// add an object and make it move //
//////////////////////////////////////////////////////////////////////////////////
var geometry = new THREE.CubeGeometry( 1, 1, 1)
var material = new THREE.MeshBasicMaterial()
var mesh = new THREE.Mesh( geometry, material )
scene.add( mesh )
updateFcts.push(function(delta, now){
var angle = 0.1 * now * Math.PI*2
mesh.position.x = Math.cos(angle)*3
mesh.position.z = Math.sin(angle)*3
})
updateFcts.push(function(delta, now){
mesh.rotation.x += 3 * delta;
mesh.rotation.y += 2 * delta;
})
// put color hue on perlin
var perlin = new ImprovedNoise();
var analyser2Vol= new WebAudiox.Analyser2Volume(analyser)
updateFcts.push(function(delta, now){
var lightness = analyser2Vol.smoothedValue()
lightness *= 2
lightness = Math.min(lightness, 1)
var noiseAge = 0.1*now
var hue = perlin.noise(noiseAge, 0, 0) + 0.5
material.color.setHSL(hue, 1, lightness)
})
//////////////////////////////////////////////////////////////////////////////////
// Camera Controls //
//////////////////////////////////////////////////////////////////////////////////
var mouse = {x : 0, y : 0}
document.addEventListener('mousemove', function(event){
mouse.x = (event.clientX / window.innerWidth ) - 0.5
mouse.y = (event.clientY / window.innerHeight) - 0.5
}, false)
updateFcts.push(function(delta, now){
// camera.position.x += (mouse.x*50 - camera.position.x) * (delta*3)
// camera.position.y += (mouse.y*50 - camera.position.y) * (delta*3)
// camera.lookAt( scene.position )
})
//////////////////////////////////////////////////////////////////////////////////
// render the scene //
//////////////////////////////////////////////////////////////////////////////////
updateFcts.push(function(){
renderer.render( scene, camera );
})
//////////////////////////////////////////////////////////////////////////////////
// loop runner //
//////////////////////////////////////////////////////////////////////////////////
var lastTimeMsec= null
requestAnimationFrame(function animate(nowMsec){
// keep looping
requestAnimationFrame( animate );
// measure time
lastTimeMsec = lastTimeMsec || nowMsec-1000/60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
updateFcts.forEach(function(updateFn){
updateFn(deltaMsec/1000, nowMsec/1000)
})
})
</script></body>