Add cancer
This commit is contained in:
parent
b547dc3339
commit
3414f38c3a
14 changed files with 246 additions and 149 deletions
|
|
@ -1,9 +1,9 @@
|
||||||
export interface GameLoopSettings {
|
export interface GameLoopSettings {
|
||||||
agentCount: number;
|
agentCount: number;
|
||||||
initialDeadRatio: number;
|
|
||||||
renderSpeed: number;
|
renderSpeed: number;
|
||||||
simulatedDelayMs: number;
|
simulatedDelayMs: number;
|
||||||
|
|
||||||
aggressionFactor: number;
|
aggressionFactor: number;
|
||||||
nextGenerationSpawnRadius: number;
|
nextGenerationSpawnRadius: number;
|
||||||
|
nextGenerationSpawnInterval: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,8 @@ export default class GameLoop {
|
||||||
this.trailMapB = new ResizableTexture(this.device, this.canvasSize);
|
this.trailMapB = new ResizableTexture(this.device, this.canvasSize);
|
||||||
this.resize();
|
this.resize();
|
||||||
|
|
||||||
|
this.copyPipeline = new CopyPipeline(this.device);
|
||||||
|
|
||||||
this.commonState = new CommonState(this.device);
|
this.commonState = new CommonState(this.device);
|
||||||
this.commonState.setParameters({
|
this.commonState.setParameters({
|
||||||
canvasSize: this.canvasSize,
|
canvasSize: this.canvasSize,
|
||||||
|
|
@ -55,13 +57,12 @@ export default class GameLoop {
|
||||||
deltaTime: 0,
|
deltaTime: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.copyPipeline = new CopyPipeline(this.device);
|
|
||||||
|
|
||||||
this.agentGenerationPipeline = new AgentGenerationPipeline(
|
this.agentGenerationPipeline = new AgentGenerationPipeline(
|
||||||
this.device,
|
this.device,
|
||||||
this.commonState,
|
this.commonState,
|
||||||
settings.agentCount
|
settings.agentCount
|
||||||
);
|
);
|
||||||
|
this.agentGenerationPipeline.spawnFirstGeneration();
|
||||||
|
|
||||||
this.agentPipeline = new AgentPipeline(
|
this.agentPipeline = new AgentPipeline(
|
||||||
this.device,
|
this.device,
|
||||||
|
|
@ -87,10 +88,23 @@ export default class GameLoop {
|
||||||
|
|
||||||
public async start(): Promise<void> {
|
public async start(): Promise<void> {
|
||||||
requestAnimationFrame(this.render.bind(this));
|
requestAnimationFrame(this.render.bind(this));
|
||||||
|
requestAnimationFrame(this.updateCounts.bind(this));
|
||||||
return this.hasFinishedPromise;
|
return this.hasFinishedPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
public get aliveAgentCounts(): GenerationCounts {
|
private async updateCounts(): Promise<void> {
|
||||||
|
if (this.hasFinished) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const generationCounts = await this.agentGenerationPipeline.countAgents();
|
||||||
|
this.gameRules.updateGenerationCounts(generationCounts);
|
||||||
|
requestAnimationFrame(this.updateCounts.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public get aliveAgentCounts(): {
|
||||||
|
currentGenerationCount: number;
|
||||||
|
nextGenerationCount: number;
|
||||||
|
} {
|
||||||
return this.gameRules.generationCounts;
|
return this.gameRules.generationCounts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -131,7 +145,15 @@ export default class GameLoop {
|
||||||
].forEach((pipeline) =>
|
].forEach((pipeline) =>
|
||||||
pipeline.setParameters({
|
pipeline.setParameters({
|
||||||
time,
|
time,
|
||||||
nextGenerationAggression: this.gameRules.nextGenerationAgression,
|
evenGenerationAggression:
|
||||||
|
this.gameRules.nextGenerationId % 2
|
||||||
|
? -1
|
||||||
|
: this.gameRules.nextGenerationAgression,
|
||||||
|
oddGenerationAggression:
|
||||||
|
this.gameRules.nextGenerationId % 2
|
||||||
|
? this.gameRules.nextGenerationAgression
|
||||||
|
: -1,
|
||||||
|
nextGenerationId: this.gameRules.nextGenerationId,
|
||||||
deltaTime,
|
deltaTime,
|
||||||
canvasSize: this.canvasSize,
|
canvasSize: this.canvasSize,
|
||||||
...settings,
|
...settings,
|
||||||
|
|
@ -141,26 +163,11 @@ export default class GameLoop {
|
||||||
for (let i = 0; i < settings.renderSpeed; i++) {
|
for (let i = 0; i < settings.renderSpeed; i++) {
|
||||||
const commandEncoder = this.device.createCommandEncoder();
|
const commandEncoder = this.device.createCommandEncoder();
|
||||||
|
|
||||||
if (
|
|
||||||
this.gameRules.generationCounts.currentGenerationCount == 0 &&
|
|
||||||
this.gameRules.generationCounts.nextGenerationCount == 0
|
|
||||||
) {
|
|
||||||
this.gameRules.updateGenerationCounts(
|
|
||||||
await this.agentGenerationPipeline.spawnNextGenerationCover(
|
|
||||||
0,
|
|
||||||
settings.agentCount * (1 - settings.initialDeadRatio)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const spawnAction = this.gameRules.getSpawnAction(timeInSeconds, this.canvasSize);
|
const spawnAction = this.gameRules.getSpawnAction(timeInSeconds, this.canvasSize);
|
||||||
this.gameRules.updateGenerationCounts(
|
this.agentGenerationPipeline.spawnNextGeneration(
|
||||||
await this.agentGenerationPipeline.spawnNextGenerationCircle(
|
|
||||||
spawnAction.generation,
|
|
||||||
spawnAction.count,
|
|
||||||
spawnAction.position,
|
spawnAction.position,
|
||||||
settings.nextGenerationSpawnRadius
|
spawnAction.radius,
|
||||||
)
|
spawnAction.generation
|
||||||
);
|
);
|
||||||
|
|
||||||
this.copyPipeline.execute(
|
this.copyPipeline.execute(
|
||||||
|
|
|
||||||
11
src/game-loop/game-presentation.ts
Normal file
11
src/game-loop/game-presentation.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { hsl } from '../utils/colors/hsl';
|
||||||
|
import { hash } from '../utils/hash';
|
||||||
|
|
||||||
|
import { vec3 } from 'gl-matrix';
|
||||||
|
|
||||||
|
export class GamePresentation {
|
||||||
|
public getGenerationColor(generation: number): vec3 {
|
||||||
|
const hue = Math.round(hash(generation) * 360);
|
||||||
|
return hsl(hue, 100, 50);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,15 +8,16 @@ import { vec2 } from 'gl-matrix';
|
||||||
export interface SpawnAction {
|
export interface SpawnAction {
|
||||||
generation: number;
|
generation: number;
|
||||||
position: vec2;
|
position: vec2;
|
||||||
count: number;
|
radius: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GameRules {
|
export class GameRules {
|
||||||
private static SPAWN_INTERVAL = 3;
|
|
||||||
|
|
||||||
private lastSpawnTimeInSeconds = 0;
|
private lastSpawnTimeInSeconds = 0;
|
||||||
private nextGenerationId = 0;
|
public nextGenerationId = 1;
|
||||||
public generationCounts: GenerationCounts = {
|
public generationCounts: {
|
||||||
|
currentGenerationCount: number;
|
||||||
|
nextGenerationCount: number;
|
||||||
|
} = {
|
||||||
currentGenerationCount: 0,
|
currentGenerationCount: 0,
|
||||||
nextGenerationCount: 0,
|
nextGenerationCount: 0,
|
||||||
};
|
};
|
||||||
|
|
@ -26,11 +27,14 @@ export class GameRules {
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSpawnAction(timeInSeconds: number, canvasSize: vec2): SpawnAction {
|
public getSpawnAction(timeInSeconds: number, canvasSize: vec2): SpawnAction {
|
||||||
if (timeInSeconds - this.lastSpawnTimeInSeconds < GameRules.SPAWN_INTERVAL) {
|
if (
|
||||||
|
timeInSeconds - this.lastSpawnTimeInSeconds <
|
||||||
|
settings.nextGenerationSpawnInterval
|
||||||
|
) {
|
||||||
return {
|
return {
|
||||||
generation: this.nextGenerationId,
|
generation: this.nextGenerationId,
|
||||||
position: vec2.create(),
|
position: vec2.create(),
|
||||||
count: 0,
|
radius: 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,17 +46,19 @@ export class GameRules {
|
||||||
Random.randomBetween(0, canvasSize.x),
|
Random.randomBetween(0, canvasSize.x),
|
||||||
Random.randomBetween(0, canvasSize.y)
|
Random.randomBetween(0, canvasSize.y)
|
||||||
),
|
),
|
||||||
count:
|
radius: settings.nextGenerationSpawnRadius,
|
||||||
settings.agentCount -
|
|
||||||
this.generationCounts.nextGenerationCount -
|
|
||||||
this.generationCounts.currentGenerationCount,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateGenerationCounts({
|
public updateGenerationCounts({
|
||||||
currentGenerationCount,
|
evenGenerationCount,
|
||||||
nextGenerationCount,
|
oddGenerationCount,
|
||||||
}: GenerationCounts): void {
|
}: GenerationCounts): void {
|
||||||
|
const nextGenerationCount =
|
||||||
|
this.nextGenerationId % 2 === 1 ? oddGenerationCount : evenGenerationCount;
|
||||||
|
const currentGenerationCount =
|
||||||
|
this.nextGenerationId % 2 === 1 ? evenGenerationCount : oddGenerationCount;
|
||||||
|
|
||||||
if (currentGenerationCount === 0) {
|
if (currentGenerationCount === 0) {
|
||||||
this.nextGenerationId++;
|
this.nextGenerationId++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
21
src/pipelines/agents/agent-generation/agent-counting.wgsl
Normal file
21
src/pipelines/agents/agent-generation/agent-counting.wgsl
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
struct Counters {
|
||||||
|
evenGenerationAlive: atomic<i32>,
|
||||||
|
oddGenerationAlive: atomic<i32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
@group(1) @binding(2) var<storage, read_write> counters: Counters;
|
||||||
|
|
||||||
|
@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 agents[id].species % 2 == 0 {
|
||||||
|
atomicAdd(&counters.evenGenerationAlive, 1);
|
||||||
|
} else {
|
||||||
|
atomicAdd(&counters.oddGenerationAlive, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
@compute @workgroup_size(64)
|
||||||
|
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
|
let id = global_id.x;
|
||||||
|
|
||||||
|
if id >= arrayLength(&agents) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let position = vec2(
|
||||||
|
hash(id) * state.size.x,
|
||||||
|
hash(id * id) * state.size.y,
|
||||||
|
);
|
||||||
|
let center = state.size / 2.0;
|
||||||
|
let direction = position - center;
|
||||||
|
|
||||||
|
agents[id] = Agent(
|
||||||
|
position,
|
||||||
|
atan2(direction.y, direction.x),
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,9 @@ import random from '../../../utils/graphics/random.wgsl';
|
||||||
import { smartCompile } from '../../../utils/graphics/smart-compile';
|
import { smartCompile } from '../../../utils/graphics/smart-compile';
|
||||||
import { CommonState } from '../../common-state/common-state';
|
import { CommonState } from '../../common-state/common-state';
|
||||||
import { AGENT_SIZE_IN_BYTES, Agent } from './agent';
|
import { AGENT_SIZE_IN_BYTES, Agent } from './agent';
|
||||||
import shader from './agent-generation.wgsl';
|
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 agentSchema from './agent-schema.wgsl';
|
||||||
import { GenerationCounts } from './generation-counts';
|
import { GenerationCounts } from './generation-counts';
|
||||||
|
|
||||||
|
|
@ -10,14 +12,17 @@ import { vec2 } from 'gl-matrix';
|
||||||
|
|
||||||
export class AgentGenerationPipeline {
|
export class AgentGenerationPipeline {
|
||||||
private static readonly WORKGROUP_SIZE = 64;
|
private static readonly WORKGROUP_SIZE = 64;
|
||||||
private static readonly UNIFORM_COUNT = 6;
|
private static readonly UNIFORM_COUNT = 4;
|
||||||
private static readonly COUNTER_COUNT = 3;
|
private static readonly COUNTER_COUNT = 3;
|
||||||
|
|
||||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||||
private readonly pipeline: GPUComputePipeline;
|
|
||||||
private readonly uniforms: GPUBuffer;
|
private readonly uniforms: GPUBuffer;
|
||||||
private readonly bindGroup: GPUBindGroup;
|
private readonly bindGroup: GPUBindGroup;
|
||||||
|
|
||||||
|
private readonly firstGenerationPipeline: GPUComputePipeline;
|
||||||
|
private readonly nextGenerationPipeline: GPUComputePipeline;
|
||||||
|
private readonly countingPipeline: GPUComputePipeline;
|
||||||
|
|
||||||
public readonly agentsBuffer: GPUBuffer;
|
public readonly agentsBuffer: GPUBuffer;
|
||||||
public readonly countersBuffer: GPUBuffer;
|
public readonly countersBuffer: GPUBuffer;
|
||||||
public readonly countersStagingBuffer: GPUBuffer;
|
public readonly countersStagingBuffer: GPUBuffer;
|
||||||
|
|
@ -101,54 +106,98 @@ export class AgentGenerationPipeline {
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
this.pipeline = device.createComputePipeline({
|
this.firstGenerationPipeline = device.createComputePipeline({
|
||||||
layout: device.createPipelineLayout({
|
layout: device.createPipelineLayout({
|
||||||
bindGroupLayouts: [commonState.bindGroupLayout, this.bindGroupLayout],
|
bindGroupLayouts: [commonState.bindGroupLayout, this.bindGroupLayout],
|
||||||
}),
|
}),
|
||||||
compute: {
|
compute: {
|
||||||
module: smartCompile(device, CommonState.shaderCode, random, agentSchema, shader),
|
module: smartCompile(
|
||||||
|
device,
|
||||||
|
CommonState.shaderCode,
|
||||||
|
random,
|
||||||
|
agentSchema,
|
||||||
|
firstGenerationShader
|
||||||
|
),
|
||||||
|
entryPoint: 'main',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
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],
|
||||||
|
}),
|
||||||
|
compute: {
|
||||||
|
module: smartCompile(
|
||||||
|
device,
|
||||||
|
CommonState.shaderCode,
|
||||||
|
random,
|
||||||
|
agentSchema,
|
||||||
|
countingShader
|
||||||
|
),
|
||||||
entryPoint: 'main',
|
entryPoint: 'main',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async spawnNextGenerationCover(
|
public spawnFirstGeneration(): void {
|
||||||
generationId: number,
|
|
||||||
count: number
|
|
||||||
): Promise<GenerationCounts> {
|
|
||||||
this.device.queue.writeBuffer(
|
|
||||||
this.uniforms,
|
|
||||||
0,
|
|
||||||
new Float32Array([0, 0, 0, generationId, 0])
|
|
||||||
);
|
|
||||||
|
|
||||||
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0, count]));
|
|
||||||
|
|
||||||
return this.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async spawnNextGenerationCircle(
|
|
||||||
generationId: number,
|
|
||||||
count: number,
|
|
||||||
center: vec2,
|
|
||||||
radius: number
|
|
||||||
): Promise<GenerationCounts> {
|
|
||||||
this.device.queue.writeBuffer(
|
|
||||||
this.uniforms,
|
|
||||||
0,
|
|
||||||
new Float32Array([...center, radius, generationId, 1])
|
|
||||||
);
|
|
||||||
|
|
||||||
this.device.queue.writeBuffer(this.countersBuffer, 0, new Int32Array([0, 0, count]));
|
|
||||||
|
|
||||||
return this.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async execute(): Promise<GenerationCounts> {
|
|
||||||
const commandEncoder = this.device.createCommandEncoder();
|
const commandEncoder = this.device.createCommandEncoder();
|
||||||
|
|
||||||
const passEncoder = commandEncoder.beginComputePass();
|
const passEncoder = commandEncoder.beginComputePass();
|
||||||
passEncoder.setPipeline(this.pipeline);
|
this.commonState.execute(passEncoder);
|
||||||
|
passEncoder.setPipeline(this.firstGenerationPipeline);
|
||||||
|
passEncoder.setBindGroup(1, this.bindGroup);
|
||||||
|
passEncoder.dispatchWorkgroups(
|
||||||
|
Math.ceil(this.agentCount / AgentGenerationPipeline.WORKGROUP_SIZE)
|
||||||
|
);
|
||||||
|
passEncoder.end();
|
||||||
|
|
||||||
|
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]));
|
||||||
|
|
||||||
|
const commandEncoder = this.device.createCommandEncoder();
|
||||||
|
|
||||||
|
const passEncoder = commandEncoder.beginComputePass();
|
||||||
|
passEncoder.setPipeline(this.countingPipeline);
|
||||||
this.commonState.execute(passEncoder);
|
this.commonState.execute(passEncoder);
|
||||||
passEncoder.setBindGroup(1, this.bindGroup);
|
passEncoder.setBindGroup(1, this.bindGroup);
|
||||||
passEncoder.dispatchWorkgroups(
|
passEncoder.dispatchWorkgroups(
|
||||||
|
|
@ -171,8 +220,8 @@ export class AgentGenerationPipeline {
|
||||||
const data = new Int32Array(this.countersStagingBuffer.getMappedRange().slice(0));
|
const data = new Int32Array(this.countersStagingBuffer.getMappedRange().slice(0));
|
||||||
this.countersStagingBuffer.unmap();
|
this.countersStagingBuffer.unmap();
|
||||||
return {
|
return {
|
||||||
currentGenerationCount: data[0],
|
evenGenerationCount: data[0],
|
||||||
nextGenerationCount: data[1],
|
oddGenerationCount: data[1],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,9 @@ struct Settings {
|
||||||
center: vec2<f32>,
|
center: vec2<f32>,
|
||||||
radius: f32,
|
radius: f32,
|
||||||
nextGenerationId: f32,
|
nextGenerationId: f32,
|
||||||
shape: f32,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Counters {
|
|
||||||
currentGenerationAlive: atomic<i32>,
|
|
||||||
nextGenerationAlive: atomic<i32>,
|
|
||||||
remaining: atomic<i32>
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||||
@group(1) @binding(2) var<storage, read_write> counters: Counters;
|
|
||||||
|
|
||||||
@compute @workgroup_size(64)
|
@compute @workgroup_size(64)
|
||||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
|
|
@ -22,44 +14,7 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if agents[id].timeToLive > 0 {
|
if length(settings.center - agents[id].position) < settings.radius {
|
||||||
if agents[id].species == settings.nextGenerationId {
|
agents[id].species = settings.nextGenerationId;
|
||||||
atomicAdd(&counters.nextGenerationAlive, 1);
|
|
||||||
} else {
|
|
||||||
atomicAdd(&counters.currentGenerationAlive, 1);
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if atomicSub(&counters.remaining, 1) <= 0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var position: vec2<f32>;
|
|
||||||
var angle: f32;
|
|
||||||
|
|
||||||
if settings.shape == 0.0 {
|
|
||||||
position = vec2(
|
|
||||||
hash(id) * state.size.x,
|
|
||||||
hash(id * id) * state.size.y,
|
|
||||||
);
|
|
||||||
|
|
||||||
let center = state.size / 2.0;
|
|
||||||
let direction = position - center;
|
|
||||||
angle = atan2(direction.y, direction.x);
|
|
||||||
} else if settings.shape == 1.0 {
|
|
||||||
angle = hash(id) * 2.0 * 3.1415;
|
|
||||||
let direction = vec2(cos(angle), sin(angle));
|
|
||||||
position = settings.center + direction * settings.radius * hash(id * 12 + 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
atomicAdd(&counters.nextGenerationAlive, 1);
|
|
||||||
|
|
||||||
agents[id] = Agent(
|
|
||||||
position,
|
|
||||||
angle,
|
|
||||||
settings.nextGenerationId,
|
|
||||||
1000000,
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ struct Agent {
|
||||||
position: vec2<f32>,
|
position: vec2<f32>,
|
||||||
angle: f32,
|
angle: f32,
|
||||||
species: f32,
|
species: f32,
|
||||||
timeToLive: f32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@group(1) @binding(1) var<storage, read_write> agents: array<Agent>;
|
@group(1) @binding(1) var<storage, read_write> agents: array<Agent>;
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ export interface Agent {
|
||||||
position: vec2;
|
position: vec2;
|
||||||
angle: number;
|
angle: number;
|
||||||
species: number;
|
species: number;
|
||||||
timeToLive: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AGENT_SIZE_IN_BYTES = 5 * Float32Array.BYTES_PER_ELEMENT;
|
export const AGENT_SIZE_IN_BYTES = 4 * Float32Array.BYTES_PER_ELEMENT;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
export interface GenerationCounts {
|
export interface GenerationCounts {
|
||||||
currentGenerationCount: number;
|
evenGenerationCount: number;
|
||||||
nextGenerationCount: number;
|
oddGenerationCount: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import shader from './agent.wgsl';
|
||||||
|
|
||||||
export class AgentPipeline {
|
export class AgentPipeline {
|
||||||
private static readonly WORKGROUP_SIZE = 64;
|
private static readonly WORKGROUP_SIZE = 64;
|
||||||
private static readonly UNIFORM_COUNT = 6;
|
private static readonly UNIFORM_COUNT = 8;
|
||||||
|
|
||||||
private readonly bindGroupLayout: GPUBindGroupLayout;
|
private readonly bindGroupLayout: GPUBindGroupLayout;
|
||||||
private readonly pipeline: GPUComputePipeline;
|
private readonly pipeline: GPUComputePipeline;
|
||||||
|
|
@ -47,8 +47,14 @@ export class AgentPipeline {
|
||||||
turnSpeed,
|
turnSpeed,
|
||||||
sensorOffsetAngle,
|
sensorOffsetAngle,
|
||||||
sensorOffsetDistance,
|
sensorOffsetDistance,
|
||||||
nextGenerationAggression,
|
evenGenerationAggression,
|
||||||
}: AgentSettings & { nextGenerationAggression: number }) {
|
oddGenerationAggression,
|
||||||
|
nextGenerationId,
|
||||||
|
}: AgentSettings & {
|
||||||
|
evenGenerationAggression: number;
|
||||||
|
oddGenerationAggression: number;
|
||||||
|
nextGenerationId: number;
|
||||||
|
}) {
|
||||||
this.device.queue.writeBuffer(
|
this.device.queue.writeBuffer(
|
||||||
this.uniforms,
|
this.uniforms,
|
||||||
0,
|
0,
|
||||||
|
|
@ -58,7 +64,9 @@ export class AgentPipeline {
|
||||||
turnSpeed,
|
turnSpeed,
|
||||||
(sensorOffsetAngle * Math.PI) / 180,
|
(sensorOffsetAngle * Math.PI) / 180,
|
||||||
sensorOffsetDistance,
|
sensorOffsetDistance,
|
||||||
nextGenerationAggression,
|
evenGenerationAggression,
|
||||||
|
oddGenerationAggression,
|
||||||
|
nextGenerationId,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,9 @@ struct Settings {
|
||||||
turnRate: f32,
|
turnRate: f32,
|
||||||
sensorAngle: f32,
|
sensorAngle: f32,
|
||||||
sensorOffset: f32,
|
sensorOffset: f32,
|
||||||
nextGenerationAggression: f32,
|
evenGenerationAggression: f32,
|
||||||
// nextGenerationParity: f32,
|
oddGenerationAggression: f32,
|
||||||
|
nextGenerationId: f32
|
||||||
};
|
};
|
||||||
|
|
||||||
@group(1) @binding(0) var<uniform> settings: Settings;
|
@group(1) @binding(0) var<uniform> settings: Settings;
|
||||||
|
|
@ -16,7 +17,7 @@ struct Settings {
|
||||||
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
let id = global_id.x;
|
let id = global_id.x;
|
||||||
|
|
||||||
if (id >= arrayLength(&agents) || agents[id].timeToLive <= 0) {
|
if (id >= arrayLength(&agents)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,19 +27,11 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
let trailCurrent = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
|
let trailCurrent = textureLoad(trailMapIn, vec2<i32>(agent.position), 0);
|
||||||
|
|
||||||
var weight: f32;
|
var weight: f32;
|
||||||
if(agent.species == 0) {
|
|
||||||
if trailCurrent.r < trailCurrent.g {
|
// even generation id -> red channel
|
||||||
agent.species = 1;
|
// odd generation id -> green channel
|
||||||
agents[id] = agent;
|
|
||||||
return;
|
let isFromEvenGeneration = agent.species % 2 == 0;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if trailCurrent.g < trailCurrent.r {
|
|
||||||
agent.timeToLive = 0;
|
|
||||||
agents[id] = agent;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
|
let trailForward = sense(agent.position, agent.angle, settings.sensorOffset, 0);
|
||||||
let trailLeft = sense(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
|
let trailLeft = sense(agent.position, agent.angle, settings.sensorOffset, settings.sensorAngle);
|
||||||
|
|
@ -47,14 +40,14 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
var weightForward: f32 = trailForward.a * settings.brushTrailWeight;
|
var weightForward: f32 = trailForward.a * settings.brushTrailWeight;
|
||||||
var weightLeft: f32 = trailLeft.a * settings.brushTrailWeight;
|
var weightLeft: f32 = trailLeft.a * settings.brushTrailWeight;
|
||||||
var weightRight: f32 = trailRight.a * settings.brushTrailWeight;
|
var weightRight: f32 = trailRight.a * settings.brushTrailWeight;
|
||||||
if (agent.species == 0) {
|
if (isFromEvenGeneration) {
|
||||||
weightForward += trailForward.r - trailForward.g;
|
weightForward += trailForward.r + settings.evenGenerationAggression * trailForward.g;
|
||||||
weightLeft += trailLeft.r - trailLeft.g;
|
weightLeft += trailLeft.r + settings.evenGenerationAggression * trailLeft.g;
|
||||||
weightRight += trailRight.r - trailRight.g;
|
weightRight += trailRight.r + settings.evenGenerationAggression * trailRight.g;
|
||||||
} else {
|
} else {
|
||||||
weightForward += trailForward.g + trailForward.r * settings.nextGenerationAggression;
|
weightForward += trailForward.g + settings.oddGenerationAggression * trailForward.r;
|
||||||
weightLeft += trailLeft.g + trailLeft.r * settings.nextGenerationAggression;
|
weightLeft += trailLeft.g + settings.oddGenerationAggression * trailLeft.r;
|
||||||
weightRight += trailRight.g + trailRight.r * settings.nextGenerationAggression;
|
weightRight += trailRight.g + settings.oddGenerationAggression * trailRight.r;
|
||||||
}
|
}
|
||||||
|
|
||||||
var rotation: f32 = 0;
|
var rotation: f32 = 0;
|
||||||
|
|
@ -77,16 +70,35 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var trail = vec4<f32>(0, 0.1, 0, 0);
|
var trail = vec4<f32>(0, 0.1, 0, 0);
|
||||||
if (agent.species == 0) {
|
if (isFromEvenGeneration) {
|
||||||
trail = vec4(0.1, 0, 0, 0);
|
trail = vec4(0.1, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let current = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
|
let current = textureLoad(trailMapIn, vec2<i32>(nextPosition), 0);
|
||||||
textureStore(trailMapOut, vec2<i32>(nextPosition), vec4(trail.rgb + current.rgb, current.a));
|
let next = vec4(trail.rgb + current.rgb, current.a);
|
||||||
|
textureStore(trailMapOut, vec2<i32>(nextPosition), next);
|
||||||
|
|
||||||
|
|
||||||
|
if(isFromEvenGeneration) {
|
||||||
|
if next.r < next.g {
|
||||||
|
if agent.species == settings.nextGenerationId {
|
||||||
|
// agent.species -= 1;
|
||||||
|
} else {
|
||||||
|
agent.species += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if next.g < next.r {
|
||||||
|
if agent.species == settings.nextGenerationId {
|
||||||
|
// agent.species -= 1;
|
||||||
|
} else {
|
||||||
|
agent.species += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
agent.position = nextPosition;
|
agent.position = nextPosition;
|
||||||
agent.angle = nextAngle;
|
agent.angle = nextAngle;
|
||||||
agent.timeToLive -= state.deltaTime;
|
|
||||||
agents[id] = agent;
|
agents[id] = agent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
9
src/utils/hash.ts
Normal file
9
src/utils/hash.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
export const hash = (state: number): number => {
|
||||||
|
state ^= 2747636419;
|
||||||
|
state *= 2654435769;
|
||||||
|
state ^= state >> 16;
|
||||||
|
state *= 2654435769;
|
||||||
|
state ^= state >> 16;
|
||||||
|
state *= 2654435769;
|
||||||
|
return state / 4294967295.0;
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue