webgl_materials_texture_partialupdate.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. var camera, scene, renderer, clock, dataTexture, diffuseMap;
  17. init();
  18. function init() {
  19. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  20. camera.position.z = 2;
  21. scene = new THREE.Scene();
  22. clock = new THREE.Clock();
  23. var loader = new THREE.TextureLoader();
  24. diffuseMap = loader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg', animate );
  25. diffuseMap.minFilter = THREE.LinearFilter;
  26. diffuseMap.generateMipmaps = false;
  27. var geometry = new THREE.PlaneBufferGeometry( 2, 2 );
  28. var material = new THREE.MeshBasicMaterial( { map: diffuseMap } );
  29. var mesh = new THREE.Mesh( geometry, material );
  30. scene.add( mesh );
  31. //
  32. var width = 32;
  33. var height = 32;
  34. var data = new Uint8Array( width * height * 3 );
  35. dataTexture = new THREE.DataTexture( data, width, height, THREE.RGBFormat );
  36. //
  37. renderer = new THREE.WebGLRenderer( { antialias: true } );
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. document.body.appendChild( renderer.domElement );
  41. //
  42. window.addEventListener( 'resize', onWindowResize, false );
  43. }
  44. function onWindowResize() {
  45. camera.aspect = window.innerWidth / window.innerHeight;
  46. camera.updateProjectionMatrix();
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. }
  49. var last = 0;
  50. var position = new THREE.Vector2();
  51. function animate() {
  52. requestAnimationFrame( animate );
  53. var elapsedTime = clock.getElapsedTime();
  54. if ( elapsedTime - last > 0.1 ) {
  55. last = elapsedTime;
  56. position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  57. position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
  58. // generate new color data
  59. updateDataTexture( dataTexture );
  60. // perform copy from src to dest texture to a random position
  61. renderer.copyTextureToTexture( position, dataTexture, diffuseMap );
  62. }
  63. renderer.render( scene, camera );
  64. }
  65. var color = new THREE.Color();
  66. function updateDataTexture( texture ) {
  67. var size = texture.image.width * texture.image.height;
  68. var data = texture.image.data;
  69. // generate a random color and update texture data
  70. color.setHex( Math.random() * 0xffffff );
  71. var r = Math.floor( color.r * 255 );
  72. var g = Math.floor( color.g * 255 );
  73. var b = Math.floor( color.b * 255 );
  74. for ( var i = 0; i < size; i ++ ) {
  75. var 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>