threex.videotexture
Version:
three.js helper to display video texture
101 lines (90 loc) • 3.5 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 //
//////////////////////////////////////////////////////////////////////////////////
// create the videoTexture
var videoTexture= new THREEx.VideoTexture('videos/sintel.ogv')
var video = videoTexture.video
video.addEventListener('loadedmetadata', function(){
console.log('duration', video.duration, Date.now())
var currentTime = Date.now()/1000 % video.duration
console.log('initial currentTime', currentTime)
video.currentTime = currentTime;
})
updateFcts.push(function(delta, now){
videoTexture.update(delta, now)
})
setInterval(function(){
console.log('currentTime', video.currentTime)
}, 1000)
/**
* NOTE: require a ttp server which accept range
* $ lighttpd -D -f lighttpd.conf
* # see conf file below
*/
/*
server.document-root = "/Users/jeromeetienne/webwork/threex/"
server.port = 8000
*/
// setInterval(function(){
// var video = videoTexture.video
// video.currentTime = 5.0;
// console.log('seek to ', video.currentTime)
// }, 3000)
// 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>