canvas_materials_video.html 5.8 KB

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