building a pure graphics pipeline version of this
This commit is contained in:
88
slime_simulation_cs/shaders/agents_cs.glsl
Normal file
88
slime_simulation_cs/shaders/agents_cs.glsl
Normal file
@@ -0,0 +1,88 @@
|
||||
#[compute]
|
||||
#version 450
|
||||
|
||||
// Invocations in the (x, y, z) dimension
|
||||
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];
|
||||
} agent_buffer;
|
||||
|
||||
layout(push_constant, std430) uniform Params {
|
||||
vec2 texture_size;
|
||||
float sensor_angle;
|
||||
float sensor_distance;
|
||||
} params;
|
||||
|
||||
const float SPEED = 1.0;
|
||||
const float DEPOSIT = 0.5;
|
||||
|
||||
|
||||
float random(vec2 coords) {
|
||||
return fract(sin(dot(coords.xy, vec2(12.9898,78.233))) * 43758.5453);
|
||||
}
|
||||
|
||||
vec2 loop_position(vec2 pos) {
|
||||
return mod(pos, params.texture_size);
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
||||
uint id = gl_GlobalInvocationID.x;
|
||||
vec2 position = agent_buffer.positions[id];
|
||||
float heading = agent_buffer.headings[id];
|
||||
|
||||
float current_v = imageLoad(current_trail, ivec2(position)).r;
|
||||
|
||||
float sa = params.sensor_angle;
|
||||
float left_heading = heading - sa;
|
||||
float right_heading = heading + sa;
|
||||
|
||||
vec2 left_detect_dir = vec2(cos(left_heading), sin(left_heading));
|
||||
vec2 right_detect_dir = vec2(cos(right_heading), sin(right_heading));
|
||||
vec2 forward_detect_dir = vec2(cos(heading), sin(heading));
|
||||
|
||||
float sd = params.sensor_distance;
|
||||
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 if (forward < left && forward < right) {
|
||||
// uuugh
|
||||
float r = random(pos);
|
||||
if (r < 0.5) {
|
||||
pos += right_detect_dir * SPEED;
|
||||
new_heading = right_heading;
|
||||
} else {
|
||||
pos += left_detect_dir * SPEED;
|
||||
new_heading = left_heading;
|
||||
}
|
||||
} else {
|
||||
pos += forward_detect_dir * SPEED;
|
||||
}
|
||||
|
||||
pos = mod(pos, params.texture_size);
|
||||
agent_buffer.positions[id] = pos;
|
||||
agent_buffer.headings[id] = new_heading;
|
||||
|
||||
float new_v = current_v + DEPOSIT;
|
||||
if (new_v > 1.0) new_v = 1.0;
|
||||
|
||||
vec4 result = vec4(new_v, new_v, new_v, 1.0);
|
||||
imageStore(output_trail, ivec2(pos), result);
|
||||
}
|
||||
14
slime_simulation_cs/shaders/agents_cs.glsl.import
Normal file
14
slime_simulation_cs/shaders/agents_cs.glsl.import
Normal file
@@ -0,0 +1,14 @@
|
||||
[remap]
|
||||
|
||||
importer="glsl"
|
||||
type="RDShaderFile"
|
||||
uid="uid://u3grg3r2bgi1"
|
||||
path="res://.godot/imported/agents_cs.glsl-f5fde0e4c35a05360ed8814f4e4a012a.res"
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://slime_simulation_cs/shaders/agents_cs.glsl"
|
||||
dest_files=["res://.godot/imported/agents_cs.glsl-f5fde0e4c35a05360ed8814f4e4a012a.res"]
|
||||
|
||||
[params]
|
||||
|
||||
61
slime_simulation_cs/shaders/decay_cs.glsl
Normal file
61
slime_simulation_cs/shaders/decay_cs.glsl
Normal file
@@ -0,0 +1,61 @@
|
||||
#[compute]
|
||||
#version 450
|
||||
|
||||
// Invocations in the (x, y, z) dimension
|
||||
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
|
||||
|
||||
// Our textures.
|
||||
layout(r32f, set = 0, binding = 0) uniform restrict readonly image2D current_image;
|
||||
layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D output_image;
|
||||
|
||||
// PushConstants
|
||||
layout(push_constant, std430) uniform Params {
|
||||
ivec2 texture_size;
|
||||
float decay_factor;
|
||||
float dissipation_factor;
|
||||
} params;
|
||||
|
||||
float get_dissipation_from(ivec2 uv) {
|
||||
float v = imageLoad(current_image, uv).r;
|
||||
// return v * params.dissipation_factor;
|
||||
return v;
|
||||
}
|
||||
|
||||
ivec2 loop_position(ivec2 pos) {
|
||||
return pos % params.texture_size;
|
||||
}
|
||||
|
||||
// The code we want to execute in each invocation
|
||||
void main() {
|
||||
|
||||
ivec2 uv = ivec2(gl_GlobalInvocationID.xy);
|
||||
|
||||
float current_sum = 0.0;
|
||||
for (int i = -1; i <= 1; i++) {
|
||||
for (int j = -1; j <= 1; j++) {
|
||||
ivec2 kuv = loop_position(uv + ivec2(i,j));
|
||||
current_sum += get_dissipation_from(kuv);
|
||||
}
|
||||
}
|
||||
// ivec2 up_uv = loop_position(uv - ivec2(0, 1));
|
||||
// ivec2 down_uv = loop_position(uv + ivec2(0, 1));
|
||||
// ivec2 left_uv = loop_position(uv - ivec2(1, 0));
|
||||
// ivec2 right_uv = loop_position(uv + ivec2(1, 0));
|
||||
|
||||
|
||||
// Kill very old trails completely
|
||||
// if (new_v < 0.001) {
|
||||
// new_v = 0;
|
||||
// }
|
||||
|
||||
// new_v = clamp(new_v, 0, 1);
|
||||
|
||||
// This is good when not using trail for showind as is.
|
||||
// https://github.com/Bleuje/physarum-36p/blob/main/bin/data/shaders/computeshader_deposit.glsl
|
||||
current_sum = current_sum / pow(3.0, 2.0);
|
||||
|
||||
vec4 result = vec4(vec3(current_sum), 1.0);
|
||||
imageStore(output_image, uv, result);
|
||||
|
||||
|
||||
}
|
||||
14
slime_simulation_cs/shaders/decay_cs.glsl.import
Normal file
14
slime_simulation_cs/shaders/decay_cs.glsl.import
Normal file
@@ -0,0 +1,14 @@
|
||||
[remap]
|
||||
|
||||
importer="glsl"
|
||||
type="RDShaderFile"
|
||||
uid="uid://vn5yxao0oxx0"
|
||||
path="res://.godot/imported/decay_cs.glsl-ddb2bd80ce1fa22519e2cc84d49ab13a.res"
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://slime_simulation_cs/shaders/decay_cs.glsl"
|
||||
dest_files=["res://.godot/imported/decay_cs.glsl-ddb2bd80ce1fa22519e2cc84d49ab13a.res"]
|
||||
|
||||
[params]
|
||||
|
||||
Reference in New Issue
Block a user