webgl_materials_video.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 { BloomPass } from 'three/addons/postprocessing/BloomPass.js';
  30. let container;
  31. let camera, scene, renderer;
  32. let video, texture, material, mesh;
  33. let mouseX = 0;
  34. let mouseY = 0;
  35. let windowHalfX = window.innerWidth / 2;
  36. let windowHalfY = window.innerHeight / 2;
  37. let cube_count;
  38. const meshes = [],
  39. materials = [],
  40. xgrid = 20,
  41. ygrid = 10;
  42. init();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  47. camera.position.z = 500;
  48. scene = new THREE.Scene();
  49. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  50. light.position.set( 0.5, 1, 1 ).normalize();
  51. scene.add( light );
  52. renderer = new THREE.WebGLRenderer( { outputBufferType: THREE.HalfFloatType } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.setAnimationLoop( animate );
  56. container.appendChild( renderer.domElement );
  57. video = document.getElementById( 'video' );
  58. video.play();
  59. video.addEventListener( 'play', function () {
  60. this.currentTime = 3;
  61. } );
  62. texture = new THREE.VideoTexture( video );
  63. texture.colorSpace = THREE.SRGBColorSpace;
  64. //
  65. let i, j, ox, oy, geometry;
  66. const ux = 1 / xgrid;
  67. const uy = 1 / ygrid;
  68. const xsize = 480 / xgrid;
  69. const ysize = 204 / ygrid;
  70. const parameters = { color: 0xffffff, map: texture };
  71. cube_count = 0;
  72. for ( i = 0; i < xgrid; i ++ ) {
  73. for ( j = 0; j < ygrid; j ++ ) {
  74. ox = i;
  75. oy = j;
  76. geometry = new THREE.BoxGeometry( xsize, ysize, xsize );
  77. change_uvs( geometry, ux, uy, ox, oy );
  78. materials[ cube_count ] = new THREE.MeshLambertMaterial( parameters );
  79. material = materials[ cube_count ];
  80. material.hue = i / xgrid;
  81. material.saturation = 1 - j / ygrid;
  82. material.color.setHSL( material.hue, material.saturation, 0.5 );
  83. mesh = new THREE.Mesh( geometry, material );
  84. mesh.position.x = ( i - xgrid / 2 ) * xsize;
  85. mesh.position.y = ( j - ygrid / 2 ) * ysize;
  86. mesh.position.z = 0;
  87. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
  88. scene.add( mesh );
  89. mesh.dx = 0.001 * ( 0.5 - Math.random() );
  90. mesh.dy = 0.001 * ( 0.5 - Math.random() );
  91. meshes[ cube_count ] = mesh;
  92. cube_count += 1;
  93. }
  94. }
  95. document.addEventListener( 'mousemove', onDocumentMouseMove );
  96. // postprocessing
  97. const bloomPass = new BloomPass( 1.3 );
  98. renderer.setEffects( [ bloomPass ] );
  99. //
  100. window.addEventListener( 'resize', onWindowResize );
  101. }
  102. function onWindowResize() {
  103. windowHalfX = window.innerWidth / 2;
  104. windowHalfY = window.innerHeight / 2;
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. function change_uvs( geometry, unitx, unity, offsetx, offsety ) {
  110. const uvs = geometry.attributes.uv.array;
  111. for ( let i = 0; i < uvs.length; i += 2 ) {
  112. uvs[ i ] = ( uvs[ i ] + offsetx ) * unitx;
  113. uvs[ i + 1 ] = ( uvs[ i + 1 ] + offsety ) * unity;
  114. }
  115. }
  116. function onDocumentMouseMove( event ) {
  117. mouseX = ( event.clientX - windowHalfX );
  118. mouseY = ( event.clientY - windowHalfY ) * 0.3;
  119. }
  120. //
  121. let h, counter = 1;
  122. function animate() {
  123. const time = Date.now() * 0.00005;
  124. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  125. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  126. camera.lookAt( scene.position );
  127. for ( let i = 0; i < cube_count; i ++ ) {
  128. material = materials[ i ];
  129. h = ( 360 * ( material.hue + time ) % 360 ) / 360;
  130. material.color.setHSL( h, material.saturation, 0.5 );
  131. }
  132. if ( counter % 1000 > 200 ) {
  133. for ( let i = 0; i < cube_count; i ++ ) {
  134. mesh = meshes[ i ];
  135. mesh.rotation.x += 10 * mesh.dx;
  136. mesh.rotation.y += 10 * mesh.dy;
  137. mesh.position.x -= 150 * mesh.dx;
  138. mesh.position.y += 150 * mesh.dy;
  139. mesh.position.z += 300 * mesh.dx;
  140. }
  141. }
  142. if ( counter % 1000 === 0 ) {
  143. for ( let i = 0; i < cube_count; i ++ ) {
  144. mesh = meshes[ i ];
  145. mesh.dx *= - 1;
  146. mesh.dy *= - 1;
  147. }
  148. }
  149. counter ++;
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>
粤ICP备19079148号