webgpu_materials_video.html 5.5 KB

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