Skip to content

Commit 6bd7487

Browse files
committed
Update example to temporal blur
1 parent 127272e commit 6bd7487

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

examples/webgpu_compute_texture_pingpong.html

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525
<script type="module">
2626

2727
import * as THREE from 'three';
28-
import { texture, textureStore, wgslFn, code, instanceIndex } from 'three/nodes';
28+
import { texture, textureStore, wgslFn, code, instanceIndex, uniform } from 'three/nodes';
2929

3030
import WebGPU from 'three/addons/capabilities/WebGPU.js';
3131
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
3232
import StorageTexture from 'three/addons/renderers/common/StorageTexture.js';
3333

3434
let camera, scene, renderer;
35-
let computeToPing, computeToPong;
35+
let computeInitNode, computeToPing, computeToPong;
3636
let pingTexture, pongTexture;
3737
let material;
3838
let phase = true;
39+
let seed = uniform( new THREE.Vector2() );
3940

4041
init();
4142
render();
@@ -58,11 +59,21 @@
5859

5960
// texture
6061

62+
const hdr = true;
6163
const width = 512, height = 512;
6264

6365
pingTexture = new StorageTexture( width, height );
6466
pongTexture = new StorageTexture( width, height );
6567

68+
if ( hdr ) {
69+
70+
pingTexture.type = THREE.HalfFloatType;
71+
pongTexture.type = THREE.HalfFloatType;
72+
73+
}
74+
75+
const wgslFormat = hdr ? 'rgba16float' : 'rgba8unorm';
76+
6677
// compute init
6778

6879
const rand2 = code( `
@@ -71,18 +82,18 @@
7182
return fract( sin( dot( n, vec2f( 12.9898, 4.1414 ) ) ) * 43758.5453 );
7283
7384
}
74-
` );
7585
76-
const computeInitWGSL = wgslFn( `
77-
fn computeInitWGSL( writeTex: texture_storage_2d<rgba8unorm, write>, index: u32 ) -> void {
86+
fn blur( image : texture_2d<f32>, uv : vec2i ) -> vec4f {
7887
79-
let posX = index % ${ width };
80-
let posY = index / ${ width };
81-
let indexUV = vec2u( posX, posY );
82-
let uv = getUV( posX, posY );
88+
var color = vec4f( 0.0 );
8389
84-
textureStore( writeTex, indexUV, vec4f( vec3f( rand2( uv ) ), 1 ) );
90+
color += textureLoad( image, uv + vec2i( - 1, 1 ), 0 );
91+
color += textureLoad( image, uv + vec2i( - 1, - 1 ), 0 );
92+
color += textureLoad( image, uv + vec2i( 0, 0 ), 0 );
93+
color += textureLoad( image, uv + vec2i( 1, - 1 ), 0 );
94+
color += textureLoad( image, uv + vec2i( 1, 1 ), 0 );
8595
96+
return color / 5.0;
8697
}
8798
8899
fn getUV( posX: u32, posY: u32 ) -> vec2f {
@@ -91,27 +102,42 @@
91102
92103
return uv;
93104
105+
}
106+
` );
107+
108+
const computeInitWGSL = wgslFn( `
109+
fn computeInitWGSL( writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32, seed: vec2f ) -> void {
110+
111+
let posX = index % ${ width };
112+
let posY = index / ${ width };
113+
let indexUV = vec2u( posX, posY );
114+
let uv = getUV( posX, posY );
115+
116+
textureStore( writeTex, indexUV, vec4f( vec3f( rand2( uv + seed ) ), 1 ) );
117+
94118
}
95119
`, [ rand2 ] );
96120

97-
const computeInitNode = computeInitWGSL( { writeTex: textureStore( pingTexture ), index: instanceIndex } ).compute( width * height );
121+
computeInitNode = computeInitWGSL( { writeTex: textureStore( pingTexture ), index: instanceIndex, seed } ).compute( width * height );
98122

99123
// compute loop
100124

101125
const computePingPongWGSL = wgslFn( `
102-
fn computePingPongWGSL( readTex: texture_2d<f32>, writeTex: texture_storage_2d<rgba8unorm, write>, index: u32 ) -> void {
126+
fn computePingPongWGSL( readTex: texture_2d<f32>, writeTex: texture_storage_2d<${ wgslFormat }, write>, index: u32 ) -> void {
103127
104128
let posX = index % ${ width };
105129
let posY = index / ${ width };
106-
let indexUV = vec2u( posX, posY );
130+
let indexUV = vec2i( i32( posX ), i32( posY ) );
107131
108-
let color = vec3f( rand2( textureLoad( readTex, indexUV, 0 ).xy ) );
132+
let color = blur( readTex, indexUV ).rgb;
109133
110134
textureStore( writeTex, indexUV, vec4f( color, 1 ) );
111135
112136
}
113137
`, [ rand2 ] );
114138

139+
//
140+
115141
computeToPong = computePingPongWGSL( { readTex: texture( pingTexture ), writeTex: textureStore( pongTexture ), index: instanceIndex } ).compute( width * height );
116142
computeToPing = computePingPongWGSL( { readTex: texture( pongTexture ), writeTex: textureStore( pingTexture ), index: instanceIndex } ).compute( width * height );
117143

@@ -155,6 +181,16 @@
155181

156182
function render() {
157183

184+
// reset every 50 frames
185+
186+
if ( renderer.info.render.frame % 50 === 0 ) {
187+
188+
seed.value.set( Math.random(), Math.random() );
189+
190+
renderer.compute( computeInitNode );
191+
192+
}
193+
158194
// compute step
159195

160196
renderer.compute( phase ? computeToPong : computeToPing );

0 commit comments

Comments
 (0)