webgpu_materials_video.html 5.6 KB

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