Various improvements

This commit is contained in:
Andras Schmelczer 2023-05-28 22:28:44 +01:00
commit abf3803cdc
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
15 changed files with 283 additions and 250 deletions

View file

@ -9,20 +9,26 @@ fn main(
return;
}
let clusterId = f32(id % 1000);
let random = textureSampleLevel(
noise,
noiseSampler,
vec2(f32(id % 1999) / 2000, f32(id) / 1999 / 2000),
0
);
let randomPosition = textureSampleLevel(
noise,
noiseSampler,
vec2(clusterId / 2000, clusterId / 2000),
0
);
let position = random.xy * state.size;
let center = state.size / 2.0;
let direction = position - center;
agents[id] = Agent(
state.size / 2.0,
atan2(direction.y, direction.x),
randomPosition.xz * state.size,
random.r * 3.14 * 2,
0,
);
}

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 = 17;
private static readonly UNIFORM_COUNT = 16;
private readonly bindGroupLayout: GPUBindGroupLayout;
private readonly pipeline: GPUComputePipeline;
@ -54,7 +54,6 @@ export class AgentPipeline {
isNextGenerationOdd,
center,
radius,
turnWhenGoingInTheRightDirection,
turnWhenLost,
individualTrailWeight,
deinfectionProbability,
@ -72,20 +71,24 @@ export class AgentPipeline {
this.uniforms,
0,
new Float32Array([
...center,
radius,
brushTrailWeight,
moveSpeed,
turnSpeed,
(sensorOffsetAngle * Math.PI) / 180,
sensorOffsetDistance,
currentGenerationAggression,
nextGenerationAggression,
isNextGenerationOdd,
...center,
radius,
turnWhenGoingInTheRightDirection,
turnWhenLost,
individualTrailWeight,
deinfectionProbability,
agentCount,
])
);

View file

@ -4,7 +4,6 @@ export interface AgentSettings {
turnSpeed: number;
sensorOffsetAngle: number;
sensorOffsetDistance: number;
turnWhenGoingInTheRightDirection: number;
turnWhenLost: number;
individualTrailWeight: number;
deinfectionProbability: number;

View file

@ -1,4 +1,7 @@
struct Settings {
center: vec2<f32>,
radius: f32,
brushTrailWeight: f32,
moveRate: f32,
turnRate: f32,
@ -10,10 +13,6 @@ struct Settings {
nextGenerationAggression: f32,
isNextGenerationOdd: f32,
center: vec2<f32>,
radius: f32,
turnWhenGoingInTheRightDirection: f32,
turnWhenLost: f32,
individualTrailWeight: f32,
deinfectionProbability: f32,
@ -47,10 +46,12 @@ fn main(
let random = textureSampleLevel(
noise,
noiseSampler,
vec2(f32(id) % 23647 / 2000,
state.time % 6294 / 2000),
vec2(
f32(id) % 23647 / 2000,
agent.angle / 10
) + agent.position / state.size,
0
).a;
);
let isFromCurrentGeneration = abs(agent.generation - settings.isNextGenerationOdd);
let isFromOddGeneration = agent.generation == 1.0;
@ -59,9 +60,10 @@ fn main(
let trailLeft = sense(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
let trailRight = sense(agent.position, agent.angle, settings.sensorOffset, -settings.sensorAngle);
var weightForward: f32 = isFromCurrentGeneration * trailForward.a * settings.brushTrailWeight;
var weightLeft: f32 = isFromCurrentGeneration * trailLeft.a * settings.brushTrailWeight;
var weightRight: f32 = isFromCurrentGeneration * trailRight.a * settings.brushTrailWeight;
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;
let agression = isFromCurrentGeneration * settings.currentGenerationAggression + (1.0 - isFromCurrentGeneration) * settings.nextGenerationAggression;
if (isFromOddGeneration) {
@ -75,32 +77,34 @@ fn main(
}
var rotation: f32 = 0;
if weightForward > weightLeft && weightForward > weightRight {
rotation = (random - 0.5) * settings.turnWhenGoingInTheRightDirection * settings.turnRate * state.deltaTime;
if weightForward >= weightLeft && weightForward >= weightRight {
rotation = (random.r - 0.5) * 0.0 * settings.turnRate * state.deltaTime;
} else if weightLeft < weightRight {
rotation = -min(settings.sensorAngle, settings.turnRate * state.deltaTime);
rotation = -settings.turnRate * state.deltaTime;
} else if weightRight < weightLeft {
rotation = min(settings.sensorAngle, settings.turnRate * state.deltaTime);
rotation = settings.turnRate * state.deltaTime;
} else {
rotation = (random - 0.5) * settings.turnWhenLost * settings.turnRate * state.deltaTime;
rotation = (random.r - 0.5) * settings.turnWhenLost * settings.turnRate * state.deltaTime;
}
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);
if nextPosition.x == 0 || nextPosition.x == state.size.x || nextPosition.y == 0 || nextPosition.y == state.size.y {
rotation = 3.14159265359 + random - 0.5;
rotation = 3.14159265359 + random.a - 0.5;
}
var trail = vec4<f32>(settings.individualTrailWeight, 0, 0, 0);
if isFromOddGeneration {
trail = vec4(0, settings.individualTrailWeight, 0, 0);
trail = vec4<f32>(0, settings.individualTrailWeight, 0, 0);
}
var trailBelow = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
agent.position = nextPosition;
agent.angle += rotation;
var trailBelow = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
if settings.radius > 0 && length(settings.center - agent.position) < settings.radius {
agents[id].generation = settings.isNextGenerationOdd;
agent.generation = settings.isNextGenerationOdd;
// clear trail map below so the agent won't die immediately
if (settings.isNextGenerationOdd == 1.0) {
@ -111,25 +115,21 @@ fn main(
trailBelow.g = 0;
}
textureStore(trailMapOut, vec2<i32>(agent.position), trailBelow);
return;
}
let next = vec4(trail.rgb + trailBelow.rgb, trailBelow.a);
textureStore(trailMapOut, vec2<i32>(nextPosition), next);
if isFromOddGeneration {
if next.g < next.r && (isFromCurrentGeneration == 1.0 || (isFromCurrentGeneration == 0.0 && random < settings.deinfectionProbability)) {
agent.generation = 0;
}
textureStore(trailMapOut, vec2<i32>(nextPosition), trailBelow);
} else {
if next.r < next.g && (isFromCurrentGeneration == 1.0 || (isFromCurrentGeneration == 0.0 && random < settings.deinfectionProbability)) {
agent.generation = 1;
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;
}
}
}
agent.position = nextPosition;
agent.angle += rotation;
agents[id] = agent;
}