webgl_materials_video.html 6.2 KB

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