webgl_materials_video.html 6.0 KB

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