Minor improvements xd

This commit is contained in:
Andras Schmelczer 2023-05-21 19:19:12 +01:00
parent f3f2547724
commit 0e97e54ffe
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
21 changed files with 189 additions and 184 deletions

View file

@ -1,15 +1,12 @@
import random from '../../../utils/graphics/random.wgsl';
import { smartCompile } from '../../../utils/graphics/smart-compile';
import { CommonState } from '../../common-state/common-state';
import { AGENT_SIZE_IN_BYTES, Agent } from './agent';
import { AGENT_SIZE_IN_BYTES } from './agent';
import countingShader from './agent-counting.wgsl';
import firstGenerationShader from './agent-first-generation.wgsl';
import agentGenerationShader from './agent-generation.wgsl';
import agentSchema from './agent-schema.wgsl';
import { GenerationCounts } from './generation-counts';
import { vec2 } from 'gl-matrix';
export class AgentGenerationPipeline {
private static readonly WORKGROUP_SIZE = 64;
private static readonly UNIFORM_COUNT = 4;
@ -20,7 +17,6 @@ export class AgentGenerationPipeline {
private readonly bindGroup: GPUBindGroup;
private readonly firstGenerationPipeline: GPUComputePipeline;
private readonly nextGenerationPipeline: GPUComputePipeline;
private readonly countingPipeline: GPUComputePipeline;
public readonly agentsBuffer: GPUBuffer;
@ -122,22 +118,6 @@ export class AgentGenerationPipeline {
},
});
this.nextGenerationPipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [commonState.bindGroupLayout, this.bindGroupLayout],
}),
compute: {
module: smartCompile(
device,
CommonState.shaderCode,
random,
agentSchema,
agentGenerationShader
),
entryPoint: 'main',
},
});
this.countingPipeline = device.createComputePipeline({
layout: device.createPipelineLayout({
bindGroupLayouts: [commonState.bindGroupLayout, this.bindGroupLayout],
@ -170,27 +150,6 @@ export class AgentGenerationPipeline {
this.device.queue.submit([commandEncoder.finish()]);
}
public spawnNextGeneration(center: vec2, radius: number, generationId: number): void {
this.device.queue.writeBuffer(
this.uniforms,
0,
new Float32Array([...center, radius, generationId])
);
const commandEncoder = this.device.createCommandEncoder();
const passEncoder = commandEncoder.beginComputePass();
this.commonState.execute(passEncoder);
passEncoder.setPipeline(this.nextGenerationPipeline);
passEncoder.setBindGroup(1, this.bindGroup);
passEncoder.dispatchWorkgroups(
Math.ceil(this.agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
);
passEncoder.end();
this.device.queue.submit([commandEncoder.finish()]);
}
public async countAgents(): Promise<GenerationCounts> {
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0]));

View file

@ -1,20 +0,0 @@
struct Settings {
center: vec2<f32>,
radius: f32,
nextGenerationId: f32,
};
@group(1) @binding(0) var<uniform> settings: Settings;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
let id = global_id.x;
if id >= arrayLength(&agents) {
return;
}
if length(settings.center - agents[id].position) < settings.radius {
agents[id].generation = settings.nextGenerationId;
}
}

View file

@ -1,19 +1,20 @@
import random from '../../utils/graphics/random.wgsl';
import { smartCompile } from '../../utils/graphics/smart-compile';
import { CommonState } from '../common-state/common-state';
import { AGENT_SIZE_IN_BYTES, Agent } from './agent-generation/agent';
import { AGENT_SIZE_IN_BYTES } from './agent-generation/agent';
import agentSchme from './agent-generation/agent-schema.wgsl';
import { AgentSettings } from './agent-settings';
import shader from './agent.wgsl';
import { vec2 } from 'gl-matrix';
export class AgentPipeline {
private static readonly WORKGROUP_SIZE = 64;
private static readonly UNIFORM_COUNT = 8;
private static readonly UNIFORM_COUNT = 16;
private readonly bindGroupLayout: GPUBindGroupLayout;
private readonly pipeline: GPUComputePipeline;
private readonly uniforms: GPUBuffer;
private bindGroup?: GPUBindGroup;
private previousTrailMapIn?: GPUTextureView;
private previousTrailMapOut?: GPUTextureView;
@ -50,10 +51,18 @@ export class AgentPipeline {
evenGenerationAggression,
oddGenerationAggression,
nextGenerationId,
center,
radius,
turnWhenGoingInTheRightDirection,
turnWhenLost,
individualTrailWeight,
deinfectionProbability,
}: AgentSettings & {
evenGenerationAggression: number;
oddGenerationAggression: number;
nextGenerationId: number;
center: vec2;
radius: number;
}) {
this.device.queue.writeBuffer(
this.uniforms,
@ -67,6 +76,12 @@ export class AgentPipeline {
evenGenerationAggression,
oddGenerationAggression,
nextGenerationId,
...center,
radius,
turnWhenGoingInTheRightDirection,
turnWhenLost,
individualTrailWeight,
deinfectionProbability,
])
);
}

View file

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

View file

@ -1,15 +1,32 @@
struct Settings {
brushTrailWeight: f32,
moveRate: f32,
turnRate: f32,
sensorAngle: f32,
sensorOffset: f32,
evenGenerationAggression: f32,
oddGenerationAggression: f32,
nextGenerationId: f32
nextGenerationId: f32,
center: vec2<f32>,
radius: f32,
turnWhenGoingInTheRightDirection: f32,
turnWhenLost: f32,
individualTrailWeight: f32,
deinfectionProbability: f32
};
@group(1) @binding(0) var<uniform> settings: Settings;
// even generation's trail -> red channel
// odd generation's trail -> green channel
// unused -> blue channel
// brush -> alpha channel
@group(1) @binding(2) var trailMapIn: texture_2d<f32>;
@group(1) @binding(3) var trailMapOut: texture_storage_2d<rgba16float, write>;
@ -22,24 +39,38 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
}
var agent = agents[id];
var trailBelow = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
if settings.radius > 0 && length(settings.center - agent.position) < settings.radius {
agents[id].generation = settings.nextGenerationId;
// clear trail map below so the agent won't die immediately
if (settings.nextGenerationId % 2 == 0) {
trailBelow.r += trailBelow.g;
trailBelow.g = 0;
} else {
trailBelow.g += trailBelow.r;
trailBelow.r = 0;
}
textureStore(trailMapOut, vec2<i32>(agent.position), trailBelow);
return;
}
let random = hash(id + u32(state.time % 107 * 1673.7));
let trailCurrent = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
var weight: f32;
// even generation id -> red channel
// odd generation id -> green channel
let isFromEvenGeneration = agent.generation % 2 == 0;
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
let isFromNextGeneration = agent.generation == settings.nextGenerationId;
let isFromCurrentGeneration = !isFromNextGeneration;
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);
var weightForward: f32 = trailForward.a * settings.brushTrailWeight;
var weightLeft: f32 = trailLeft.a * settings.brushTrailWeight;
var weightRight: f32 = trailRight.a * settings.brushTrailWeight;
var weightForward: f32 = f32(isFromCurrentGeneration) * trailForward.a * settings.brushTrailWeight;
var weightLeft: f32 = f32(isFromCurrentGeneration) * trailLeft.a * settings.brushTrailWeight;
var weightRight: f32 = f32(isFromCurrentGeneration) * trailRight.a * settings.brushTrailWeight;
if (isFromEvenGeneration) {
weightForward += trailForward.r + settings.evenGenerationAggression * trailForward.g;
weightLeft += trailLeft.r + settings.evenGenerationAggression * trailLeft.g;
@ -51,38 +82,37 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
}
var rotation: f32 = 0;
if (weightForward < weightLeft && weightForward < weightRight) {
rotation = (random - 0.5) * 2. * settings.turnRate * state.deltaTime;
} else if (weightLeft < weightRight) {
rotation = random * -settings.turnRate * state.deltaTime;
} else if (weightRight < weightLeft) {
rotation = random * settings.turnRate * state.deltaTime;
if weightForward > weightLeft && weightForward > weightRight {
rotation = (random - 0.5) * settings.turnWhenGoingInTheRightDirection * settings.turnRate * state.deltaTime;
} else if weightLeft < weightRight {
rotation = -min(settings.sensorAngle, settings.turnRate * state.deltaTime);
} else if weightRight < weightLeft {
rotation = min(settings.sensorAngle, settings.turnRate * state.deltaTime);
} else {
rotation = (random - 0.5) * settings.turnWhenLost * settings.turnRate * state.deltaTime;
}
var nextAngle = agent.angle + rotation;
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;
nextAngle = agent.angle + rotation;
}
var trail = vec4<f32>(0, 0.1, 0, 0);
if (isFromEvenGeneration) {
trail = vec4(0.1, 0, 0, 0);
var trail = vec4<f32>(0, settings.individualTrailWeight, 0, 0);
if isFromEvenGeneration {
trail = vec4(settings.individualTrailWeight, 0, 0, 0);
}
let current = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
let next = vec4(trail.rgb + current.rgb, current.a);
let next = vec4(trail.rgb + trailBelow.rgb, trailBelow.a);
textureStore(trailMapOut, vec2<i32>(nextPosition), next);
if(isFromEvenGeneration) {
if isFromEvenGeneration {
if next.r < next.g {
if agent.generation == settings.nextGenerationId {
// agent.generation -= 1;
if random < settings.deinfectionProbability {
agent.generation -= 1;
}
} else {
agent.generation += 1;
}
@ -90,7 +120,9 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
} else {
if next.g < next.r {
if agent.generation == settings.nextGenerationId {
// agent.generation -= 1;
if random < settings.deinfectionProbability {
agent.generation -= 1;
}
} else {
agent.generation += 1;
}
@ -98,7 +130,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
}
agent.position = nextPosition;
agent.angle = nextAngle;
agent.angle += rotation;
agents[id] = agent;
}

View file

@ -1,4 +1,3 @@
import { setUpFullScreenQuad } from '../../utils/graphics/full-screen-quad/full-screen-quad';
import { smartCompile } from '../../utils/graphics/smart-compile';
import shader from './copy.wgsl';

View file

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

View file

@ -1,10 +1,11 @@
import { generateFbmNoise } from '../../utils/graphics/fbm-noise/fbm-noise';
import { setUpFullScreenQuad } from '../../utils/graphics/full-screen-quad/full-screen-quad';
import { smartCompile } from '../../utils/graphics/smart-compile';
import { CommonState } from '../common-state/common-state';
import { RenderSettings } from './render-settings';
import shader from './render.wgsl';
import { vec3 } from 'gl-matrix';
export class RenderPipeline {
private static readonly UNIFORM_COUNT = 13;
@ -56,7 +57,11 @@ export class RenderPipeline {
evenGenerationColor,
oddGenerationColor,
clarity,
}: RenderSettings) {
}: RenderSettings & {
brushColor: vec3;
evenGenerationColor: vec3;
oddGenerationColor: vec3;
}) {
this.device.queue.writeBuffer(
this.uniforms,
0,

View file

@ -1,8 +1,3 @@
import { vec3 } from 'gl-matrix';
export interface RenderSettings {
brushColor: vec3;
evenGenerationColor: vec3;
oddGenerationColor: vec3;
clarity: number;
}

View file

@ -16,15 +16,19 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
let backgroundColor = vec3(0.9) + 0.075 * random.r;
let evenGenerationStrength = clamp(pow(traces.r, settings.clarity), 0, 1);
let oddGenerationStrength = clamp(pow(traces.g, settings.clarity), 0, 1);
let evenGenerationStrength = pow(traces.r, settings.clarity);
let oddGenerationStrength = pow(traces.g, settings.clarity);
let brushStrength = traces.a;
let agentColor = step(evenGenerationStrength, oddGenerationStrength) * settings.oddGenerationColor * oddGenerationStrength + step(oddGenerationStrength, evenGenerationStrength) * settings.evenGenerationColor * evenGenerationStrength;
let agentStrength = evenGenerationStrength + oddGenerationStrength;
let color = max(
mix(
evenGenerationStrength * settings.evenGenerationColor,
oddGenerationStrength * settings.oddGenerationColor,
oddGenerationStrength / (evenGenerationStrength + oddGenerationStrength + 0.000001)
),
brushStrength * settings.brushColor);
let rgbColor = sqrt(
mix(agentColor, settings.brushColor * brushStrength, clamp(brushStrength - agentStrength, 0, 1))
);
return vec4(backgroundColor - rgbColor, 1);
let strength = max(evenGenerationStrength, max(oddGenerationStrength, brushStrength));
return vec4(mix(backgroundColor, color, strength), 1);
}