webgpu_materials_video.html 5.5 KB

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