webgl_materials_texture_partialupdate.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - texture - 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 texture update <br/>
  12. replace parts of an existing texture with all data of another texture
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. let camera, scene, renderer, clock, dataTexture, diffuseMap;
  17. let last = 0;
  18. const position = new THREE.Vector2();
  19. const color = new THREE.Color();
  20. init();
  21. function init() {
  22. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  23. camera.position.z = 2;
  24. scene = new THREE.Scene();
  25. clock = new THREE.Clock();
  26. const loader = new THREE.TextureLoader();
  27. diffuseMap = loader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg', animate );
  28. diffuseMap.minFilter = THREE.LinearFilter;
  29. diffuseMap.generateMipmaps = false;
  30. const geometry = new THREE.PlaneGeometry( 2, 2 );
  31. const material = new THREE.MeshBasicMaterial( { map: diffuseMap } );
  32. const mesh = new THREE.Mesh( geometry, material );
  33. scene.add( mesh );
  34. //
  35. const width = 32;
  36. const height = 32;
  37. const data = new Uint8Array( width * height * 3 );
  38. dataTexture = new THREE.DataTexture( data, width, height, THREE.RGBFormat );
  39. //
  40. renderer = new THREE.WebGLRenderer( { antialias: true } );
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( window.innerWidth, window.innerHeight );
  43. document.body.appendChild( renderer.domElement );
  44. //
  45. window.addEventListener( 'resize', onWindowResize );
  46. }
  47. function onWindowResize() {
  48. camera.aspect = window.innerWidth / window.innerHeight;
  49. camera.updateProjectionMatrix();
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. }
  52. function animate() {
  53. requestAnimationFrame( animate );
  54. const elapsedTime = clock.getElapsedTime();
  55. if ( elapsedTime - last > 0.1 ) {
  56. last = elapsedTime;
  57. position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  58. position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  59. // generate new color data
  60. updateDataTexture( dataTexture );
  61. // perform copy from src to dest texture to a random position
  62. renderer.copyTextureToTexture( position, dataTexture, diffuseMap );
  63. }
  64. renderer.render( scene, camera );
  65. }
  66. function updateDataTexture( texture ) {
  67. const size = texture.image.width * texture.image.height;
  68. const data = texture.image.data;
  69. // generate a random color and update texture data
  70. color.setHex( Math.random() * 0xffffff );
  71. const r = Math.floor( color.r * 255 );
  72. const g = Math.floor( color.g * 255 );
  73. const b = Math.floor( color.b * 255 );
  74. for ( let i = 0; i < size; i ++ ) {
  75. const stride = i * 3;
  76. data[ stride ] = r;
  77. data[ stride + 1 ] = g;
  78. data[ stride + 2 ] = b;
  79. }
  80. }
  81. </script>
  82. </body>
  83. </html>