1
0

webgpu_materials_video.html 5.8 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. <meta property="og:title" content="three.js webgpu - video material">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_materials_video.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_materials_video.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  17. <div class="title-wrapper">
  18. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Video Material</span>
  19. </div>
  20. <small>
  21. Playing <a href="http://durian.blender.org/" target="_blank" rel="noopener">sintel</a> trailer.
  22. </small>
  23. </div>
  24. <video id="video" loop muted crossOrigin="anonymous" playsinline style="display:none">
  25. <source src="textures/sintel.ogv" type='video/ogg; codecs="theora, vorbis"'>
  26. <source src="textures/sintel.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  27. </video>
  28. <script type="importmap">
  29. {
  30. "imports": {
  31. "three": "../build/three.webgpu.js",
  32. "three/webgpu": "../build/three.webgpu.js",
  33. "three/tsl": "../build/three.tsl.js",
  34. "three/addons/": "./jsm/"
  35. }
  36. }
  37. </script>
  38. <script type="module">
  39. import * as THREE from 'three/webgpu';
  40. let container;
  41. let camera, scene, renderer;
  42. let video, texture, material, mesh;
  43. let mouseX = 0;
  44. let mouseY = 0;
  45. let windowHalfX = window.innerWidth / 2;
  46. let windowHalfY = window.innerHeight / 2;
  47. let cube_count;
  48. const meshes = [],
  49. materials = [],
  50. xgrid = 20,
  51. ygrid = 10;
  52. init();
  53. function init() {
  54. container = document.createElement( 'div' );
  55. document.body.appendChild( container );
  56. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  57. camera.position.z = 500;
  58. scene = new THREE.Scene();
  59. const light = new THREE.DirectionalLight( 0xffffff, 7 );
  60. light.position.set( 0.5, 1, 1 ).normalize();
  61. scene.add( light );
  62. renderer = new THREE.WebGPURenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. renderer.setAnimationLoop( render );
  66. container.appendChild( renderer.domElement );
  67. video = document.getElementById( 'video' );
  68. video.play();
  69. video.addEventListener( 'play', function () {
  70. this.currentTime = 3;
  71. } );
  72. texture = new THREE.VideoTexture( video );
  73. texture.colorSpace = THREE.SRGBColorSpace;
  74. //
  75. let i, j, ox, oy, geometry;
  76. const ux = 1 / xgrid;
  77. const uy = 1 / ygrid;
  78. const xsize = 480 / xgrid;
  79. const ysize = 204 / ygrid;
  80. const parameters = { color: 0xffffff, map: texture };
  81. cube_count = 0;
  82. for ( i = 0; i < xgrid; i ++ ) {
  83. for ( j = 0; j < ygrid; j ++ ) {
  84. ox = i;
  85. oy = j;
  86. geometry = new THREE.BoxGeometry( xsize, ysize, xsize );
  87. change_uvs( geometry, ux, uy, ox, oy );
  88. materials[ cube_count ] = new THREE.MeshPhongMaterial( parameters );
  89. material = materials[ cube_count ];
  90. material.hue = i / xgrid;
  91. material.saturation = 1 - j / ygrid;
  92. material.color.setHSL( material.hue, material.saturation, 0.5 );
  93. mesh = new THREE.Mesh( geometry, material );
  94. mesh.position.x = ( i - xgrid / 2 ) * xsize;
  95. mesh.position.y = ( j - ygrid / 2 ) * ysize;
  96. mesh.position.z = 0;
  97. mesh.scale.x = mesh.scale.y = mesh.scale.z = 1;
  98. scene.add( mesh );
  99. mesh.dx = 0.001 * ( 0.5 - Math.random() );
  100. mesh.dy = 0.001 * ( 0.5 - Math.random() );
  101. meshes[ cube_count ] = mesh;
  102. cube_count += 1;
  103. }
  104. }
  105. document.addEventListener( 'mousemove', onDocumentMouseMove );
  106. //
  107. window.addEventListener( 'resize', onWindowResize );
  108. }
  109. function onWindowResize() {
  110. windowHalfX = window.innerWidth / 2;
  111. windowHalfY = window.innerHeight / 2;
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. function change_uvs( geometry, unitx, unity, offsetx, offsety ) {
  117. const uvs = geometry.attributes.uv.array;
  118. for ( let i = 0; i < uvs.length; i += 2 ) {
  119. uvs[ i ] = ( uvs[ i ] + offsetx ) * unitx;
  120. uvs[ i + 1 ] = ( uvs[ i + 1 ] + offsety ) * unity;
  121. }
  122. }
  123. function onDocumentMouseMove( event ) {
  124. mouseX = ( event.clientX - windowHalfX );
  125. mouseY = ( event.clientY - windowHalfY ) * 0.3;
  126. }
  127. //
  128. let h, counter = 1;
  129. function render() {
  130. const time = Date.now() * 0.00005;
  131. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  132. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  133. camera.lookAt( scene.position );
  134. for ( let i = 0; i < cube_count; i ++ ) {
  135. material = materials[ i ];
  136. h = ( 360 * ( material.hue + time ) % 360 ) / 360;
  137. material.color.setHSL( h, material.saturation, 0.5 );
  138. }
  139. if ( counter % 1000 > 200 ) {
  140. for ( let i = 0; i < cube_count; i ++ ) {
  141. mesh = meshes[ i ];
  142. mesh.rotation.x += 10 * mesh.dx;
  143. mesh.rotation.y += 10 * mesh.dy;
  144. mesh.position.x -= 150 * mesh.dx;
  145. mesh.position.y += 150 * mesh.dy;
  146. mesh.position.z += 300 * mesh.dx;
  147. }
  148. }
  149. if ( counter % 1000 === 0 ) {
  150. for ( let i = 0; i < cube_count; i ++ ) {
  151. mesh = meshes[ i ];
  152. mesh.dx *= - 1;
  153. mesh.dy *= - 1;
  154. }
  155. }
  156. counter ++;
  157. renderer.render( scene, camera );
  158. }
  159. </script>
  160. </body>
  161. </html>
粤ICP备19079148号