blob: dd981f4a868f9bbb1c5a89d3881d3c5e9a83c55e [file] [log] [blame]
Ben Claytond1232672021-05-18 09:24:18 +00001[[stage(vertex)]]
2fn vert_main([[location(0)]] a_particlePos : vec2<f32>, [[location(1)]] a_particleVel : vec2<f32>, [[location(2)]] a_pos : vec2<f32>) -> [[builtin(position)]] vec4<f32> {
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
8[[stage(fragment)]]
9fn frag_main() -> [[location(0)]] vec4<f32> {
10 return vec4<f32>(1.0, 1.0, 1.0, 1.0);
11}
12
13struct Particle {
14 pos : vec2<f32>;
15 vel : vec2<f32>;
16};
17
18[[block]]
19struct SimParams {
20 deltaT : f32;
21 rule1Distance : f32;
22 rule2Distance : f32;
23 rule3Distance : f32;
24 rule1Scale : f32;
25 rule2Scale : f32;
26 rule3Scale : f32;
27};
28
29[[block]]
30struct Particles {
31 particles : array<Particle, 5>;
32};
33
34[[binding(0), group(0)]] var<uniform> params : SimParams;
35
Ben Clayton93e8f522021-06-04 20:41:47 +000036[[binding(1), group(0)]] var<storage, read_write> particlesA : Particles;
Ben Claytond1232672021-05-18 09:24:18 +000037
Ben Clayton93e8f522021-06-04 20:41:47 +000038[[binding(2), group(0)]] var<storage, read_write> particlesB : Particles;
Ben Claytond1232672021-05-18 09:24:18 +000039
Sarahe6cb51e2021-06-29 18:39:44 +000040[[stage(compute), workgroup_size(1)]]
Ben Claytond1232672021-05-18 09:24:18 +000041fn comp_main([[builtin(global_invocation_id)]] gl_GlobalInvocationID : vec3<u32>) {
42 var index : u32 = gl_GlobalInvocationID.x;
43 if ((index >= 5u)) {
44 return;
45 }
46 var vPos : vec2<f32> = particlesA.particles[index].pos;
47 var vVel : vec2<f32> = particlesA.particles[index].vel;
48 var cMass : vec2<f32> = vec2<f32>(0.0, 0.0);
49 var cVel : vec2<f32> = vec2<f32>(0.0, 0.0);
50 var colVel : vec2<f32> = vec2<f32>(0.0, 0.0);
51 var cMassCount : i32 = 0;
52 var cVelCount : i32 = 0;
53 var pos : vec2<f32>;
54 var vel : vec2<f32>;
Ben Clayton1b03f0a2021-07-08 21:23:33 +000055 for(var i : u32 = 0u; (i < 5u); i = (i + 1u)) {
56 if ((i == index)) {
57 continue;
58 }
59 pos = particlesA.particles[i].pos.xy;
60 vel = particlesA.particles[i].vel.xy;
61 if ((distance(pos, vPos) < params.rule1Distance)) {
62 cMass = (cMass + pos);
63 cMassCount = (cMassCount + 1);
64 }
65 if ((distance(pos, vPos) < params.rule2Distance)) {
66 colVel = (colVel - (pos - vPos));
67 }
68 if ((distance(pos, vPos) < params.rule3Distance)) {
69 cVel = (cVel + vel);
70 cVelCount = (cVelCount + 1);
Ben Claytond1232672021-05-18 09:24:18 +000071 }
72 }
73 if ((cMassCount > 0)) {
74 cMass = ((cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos);
75 }
76 if ((cVelCount > 0)) {
77 cVel = (cVel / vec2<f32>(f32(cVelCount), f32(cVelCount)));
78 }
79 vVel = (((vVel + (cMass * params.rule1Scale)) + (colVel * params.rule2Scale)) + (cVel * params.rule3Scale));
80 vVel = (normalize(vVel) * clamp(length(vVel), 0.0, 0.100000001));
81 vPos = (vPos + (vVel * params.deltaT));
82 if ((vPos.x < -1.0)) {
83 vPos.x = 1.0;
84 }
85 if ((vPos.x > 1.0)) {
86 vPos.x = -1.0;
87 }
88 if ((vPos.y < -1.0)) {
89 vPos.y = 1.0;
90 }
91 if ((vPos.y > 1.0)) {
92 vPos.y = -1.0;
93 }
94 particlesB.particles[index].pos = vPos;
95 particlesB.particles[index].vel = vVel;
96}