blob: e7e3d62f2f54120dedfd0d8ae78b71deb02a6be7 [file] [log] [blame]
dan sinclairb29892b2022-06-07 13:55:34 +00001@vertex
Ben Clayton01e4b6f2022-01-19 22:46:57 +00002fn 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 Claytond1232672021-05-18 09:24:18 +00003 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 sinclairb29892b2022-06-07 13:55:34 +00008@fragment
Ben Clayton01e4b6f2022-01-19 22:46:57 +00009fn frag_main() -> @location(0) vec4<f32> {
Ben Claytond1232672021-05-18 09:24:18 +000010 return vec4<f32>(1.0, 1.0, 1.0, 1.0);
11}
12
13struct Particle {
James Price3b671cb2022-03-28 14:31:22 +000014 pos : vec2<f32>,
15 vel : vec2<f32>,
James Price6b1e5f52022-01-19 18:11:17 +000016}
Ben Claytond1232672021-05-18 09:24:18 +000017
Ben Claytond1232672021-05-18 09:24:18 +000018struct SimParams {
James Price3b671cb2022-03-28 14:31:22 +000019 deltaT : f32,
20 rule1Distance : f32,
21 rule2Distance : f32,
22 rule3Distance : f32,
23 rule1Scale : f32,
24 rule2Scale : f32,
25 rule3Scale : f32,
James Price6b1e5f52022-01-19 18:11:17 +000026}
Ben Claytond1232672021-05-18 09:24:18 +000027
Ben Claytond1232672021-05-18 09:24:18 +000028struct Particles {
James Price3b671cb2022-03-28 14:31:22 +000029 particles : array<Particle, 5>,
James Price6b1e5f52022-01-19 18:11:17 +000030}
Ben Claytond1232672021-05-18 09:24:18 +000031
Ben Clayton01e4b6f2022-01-19 22:46:57 +000032@binding(0) @group(0) var<uniform> params : SimParams;
Ben Claytond1232672021-05-18 09:24:18 +000033
Ben Clayton01e4b6f2022-01-19 22:46:57 +000034@binding(1) @group(0) var<storage, read_write> particlesA : Particles;
Ben Claytond1232672021-05-18 09:24:18 +000035
Ben Clayton01e4b6f2022-01-19 22:46:57 +000036@binding(2) @group(0) var<storage, read_write> particlesB : Particles;
Ben Claytond1232672021-05-18 09:24:18 +000037
dan sinclairb29892b2022-06-07 13:55:34 +000038@compute @workgroup_size(1)
Ben Clayton01e4b6f2022-01-19 22:46:57 +000039fn comp_main(@builtin(global_invocation_id) gl_GlobalInvocationID : vec3<u32>) {
Ben Claytond1232672021-05-18 09:24:18 +000040 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 Clayton1b03f0a2021-07-08 21:23:33 +000053 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 Claytond1232672021-05-18 09:24:18 +000069 }
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 sinclair6cc183c2023-03-02 21:28:45 +000078 vVel = (normalize(vVel) * clamp(length(vVel), 0.0, 0.10000000000000000555));
Ben Claytond1232672021-05-18 09:24:18 +000079 vPos = (vPos + (vVel * params.deltaT));
dan sinclairb3b027d2022-10-26 15:20:47 +000080 if ((vPos.x < -(1.0))) {
Ben Claytond1232672021-05-18 09:24:18 +000081 vPos.x = 1.0;
82 }
83 if ((vPos.x > 1.0)) {
dan sinclairb3b027d2022-10-26 15:20:47 +000084 vPos.x = -(1.0);
Ben Claytond1232672021-05-18 09:24:18 +000085 }
dan sinclairb3b027d2022-10-26 15:20:47 +000086 if ((vPos.y < -(1.0))) {
Ben Claytond1232672021-05-18 09:24:18 +000087 vPos.y = 1.0;
88 }
89 if ((vPos.y > 1.0)) {
dan sinclairb3b027d2022-10-26 15:20:47 +000090 vPos.y = -(1.0);
Ben Claytond1232672021-05-18 09:24:18 +000091 }
92 particlesB.particles[index].pos = vPos;
93 particlesB.particles[index].vel = vVel;
94}