webgpu_textures_partialupdate.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - texture - webgpu partial 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - partial webgpu texture update <br/>
  12. replace parts of an existing texture with all data of another texture
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/",
  19. "three/nodes": "./jsm/nodes/Nodes.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  26. let camera, scene, renderer, clock, dataTexture, diffuseMap;
  27. let last = 0;
  28. const position = new THREE.Vector2();
  29. const color = new THREE.Color();
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  33. camera.position.z = 2;
  34. scene = new THREE.Scene();
  35. clock = new THREE.Clock();
  36. const loader = new THREE.TextureLoader();
  37. diffuseMap = loader.load( 'textures/carbon/Carbon.png', animate );
  38. diffuseMap.colorSpace = THREE.SRGBColorSpace;
  39. diffuseMap.minFilter = THREE.LinearFilter;
  40. diffuseMap.generateMipmaps = false;
  41. const geometry = new THREE.PlaneGeometry( 2, 2 );
  42. const material = new THREE.MeshBasicMaterial( { map: diffuseMap } );
  43. const mesh = new THREE.Mesh( geometry, material );
  44. scene.add( mesh );
  45. //
  46. const width = 32;
  47. const height = 32;
  48. const data = new Uint8Array( width * height * 4 );
  49. dataTexture = new THREE.DataTexture( data, width, height );
  50. //
  51. renderer = new WebGPURenderer( { antialias: true, forceWebGL: false } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. document.body.appendChild( renderer.domElement );
  55. //
  56. window.addEventListener( 'resize', onWindowResize );
  57. }
  58. function onWindowResize() {
  59. camera.aspect = window.innerWidth / window.innerHeight;
  60. camera.updateProjectionMatrix();
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. }
  63. async function animate() {
  64. requestAnimationFrame( animate );
  65. const elapsedTime = clock.getElapsedTime();
  66. await renderer.renderAsync( scene, camera );
  67. if ( elapsedTime - last > 0.1 ) {
  68. last = elapsedTime;
  69. position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  70. position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  71. // generate new color data
  72. updateDataTexture( dataTexture );
  73. // perform copy from src to dest texture to a random position
  74. renderer.copyTextureToTexture( dataTexture, diffuseMap, null, position );
  75. }
  76. }
  77. function updateDataTexture( texture ) {
  78. const size = texture.image.width * texture.image.height;
  79. const data = texture.image.data;
  80. // generate a random color and update texture data
  81. color.setHex( Math.random() * 0xffffff );
  82. const r = Math.floor( color.r * 255 );
  83. const g = Math.floor( color.g * 255 );
  84. const b = Math.floor( color.b * 255 );
  85. for ( let i = 0; i < size; i ++ ) {
  86. const stride = i * 4;
  87. data[ stride ] = r;
  88. data[ stride + 1 ] = g;
  89. data[ stride + 2 ] = b;
  90. data[ stride + 3 ] = 1;
  91. }
  92. texture.needsUpdate = true;
  93. }
  94. </script>
  95. </body>
  96. </html>