Various improvements
This commit is contained in:
parent
488494634d
commit
abf3803cdc
15 changed files with 259 additions and 226 deletions
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
])
|
||||
);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ export interface AgentSettings {
|
|||
turnSpeed: number;
|
||||
sensorOffsetAngle: number;
|
||||
sensorOffsetDistance: number;
|
||||
turnWhenGoingInTheRightDirection: number;
|
||||
turnWhenLost: number;
|
||||
individualTrailWeight: number;
|
||||
deinfectionProbability: number;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,40 +1,48 @@
|
|||
struct Settings {
|
||||
diffusionRateTrails: f32,
|
||||
inverseDiffusionRateTrails: f32,
|
||||
decayRateTrails: f32,
|
||||
diffusionRateBrush: f32,
|
||||
inverseDiffusionRateBrush: f32,
|
||||
decayRateBrush: f32,
|
||||
};
|
||||
|
||||
|
||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||
@group(1) @binding(1) var Sampler: sampler;
|
||||
@group(1) @binding(2) var trailMap: texture_2d<f32>;
|
||||
|
||||
|
||||
@fragment
|
||||
fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
|
||||
var current = textureSample(trailMap, Sampler, uv);
|
||||
var change = vec4<f32>(0);
|
||||
for (var x: i32 = -1; x <= 1; x++) {
|
||||
for (var y: i32 = -1; y <= 1; y++) {
|
||||
if (x != 0 || y != 0) {
|
||||
let offset = vec2(f32(x), f32(y));
|
||||
let neighbour = textureSample(trailMap, Sampler, uv + offset / state.size);
|
||||
let random = textureSample(noise, noiseSampler, uv + offset / state.size * 0.5).r;
|
||||
|
||||
let difference = clamp(neighbour - current, vec4(0), vec4(1));
|
||||
change += vec4(
|
||||
length(neighbour.rgb) * pow(random, settings.diffusionRateTrails) * difference.rgb,
|
||||
min(1.0, length(neighbour.a)) * pow(random, settings.diffusionRateBrush) * difference.a
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current += change / 4;
|
||||
current += (
|
||||
propagate(uv, vec2(-1.0, -1.0), current)
|
||||
+ propagate(uv, vec2(-1.0, 1.0), current)
|
||||
+ propagate(uv, vec2(1.0, -1.0), current)
|
||||
+ propagate(uv, vec2(1.0, 1.0), current)
|
||||
|
||||
+ propagate(uv, vec2(-1.0, 0.0), current)
|
||||
+ propagate(uv, vec2(0.0, -1.0), current)
|
||||
+ propagate(uv, vec2(1.0, 0.0), current)
|
||||
+ propagate(uv, vec2(0.0, 1.0), current)
|
||||
) / 8;
|
||||
|
||||
let decayed = clamp(vec4(
|
||||
current.rgb * settings.decayRateTrails,
|
||||
max(0, current.a - settings.decayRateBrush)
|
||||
current.rgb - settings.decayRateTrails,
|
||||
max(0, current.a + (current.a - 1.001) * settings.decayRateBrush)
|
||||
), vec4(0), vec4(1));
|
||||
|
||||
return decayed;
|
||||
}
|
||||
|
||||
|
||||
fn propagate(uv: vec2<f32>, offset: vec2<f32>, currentColor: vec4<f32>) -> vec4<f32> {
|
||||
let neighbour = textureSample(trailMap, Sampler, uv + offset / state.size);
|
||||
var random = textureSample(noise, noiseSampler, uv + offset / state.size * 0.5).r;
|
||||
let difference = clamp(neighbour - currentColor, vec4(0), vec4(1));
|
||||
|
||||
return vec4(
|
||||
vec3(length(neighbour.rgb) * pow(random, settings.inverseDiffusionRateTrails)),
|
||||
length(neighbour.a) * pow(random, settings.inverseDiffusionRateBrush)
|
||||
) * difference;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,10 +62,10 @@ export class DiffusionPipeline {
|
|||
this.uniforms,
|
||||
0,
|
||||
new Float32Array([
|
||||
diffusionRateTrails,
|
||||
decayRateTrails,
|
||||
diffusionRateBrush,
|
||||
decayRateBrush,
|
||||
1 / diffusionRateTrails,
|
||||
decayRateTrails / 1000,
|
||||
1 / diffusionRateBrush,
|
||||
decayRateBrush / 1000,
|
||||
])
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
|
|||
|
||||
let backgroundColor = vec3(0.9) + 0.075 * random.r;
|
||||
|
||||
let evenGenerationStrength = pow(traces.r, settings.clarity);
|
||||
let oddGenerationStrength = pow(traces.g, settings.clarity);
|
||||
let evenGenerationStrength = clarity(traces.r);
|
||||
let oddGenerationStrength = clarity(traces.g);
|
||||
let brushStrength = traces.a;
|
||||
|
||||
let color = max(
|
||||
|
|
@ -32,3 +32,7 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
|
|||
|
||||
return vec4(mix(backgroundColor, color, strength), 1);
|
||||
}
|
||||
|
||||
fn clarity(strength: f32) -> f32 {
|
||||
return pow(strength, 5) - strength * settings.clarity + sign(strength) * settings.clarity;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue