webgpu_materials_video.html 6.0 KB

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