Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c769966c68
Fetching contributors…

Cannot retrieve contributors at this time

file 214 lines (146 sloc) 5.735 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>three.js canvas - materials - video</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<style type="text/css">
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>

<script type="text/javascript" src="../build/Three.js"></script>

<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="js/Stats.js"></script>

<video id="video" autoplay style="display:none">
<source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
<source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
</video>

<script type="text/javascript">

var AMOUNT = 100;

var container, stats;

var camera, scene, renderer;

var video, image, imageContext,
imageReflection, imageReflectionContext, imageReflectionGradient,
texture, textureReflection;

var mesh;

var mouseX = 0;
var mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

init();
animate();

function init() {

container = document.createElement('div');
document.body.appendChild( container );

var info = document.createElement('div');
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - video demo. playing <a href="http://durian.blender.org/" target="_blank">sintel</a> trailer';
container.appendChild(info);

camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 1000;

scene = new THREE.Scene();

video = document.getElementById( 'video' );

//

image = document.createElement( 'canvas' );
image.width = 480;
image.height = 204;

imageContext = image.getContext( '2d' );
imageContext.fillStyle = '#000000';
imageContext.fillRect( 0, 0, 480, 204 );

texture = new THREE.Texture( image );
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;

var material = new THREE.MeshBasicMaterial( { map: texture } );

imageReflection = document.createElement( 'canvas' );
imageReflection.width = 480;
imageReflection.height = 204;

imageReflectionContext = imageReflection.getContext( '2d' );
imageReflectionContext.fillStyle = '#000000';
imageReflectionContext.fillRect( 0, 0, 480, 204 );

imageReflectionGradient = imageReflectionContext.createLinearGradient( 0, 0, 0, 204 );
imageReflectionGradient.addColorStop( 0.2, 'rgba(240, 240, 240, 1)' );
imageReflectionGradient.addColorStop( 1, 'rgba(240, 240, 240, 0.8)' );

textureReflection = new THREE.Texture( imageReflection );
textureReflection.minFilter = THREE.LinearFilter;
textureReflection.magFilter = THREE.LinearFilter;

var materialReflection = new THREE.MeshBasicMaterial( { map: textureReflection } );

//

var plane = new THREE.Plane( 480, 204, 4, 4 );

mesh = new THREE.Mesh( plane, material );
mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
mesh.overdraw = true;
scene.addObject(mesh);

mesh = new THREE.Mesh( plane, materialReflection );
mesh.position.y = -306;
mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
mesh.rotation.x = 180 * Math.PI / 180;
mesh.doubleSided = true;
mesh.overdraw = true;
scene.addObject( mesh );

//

var separation = 150;
var amountx = 10;
var amounty = 10;

var PI2 = Math.PI * 2;
var material = new THREE.ParticleCanvasMaterial( {

color: 0x0808080,
program: function ( context ) {

context.beginPath();
context.arc( 0, 0, 1, 0, PI2, true );
context.closePath();
context.fill();

}

} );

for ( var ix = 0; ix < amountx; ix++ ) {

for ( var iy = 0; iy < amounty; iy++ ) {

particle = new THREE.Particle( material );
particle.position.x = ix * separation - ( ( amountx * separation ) / 2 );
particle.position.y = -153
particle.position.z = iy * separation - ( ( amounty * separation ) / 2 );
scene.addObject( particle );

}
}

renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );

container.appendChild( renderer.domElement );

stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );

document.addEventListener( 'mousemove', onDocumentMouseMove, false );

}

function onDocumentMouseMove(event) {

mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY ) * 0.2;

}

//

function animate() {

requestAnimationFrame( animate );

render();
stats.update();

}

function render() {

camera.position.x += ( mouseX - camera.position.x ) * 0.05;
camera.position.y += ( - mouseY - camera.position.y ) * 0.05;

if ( video.readyState === video.HAVE_ENOUGH_DATA ) {

imageContext.drawImage( video, 0, 0 );

if ( texture ) texture.needsUpdate = true;
if ( textureReflection ) textureReflection.needsUpdate = true;

}

imageReflectionContext.drawImage( image, 0, 0 );
imageReflectionContext.fillStyle = imageReflectionGradient;
imageReflectionContext.fillRect( 0, 0, 480, 204 );

renderer.render( scene, camera );

}


</script>

</body>
</html>
Something went wrong with that request. Please try again.