Skip to content

Commit b94e225

Browse files
committed
double the space
1 parent b5befcb commit b94e225

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/pages/particle-life-gpu/draw.wgsl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
1111
const 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
}

src/pages/particle-life-gpu/index.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export default function ParticleLifeGPUPage() {
2929
const canvas = canvasRef.current!
3030
const controller = new AbortController()
3131

32-
canvas.width = window.innerWidth * devicePixelRatio
33-
canvas.height = window.innerHeight * devicePixelRatio
32+
canvas.width = window.innerWidth * devicePixelRatio * 2
33+
canvas.height = window.innerHeight * devicePixelRatio * 2
3434

3535
const randomizeButton = randomizeRef.current!
3636

@@ -633,11 +633,25 @@ async function start({
633633
fragment: {
634634
module: drawModule,
635635
entryPoint: 'fs',
636-
targets: [{ format }],
637-
},
638-
primitive: {
639-
topology: 'point-list',
636+
targets: [{
637+
format,
638+
blend: {
639+
color: {
640+
srcFactor: 'src-alpha',
641+
dstFactor: 'one-minus-src-alpha',
642+
operation: 'add'
643+
},
644+
alpha: {
645+
srcFactor: 'one',
646+
dstFactor: 'one-minus-src-alpha',
647+
operation: 'add'
648+
}
649+
}
650+
}],
640651
},
652+
// primitive: {
653+
// topology: 'point-list',
654+
// },
641655
})
642656
const renderPassDescriptor = {
643657
label: 'draw render pass descriptor',
@@ -659,7 +673,7 @@ async function start({
659673
pass.setPipeline(drawPipeline)
660674
pass.setBindGroup(0, drawConfigBindGroup)
661675
pass.setBindGroup(1, drawParticlesBindGroup)
662-
pass.draw(1, particleCount)
676+
pass.draw(6, particleCount)
663677
pass.end()
664678
const commandBuffer = encoder.finish()
665679
device.queue.submit([commandBuffer])

src/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export const ROUTES = {
236236
image: particle_life_gpu_image
237237
},
238238
git: {
239-
lastModified: 1762626646000,
239+
lastModified: 1762630501000,
240240
firstAdded: 1762551884000
241241
},
242242
},

0 commit comments

Comments
 (0)