Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance WebGPU partial update texture example with random size option in GUI #30456

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions examples/webgpu_textures_partialupdate.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@
<script type="module">

import * as THREE from 'three';
import { FullScreenQuad } from 'three/addons/postprocessing/Pass.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

let camera, scene, renderer, clock, dataTexture, diffuseMap;
let rt, rtQuad, rtScene, rtCamera;
let gui, params;

let last = 0;
const position = new THREE.Vector2();
const color = new THREE.Color();

init();

function init() {
async function init() {

params = { randomSize: false };
gui = new GUI();
gui.add( params, 'randomSize' ).name( 'Random Size' );

camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 2;

Expand Down Expand Up @@ -65,11 +73,18 @@
const data = new Uint8Array( width * height * 4 );
dataTexture = new THREE.DataTexture( data, width, height );

rt = new THREE.RenderTarget( 16, 16, { depth: false, stencil: false } );
rtQuad = new FullScreenQuad( new THREE.MeshBasicMaterial( { map: dataTexture } ) );
rtScene = new THREE.Scene();
rtScene.add( rtQuad._mesh );
rtCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );

//

renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: false } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
await renderer.init();

document.body.appendChild( renderer.domElement );

Expand Down Expand Up @@ -102,20 +117,35 @@

last = elapsedTime;

position.x = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;
position.y = ( 32 * THREE.MathUtils.randInt( 1, 16 ) ) - 32;

// generate new color data
updateDataTexture( dataTexture );

// perform copy from src to dest texture to a random position

renderer.copyTextureToTexture( dataTexture, diffuseMap, null, position );
const size = Math.pow( 2, params.randomSize ? THREE.MathUtils.randInt( 2, 7 ) : 4 );
position.x = ( size * THREE.MathUtils.randInt( 1, 16 * 32 / size ) ) - size;
position.y = ( size * THREE.MathUtils.randInt( 1, 16 * 32 / size ) ) - size;

if ( params.randomSize ) {

rt.setSize( size, size );
renderer.setRenderTarget( rt );
renderer.render( rtScene, rtCamera );
renderer.setRenderTarget( null );

renderer.copyTextureToTexture( rt.texture, diffuseMap, null, position );

} else {

renderer.copyTextureToTexture( dataTexture, diffuseMap, null, position );

}

}

}


function updateDataTexture( texture ) {

const size = texture.image.width * texture.image.height;
Expand Down