From 6085f9da9f6fc4eef7c6d08c68a152f14635a3cc Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Tue, 18 Apr 2023 21:37:31 +0100 Subject: [PATCH] Improve spawn shape --- src/pipelines/agents/agent-pipeline.ts | 3 --- src/pipelines/agents/agent.wgsl | 31 ++++++++++----------- src/renderer.ts | 37 ++++++++++++++++++++------ src/settings.ts | 18 +++++++------ 4 files changed, 53 insertions(+), 36 deletions(-) diff --git a/src/pipelines/agents/agent-pipeline.ts b/src/pipelines/agents/agent-pipeline.ts index ef0e430..525d170 100644 --- a/src/pipelines/agents/agent-pipeline.ts +++ b/src/pipelines/agents/agent-pipeline.ts @@ -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, ]) ); } diff --git a/src/pipelines/agents/agent.wgsl b/src/pipelines/agents/agent.wgsl index a0b1c4a..6c136d3 100644 --- a/src/pipelines/agents/agent.wgsl +++ b/src/pipelines/agents/agent.wgsl @@ -13,7 +13,6 @@ struct Settings { sensorAngle : f32, sensorOffsetDst : f32, - sensorSize : f32, }; @group(0) @binding(0) var settings : Settings; @@ -21,20 +20,7 @@ struct Settings { @group(0) @binding(2) var TrailMapIn : texture_2d; @group(0) @binding(3) var TrailMapOut : texture_storage_2d; - -// 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) { let id = global_id.x; @@ -60,10 +46,9 @@ fn main(@builtin(global_invocation_id) global_id : vec3) { let direction = vec2(cos(agent.angle), sin(agent.angle)); var newPos = agent.position + direction / normalize(settings.size) * settings.moveRate; - newPos = clamp(newPos, vec2(0, 0), vec2(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) { 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 = vec2(cos(sensorAngle), sin(sensorAngle)) / normalize(settings.size); diff --git a/src/renderer.ts b/src/renderer.ts index bb9e7b7..7ca3f49 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -36,10 +36,29 @@ export default class Renderer { this.resize(); window.addEventListener('resize', this.resize.bind(this)); - const agents: Array = 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 = 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()]); diff --git a/src/settings.ts b/src/settings.ts index 6b8ff0d..97396d1 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -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, };