2021-01-12 18:13:28 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// vertex shader
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-09-22 14:53:03 +00:00
|
|
|
[[stage(vertex)]]
|
2021-04-06 18:08:47 +00:00
|
|
|
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> {
|
2020-09-22 19:42:13 +00:00
|
|
|
var angle : f32 = -atan2(a_particleVel.x, a_particleVel.y);
|
2020-03-02 20:47:43 +00:00
|
|
|
var pos : vec2<f32> = vec2<f32>(
|
2020-09-22 19:42:13 +00:00
|
|
|
(a_pos.x * cos(angle)) - (a_pos.y * sin(angle)),
|
|
|
|
(a_pos.x * sin(angle)) + (a_pos.y * cos(angle)));
|
2021-04-06 18:08:47 +00:00
|
|
|
return vec4<f32>(pos + a_particlePos, 0.0, 1.0);
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:13:28 +00:00
|
|
|
// fragment shader
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-09-22 14:53:03 +00:00
|
|
|
[[stage(fragment)]]
|
2021-04-06 18:08:47 +00:00
|
|
|
fn frag_main() -> [[location(0)]] vec4<f32> {
|
|
|
|
return vec4<f32>(1.0, 1.0, 1.0, 1.0);
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:13:28 +00:00
|
|
|
// compute shader
|
2021-04-21 16:45:02 +00:00
|
|
|
struct Particle {
|
2021-03-15 10:43:11 +00:00
|
|
|
pos : vec2<f32>;
|
|
|
|
vel : vec2<f32>;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
2021-12-09 15:45:03 +00:00
|
|
|
struct SimParams {
|
2021-03-15 10:43:11 +00:00
|
|
|
deltaT : f32;
|
|
|
|
rule1Distance : f32;
|
|
|
|
rule2Distance : f32;
|
|
|
|
rule3Distance : f32;
|
|
|
|
rule1Scale : f32;
|
|
|
|
rule2Scale : f32;
|
|
|
|
rule3Scale : f32;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
2021-12-09 15:45:03 +00:00
|
|
|
struct Particles {
|
2021-03-15 10:43:11 +00:00
|
|
|
particles : array<Particle, 5>;
|
2020-03-02 20:47:43 +00:00
|
|
|
};
|
|
|
|
|
2021-04-21 17:55:12 +00:00
|
|
|
[[binding(0), group(0)]] var<uniform> params : SimParams;
|
2021-06-04 20:41:47 +00:00
|
|
|
[[binding(1), group(0)]] var<storage, read_write> particlesA : Particles;
|
|
|
|
[[binding(2), group(0)]] var<storage, read_write> particlesB : Particles;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-01-12 18:13:28 +00:00
|
|
|
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
|
2021-06-29 18:39:44 +00:00
|
|
|
[[stage(compute), workgroup_size(1)]]
|
2021-04-06 18:08:47 +00:00
|
|
|
fn comp_main(
|
2021-04-08 14:39:47 +00:00
|
|
|
[[builtin(global_invocation_id)]] gl_GlobalInvocationID : vec3<u32>) {
|
2020-03-02 20:47:43 +00:00
|
|
|
var index : u32 = gl_GlobalInvocationID.x;
|
2020-09-23 14:40:54 +00:00
|
|
|
if (index >= 5u) {
|
2020-03-02 20:47:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var vPos : vec2<f32> = particlesA.particles[index].pos;
|
|
|
|
var vVel : vec2<f32> = particlesA.particles[index].vel;
|
|
|
|
|
2020-05-01 19:02:35 +00:00
|
|
|
var cMass : vec2<f32> = vec2<f32>(0.0, 0.0);
|
|
|
|
var cVel : vec2<f32> = vec2<f32>(0.0, 0.0);
|
|
|
|
var colVel : vec2<f32> = vec2<f32>(0.0, 0.0);
|
2020-03-02 20:47:43 +00:00
|
|
|
var cMassCount : i32 = 0;
|
|
|
|
var cVelCount : i32 = 0;
|
|
|
|
|
|
|
|
var pos : vec2<f32>;
|
|
|
|
var vel : vec2<f32>;
|
2021-03-17 18:43:53 +00:00
|
|
|
for(var i : u32 = 0u; i < 5u; i = i + 1u) {
|
2020-03-02 20:47:43 +00:00
|
|
|
if (i == index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = particlesA.particles[i].pos.xy;
|
|
|
|
vel = particlesA.particles[i].vel.xy;
|
|
|
|
|
2020-09-22 19:42:13 +00:00
|
|
|
if (distance(pos, vPos) < params.rule1Distance) {
|
2020-03-02 20:47:43 +00:00
|
|
|
cMass = cMass + pos;
|
|
|
|
cMassCount = cMassCount + 1;
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (distance(pos, vPos) < params.rule2Distance) {
|
2020-03-02 20:47:43 +00:00
|
|
|
colVel = colVel - (pos - vPos);
|
|
|
|
}
|
2020-09-22 19:42:13 +00:00
|
|
|
if (distance(pos, vPos) < params.rule3Distance) {
|
2020-03-02 20:47:43 +00:00
|
|
|
cVel = cVel + vel;
|
|
|
|
cVelCount = cVelCount + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cMassCount > 0) {
|
2020-05-01 19:05:43 +00:00
|
|
|
cMass =
|
2020-09-30 14:00:10 +00:00
|
|
|
(cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos;
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
if (cVelCount > 0) {
|
2020-09-30 14:00:10 +00:00
|
|
|
cVel = cVel / vec2<f32>(f32(cVelCount), f32(cVelCount));
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vVel = vVel + (cMass * params.rule1Scale) + (colVel * params.rule2Scale) +
|
|
|
|
(cVel * params.rule3Scale);
|
|
|
|
|
2021-01-12 18:13:28 +00:00
|
|
|
// clamp velocity for a more pleasing simulation
|
2020-09-22 19:42:13 +00:00
|
|
|
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2021-01-12 18:13:28 +00:00
|
|
|
// kinematic update
|
2020-03-02 20:47:43 +00:00
|
|
|
vPos = vPos + (vVel * params.deltaT);
|
|
|
|
|
2021-01-12 18:13:28 +00:00
|
|
|
// Wrap around boundary
|
2020-03-02 20:47:43 +00:00
|
|
|
if (vPos.x < -1.0) {
|
|
|
|
vPos.x = 1.0;
|
|
|
|
}
|
|
|
|
if (vPos.x > 1.0) {
|
|
|
|
vPos.x = -1.0;
|
|
|
|
}
|
|
|
|
if (vPos.y < -1.0) {
|
|
|
|
vPos.y = 1.0;
|
|
|
|
}
|
|
|
|
if (vPos.y > 1.0) {
|
|
|
|
vPos.y = -1.0;
|
|
|
|
}
|
|
|
|
|
2021-01-12 18:13:28 +00:00
|
|
|
// Write back
|
2020-03-02 20:47:43 +00:00
|
|
|
particlesB.particles[index].pos = vPos;
|
|
|
|
particlesB.particles[index].vel = vVel;
|
|
|
|
}
|