123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <html lang="en">
- <head>
- <title>three.js - WebGPU - Compute Ping/Pong Texture</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute Ping/Pong Texture
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/tsl": "../build/three.webgpu.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three';
- import { storageTexture, wgslFn, code, instanceIndex, uniform } from 'three/tsl';
- import WebGPU from 'three/addons/capabilities/WebGPU.js';
- let camera, scene, renderer;
- let computeInitNode, computeToPing, computeToPong;
- let pingTexture, pongTexture;
- let material;
- let phase = true;
- let lastUpdate = - 1;
- const seed = uniform( new THREE.Vector2() );
- init();
- function init() {
- if ( WebGPU.isAvailable() === false ) {
- document.body.appendChild( WebGPU.getErrorMessage() );
- throw new Error( 'No WebGPU support' );
- }
- const aspect = window.innerWidth / window.innerHeight;
- camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
- camera.position.z = 1;
- scene = new THREE.Scene();
- // texture
- const hdr = true;
- const width = 512, height = 512;
- pingTexture = new THREE.StorageTexture( width, height );
- pongTexture = new THREE.StorageTexture( width, height );
- if ( hdr ) {
- pingTexture.type = THREE.HalfFloatType;
- pongTexture.type = THREE.HalfFloatType;
- }
- const wgslFormat = hdr ? 'rgba16float' : 'rgba8unorm';
-
- const readPing = storageTexture( pingTexture ).setAccess( 'read-only' );
- const writePing = storageTexture( pingTexture ).setAccess( 'write-only' );
- const readPong = storageTexture( pongTexture ).setAccess( 'read-only' );
- const writePong = storageTexture( pongTexture ).setAccess( 'write-only' );
- // compute init
- const rand2 = code( `
- fn rand2( n: vec2f ) -> f32 {
- return fract( sin( dot( n, vec2f( 12.9898, 4.1414 ) ) ) * 43758.5453 );
- }
- fn blur( image : texture_storage_2d<${wgslFormat}, read>, uv : vec2i ) -> vec4f {
- var color = vec4f( 0.0 );
- color += textureLoad( image, uv + vec2i( - 1, 1 ));
- color += textureLoad( image, uv + vec2i( - 1, - 1 ));
- color += textureLoad( image, uv + vec2i( 0, 0 ));
- color += textureLoad( image, uv + vec2i( 1, - 1 ));
- color += textureLoad( image, uv + vec2i( 1, 1 ));
- return color / 5.0;
- }
- fn getUV( posX: u32, posY: u32 ) -> vec2f {
- let uv = vec2f( f32( posX ) / ${ width }.0, f32( posY ) / ${ height }.0 );
- return uv;
- }
- ` );
- const computeInitWGSL = wgslFn( `
- fn computeInitWGSL( writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32, seed: vec2f ) -> void {
- let posX = index % ${ width };
- let posY = index / ${ width };
- let indexUV = vec2u( posX, posY );
- let uv = getUV( posX, posY );
- let r = rand2( uv + seed * 100 ) - rand2( uv + seed * 300 );
- let g = rand2( uv + seed * 200 ) - rand2( uv + seed * 300 );
- let b = rand2( uv + seed * 200 ) - rand2( uv + seed * 100 );
- textureStore( writeTex, indexUV, vec4( r, g, b, 1 ) );
- }
- `, [ rand2 ] );
- computeInitNode = computeInitWGSL( { writeTex: storageTexture( pingTexture ), index: instanceIndex, seed } ).compute( width * height );
- // compute loop
- const computePingPongWGSL = wgslFn( `
- fn computePingPongWGSL( readTex: texture_storage_2d<${wgslFormat}, read>, writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32 ) -> void {
- let posX = index % ${ width };
- let posY = index / ${ width };
- let indexUV = vec2i( i32( posX ), i32( posY ) );
- let color = blur( readTex, indexUV ).rgb;
- textureStore( writeTex, indexUV, vec4f( color * 1.05, 1 ) );
- }
- `, [ rand2 ] );
- //
- computeToPong = computePingPongWGSL( { readTex: readPing, writeTex: writePong, index: instanceIndex } ).compute( width * height );
- computeToPing = computePingPongWGSL( { readTex: readPong, writeTex: writePing, index: instanceIndex } ).compute( width * height );
- //
- material = new THREE.MeshBasicMaterial( { color: 0xffffff, map: pongTexture } );
- const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
- scene.add( plane );
- renderer = new THREE.WebGPURenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( render );
- document.body.appendChild( renderer.domElement );
- window.addEventListener( 'resize', onWindowResize );
- // compute init
- renderer.computeAsync( computeInitNode );
- }
- function onWindowResize() {
- renderer.setSize( window.innerWidth, window.innerHeight );
- const aspect = window.innerWidth / window.innerHeight;
- const frustumHeight = camera.top - camera.bottom;
- camera.left = - frustumHeight * aspect / 2;
- camera.right = frustumHeight * aspect / 2;
- camera.updateProjectionMatrix();
- }
- function render() {
- const time = performance.now();
- const seconds = Math.floor( time / 1000 );
- // reset every second
- if ( phase && seconds !== lastUpdate ) {
- seed.value.set( Math.random(), Math.random() );
- renderer.compute( computeInitNode );
- lastUpdate = seconds;
- }
- // compute step
- renderer.compute( phase ? computeToPong : computeToPing );
- material.map = phase ? pongTexture : pingTexture;
- phase = ! phase;
- // render step
- // update material texture node
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|