materials_video.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - 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="http://github.com/mrdoob/stats.js/raw/master/build/stats.js"></script>
  18. <script type="text/javascript" src="../build/three.js"></script>
  19. <script type="text/javascript" src="primitives/Plane.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 type="text/javascript">
  25. var SCREEN_WIDTH = window.innerWidth;
  26. var SCREEN_HEIGHT = window.innerHeight;
  27. var AMOUNT = 100;
  28. var container, stats;
  29. var camera, scene, renderer;
  30. var video, texture, textureContext,
  31. textureReflection, textureReflectionContext, textureReflectionGradient;
  32. var mesh;
  33. var mouseX = 0;
  34. var mouseY = 0;
  35. var windowHalfX = window.innerWidth >> 1;
  36. var windowHalfY = window.innerHeight >> 1;
  37. document.addEventListener('mousemove', onDocumentMouseMove, false);
  38. init();
  39. setInterval(loop, 1000/60);
  40. function init() {
  41. container = document.createElement('div');
  42. document.body.appendChild(container);
  43. var info = document.createElement('div');
  44. info.style.position = 'absolute';
  45. info.style.top = '10px';
  46. info.style.width = '100%';
  47. info.style.textAlign = 'center';
  48. 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';
  49. container.appendChild(info);
  50. camera = new THREE.Camera( 45, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  51. camera.position.z = 1000;
  52. scene = new THREE.Scene();
  53. video = document.getElementById( 'video' );
  54. //
  55. texture = document.createElement( 'canvas' );
  56. texture.width = 480;
  57. texture.height = 204;
  58. textureContext = texture.getContext( '2d' );
  59. textureContext.fillStyle = '#000000';
  60. textureContext.fillRect( 0, 0, 480, 204 );
  61. var material = new THREE.BitmapUVMappingMaterial( texture );
  62. textureReflection = document.createElement( 'canvas' );
  63. textureReflection.width = 480;
  64. textureReflection.height = 204;
  65. textureReflectionContext = textureReflection.getContext( '2d' );
  66. textureReflectionContext.fillStyle = '#000000';
  67. textureReflectionContext.fillRect( 0, 0, 480, 204 );
  68. textureReflectionGradient = textureReflectionContext.createLinearGradient( 0, 0, 0, 204 );
  69. textureReflectionGradient.addColorStop( 0.2, 'rgba(240, 240, 240, 1)' );
  70. textureReflectionGradient.addColorStop( 1, 'rgba(240, 240, 240, 0.8)' );
  71. var materialReflection = new THREE.BitmapUVMappingMaterial( textureReflection );
  72. //
  73. var plane = new Plane( 480, 204, 4, 4 );
  74. mesh = new THREE.Mesh( plane, material );
  75. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
  76. mesh.overdraw = true;
  77. scene.add(mesh);
  78. mesh = new THREE.Mesh( plane, materialReflection );
  79. mesh.position.y = -306;
  80. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1.5;
  81. mesh.rotation.x = 180 * Math.PI / 180;
  82. mesh.doubleSided = true;
  83. mesh.overdraw = true;
  84. scene.add(mesh);
  85. //
  86. var separation = 150;
  87. var amountx = 10;
  88. var amounty = 10;
  89. var material = new THREE.ColorFillMaterial(0x808080);
  90. for (var ix = 0; ix < amountx; ix++) {
  91. for(var iy = 0; iy < amounty; iy++) {
  92. particle = new THREE.Particle( material );
  93. particle.position.x = ix * separation - ((amountx * separation) / 2);
  94. particle.position.y = -153
  95. particle.position.z = iy * separation - ((amounty * separation) / 2);
  96. scene.add( particle );
  97. }
  98. }
  99. renderer = new THREE.CanvasRenderer();
  100. renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
  101. container.appendChild(renderer.domElement);
  102. stats = new Stats();
  103. stats.domElement.style.position = 'absolute';
  104. stats.domElement.style.top = '0px';
  105. container.appendChild(stats.domElement);
  106. }
  107. function onDocumentMouseMove(event) {
  108. mouseX = (event.clientX - windowHalfX);
  109. mouseY = (event.clientY - windowHalfY) * .2;
  110. }
  111. function loop() {
  112. camera.position.x += (mouseX - camera.position.x) * .05;
  113. camera.position.y += (-mouseY - camera.position.y) * .05;
  114. if (video.readyState === video.HAVE_ENOUGH_DATA) {
  115. textureContext.drawImage( video, 0, 0 );
  116. }
  117. textureReflectionContext.drawImage( texture, 0, 0 );
  118. textureReflectionContext.fillStyle = textureReflectionGradient;
  119. textureReflectionContext.fillRect(0, 0, 480, 204);
  120. renderer.render(scene, camera);
  121. stats.update();
  122. }
  123. </script>
  124. </body>
  125. </html>