webgpu_materials_video.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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="example.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" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Video Material</span>
  18. </div>
  19. <small>
  20. Playing <a href="http://durian.blender.org/" target="_blank" rel="noopener">sintel</a> trailer.
  21. </small>
  22. </div>
  23. <video id="video" loop crossOrigin="anonymous" playsinline style="display:none">
  24. <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
  25. <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  26. </video>
  27. <script type="importmap">
  28. {
  29. "imports": {
  30. "three": "../build/three.webgpu.js",
  31. "three/webgpu": "../build/three.webgpu.js",
  32. "three/tsl": "../build/three.tsl.js",
  33. "three/addons/": "./jsm/"
  34. }
  35. }
  36. </script>
  37. <script type="module">
  38. import * as THREE from 'three/webgpu';
  39. let container;
  40. let camera, scene, renderer;
  41. let video, texture, material, mesh;
  42. let mouseX = 0;
  43. let mouseY = 0;
  44. let windowHalfX = window.innerWidth / 2;
  45. let windowHalfY = window.innerHeight / 2;
  46. let cube_count;
  47. const meshes = [],
  48. materials = [],
  49. xgrid = 20,
  50. ygrid = 10;
  51. const startButton = document.getElementById( 'startButton' );
  52. startButton.addEventListener( 'click', function () {
  53. init();
  54. } );
  55. function init() {
  56. const overlay = document.getElementById( 'overlay' );
  57. overlay.remove();
  58. container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  61. camera.position.z = 500;
  62. scene = new THREE.Scene();
  63. const light = new THREE.DirectionalLight( 0xffffff, 7 );
  64. light.position.set( 0.5, 1, 1 ).normalize();
  65. scene.add( light );
  66. renderer = new THREE.WebGPURenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.setAnimationLoop( render );
  70. container.appendChild( renderer.domElement );
  71. video = document.getElementById( 'video' );
  72. video.play();
  73. video.addEventListener( 'play', function () {
  74. this.currentTime = 3;
  75. } );
  76. texture = new THREE.VideoTexture( video );
  77. texture.colorSpace = THREE.SRGBColorSpace;
  78. //
  79. let i, j, ox, oy, geometry;
  80. const ux = 1 / xgrid;
  81. const uy = 1 / ygrid;
  82. const xsize = 480 / xgrid;
  83. const ysize = 204 / ygrid;
  84. const parameters = { color: 0xffffff, map: texture };
  85. cube_count = 0;
  86. for ( i = 0; i < xgrid; i ++ ) {
  87. for ( j = 0; j < ygrid; j ++ ) {
  88. ox = i;
  89. oy = j;
  90. geometry = new THREE.BoxGeometry( xsize, ysize, xsize );
  91. change_uvs( geometry, ux, uy, ox, oy );
  92. materials[ cube_count ] = new THREE.MeshPhongMaterial( parameters );
  93. material = materials[ cube_count ];
  94. material.hue = i / xgrid;
  95. material.saturation = 1 - j / ygrid;
  96. material.color.setHSL( material.hue, material.saturation, 0.5 );
  97. mesh = new THREE.Mesh( geometry, material );
  98. mesh.position.x = ( i - xgrid / 2 ) * xsize;
  99. mesh.position.y = ( j - ygrid / 2 ) * ysize;
  100. mesh.position.z = 0;
  101. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
  102. scene.add( mesh );
  103. mesh.dx = 0.001 * ( 0.5 - Math.random() );
  104. mesh.dy = 0.001 * ( 0.5 - Math.random() );
  105. meshes[ cube_count ] = mesh;
  106. cube_count += 1;
  107. }
  108. }
  109. document.addEventListener( 'mousemove', onDocumentMouseMove );
  110. //
  111. window.addEventListener( 'resize', onWindowResize );
  112. }
  113. function onWindowResize() {
  114. windowHalfX = window.innerWidth / 2;
  115. windowHalfY = window.innerHeight / 2;
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.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 render() {
  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.render( scene, camera );
  162. }
  163. </script>
  164. </body>
  165. </html>
粤ICP备19079148号