@@ -7,7 +7,7 @@ struct Config {
77@group (1 ) @binding (0 ) var <storage , read > particlePositions : array <vec2f >;
88@group (1 ) @binding (1 ) var <storage , read > particleColors : array <u32 >;
99
10- const size = 1 .0 ;
10+ const size = 2 .0 ;
1111const positions = array <vec2f , 6 >(
1212 vec2f (- 1 , - 1 ),
1313 vec2f ( 1 , - 1 ),
@@ -33,26 +33,35 @@ struct VertexOutput {
3333 @builtin (position ) position : vec4f ,
3434 // color of the particle
3535 @location (1 ) color : vec4f ,
36+ // offset from center (for fragment shader)
37+ @location (0 ) offset : vec2f ,
3638}
3739
3840
3941// for each particle, draw a square centered at its position (i.e. 6 vertices per particle to make a square)
4042@vertex fn vs (
43+ @builtin (vertex_index ) vertexID : u32 ,
4144 @builtin (instance_index ) instanceID : u32
4245) -> VertexOutput {
4346 let pos = particlePositions [instanceID ]; // screen space
44- let vertex_pos = pos ; // screen space
47+ let offset = positions [vertexID ];
48+ let vertex_pos = pos + offset * size ; // screen space
4549 let clip_vertex_pos = vec4f (
4650 (vertex_pos . x / (config . width / 2 .0 )) - 1 .0 ,
4751 - ((vertex_pos . y / (config . height / 2 .0 )) - 1 .0 ),
4852 0 .0 ,
4953 1 .0
5054 );
5155 let color = colors [particleColors [instanceID ]];
52- return VertexOutput (clip_vertex_pos , color );
56+ return VertexOutput (clip_vertex_pos , color , offset );
5357}
5458
5559// paint a circle centered at offset
5660@fragment fn fs (data : VertexOutput ) -> @location (0 ) vec4f {
57- return data . color ;
61+ let dist = length (data . offset );
62+ if (dist > 1 .0 ) {
63+ discard ;
64+ }
65+ let alpha = 1 .0 - smoothstep (0 .6 , 1 .0 , dist );
66+ return vec4f (data . color . rgb , data . color . a * alpha );
5867}
0 commit comments