|
| 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 | + |
| 11 | + <div id="info"> |
| 12 | + <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - partial webgpu texture update <br/> |
| 13 | + replace parts of an existing texture with all data of another texture |
| 14 | + </div> |
| 15 | + |
| 16 | + <script type="importmap"> |
| 17 | + { |
| 18 | + "imports": { |
| 19 | + "three": "../build/three.module.js", |
| 20 | + "three/addons/": "./jsm/", |
| 21 | + "three/nodes": "./jsm/nodes/Nodes.js" |
| 22 | + } |
| 23 | + } |
| 24 | + </script> |
| 25 | + |
| 26 | + <script type="module"> |
| 27 | + |
| 28 | + import * as THREE from 'three'; |
| 29 | + import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js'; |
| 30 | + |
| 31 | + let camera, scene, renderer, clock, dataTexture, diffuseMap; |
| 32 | + |
| 33 | + let last = 0; |
| 34 | + const position = new THREE.Vector2(); |
| 35 | + const color = new THREE.Color(); |
| 36 | + |
| 37 | + init(); |
| 38 | + |
| 39 | + function init() { |
| 40 | + |
| 41 | + camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 ); |
| 42 | + camera.position.z = 2; |
| 43 | + |
| 44 | + scene = new THREE.Scene(); |
| 45 | + |
| 46 | + clock = new THREE.Clock(); |
| 47 | + |
| 48 | + const loader = new THREE.TextureLoader(); |
| 49 | + diffuseMap = loader.load( 'textures/carbon/Carbon.png', animate ); |
| 50 | + diffuseMap.colorSpace = THREE.SRGBColorSpace; |
| 51 | + diffuseMap.minFilter = THREE.LinearFilter; |
| 52 | + diffuseMap.generateMipmaps = false; |
| 53 | + |
| 54 | + const geometry = new THREE.PlaneGeometry( 2, 2 ); |
| 55 | + const material = new THREE.MeshBasicMaterial( { map: diffuseMap } ); |
| 56 | + |
| 57 | + const mesh = new THREE.Mesh( geometry, material ); |
| 58 | + scene.add( mesh ); |
| 59 | + |
| 60 | + // |
| 61 | + |
| 62 | + const width = 32; |
| 63 | + const height = 32; |
| 64 | + |
| 65 | + const data = new Uint8Array( width * height * 4 ); |
| 66 | + dataTexture = new THREE.DataTexture( data, width, height ); |
| 67 | + |
| 68 | + // |
| 69 | + |
| 70 | + renderer = new WebGPURenderer( { antialias: true, forceWebGL: false } ); |
| 71 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 72 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 73 | + |
| 74 | + document.body.appendChild( renderer.domElement ); |
| 75 | + |
| 76 | + // |
| 77 | + |
| 78 | + window.addEventListener( 'resize', onWindowResize ); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + function onWindowResize() { |
| 83 | + |
| 84 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 85 | + camera.updateProjectionMatrix(); |
| 86 | + |
| 87 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + async function animate() { |
| 92 | + |
| 93 | + requestAnimationFrame( animate ); |
| 94 | + |
| 95 | + const elapsedTime = clock.getElapsedTime(); |
| 96 | + |
| 97 | + |
| 98 | + await renderer.renderAsync( scene, camera ); |
| 99 | + |
| 100 | + |
| 101 | + if ( elapsedTime - last > 0.1 ) { |
| 102 | + |
| 103 | + last = elapsedTime; |
| 104 | + |
| 105 | + position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32; |
| 106 | + position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32; |
| 107 | + |
| 108 | + // generate new color data |
| 109 | + updateDataTexture( dataTexture ); |
| 110 | + |
| 111 | + // perform copy from src to dest texture to a random position |
| 112 | + |
| 113 | + renderer.copyTextureToTexture( position, dataTexture, diffuseMap ); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + function updateDataTexture( texture ) { |
| 120 | + |
| 121 | + const size = texture.image.width * texture.image.height; |
| 122 | + const data = texture.image.data; |
| 123 | + |
| 124 | + // generate a random color and update texture data |
| 125 | + |
| 126 | + color.setHex( Math.random() * 0xffffff ); |
| 127 | + |
| 128 | + const r = Math.floor( color.r * 255 ); |
| 129 | + const g = Math.floor( color.g * 255 ); |
| 130 | + const b = Math.floor( color.b * 255 ); |
| 131 | + |
| 132 | + for ( let i = 0; i < size; i ++ ) { |
| 133 | + |
| 134 | + const stride = i * 4; |
| 135 | + |
| 136 | + data[ stride ] = r; |
| 137 | + data[ stride + 1 ] = g; |
| 138 | + data[ stride + 2 ] = b; |
| 139 | + data[ stride + 3 ] = 1; |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + texture.needsUpdate = true; |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + </script> |
| 148 | + |
| 149 | + </body> |
| 150 | +</html> |
0 commit comments