Add differentiated rendering

This commit is contained in:
Andras Schmelczer 2023-04-29 21:38:57 +01:00
commit 1578c8796a
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
7 changed files with 59 additions and 23 deletions

View file

@ -53,11 +53,7 @@ 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( textureStore(TrailMapOut, vec2<i32>(newPos * settings.size), vec4(1, 0, 0, 0));
TrailMapOut,
vec2<i32>(newPos * settings.size),
vec4(1)
);
agent.position = newPos; agent.position = newPos;
agents[id] = agent; agents[id] = agent;
@ -67,7 +63,7 @@ fn sense(agent: Agent, sensorAngleOffset: f32) -> f32 {
let sensorAngle: f32 = agent.angle + sensorAngleOffset; let sensorAngle: f32 = agent.angle + sensorAngleOffset;
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 * settings.sensorOffsetDst;
return textureLoad(TrailMapIn, vec2<i32>(sensorPos * settings.size), 0).x; return length(textureLoad(TrailMapIn, vec2<i32>(sensorPos * settings.size), 0));
} }
fn random(state0: u32) -> f32 { fn random(state0: u32) -> f32 {

View file

@ -75,11 +75,15 @@ export class BrushPipeline {
format: 'rgba16float', format: 'rgba16float',
blend: { blend: {
color: { color: {
operation: 'add',
srcFactor: 'zero',
dstFactor: 'one',
},
alpha: {
operation: 'max', operation: 'max',
srcFactor: 'one', srcFactor: 'one',
dstFactor: 'one', dstFactor: 'one',
}, },
alpha: {},
}, },
}, },
], ],

View file

@ -45,5 +45,6 @@ fn fragment(
discard; discard;
} }
return vec4(clamp((settings.brushWidth - distance) / settings.brushBlurWidth, 0, 1)); let strength = clamp((settings.brushWidth - distance) / settings.brushBlurWidth, 0, 1);
return vec4(0, 0, 0, strength);
} }

View file

@ -5,7 +5,7 @@ import { RenderSettings } from './render-settings';
import shader from './render.wgsl'; import shader from './render.wgsl';
export class RenderPipeline { export class RenderPipeline {
private static readonly UNIFORM_COUNT = 4; private static readonly UNIFORM_COUNT = 16;
private readonly pipeline: GPURenderPipeline; private readonly pipeline: GPURenderPipeline;
private readonly uniforms: GPUBuffer; private readonly uniforms: GPUBuffer;
@ -48,11 +48,24 @@ export class RenderPipeline {
canvasSize, canvasSize,
deltaTime, deltaTime,
time, time,
brushColor,
speciesColorA,
speciesColorB,
}: CommonParameters & RenderSettings) { }: CommonParameters & RenderSettings) {
this.device.queue.writeBuffer( this.device.queue.writeBuffer(
this.uniforms, this.uniforms,
0, 0,
new Float32Array([...canvasSize, deltaTime, time]) new Float32Array([
...canvasSize,
deltaTime,
time,
...brushColor,
0, //padding
...speciesColorA,
0, //padding
...speciesColorB,
0, //padding
])
); );
} }

View file

@ -1 +1,7 @@
export interface RenderSettings {} import { vec3 } from 'gl-matrix';
export interface RenderSettings {
brushColor: vec3;
speciesColorA: vec3;
speciesColorB: vec3;
}

View file

@ -2,6 +2,9 @@ struct Settings {
size: vec2<f32>, size: vec2<f32>,
deltaTime: f32, deltaTime: f32,
time: f32, time: f32,
brushColor: vec3<f32>,
speciesColorA: vec3<f32>,
speciesColorB: vec3<f32>,
}; };
@group(0) @binding(0) var<uniform> settings: Settings; @group(0) @binding(0) var<uniform> settings: Settings;
@ -10,6 +13,15 @@ struct Settings {
@fragment @fragment
fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> { fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
return vec4(textureSample(TargetTexture, mySampler, uv).rgb, 1.0); let traces = textureSample(TargetTexture, mySampler, uv);
return vec4(0, settings.deltaTime * 0.0, 0.0, 1.0);
let speciesAStrength = traces.r;
let speciesBStrength = traces.g;
let brushStrength = traces.a;
return vec4(
settings.speciesColorA * speciesAStrength +
settings.speciesColorB * speciesBStrength +
settings.brushColor * brushStrength,
1
);
} }

View file

@ -5,6 +5,15 @@ import { DiffusionSettings } from './pipelines/diffusion/diffusion-settings';
import { RenderSettings } from './pipelines/render/render-settings'; import { RenderSettings } from './pipelines/render/render-settings';
import { rgb255 } from './utils/colors/rgb255'; import { rgb255 } from './utils/colors/rgb255';
const palette = {
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),
};
export const settings: GameLoopSettings & export const settings: GameLoopSettings &
AgentSettings & AgentSettings &
BrushSettings & BrushSettings &
@ -23,15 +32,10 @@ export const settings: GameLoopSettings &
sensorAngleDegrees: 30, sensorAngleDegrees: 30,
sensorOffsetDst: 0.025, sensorOffsetDst: 0.025,
decayRate: 0.01, decayRate: 0.005,
diffusionRate: 0.8, diffusionRate: 0.9,
};
const paletteMap = { brushColor: palette.beige,
blue: rgb255(0, 110, 202), speciesColorA: palette.yellow,
red: rgb255(232, 141, 122), speciesColorB: palette.green,
green: rgb255(90, 188, 94),
purple: rgb255(161, 90, 188),
yellow: rgb255(255, 204, 0),
beige: rgb255(229, 204, 175),
}; };