canvas_materials_video.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - materials - video</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/Three.js"></script>
  18. <script src="js/RequestAnimationFrame.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <video id="video" autoplay style="display:none">
  21. <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  22. <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
  23. </video>
  24. <script>
  25. var AMOUNT = 100;
  26. var container, stats;
  27. var camera, scene, renderer;
  28. var video, image, imageContext,
  29. imageReflection, imageReflectionContext, imageReflectionGradient,
  30. texture, textureReflection;
  31. var mesh;
  32. var mouseX = 0;
  33. var mouseY = 0;
  34. var windowHalfX = window.innerWidth / 2;
  35. var windowHalfY = window.innerHeight / 2;
  36. init();
  37. animate();
  38. function init() {
  39. container = document.createElement('div');
  40. document.body.appendChild( container );
  41. var info = document.createElement('div');
  42. info.style.position = 'absolute';
  43. info.style.top = '10px';
  44. info.style.width = '100%';
  45. info.style.textAlign = 'center';
  46. 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';
  47. container.appendChild(info);
  48. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  49. camera.position.z = 1000;
  50. scene = new THREE.Scene();
  51. video = document.getElementById( 'video' );
  52. //
  53. image = document.createElement( 'canvas' );
  54. image.width = 480;
  55. image.height = 204;
  56. imageContext = image.getContext( '2d' );
  57. imageContext.fillStyle = '#000000';
  58. imageContext.fillRect( 0, 0, 480, 204 );
  59. texture = new THREE.Texture( image );
  60. texture.minFilter = THREE.LinearFilter;
  61. texture.magFilter = THREE.LinearFilter;
  62. var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: true } );
  63. imageReflection = document.createElement( 'canvas' );
  64. imageReflection.width = 480;
  65. imageReflection.height = 204;
  66. imageReflectionContext = imageReflection.getContext( '2d' );
  67. imageReflectionContext.fillStyle = '#000000';
  68. imageReflectionContext.fillRect( 0, 0, 480, 204 );
  69. imageReflectionGradient = imageReflectionContext.createLinearGradient( 0, 0, 0, 204 );
  70. imageReflectionGradient.addColorStop( 0.2, 'rgba(240, 240, 240, 1)' );
  71. imageReflectionGradient.addColorStop( 1, 'rgba(240, 240, 240, 0.8)' );
  72. textureReflection = new THREE.Texture( imageReflection );
  73. textureReflection.minFilter = THREE.LinearFilter;
  74. textureReflection.magFilter = THREE.LinearFilter;
  75. var materialReflection = new THREE.MeshBasicMaterial( { map: textureReflection, overdraw: true } );
  76. //
  77. var plane = new THREE.PlaneGeometry( 480, 204, 4, 4 );
  78. mesh = new THREE.Mesh( plane, material );
  79. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
  80. scene.add(mesh);
  81. mesh = new THREE.Mesh( plane, materialReflection );
  82. mesh.position.y = -306;
  83. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
  84. mesh.rotation.x = 180 * Math.PI / 180;
  85. mesh.doubleSided = true;
  86. mesh.overdraw = true;
  87. scene.add( mesh );
  88. //
  89. var separation = 150;
  90. var amountx = 10;
  91. var amounty = 10;
  92. var PI2 = Math.PI * 2;
  93. var material = new THREE.ParticleCanvasMaterial( {
  94. color: 0x0808080,
  95. program: function ( context ) {
  96. context.beginPath();
  97. context.arc( 0, 0, 1, 0, PI2, true );
  98. context.closePath();
  99. context.fill();
  100. }
  101. } );
  102. for ( var ix = 0; ix < amountx; ix++ ) {
  103. for ( var iy = 0; iy < amounty; iy++ ) {
  104. particle = new THREE.Particle( material );
  105. particle.position.x = ix * separation - ( ( amountx * separation ) / 2 );
  106. particle.position.y = -153
  107. particle.position.z = iy * separation - ( ( amounty * separation ) / 2 );
  108. scene.add( particle );
  109. }
  110. }
  111. renderer = new THREE.CanvasRenderer();
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. container.appendChild( renderer.domElement );
  114. stats = new Stats();
  115. stats.domElement.style.position = 'absolute';
  116. stats.domElement.style.top = '0px';
  117. container.appendChild( stats.domElement );
  118. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  119. }
  120. function onDocumentMouseMove(event) {
  121. mouseX = ( event.clientX - windowHalfX );
  122. mouseY = ( event.clientY - windowHalfY ) * 0.2;
  123. }
  124. //
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. render();
  128. stats.update();
  129. }
  130. function render() {
  131. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  132. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  133. camera.lookAt( scene.position );
  134. if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
  135. imageContext.drawImage( video, 0, 0 );
  136. if ( texture ) texture.needsUpdate = true;
  137. if ( textureReflection ) textureReflection.needsUpdate = true;
  138. }
  139. imageReflectionContext.drawImage( image, 0, 0 );
  140. imageReflectionContext.fillStyle = imageReflectionGradient;
  141. imageReflectionContext.fillRect( 0, 0, 480, 204 );
  142. renderer.render( scene, camera );
  143. }
  144. </script>
  145. </body>
  146. </html>