Generate agents with compute shader

This commit is contained in:
Andras Schmelczer 2023-05-20 13:28:15 +01:00
commit 6b17a8e023
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
15 changed files with 206 additions and 154 deletions

View file

@ -6,7 +6,7 @@ import { RenderSettings } from './render-settings';
import shader from './render.wgsl';
export class RenderPipeline {
private static readonly UNIFORM_COUNT = 12;
private static readonly UNIFORM_COUNT = 13;
private readonly bindGroupLayout: GPUBindGroupLayout;
private readonly pipeline: GPURenderPipeline;
@ -51,7 +51,12 @@ export class RenderPipeline {
});
}
public setParameters({ brushColor, speciesColorA, speciesColorB }: RenderSettings) {
public setParameters({
brushColor,
speciesColorA,
speciesColorB,
clarity,
}: RenderSettings) {
this.device.queue.writeBuffer(
this.uniforms,
0,
@ -61,7 +66,7 @@ export class RenderPipeline {
...speciesColorA,
0, //padding
...speciesColorB,
0, //padding
clarity,
])
);
}

View file

@ -4,4 +4,5 @@ export interface RenderSettings {
brushColor: vec3;
speciesColorA: vec3;
speciesColorB: vec3;
clarity: number;
}

View file

@ -2,6 +2,7 @@ struct Settings {
brushColor: vec3<f32>,
speciesColorA: vec3<f32>,
speciesColorB: vec3<f32>,
clarity: f32,
};
@group(1) @binding(0) var<uniform> settings: Settings;
@ -13,19 +14,17 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
let traces = textureSample(trailMap, Sampler, uv);
let random = textureSample(noise, noiseSampler, uv);
let backgroundColor = vec3(0.9) + 0.075 * random.r;
let speciesAStrength = traces.r;
let speciesBStrength = traces.g;
let brushStrength = traces.a;
let rgbColor = sqrt(vec3(
settings.speciesColorA * clamp(speciesAStrength, 0, 1) +
settings.speciesColorB * clamp(speciesBStrength, 0, 1) +
settings.speciesColorA * clamp(pow(speciesAStrength, settings.clarity), 0, 1) +
settings.speciesColorB * clamp(pow(speciesBStrength, settings.clarity), 0, 1) +
settings.brushColor * brushStrength
));
let bg = vec3(0.9) + 0.075 * random.r;
return vec4(bg - rgbColor, 1);
return vec4(backgroundColor - rgbColor, 1);
}