Skip to content

Commit d6c8dd0

Browse files
committedSep 28, 2023
Improved compute texture examples.
1 parent f886330 commit d6c8dd0

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed
 

‎examples/webgpu_compute_texture.html

+14-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
<div id="info">
1111
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute Texture
12-
<br>Texture generated using GPU Compute.
1312
</div>
1413

1514
<script type="importmap">
@@ -66,17 +65,25 @@
6665
let posX = index % ${ width };
6766
let posY = index / ${ width };
6867
let indexUV = vec2u( posX, posY );
69-
let uv = getUV( posX, posY );
7068
71-
textureStore( storageTex, indexUV, vec4f( uv, 0, 1 ) );
69+
// https://www.shadertoy.com/view/Xst3zN
7270
73-
}
71+
let x = f32( posX ) / 50.0;
72+
let y = f32( posY ) / 50.0;
73+
74+
let v1 = sin( x );
75+
let v2 = sin( y );
76+
let v3 = sin( x + y );
77+
let v4 = sin( sqrt( x * x + y * y ) + 5.0 );
78+
let v = v1 + v2 + v3 + v4;
7479
75-
fn getUV( posX: u32, posY: u32 ) -> vec2f {
80+
let PI = 3.14159265359;
7681
77-
let uv = vec2f( f32( posX ) / ${ width }.0, f32( posY ) / ${ height }.0 );
82+
let r = sin( v );
83+
let g = sin( v + PI );
84+
let b = sin( v + PI - 0.5 );
7885
79-
return uv;
86+
textureStore( storageTex, indexUV, vec4f( r, g, b, 1 ) );
8087
8188
}
8289
` );

‎examples/webgpu_compute_texture_pingpong.html

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
<div id="info">
1111
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - Compute Ping/Pong Texture
12-
<br>Texture generated using GPU Compute.
1312
</div>
1413

1514
<script type="importmap">
@@ -113,7 +112,11 @@
113112
let indexUV = vec2u( posX, posY );
114113
let uv = getUV( posX, posY );
115114
116-
textureStore( writeTex, indexUV, vec4f( vec3f( rand2( uv + seed ) ), 1 ) );
115+
let r = rand2( uv + seed * 100 ) - rand2( uv + seed * 300 );
116+
let g = rand2( uv + seed * 200 ) - rand2( uv + seed * 300 );
117+
let b = rand2( uv + seed * 200 ) - rand2( uv + seed * 100 );
118+
119+
textureStore( writeTex, indexUV, vec4( r, g, b, 1 ) );
117120
118121
}
119122
`, [ rand2 ] );
@@ -131,7 +134,7 @@
131134
132135
let color = blur( readTex, indexUV ).rgb;
133136
134-
textureStore( writeTex, indexUV, vec4f( color, 1 ) );
137+
textureStore( writeTex, indexUV, vec4f( color * 1.05, 1 ) );
135138
136139
}
137140
`, [ rand2 ] );
@@ -181,9 +184,9 @@
181184

182185
function render() {
183186

184-
// reset every 50 frames
187+
// reset every 200 frames
185188

186-
if ( renderer.info.render.frame % 50 === 0 ) {
189+
if ( renderer.info.render.frame % 200 === 0 ) {
187190

188191
seed.value.set( Math.random(), Math.random() );
189192

0 commit comments

Comments
 (0)
Please sign in to comment.