I think it is in some kind of working state now

This commit is contained in:
2025-08-01 22:46:57 +03:00
parent e2f1c8c13a
commit 15639ff098
4 changed files with 59 additions and 36 deletions

View File

@@ -7,8 +7,8 @@ layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(r32f, binding = 0) uniform restrict readonly image2D current_trail;
layout(r32f, binding = 1) uniform restrict writeonly image2D output_trail;
layout(binding = 2) restrict buffer AgentBuffer {
vec2 positions[1024*1024];
float headings[1024*1024];
vec2 positions[1024];
float headings[1024];
} agent_buffer;
layout(push_constant, std430) uniform Params {
@@ -19,9 +19,17 @@ layout(push_constant, std430) uniform Params {
const float SPEED = 1.0;
void main() {
vec2 loop_position(vec2 pos) {
int width = params.texture_size.x;
int height = params.texture_size.y;
if (pos.x > width) pos.x = mod(pos.x, width);
if (pos.y > height) pos.y = mod(pos.y, height);
if (pos.x < 0) pos.x = width - pos.x;
if (pos.y < 0) pos.y = height - pos.y;
return pos;
}
ivec2 size = ivec2(params.texture_size.x - 1, params.texture_size.y - 1);
void main() {
uint id = gl_GlobalInvocationID.x;
vec2 position = agent_buffer.positions[id];
@@ -36,35 +44,34 @@ void main() {
vec2 forward_detect_dir = vec2(cos(heading), sin(heading));
float sd = params.sensor_distance;
vec2 left_detect_pos = position + left_detect_dir * sd;
vec2 right_detect_pos = position + right_detect_dir * sd;
vec2 forward_detect_pos = position + forward_detect_dir * sd;
left_detect_pos = mod(left_detect_pos, size);
right_detect_pos = mod(right_detect_pos, size);
forward_detect_pos = mod(forward_detect_pos, size);
vec2 left_detect_pos = loop_position(position + left_detect_dir * sd);
vec2 right_detect_pos = loop_position(position + right_detect_dir * sd);
vec2 forward_detect_pos = loop_position(position + forward_detect_dir * sd);
float left = imageLoad(current_trail, ivec2(left_detect_pos)).r;
float right = imageLoad(current_trail, ivec2(right_detect_pos)).r;
float forward = imageLoad(current_trail, ivec2(forward_detect_pos)).r;
vec2 pos = position;
float new_heading = heading;
if (left > forward && left > right) {
pos += left_detect_dir * SPEED;
new_heading = left_heading;
} else if (right > forward && right > left) {
pos += right_detect_dir * SPEED;
new_heading = right_heading;
} else {
pos += forward_detect_dir * SPEED;
}
pos = mod(pos, size);
pos = loop_position(pos);
agent_buffer.positions[id] = pos;
agent_buffer.headings[id] = new_heading;
ivec2 uv = ivec2(pos);
float current_v = imageLoad(current_trail, uv).r;
float current_v = imageLoad(current_trail, ivec2(pos)).r;
float new_v = current_v + 0.3;
vec4 result = vec4(new_v, new_v, new_v, 1.0);
imageStore(output_trail, uv, result);
imageStore(output_trail, ivec2(pos), result);
}