Set up demo

This commit is contained in:
Andras Schmelczer 2023-05-31 22:22:43 +01:00
parent abf3803cdc
commit 8817e5b090
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
17 changed files with 189 additions and 145 deletions

View file

@ -28,7 +28,4 @@ fn main(
} else {
atomicAdd(&counters.oddGenerationAlive, 1);
}
// atomicStore(&counters.evenGenerationAlive, settings.agentCount);
// atomicStore(&counters.oddGenerationAlive, workgroup_count.y);
}

View file

@ -3,7 +3,7 @@ fn main(
@builtin(global_invocation_id) global_id: vec3<u32>,
@builtin(num_workgroups) workgroup_count: vec3<u32>
) {
let id = global_id.x + global_id.y * (workgroup_count.x * 64) + global_id.z * (workgroup_count.x * workgroup_count.y * 64);
let id = get_id(global_id, workgroup_count);
if id >= arrayLength(&agents) {
return;

View file

@ -5,3 +5,7 @@ struct Agent {
}
@group(1) @binding(1) var<storage, read_write> agents: array<Agent>;
fn get_id(global_id: vec3<u32>, workgroup_count: vec3<u32>) -> u32 {
return global_id.x + global_id.y * (workgroup_count.x * 64) + global_id.z * (workgroup_count.x * workgroup_count.y * 64);
}

View file

@ -9,7 +9,7 @@ import { vec2 } from 'gl-matrix';
export class AgentPipeline {
private static readonly WORKGROUP_SIZE = 64;
private static readonly UNIFORM_COUNT = 16;
private static readonly UNIFORM_COUNT = 19;
private readonly bindGroupLayout: GPUBindGroupLayout;
private readonly pipeline: GPUComputePipeline;
@ -44,26 +44,33 @@ export class AgentPipeline {
}
public setParameters({
deltaTime,
center,
radius,
brushTrailWeight,
moveSpeed,
turnSpeed,
sensorOffsetAngle,
sensorOffsetDistance,
nextGenerationSensorOffsetDistance,
currentGenerationAggression,
nextGenerationAggression,
nextGenerationSpeed,
isNextGenerationOdd,
center,
radius,
turnWhenLost,
individualTrailWeight,
deinfectionProbability,
infectionProbability,
agentCount,
}: AgentSettings & {
deltaTime: number;
currentGenerationAggression: number;
nextGenerationAggression: number;
nextGenerationSensorOffsetDistance: number;
nextGenerationSpeed: number;
isNextGenerationOdd: number;
center: vec2;
radius: number;
infectionProbability: number;
agentCount: number;
}) {
this.agentCount = agentCount;
@ -75,19 +82,21 @@ export class AgentPipeline {
radius,
brushTrailWeight,
moveSpeed,
turnSpeed,
moveSpeed * deltaTime,
turnSpeed * deltaTime,
(sensorOffsetAngle * Math.PI) / 180,
sensorOffsetDistance,
currentGenerationAggression,
nextGenerationAggression,
nextGenerationSensorOffsetDistance,
nextGenerationSpeed * deltaTime,
isNextGenerationOdd,
turnWhenLost,
individualTrailWeight,
deinfectionProbability,
infectionProbability,
agentCount,
])

View file

@ -6,7 +6,6 @@ export interface AgentSettings {
sensorOffsetDistance: number;
turnWhenLost: number;
individualTrailWeight: number;
deinfectionProbability: number;
currentGenerationAggression: number;
nextGenerationAggression: number;
}

View file

@ -3,7 +3,7 @@ struct Settings {
radius: f32,
brushTrailWeight: f32,
moveRate: f32,
currentGenerationMoveRate: f32,
turnRate: f32,
sensorAngle: f32,
@ -11,11 +11,13 @@ struct Settings {
currentGenerationAggression: f32,
nextGenerationAggression: f32,
nextGenerationSensorOffsetDistance: f32,
nextGenerationMoveRate: f32,
isNextGenerationOdd: f32,
turnWhenLost: f32,
individualTrailWeight: f32,
deinfectionProbability: f32,
infectionProbability: f32,
agentCount: f32 // might be smaller than the length of the agents array
};
@ -30,12 +32,13 @@ struct Settings {
@group(1) @binding(2) var trailMapIn: texture_2d<f32>;
@group(1) @binding(3) var trailMapOut: texture_storage_2d<rgba16float, write>;
@compute @workgroup_size(64)
fn main(
@builtin(global_invocation_id) global_id: vec3<u32>,
@builtin(num_workgroups) workgroup_count: vec3<u32>
) {
let id = global_id.x + global_id.y * (workgroup_count.x * 64) + global_id.z * (workgroup_count.x * workgroup_count.y * 64);
let id = get_id(global_id, workgroup_count);
if id >= u32(settings.agentCount) {
return;
@ -48,94 +51,84 @@ fn main(
noiseSampler,
vec2(
f32(id) % 23647 / 2000,
agent.angle / 10
) + agent.position / state.size,
state.time % 3243 / 2000
),
0
);
let isFromCurrentGeneration = abs(agent.generation - settings.isNextGenerationOdd);
let isFromOddGeneration = agent.generation == 1.0;
let isFromNextGeneration = 1.0 - isFromCurrentGeneration;
let isFromOddGeneration = agent.generation % 2;
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
let trailLeft = sense(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
let trailRight = sense(agent.position, agent.angle, settings.sensorOffset, -settings.sensorAngle);
let sensorOffset = mix(settings.sensorOffset, settings.nextGenerationSensorOffsetDistance, isFromNextGeneration);
let moveRate = mix(settings.currentGenerationMoveRate, settings.nextGenerationMoveRate, isFromNextGeneration);
let brushWeight = mix(settings.brushTrailWeight, 0, isFromNextGeneration);
let trailForward = sense(agent.position, agent.angle, sensorOffset, 0);
let trailLeft = sense(agent.position, agent.angle, sensorOffset, settings.sensorAngle);
let trailRight = sense(agent.position, agent.angle, sensorOffset, -settings.sensorAngle);
let brushWeight = isFromCurrentGeneration * settings.brushTrailWeight - (1 - isFromCurrentGeneration) * settings.brushTrailWeight;
var weightForward: f32 = brushWeight * trailForward.a;
var weightLeft: f32 = brushWeight * trailLeft.a;
var weightRight: f32 = brushWeight * trailRight.a;
var weightForward = brushWeight * trailForward.a;
var weightLeft = brushWeight * trailLeft.a;
var weightRight = brushWeight * trailRight.a;
let agression = isFromCurrentGeneration * settings.currentGenerationAggression + (1.0 - isFromCurrentGeneration) * settings.nextGenerationAggression;
if (isFromOddGeneration) {
weightForward += trailForward.g + agression * trailForward.r;
weightLeft += trailLeft.g + agression * trailLeft.r;
weightRight += trailRight.g + agression * trailRight.r;
} else {
weightForward += trailForward.r + agression * trailForward.g;
weightLeft += trailLeft.r + agression * trailLeft.g;
weightRight += trailRight.r + agression * trailRight.g;
}
let agression = mix(settings.currentGenerationAggression, settings.nextGenerationAggression, isFromNextGeneration) + weightForward;
var rotation: f32 = 0;
weightForward += mix(trailForward.r + agression * trailForward.g, trailForward.g + agression * trailForward.r, isFromOddGeneration);
weightLeft += mix(trailLeft.r + agression * trailLeft.g, trailLeft.g + agression * trailLeft.r, isFromOddGeneration);
weightRight += mix(trailRight.r + agression * trailRight.g, trailRight.g + agression * trailRight.r, isFromOddGeneration);
var rotation: f32;
if weightForward >= weightLeft && weightForward >= weightRight {
rotation = (random.r - 0.5) * 0.0 * settings.turnRate * state.deltaTime;
} else if weightLeft < weightRight {
rotation = -settings.turnRate * state.deltaTime;
} else if weightRight < weightLeft {
rotation = settings.turnRate * state.deltaTime;
rotation = 0;
} else {
rotation = (random.r - 0.5) * settings.turnWhenLost * settings.turnRate * state.deltaTime;
rotation = sign(weightLeft - weightRight) * settings.turnRate;
}
let direction = vec2(cos(agent.angle), sin(agent.angle));
var nextPosition = agent.position + direction * settings.moveRate * state.deltaTime;
nextPosition = clamp(nextPosition, vec2<f32>(0, 0), state.size);
let nextPosition = clamp(
agent.position + vec2(cos(agent.angle), sin(agent.angle)) * moveRate,
vec2<f32>(0, 0),
state.size
);
if nextPosition.x == 0 || nextPosition.x == state.size.x || nextPosition.y == 0 || nextPosition.y == state.size.y {
rotation = 3.14159265359 + random.a - 0.5;
}
var trail = vec4<f32>(settings.individualTrailWeight, 0, 0, 0);
if isFromOddGeneration {
if isFromOddGeneration == 1.0 {
trail = vec4<f32>(0, settings.individualTrailWeight, 0, 0);
}
agent.position = nextPosition;
agent.angle += rotation;
var trailBelow = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
agent.angle += rotation;
trailBelow += trail;
if settings.radius > 0 && length(settings.center - agent.position) < settings.radius {
agent.generation = settings.isNextGenerationOdd;
// clear trail map below so the agent won't die immediately
if (settings.isNextGenerationOdd == 1.0) {
trailBelow.g += trailBelow.r;
trailBelow.r = 0;
} else {
trailBelow.r += trailBelow.g;
trailBelow.g = 0;
}
textureStore(trailMapOut, vec2<i32>(nextPosition), trailBelow);
// trailBelow.r = (1 - settings.isNextGenerationOdd) * (trailBelow.r + trailBelow.g);
// trailBelow.g = settings.isNextGenerationOdd * (trailBelow.r + trailBelow.g);
} else {
textureStore(trailMapOut, vec2<i32>(nextPosition), trail + trailBelow);
if isFromOddGeneration {
if trailBelow.g < trailBelow.r && (isFromCurrentGeneration == 1.0 || (isFromCurrentGeneration == 0.0 && random.a < settings.deinfectionProbability)) {
agent.generation = 0;
}
} else {
if trailBelow.r < trailBelow.g && (isFromCurrentGeneration == 1.0 || (isFromCurrentGeneration == 0.0 && random.a < settings.deinfectionProbability)) {
agent.generation = 1;
}
let relativeWeight = mix(trailBelow.g - trailBelow.r, trailBelow.r - trailBelow.g, isFromOddGeneration);
if relativeWeight > 0 && (
(isFromCurrentGeneration == 1.0 && trailBelow.a == 0 && random.b < settings.infectionProbability)
|| (isFromCurrentGeneration == 0.0 && trailBelow.a > 0)
) {
// trailBelow.r = isFromOddGeneration * (trailBelow.r + trailBelow.g);
// trailBelow.g = (1 - isFromOddGeneration) * (trailBelow.r + trailBelow.g);
agent.generation = (agent.generation + 1) % 2;
}
}
textureStore(trailMapOut, vec2<i32>(nextPosition), trailBelow);
agent.position = nextPosition;
agents[id] = agent;
}
fn sense(agentPosition: vec2<f32>, agentAngle: f32, sensorOffset: f32, sensorOffsetAngle: f32) -> vec4<f32> {
let sensorAngle = agentAngle + sensorOffsetAngle;
let sensorDirection = vec2(cos(sensorAngle), sin(sensorAngle));
let sensorPosition = vec2<i32>(agentPosition + sensorDirection * sensorOffset);
let sensorPosition = vec2<i32>(agentPosition + vec2(cos(sensorAngle), sin(sensorAngle)) * sensorOffset);
return textureLoad(trailMapIn, sensorPosition, 0);
}

View file

@ -28,7 +28,7 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
) / 8;
let decayed = clamp(vec4(
current.rgb - settings.decayRateTrails,
clamp(current.rgb + (current.rgb - 1.001) * settings.decayRateTrails, vec3(0), vec3(1)),
max(0, current.a + (current.a - 1.001) * settings.decayRateBrush)
), vec4(0), vec4(1));

View file

@ -34,5 +34,5 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
}
fn clarity(strength: f32) -> f32 {
return pow(strength, 5) - strength * settings.clarity + sign(strength) * settings.clarity;
return sign(strength);
}