dan sinclair | b29892b | 2022-06-07 13:55:34 +0000 | [diff] [blame] | 1 | @vertex |
Ben Clayton | 01e4b6f | 2022-01-19 22:46:57 +0000 | [diff] [blame] | 2 | fn vert_main(@location(0) a_particlePos : vec2<f32>, @location(1) a_particleVel : vec2<f32>, @location(2) a_pos : vec2<f32>) -> @builtin(position) vec4<f32> { |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 3 | var angle : f32 = -(atan2(a_particleVel.x, a_particleVel.y)); |
| 4 | var pos : vec2<f32> = vec2<f32>(((a_pos.x * cos(angle)) - (a_pos.y * sin(angle))), ((a_pos.x * sin(angle)) + (a_pos.y * cos(angle)))); |
| 5 | return vec4<f32>((pos + a_particlePos), 0.0, 1.0); |
| 6 | } |
| 7 | |
dan sinclair | b29892b | 2022-06-07 13:55:34 +0000 | [diff] [blame] | 8 | @fragment |
Ben Clayton | 01e4b6f | 2022-01-19 22:46:57 +0000 | [diff] [blame] | 9 | fn frag_main() -> @location(0) vec4<f32> { |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 10 | return vec4<f32>(1.0, 1.0, 1.0, 1.0); |
| 11 | } |
| 12 | |
| 13 | struct Particle { |
James Price | 3b671cb | 2022-03-28 14:31:22 +0000 | [diff] [blame] | 14 | pos : vec2<f32>, |
| 15 | vel : vec2<f32>, |
James Price | 6b1e5f5 | 2022-01-19 18:11:17 +0000 | [diff] [blame] | 16 | } |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 17 | |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 18 | struct SimParams { |
James Price | 3b671cb | 2022-03-28 14:31:22 +0000 | [diff] [blame] | 19 | deltaT : f32, |
| 20 | rule1Distance : f32, |
| 21 | rule2Distance : f32, |
| 22 | rule3Distance : f32, |
| 23 | rule1Scale : f32, |
| 24 | rule2Scale : f32, |
| 25 | rule3Scale : f32, |
James Price | 6b1e5f5 | 2022-01-19 18:11:17 +0000 | [diff] [blame] | 26 | } |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 27 | |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 28 | struct Particles { |
James Price | 3b671cb | 2022-03-28 14:31:22 +0000 | [diff] [blame] | 29 | particles : array<Particle, 5>, |
James Price | 6b1e5f5 | 2022-01-19 18:11:17 +0000 | [diff] [blame] | 30 | } |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 31 | |
Ben Clayton | 01e4b6f | 2022-01-19 22:46:57 +0000 | [diff] [blame] | 32 | @binding(0) @group(0) var<uniform> params : SimParams; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 33 | |
Ben Clayton | 01e4b6f | 2022-01-19 22:46:57 +0000 | [diff] [blame] | 34 | @binding(1) @group(0) var<storage, read_write> particlesA : Particles; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 35 | |
Ben Clayton | 01e4b6f | 2022-01-19 22:46:57 +0000 | [diff] [blame] | 36 | @binding(2) @group(0) var<storage, read_write> particlesB : Particles; |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 37 | |
dan sinclair | b29892b | 2022-06-07 13:55:34 +0000 | [diff] [blame] | 38 | @compute @workgroup_size(1) |
Ben Clayton | 01e4b6f | 2022-01-19 22:46:57 +0000 | [diff] [blame] | 39 | fn comp_main(@builtin(global_invocation_id) gl_GlobalInvocationID : vec3<u32>) { |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 40 | var index : u32 = gl_GlobalInvocationID.x; |
| 41 | if ((index >= 5u)) { |
| 42 | return; |
| 43 | } |
| 44 | var vPos : vec2<f32> = particlesA.particles[index].pos; |
| 45 | var vVel : vec2<f32> = particlesA.particles[index].vel; |
| 46 | var cMass : vec2<f32> = vec2<f32>(0.0, 0.0); |
| 47 | var cVel : vec2<f32> = vec2<f32>(0.0, 0.0); |
| 48 | var colVel : vec2<f32> = vec2<f32>(0.0, 0.0); |
| 49 | var cMassCount : i32 = 0; |
| 50 | var cVelCount : i32 = 0; |
| 51 | var pos : vec2<f32>; |
| 52 | var vel : vec2<f32>; |
Ben Clayton | 1b03f0a | 2021-07-08 21:23:33 +0000 | [diff] [blame] | 53 | for(var i : u32 = 0u; (i < 5u); i = (i + 1u)) { |
| 54 | if ((i == index)) { |
| 55 | continue; |
| 56 | } |
| 57 | pos = particlesA.particles[i].pos.xy; |
| 58 | vel = particlesA.particles[i].vel.xy; |
| 59 | if ((distance(pos, vPos) < params.rule1Distance)) { |
| 60 | cMass = (cMass + pos); |
| 61 | cMassCount = (cMassCount + 1); |
| 62 | } |
| 63 | if ((distance(pos, vPos) < params.rule2Distance)) { |
| 64 | colVel = (colVel - (pos - vPos)); |
| 65 | } |
| 66 | if ((distance(pos, vPos) < params.rule3Distance)) { |
| 67 | cVel = (cVel + vel); |
| 68 | cVelCount = (cVelCount + 1); |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | if ((cMassCount > 0)) { |
| 72 | cMass = ((cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos); |
| 73 | } |
| 74 | if ((cVelCount > 0)) { |
| 75 | cVel = (cVel / vec2<f32>(f32(cVelCount), f32(cVelCount))); |
| 76 | } |
| 77 | vVel = (((vVel + (cMass * params.rule1Scale)) + (colVel * params.rule2Scale)) + (cVel * params.rule3Scale)); |
dan sinclair | 6cc183c | 2023-03-02 21:28:45 +0000 | [diff] [blame] | 78 | vVel = (normalize(vVel) * clamp(length(vVel), 0.0, 0.10000000000000000555)); |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 79 | vPos = (vPos + (vVel * params.deltaT)); |
dan sinclair | b3b027d | 2022-10-26 15:20:47 +0000 | [diff] [blame] | 80 | if ((vPos.x < -(1.0))) { |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 81 | vPos.x = 1.0; |
| 82 | } |
| 83 | if ((vPos.x > 1.0)) { |
dan sinclair | b3b027d | 2022-10-26 15:20:47 +0000 | [diff] [blame] | 84 | vPos.x = -(1.0); |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 85 | } |
dan sinclair | b3b027d | 2022-10-26 15:20:47 +0000 | [diff] [blame] | 86 | if ((vPos.y < -(1.0))) { |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 87 | vPos.y = 1.0; |
| 88 | } |
| 89 | if ((vPos.y > 1.0)) { |
dan sinclair | b3b027d | 2022-10-26 15:20:47 +0000 | [diff] [blame] | 90 | vPos.y = -(1.0); |
Ben Clayton | d123267 | 2021-05-18 09:24:18 +0000 | [diff] [blame] | 91 | } |
| 92 | particlesB.particles[index].pos = vPos; |
| 93 | particlesB.particles[index].vel = vVel; |
| 94 | } |