canvas_materials_video.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  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 type="text/javascript" src="../build/Three.js"></script>
  18. <script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>
  19. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  20. <script type="text/javascript" src="js/Stats.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 type="text/javascript">
  26. var AMOUNT = 100;
  27. var container, stats;
  28. var camera, scene, renderer;
  29. var video, texture, textureContext,
  30. textureReflection, textureReflectionContext, textureReflectionGradient;
  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.Camera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  49. camera.position.z = 1000;
  50. scene = new THREE.Scene();
  51. video = document.getElementById( 'video' );
  52. //
  53. texture = document.createElement( 'canvas' );
  54. texture.loaded = true;
  55. texture.width = 480;
  56. texture.height = 204;
  57. textureContext = texture.getContext( '2d' );
  58. textureContext.fillStyle = '#000000';
  59. textureContext.fillRect( 0, 0, 480, 204 );
  60. var map = new THREE.Texture( texture );
  61. var material = new THREE.MeshBasicMaterial( { map: map } );
  62. textureReflection = document.createElement( 'canvas' );
  63. textureReflection.loaded = true;
  64. textureReflection.width = 480;
  65. textureReflection.height = 204;
  66. textureReflectionContext = textureReflection.getContext( '2d' );
  67. textureReflectionContext.fillStyle = '#000000';
  68. textureReflectionContext.fillRect( 0, 0, 480, 204 );
  69. textureReflectionGradient = textureReflectionContext.createLinearGradient( 0, 0, 0, 204 );
  70. textureReflectionGradient.addColorStop( 0.2, 'rgba(240, 240, 240, 1)' );
  71. textureReflectionGradient.addColorStop( 1, 'rgba(240, 240, 240, 0.8)' );
  72. var materialReflection = new THREE.MeshBasicMaterial( { map: new THREE.Texture( textureReflection ) } );
  73. //
  74. var plane = new Plane( 480, 204, 4, 4 );
  75. mesh = new THREE.Mesh( plane, material );
  76. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
  77. mesh.overdraw = true;
  78. scene.addObject(mesh);
  79. mesh = new THREE.Mesh( plane, materialReflection );
  80. mesh.position.y = -306;
  81. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
  82. mesh.rotation.x = 180 * Math.PI / 180;
  83. mesh.doubleSided = true;
  84. mesh.overdraw = true;
  85. scene.addObject( mesh );
  86. //
  87. var separation = 150;
  88. var amountx = 10;
  89. var amounty = 10;
  90. var material = new THREE.ParticleCircleMaterial( { color: 0x808080 } );
  91. for ( var ix = 0; ix < amountx; ix++ ) {
  92. for ( var iy = 0; iy < amounty; iy++ ) {
  93. particle = new THREE.Particle( material );
  94. particle.position.x = ix * separation - ( ( amountx * separation ) / 2 );
  95. particle.position.y = -153
  96. particle.position.z = iy * separation - ( ( amounty * separation ) / 2 );
  97. scene.addObject( particle );
  98. }
  99. }
  100. renderer = new THREE.CanvasRenderer();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. container.appendChild( renderer.domElement );
  103. stats = new Stats();
  104. stats.domElement.style.position = 'absolute';
  105. stats.domElement.style.top = '0px';
  106. container.appendChild( stats.domElement );
  107. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  108. }
  109. function onDocumentMouseMove(event) {
  110. mouseX = ( event.clientX - windowHalfX );
  111. mouseY = ( event.clientY - windowHalfY ) * 0.2;
  112. }
  113. //
  114. function animate() {
  115. requestAnimationFrame( animate );
  116. render();
  117. stats.update();
  118. }
  119. function render() {
  120. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  121. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  122. if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
  123. textureContext.drawImage( video, 0, 0 );
  124. }
  125. textureReflectionContext.drawImage( texture, 0, 0 );
  126. textureReflectionContext.fillStyle = textureReflectionGradient;
  127. textureReflectionContext.fillRect( 0, 0, 480, 204 );
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>