webgpu_textures_partialupdate.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - webgpu partial texture update</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 webgl - webgpu partial texture update">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_textures_partialupdate.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_textures_partialupdate.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  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>Partial Texture Update</span>
  18. </div>
  19. <small>
  20. Replace parts of an existing texture with all data of another texture.
  21. </small>
  22. </div>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../build/three.webgpu.js",
  27. "three/webgpu": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.tsl.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three/webgpu';
  35. let camera, scene, renderer, timer, dataTexture, diffuseMap;
  36. let last = 0;
  37. const position = new THREE.Vector2();
  38. const color = new THREE.Color();
  39. init();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  42. camera.position.z = 2;
  43. scene = new THREE.Scene();
  44. timer = new THREE.Timer();
  45. timer.connect( document );
  46. const loader = new THREE.TextureLoader();
  47. diffuseMap = loader.load( 'textures/carbon/Carbon.png' );
  48. diffuseMap.colorSpace = THREE.SRGBColorSpace;
  49. diffuseMap.minFilter = THREE.LinearFilter;
  50. diffuseMap.generateMipmaps = false;
  51. const geometry = new THREE.PlaneGeometry( 2, 2 );
  52. const material = new THREE.MeshBasicMaterial( { map: diffuseMap } );
  53. const mesh = new THREE.Mesh( geometry, material );
  54. scene.add( mesh );
  55. //
  56. const width = 32;
  57. const height = 32;
  58. const data = new Uint8Array( width * height * 4 );
  59. dataTexture = new THREE.DataTexture( data, width, height );
  60. dataTexture.colorSpace = THREE.SRGBColorSpace;
  61. //
  62. renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: false } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. renderer.setAnimationLoop( animate );
  66. document.body.appendChild( renderer.domElement );
  67. //
  68. window.addEventListener( 'resize', onWindowResize );
  69. }
  70. function onWindowResize() {
  71. camera.aspect = window.innerWidth / window.innerHeight;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. }
  75. function animate() {
  76. timer.update();
  77. const elapsedTime = timer.getElapsed();
  78. renderer.render( scene, camera );
  79. if ( elapsedTime - last > 0.1 ) {
  80. last = elapsedTime;
  81. position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  82. position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  83. // generate new color data
  84. updateDataTexture( dataTexture );
  85. // perform copy from src to dest texture to a random position
  86. renderer.copyTextureToTexture( dataTexture, diffuseMap, null, position );
  87. }
  88. }
  89. function updateDataTexture( texture ) {
  90. const size = texture.image.width * texture.image.height;
  91. const data = texture.image.data;
  92. // generate a random color and update texture data
  93. color.setHex( Math.random() * 0xffffff );
  94. const r = Math.floor( color.r * 255 );
  95. const g = Math.floor( color.g * 255 );
  96. const b = Math.floor( color.b * 255 );
  97. for ( let i = 0; i < size; i ++ ) {
  98. const stride = i * 4;
  99. data[ stride ] = r;
  100. data[ stride + 1 ] = g;
  101. data[ stride + 2 ] = b;
  102. data[ stride + 3 ] = 1;
  103. }
  104. texture.needsUpdate = true;
  105. }
  106. </script>
  107. </body>
  108. </html>
粤ICP备19079148号