Refactor
This commit is contained in:
parent
a3f76f5c77
commit
99e1923066
6 changed files with 49 additions and 35 deletions
|
|
@ -80,6 +80,8 @@ export default class GameLoop {
|
|||
return {
|
||||
position,
|
||||
angle: angle + Math.PI,
|
||||
species: 0,
|
||||
timeToLive: Random.randomBetween(2, 10),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,24 +34,23 @@ export class AgentPipeline {
|
|||
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
||||
});
|
||||
|
||||
const serializedAgents = new Float32Array(
|
||||
new ArrayBuffer(agents.length * AGENT_SIZE_IN_BYTES)
|
||||
);
|
||||
agents.forEach((agent, i) => {
|
||||
serializedAgents[(i * AGENT_SIZE_IN_BYTES) / Float32Array.BYTES_PER_ELEMENT + 0] =
|
||||
agent.position[0];
|
||||
serializedAgents[(i * AGENT_SIZE_IN_BYTES) / Float32Array.BYTES_PER_ELEMENT + 1] =
|
||||
agent.position[1];
|
||||
serializedAgents[(i * AGENT_SIZE_IN_BYTES) / Float32Array.BYTES_PER_ELEMENT + 2] =
|
||||
agent.angle;
|
||||
});
|
||||
|
||||
this.agentsBuffer = device.createBuffer({
|
||||
size: agents.length * AGENT_SIZE_IN_BYTES,
|
||||
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
|
||||
usage: GPUBufferUsage.STORAGE,
|
||||
mappedAtCreation: true,
|
||||
});
|
||||
|
||||
device.queue.writeBuffer(this.agentsBuffer, 0, serializedAgents.buffer);
|
||||
new Float32Array(this.agentsBuffer.getMappedRange()).set(
|
||||
agents.flatMap((agent) => [
|
||||
agent.position[0],
|
||||
agent.position[1],
|
||||
agent.angle,
|
||||
agent.species,
|
||||
agent.timeToLive,
|
||||
0, // padding
|
||||
])
|
||||
);
|
||||
this.agentsBuffer.unmap();
|
||||
}
|
||||
|
||||
public setParameters({
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ import { vec2 } from 'gl-matrix';
|
|||
export interface Agent {
|
||||
position: vec2;
|
||||
angle: number;
|
||||
species: number;
|
||||
timeToLive: number;
|
||||
}
|
||||
|
||||
export const AGENT_SIZE_IN_BYTES = 4 * 4;
|
||||
export const AGENT_SIZE_IN_BYTES = 6 * Float32Array.BYTES_PER_ELEMENT;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
struct Agent {
|
||||
position: vec2<f32>,
|
||||
angle: f32,
|
||||
species: f32,
|
||||
timeToLive: f32
|
||||
}
|
||||
|
||||
struct Settings {
|
||||
|
|
@ -30,7 +32,7 @@ fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
|
|||
|
||||
var agent = agents[id];
|
||||
|
||||
let random = f32(hash(id + u32(settings.time * 10000 + agent.position.y * 10 + agent.position.x))) / 4294967295.0;
|
||||
let random = random(id + u32(settings.time * 10000 + agent.position.y * 10 + agent.position.x));
|
||||
|
||||
let weightForward : f32 = sense(agent, 0.);
|
||||
let weightLeft : f32 = sense(agent, settings.sensorAngle);
|
||||
|
|
@ -54,28 +56,27 @@ fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
|
|||
textureStore(
|
||||
TrailMapOut,
|
||||
vec2<i32>(newPos * settings.size),
|
||||
vec4(vec3<f32>(1.) * settings.trailWeight * 0.02, 1.)
|
||||
vec4(1)
|
||||
);
|
||||
|
||||
agent.position = newPos;
|
||||
agents[id] = agent;
|
||||
}
|
||||
|
||||
fn hash(state0 : u32) -> u32 {
|
||||
// Hash function www.cs.ubc.ca/~rbridson/docs/schechter-sca08-turbulence.pdf
|
||||
var state : u32 = state0;
|
||||
state = state ^ 2747636419u;
|
||||
state = state * 2654435769u;
|
||||
state = state ^ (state >> 16u);
|
||||
state = state * 2654435769u;
|
||||
state = state ^ (state >> 16u);
|
||||
state = state * 2654435769u;
|
||||
return state;
|
||||
}
|
||||
|
||||
fn sense(agent : Agent, sensorAngleOffset : f32) -> f32 {
|
||||
let sensorAngle : f32 = agent.angle + sensorAngleOffset;
|
||||
let sensorDir : vec2<f32> = vec2(cos(sensorAngle), sin(sensorAngle)) / normalize(settings.size);
|
||||
let sensorPos : vec2<f32> = agent.position + sensorDir * settings.sensorOffsetDst;
|
||||
return textureLoad(TrailMapIn, vec2<i32>(sensorPos * settings.size), 0).x;
|
||||
}
|
||||
|
||||
fn random(state0 : u32) -> f32 {
|
||||
var state : u32 = state0;
|
||||
state = state ^ 2747636419u;
|
||||
state = state * 2654435769u;
|
||||
state = state ^ (state >> 16u);
|
||||
state = state * 2654435769u;
|
||||
state = state ^ (state >> 16u);
|
||||
state = state * 2654435769u;
|
||||
return f32(state) / 4294967295.0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,11 +20,11 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
|
|||
+ textureSample(trailMap, Sampler, uv + vec2<f32>(0, -1) / settings.size)
|
||||
+ textureSample(trailMap, Sampler, uv + vec2<f32>(-1, 0) / settings.size)
|
||||
+ textureSample(trailMap, Sampler, uv + vec2<f32>(1, 0) / settings.size)
|
||||
);
|
||||
) / 4;
|
||||
|
||||
let mixed = mix(
|
||||
current,
|
||||
neighbours / 4.0,
|
||||
neighbours,
|
||||
settings.diffusionRate
|
||||
) * (1.0 - settings.decayRate);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,18 +3,19 @@ import { AgentSettings } from './pipelines/agents/agent-settings';
|
|||
import { BrushSettings } from './pipelines/brush/brush-settings';
|
||||
import { DiffusionSettings } from './pipelines/diffusion/diffusion-settings';
|
||||
import { RenderSettings } from './pipelines/render/render-settings';
|
||||
import { rgb255 } from './utils/colors/rgb255';
|
||||
|
||||
export const settings: GameLoopSettings &
|
||||
AgentSettings &
|
||||
BrushSettings &
|
||||
DiffusionSettings &
|
||||
RenderSettings = {
|
||||
agentCount: 1_000,
|
||||
agentCount: 1_000_000,
|
||||
renderSpeed: 1,
|
||||
startingRadius: 0.15,
|
||||
|
||||
brushWidth: 20,
|
||||
brushBlurWidth: 5,
|
||||
brushWidth: 30,
|
||||
brushBlurWidth: 8,
|
||||
|
||||
trailWeight: 5,
|
||||
moveSpeed: 0.025,
|
||||
|
|
@ -22,6 +23,15 @@ export const settings: GameLoopSettings &
|
|||
sensorAngleDegrees: 30,
|
||||
sensorOffsetDst: 0.025,
|
||||
|
||||
decayRate: 0.005,
|
||||
decayRate: 0.01,
|
||||
diffusionRate: 0.8,
|
||||
};
|
||||
|
||||
const paletteMap = {
|
||||
blue: rgb255(0, 110, 202),
|
||||
red: rgb255(232, 141, 122),
|
||||
green: rgb255(90, 188, 94),
|
||||
purple: rgb255(161, 90, 188),
|
||||
yellow: rgb255(255, 204, 0),
|
||||
beige: rgb255(229, 204, 175),
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue