webgl_materials_video.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="overlay">
  11. <div>
  12. <button id="startButton">Click to Play</button>
  13. <p>Video playback with audio requires user interaction.</p>
  14. </div>
  15. </div>
  16. <div id="container"></div>
  17. <div id="info">
  18. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl video demo<br/>
  19. playing <a href="http://durian.blender.org/" target="_blank" rel="noopener">sintel</a> trailer
  20. </div>
  21. <video id="video" loop crossOrigin="anonymous" playsinline style="display:none">
  22. <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
  23. <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  24. </video>
  25. <script type="module">
  26. import * as THREE from '../build/three.module.js';
  27. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  28. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  29. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  30. import { BloomPass } from './jsm/postprocessing/BloomPass.js';
  31. import { CopyShader } from './jsm/shaders/CopyShader.js';
  32. var container;
  33. var camera, scene, renderer;
  34. var video, texture, material, mesh;
  35. var composer;
  36. var mouseX = 0;
  37. var mouseY = 0;
  38. var windowHalfX = window.innerWidth / 2;
  39. var windowHalfY = window.innerHeight / 2;
  40. var cube_count,
  41. meshes = [],
  42. materials = [],
  43. xgrid = 20,
  44. ygrid = 10;
  45. var startButton = document.getElementById( 'startButton' );
  46. startButton.addEventListener( 'click', function () {
  47. init();
  48. animate();
  49. }, false );
  50. function init() {
  51. var overlay = document.getElementById( 'overlay' );
  52. overlay.remove();
  53. container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  56. camera.position.z = 500;
  57. scene = new THREE.Scene();
  58. var light = new THREE.DirectionalLight( 0xffffff );
  59. light.position.set( 0.5, 1, 1 ).normalize();
  60. scene.add( light );
  61. renderer = new THREE.WebGLRenderer();
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. container.appendChild( renderer.domElement );
  65. video = document.getElementById( 'video' );
  66. video.play();
  67. texture = new THREE.VideoTexture( video );
  68. //
  69. var i, j, ux, uy, ox, oy,
  70. geometry,
  71. xsize, ysize;
  72. ux = 1 / xgrid;
  73. uy = 1 / ygrid;
  74. xsize = 480 / xgrid;
  75. ysize = 204 / ygrid;
  76. var parameters = { color: 0xffffff, map: texture };
  77. cube_count = 0;
  78. for ( i = 0; i < xgrid; i ++ )
  79. for ( j = 0; j < ygrid; j ++ ) {
  80. ox = i;
  81. oy = j;
  82. geometry = new THREE.BoxBufferGeometry( xsize, ysize, xsize );
  83. change_uvs( geometry, ux, uy, ox, oy );
  84. materials[ cube_count ] = new THREE.MeshLambertMaterial( parameters );
  85. material = materials[ cube_count ];
  86. material.hue = i / xgrid;
  87. material.saturation = 1 - j / ygrid;
  88. material.color.setHSL( material.hue, material.saturation, 0.5 );
  89. mesh = new THREE.Mesh( geometry, material );
  90. mesh.position.x = ( i - xgrid / 2 ) * xsize;
  91. mesh.position.y = ( j - ygrid / 2 ) * ysize;
  92. mesh.position.z = 0;
  93. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
  94. scene.add( mesh );
  95. mesh.dx = 0.001 * ( 0.5 - Math.random() );
  96. mesh.dy = 0.001 * ( 0.5 - Math.random() );
  97. meshes[ cube_count ] = mesh;
  98. cube_count += 1;
  99. }
  100. renderer.autoClear = false;
  101. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  102. // postprocessing
  103. var renderModel = new RenderPass( scene, camera );
  104. var effectBloom = new BloomPass( 1.3 );
  105. var effectCopy = new ShaderPass( CopyShader );
  106. composer = new EffectComposer( renderer );
  107. composer.addPass( renderModel );
  108. composer.addPass( effectBloom );
  109. composer.addPass( effectCopy );
  110. //
  111. window.addEventListener( 'resize', onWindowResize, false );
  112. }
  113. function onWindowResize() {
  114. windowHalfX = window.innerWidth / 2;
  115. windowHalfY = window.innerHeight / 2;
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. composer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function change_uvs( geometry, unitx, unity, offsetx, offsety ) {
  122. var uvs = geometry.attributes.uv.array;
  123. for ( var i = 0; i < uvs.length; i += 2 ) {
  124. uvs[ i ] = ( uvs[ i ] + offsetx ) * unitx;
  125. uvs[ i + 1 ] = ( uvs[ i + 1 ] + offsety ) * unity;
  126. }
  127. }
  128. function onDocumentMouseMove( event ) {
  129. mouseX = ( event.clientX - windowHalfX );
  130. mouseY = ( event.clientY - windowHalfY ) * 0.3;
  131. }
  132. //
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. render();
  136. }
  137. var h, counter = 1;
  138. function render() {
  139. var time = Date.now() * 0.00005;
  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. for ( var i = 0; i < cube_count; i ++ ) {
  144. material = materials[ i ];
  145. h = ( 360 * ( material.hue + time ) % 360 ) / 360;
  146. material.color.setHSL( h, material.saturation, 0.5 );
  147. }
  148. if ( counter % 1000 > 200 ) {
  149. for ( var i = 0; i < cube_count; i ++ ) {
  150. mesh = meshes[ i ];
  151. mesh.rotation.x += 10 * mesh.dx;
  152. mesh.rotation.y += 10 * mesh.dy;
  153. mesh.position.x += 200 * mesh.dx;
  154. mesh.position.y += 200 * mesh.dy;
  155. mesh.position.z += 400 * mesh.dx;
  156. }
  157. }
  158. if ( counter % 1000 === 0 ) {
  159. for ( var i = 0; i < cube_count; i ++ ) {
  160. mesh = meshes[ i ];
  161. mesh.dx *= - 1;
  162. mesh.dy *= - 1;
  163. }
  164. }
  165. counter ++;
  166. renderer.clear();
  167. composer.render();
  168. }
  169. </script>
  170. </body>
  171. </html>