Improve spawn shape

This commit is contained in:
Andras Schmelczer 2023-04-18 21:37:31 +01:00
parent 8ce9b97cf2
commit 6085f9da9f
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
4 changed files with 53 additions and 36 deletions

View file

@ -63,7 +63,6 @@ export class AgentPipeline {
turnSpeed,
sensorAngleDegrees,
sensorOffsetDst,
sensorSize,
}: {
width: number;
height: number;
@ -74,7 +73,6 @@ export class AgentPipeline {
turnSpeed: number;
sensorAngleDegrees: number;
sensorOffsetDst: number;
sensorSize: number;
}) {
this.device.queue.writeBuffer(
this.uniforms,
@ -88,7 +86,6 @@ export class AgentPipeline {
turnSpeed * deltaTime,
(sensorAngleDegrees * Math.PI) / 180,
sensorOffsetDst,
sensorSize,
])
);
}

View file

@ -13,7 +13,6 @@ struct Settings {
sensorAngle : f32,
sensorOffsetDst : f32,
sensorSize : f32,
};
@group(0) @binding(0) var<uniform> settings : Settings;
@ -21,20 +20,7 @@ struct Settings {
@group(0) @binding(2) var TrailMapIn : texture_2d<f32>;
@group(0) @binding(3) var TrailMapOut : texture_storage_2d<rgba16float, write>;
// Hash function www.cs.ubc.ca/~rbridson/docs/schechter-sca08-turbulence.pdf
fn hash(state0 : u32) -> u32 {
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;
}
@compute @workgroup_size(64)
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
let id = global_id.x;
@ -60,10 +46,9 @@ fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
let direction = vec2(cos(agent.angle), sin(agent.angle));
var newPos = agent.position + direction / normalize(settings.size) * settings.moveRate;
newPos = clamp(newPos, vec2<f32>(0, 0), vec2<f32>(1, 1));
if (newPos.x == 0. || newPos.x == 1. || newPos.y == 0. || newPos.y == 1.) {
agent.angle = random * 2. * 3.1415;
agent.angle += 3.14159265359 + random - 0.5;
}
textureStore(
@ -76,6 +61,18 @@ fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
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);

View file

@ -36,10 +36,29 @@ export default class Renderer {
this.resize();
window.addEventListener('resize', this.resize.bind(this));
const agents: Array<Agent> = new Array(settings.agentCount).fill(0).map(() => ({
position: vec2.fromValues(randomBetween(1 / 3, 2 / 3), randomBetween(1 / 3, 2 / 3)),
angle: randomBetween(0, Math.PI * 2),
}));
const minSize = Math.min(this.canvas.width, this.canvas.height);
const ratio = Math.max(this.canvas.width, this.canvas.height) / minSize;
const size = vec2.fromValues(
this.canvas.width / minSize,
this.canvas.height / minSize
);
vec2.normalize(size, size);
console.log(size);
const agents: Array<Agent> = new Array(settings.agentCount).fill(0).map(() => {
const radius = randomBetween(0, settings.startingRadius / ratio);
const angle = randomBetween(0, Math.PI * 2);
const center = vec2.fromValues(0.5, 0.5);
const delta = vec2.fromValues(Math.cos(angle) * radius, Math.sin(angle) * radius);
vec2.divide(delta, delta, size);
const position = vec2.add(vec2.create(), center, delta);
return {
position,
angle: angle + Math.PI,
};
});
this.agentPipeline = new AgentPipeline(this.device, agents);
this.renderPipeline = new RenderPipeline(
@ -114,10 +133,12 @@ export default class Renderer {
});
const commandEncoder = this.device.createCommandEncoder();
this.agentPipeline.execute(commandEncoder, this.trailMapA, this.trailMapB);
this.diffusionPipeline.execute(commandEncoder, this.trailMapB, this.trailMapA);
this.renderPipeline.execute(commandEncoder, this.trailMapA);
[this.trailMapA, this.trailMapB] = [this.trailMapB, this.trailMapA];
for (let i = 0; i < settings.renderSpeed; i++) {
this.agentPipeline.execute(commandEncoder, this.trailMapA, this.trailMapB);
this.diffusionPipeline.execute(commandEncoder, this.trailMapB, this.trailMapA);
this.renderPipeline.execute(commandEncoder, this.trailMapA);
[this.trailMapA, this.trailMapB] = [this.trailMapB, this.trailMapA];
}
this.queue.submit([commandEncoder.finish()]);

View file

@ -1,5 +1,7 @@
interface Settings {
agentCount: number;
renderSpeed: number;
startingRadius: number;
trailWeight: number;
decayRate: number;
diffusionRate: number;
@ -7,19 +9,19 @@ interface Settings {
turnSpeed: number;
sensorAngleDegrees: number;
sensorOffsetDst: number;
sensorSize: number;
}
export const settings: Settings = {
agentCount: 50000,
agentCount: 1_000_000,
renderSpeed: 2,
startingRadius: 0.15,
trailWeight: 5,
decayRate: 0.05,
diffusionRate: 0.2,
diffusionRate: 0.3,
moveSpeed: 0.03,
turnSpeed: 2,
sensorAngleDegrees: 45,
sensorOffsetDst: 35 / 1000,
sensorSize: 1,
moveSpeed: 0.025,
turnSpeed: 6,
sensorAngleDegrees: 30,
sensorOffsetDst: 0.025,
};