Experiment with cancer cells
This commit is contained in:
parent
1578c8796a
commit
42d87fc2a3
6 changed files with 87 additions and 31 deletions
|
|
@ -12,7 +12,6 @@ import { vec2 } from 'gl-matrix';
|
||||||
|
|
||||||
export default class GameLoop {
|
export default class GameLoop {
|
||||||
private context: GPUCanvasContext;
|
private context: GPUCanvasContext;
|
||||||
private adapter: GPUAdapter;
|
|
||||||
private device: GPUDevice;
|
private device: GPUDevice;
|
||||||
|
|
||||||
private agentPipeline: AgentPipeline;
|
private agentPipeline: AgentPipeline;
|
||||||
|
|
@ -81,7 +80,7 @@ export default class GameLoop {
|
||||||
position,
|
position,
|
||||||
angle: angle + Math.PI,
|
angle: angle + Math.PI,
|
||||||
species: 0,
|
species: 0,
|
||||||
timeToLive: Random.randomBetween(2, 10),
|
timeToLive: Random.randomBetween(10, 15000),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -119,8 +118,8 @@ export default class GameLoop {
|
||||||
throw new Error('WebGPU is not supported');
|
throw new Error('WebGPU is not supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.adapter = await gpu.requestAdapter();
|
const adapter = await gpu.requestAdapter();
|
||||||
this.device = await this.adapter.requestDevice(); // could request more resources
|
this.device = await adapter.requestDevice(); // could request more resources
|
||||||
|
|
||||||
this.context = this.canvas.getContext('webgpu') as any;
|
this.context = this.canvas.getContext('webgpu') as any;
|
||||||
this.context.configure({
|
this.context.configure({
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ struct Settings {
|
||||||
moveRate: f32,
|
moveRate: f32,
|
||||||
turnRate: f32,
|
turnRate: f32,
|
||||||
sensorAngle: f32,
|
sensorAngle: f32,
|
||||||
sensorOffsetDst: f32,
|
sensorOffset: f32,
|
||||||
};
|
};
|
||||||
|
|
||||||
@group(0) @binding(0) var<uniform> settings: Settings;
|
@group(0) @binding(0) var<uniform> settings: Settings;
|
||||||
|
|
@ -32,11 +32,46 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
|
|
||||||
var agent = agents[id];
|
var agent = agents[id];
|
||||||
|
|
||||||
|
if (agent.timeToLive <= 0.) {
|
||||||
|
agent.position = vec2(random(id + u32(settings.time * 10 + agent.position.x * 10000)),
|
||||||
|
random(id + u32(settings.time * 10 + agent.position.y * 10000)));
|
||||||
|
agent.angle = random(id + u32(settings.time)) * 3.14159265359 * 2.;
|
||||||
|
agent.species = 1;
|
||||||
|
agent.timeToLive = 1000;
|
||||||
|
agents[id] = agent;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let random = random(id + u32(settings.time * 10000 + agent.position.y * 10 + agent.position.x));
|
let random = random(id + u32(settings.time * 10000 + agent.position.y * 10 + agent.position.x));
|
||||||
|
|
||||||
let weightForward: f32 = sense(agent, 0.);
|
let trailCurrent = sense(agent, 0, 0);
|
||||||
let weightLeft: f32 = sense(agent, settings.sensorAngle);
|
var weight: f32;
|
||||||
let weightRight: f32 = sense(agent, -settings.sensorAngle);
|
if(agent.species == 0) {
|
||||||
|
weight = trailCurrent.r - trailCurrent.g;
|
||||||
|
} else {
|
||||||
|
weight = trailCurrent.g - trailCurrent.r;
|
||||||
|
}
|
||||||
|
if (weight < 0) {
|
||||||
|
agent.timeToLive = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let trailForward = sense(agent, settings.sensorOffset, 0);
|
||||||
|
let trailLeft = sense(agent, settings.sensorOffset, settings.sensorAngle);
|
||||||
|
let trailRight = sense(agent, settings.sensorOffset, -settings.sensorAngle);
|
||||||
|
|
||||||
|
var weightForward: f32 = trailForward.a;
|
||||||
|
var weightLeft: f32 = trailLeft.a;
|
||||||
|
var weightRight: f32 = trailRight.a;
|
||||||
|
if (agent.species == 0) {
|
||||||
|
weightForward += trailForward.r - trailForward.g;
|
||||||
|
weightLeft += trailLeft.r - trailLeft.g;
|
||||||
|
weightRight += trailRight.r - trailRight.g;
|
||||||
|
} else {
|
||||||
|
weightForward += trailForward.g - trailForward.r;
|
||||||
|
weightLeft += trailLeft.g - trailLeft.r;
|
||||||
|
weightRight += trailRight.g - trailRight.r;
|
||||||
|
}
|
||||||
|
|
||||||
if (weightForward < weightLeft && weightForward < weightRight) {
|
if (weightForward < weightLeft && weightForward < weightRight) {
|
||||||
agent.angle += (random - 0.5) * 2. * settings.turnRate;
|
agent.angle += (random - 0.5) * 2. * settings.turnRate;
|
||||||
|
|
@ -53,17 +88,23 @@ fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
||||||
agent.angle += 3.14159265359 + random - 0.5;
|
agent.angle += 3.14159265359 + random - 0.5;
|
||||||
}
|
}
|
||||||
|
|
||||||
textureStore(TrailMapOut, vec2<i32>(newPos * settings.size), vec4(1, 0, 0, 0));
|
var trail = vec4<f32>(0, 1, 0, 0);
|
||||||
|
if (agent.species == 0) {
|
||||||
|
trail = vec4(1, 0, 0, 0);
|
||||||
|
}
|
||||||
|
textureStore(TrailMapOut, vec2<i32>(newPos * settings.size), trail);
|
||||||
|
|
||||||
agent.position = newPos;
|
agent.position = newPos;
|
||||||
|
agent.timeToLive -= settings.deltaTime;
|
||||||
agents[id] = agent;
|
agents[id] = agent;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sense(agent: Agent, sensorAngleOffset: f32) -> f32 {
|
fn sense(agent: Agent, sensorOffset: f32, sensorOffsetAngle: f32) -> vec4<f32> {
|
||||||
let sensorAngle: f32 = agent.angle + sensorAngleOffset;
|
let sensorAngle = agent.angle + sensorOffsetAngle;
|
||||||
|
|
||||||
let sensorDir: vec2<f32> = vec2(cos(sensorAngle), sin(sensorAngle)) / normalize(settings.size);
|
let sensorDir: vec2<f32> = vec2(cos(sensorAngle), sin(sensorAngle)) / normalize(settings.size);
|
||||||
let sensorPos: vec2<f32> = agent.position + sensorDir * settings.sensorOffsetDst;
|
let sensorPos: vec2<f32> = agent.position + sensorDir * sensorOffset;
|
||||||
return length(textureLoad(TrailMapIn, vec2<i32>(sensorPos * settings.size), 0));
|
return textureLoad(TrailMapIn, vec2<i32>(sensorPos * settings.size), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn random(state0: u32) -> f32 {
|
fn random(state0: u32) -> f32 {
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,10 @@ struct Settings {
|
||||||
deltaTime: f32,
|
deltaTime: f32,
|
||||||
time: f32,
|
time: f32,
|
||||||
|
|
||||||
diffusionRate: f32,
|
diffusionRateTrails: f32,
|
||||||
decayRate: f32,
|
decayRateTrails: f32,
|
||||||
|
diffusionRateBrush: f32,
|
||||||
|
decayRateBrush: f32,
|
||||||
};
|
};
|
||||||
|
|
||||||
@group(0) @binding(0) var<uniform> settings: Settings;
|
@group(0) @binding(0) var<uniform> settings: Settings;
|
||||||
|
|
@ -22,11 +24,17 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
|
||||||
+ textureSample(trailMap, Sampler, uv + vec2<f32>(1, 0) / settings.size)
|
+ textureSample(trailMap, Sampler, uv + vec2<f32>(1, 0) / settings.size)
|
||||||
) / 4;
|
) / 4;
|
||||||
|
|
||||||
let mixed = mix(
|
let mixedTrails = mix(
|
||||||
current,
|
current.rgb,
|
||||||
neighbours,
|
neighbours.rgb,
|
||||||
settings.diffusionRate
|
settings.diffusionRateTrails
|
||||||
) * (1.0 - settings.decayRate);
|
) * (1.0 - settings.decayRateTrails);
|
||||||
|
|
||||||
return clamp(mixed, vec4(0), vec4(1));
|
let mixedBrush = mix(
|
||||||
|
current.a,
|
||||||
|
neighbours.a,
|
||||||
|
settings.diffusionRateBrush
|
||||||
|
) * (1.0 - settings.decayRateBrush);
|
||||||
|
|
||||||
|
return clamp(vec4(mixedTrails, mixedBrush), vec4(0), vec4(1));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import shader from './diffuse.wgsl';
|
||||||
import { DiffusionSettings } from './diffusion-settings';
|
import { DiffusionSettings } from './diffusion-settings';
|
||||||
|
|
||||||
export class DiffusionPipeline {
|
export class DiffusionPipeline {
|
||||||
private static readonly UNIFORM_COUNT = 16;
|
private static readonly UNIFORM_COUNT = 18;
|
||||||
|
|
||||||
private readonly pipeline: GPURenderPipeline;
|
private readonly pipeline: GPURenderPipeline;
|
||||||
private readonly uniforms: GPUBuffer;
|
private readonly uniforms: GPUBuffer;
|
||||||
|
|
@ -45,8 +45,10 @@ export class DiffusionPipeline {
|
||||||
canvasSize,
|
canvasSize,
|
||||||
deltaTime,
|
deltaTime,
|
||||||
time,
|
time,
|
||||||
diffusionRate,
|
diffusionRateTrails,
|
||||||
decayRate,
|
decayRateTrails,
|
||||||
|
diffusionRateBrush,
|
||||||
|
decayRateBrush,
|
||||||
}: CommonParameters & DiffusionSettings) {
|
}: CommonParameters & DiffusionSettings) {
|
||||||
this.device.queue.writeBuffer(
|
this.device.queue.writeBuffer(
|
||||||
this.uniforms,
|
this.uniforms,
|
||||||
|
|
@ -56,8 +58,10 @@ export class DiffusionPipeline {
|
||||||
canvasSize[1],
|
canvasSize[1],
|
||||||
deltaTime,
|
deltaTime,
|
||||||
time,
|
time,
|
||||||
diffusionRate,
|
diffusionRateTrails,
|
||||||
decayRate,
|
decayRateTrails,
|
||||||
|
diffusionRateBrush,
|
||||||
|
decayRateBrush,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
export interface DiffusionSettings {
|
export interface DiffusionSettings {
|
||||||
diffusionRate: number;
|
diffusionRateTrails: number;
|
||||||
decayRate: number;
|
decayRateTrails: number;
|
||||||
|
diffusionRateBrush: number;
|
||||||
|
decayRateBrush: number;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,10 +32,12 @@ export const settings: GameLoopSettings &
|
||||||
sensorAngleDegrees: 30,
|
sensorAngleDegrees: 30,
|
||||||
sensorOffsetDst: 0.025,
|
sensorOffsetDst: 0.025,
|
||||||
|
|
||||||
decayRate: 0.005,
|
diffusionRateTrails: 0.8,
|
||||||
diffusionRate: 0.9,
|
decayRateTrails: 0.03,
|
||||||
|
diffusionRateBrush: 0.9,
|
||||||
|
decayRateBrush: 0.003,
|
||||||
|
|
||||||
brushColor: palette.beige,
|
brushColor: palette.yellow,
|
||||||
speciesColorA: palette.yellow,
|
speciesColorA: palette.yellow,
|
||||||
speciesColorB: palette.green,
|
speciesColorB: palette.green,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue