materials_video.html 5.0 KB

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