canvas_materials_video.html 6.0 KB

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