webgl_materials_texture_partialupdate.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. let camera, scene, renderer, clock, dataTexture, diffuseMap;
  28. let last = 0;
  29. const position = new THREE.Vector2();
  30. const color = new THREE.Color();
  31. init();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  34. camera.position.z = 2;
  35. scene = new THREE.Scene();
  36. clock = new THREE.Clock();
  37. const loader = new THREE.TextureLoader();
  38. diffuseMap = loader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg', animate );
  39. diffuseMap.colorSpace = THREE.SRGBColorSpace;
  40. diffuseMap.minFilter = THREE.LinearFilter;
  41. diffuseMap.generateMipmaps = false;
  42. const geometry = new THREE.PlaneGeometry( 2, 2 );
  43. const material = new THREE.MeshBasicMaterial( { map: diffuseMap } );
  44. const mesh = new THREE.Mesh( geometry, material );
  45. scene.add( mesh );
  46. //
  47. const width = 32;
  48. const height = 32;
  49. const data = new Uint8Array( width * height * 4 );
  50. dataTexture = new THREE.DataTexture( data, width, height );
  51. //
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.useLegacyLights = false;
  56. document.body.appendChild( renderer.domElement );
  57. //
  58. window.addEventListener( 'resize', onWindowResize );
  59. }
  60. function onWindowResize() {
  61. camera.aspect = window.innerWidth / window.innerHeight;
  62. camera.updateProjectionMatrix();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. }
  65. function animate() {
  66. requestAnimationFrame( animate );
  67. const elapsedTime = clock.getElapsedTime();
  68. if ( elapsedTime - last > 0.1 ) {
  69. last = elapsedTime;
  70. position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  71. position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  72. // generate new color data
  73. updateDataTexture( dataTexture );
  74. // perform copy from src to dest texture to a random position
  75. renderer.copyTextureToTexture( position, dataTexture, diffuseMap );
  76. }
  77. renderer.render( scene, camera );
  78. }
  79. function updateDataTexture( texture ) {
  80. const size = texture.image.width * texture.image.height;
  81. const data = texture.image.data;
  82. // generate a random color and update texture data
  83. color.setHex( Math.random() * 0xffffff );
  84. const r = Math.floor( color.r * 255 );
  85. const g = Math.floor( color.g * 255 );
  86. const b = Math.floor( color.b * 255 );
  87. for ( let i = 0; i < size; i ++ ) {
  88. const stride = i * 4;
  89. data[ stride ] = r;
  90. data[ stride + 1 ] = g;
  91. data[ stride + 2 ] = b;
  92. data[ stride + 3 ] = 1;
  93. }
  94. }
  95. </script>
  96. </body>
  97. </html>