threex.videotexture
Version:
three.js helper to display video texture
87 lines (76 loc) • 3.21 kB
HTML
<!DOCTYPE html>
<script src='vendor/three.js/build/three.min.js'></script>
<script src='../threex.videotexture.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 = 3;
//////////////////////////////////////////////////////////////////////////////////
// add an object and make it move //
//////////////////////////////////////////////////////////////////////////////////
// trick to work around bugs in the video loop
// NOTE: this may not work because .currentTime and .duration are float and the equality would fail
// - possible other workaround from @pyalot: test if .currentTime is always increasing
updateFcts.push(function(delta, now){
var videoElement = videoTexture.video
if( videoElement.currentTime === videoElement.duration ){
videoElement.src = videoElement.src
}
})
// create the videoTexture
var url = 'videos/sintel.ogv'
var videoTexture= new THREEx.VideoTexture(url)
updateFcts.push(function(delta, now){
videoTexture.update(delta, now)
})
// use the texture in a THREE.Mesh
var geometry = new THREE.CubeGeometry(1,1,1);
var material = new THREE.MeshBasicMaterial({
map : videoTexture.texture
});
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
updateFcts.push(function(delta, now){
mesh.rotation.x += 1 * delta;
mesh.rotation.y += 2 * delta;
})
//////////////////////////////////////////////////////////////////////////////////
// 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*5 - camera.position.x) * (delta*3)
camera.position.y += (mouse.y*5 - 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>