Improve rendering

This commit is contained in:
Andras Schmelczer 2023-04-30 16:02:07 +01:00
commit afe2a67ba0
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
20 changed files with 147 additions and 95 deletions

View file

@ -12,12 +12,12 @@ struct Settings {
@group(0) @binding(0) var<uniform> settings: Settings;
@group(0) @binding(1) var Sampler: sampler;
@group(0) @binding(2) var trailMap: texture_2d<f32>;
@group(0) @binding(3) var noise: texture_2d<f32>;
@group(0) @binding(3) var noiseMap: texture_2d<f32>;
@fragment
fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
var current = textureSample(trailMap, Sampler, uv);
let noise = textureSample(noise, Sampler, uv);
let noise = textureSample(noiseMap, Sampler, uv);
let neighbours: vec4<f32> = (
textureSample(trailMap, Sampler, uv + vec2<f32>(0, 1) / settings.size)
@ -26,17 +26,33 @@ fn fragment(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
+ textureSample(trailMap, Sampler, uv + vec2<f32>(1, 0) / settings.size)
) / 4;
let mixedTrails = mix(
var q = vec4<f32>(0);
for (var x: i32 = -1; x <= 1; x++) {
for (var y: i32 = -1; y <= 1; y++) {
if (x != 0 || y != 0) {
let offset = vec2(f32(x), f32(y));
let neighbour = textureSample(trailMap, Sampler, uv + offset / settings.size);
// let noise = textureSample(noiseMap, Sampler, uv + offset / settings.size * 0.5).r;
let noise = random(uv + offset / settings.size * 0.5);
let difference = neighbour - current;
q += vec4(
min(1.0, length(neighbour.rgb)) * pow(noise, settings.diffusionRateTrails) * difference.rgb,
min(1.0, length(neighbour.a)) * pow(noise, settings.diffusionRateBrush) * difference.a
);
}
}
}
current += q / 4;
let noise1 = random(uv);
let decayed = vec4(
current.rgb,
neighbours.rgb,
settings.diffusionRateTrails
) * (1.0 - settings.decayRateTrails);
let mixedBrush = mix(
current.a + (noise.a - 0.5) * 0.1,
neighbours.a ,
settings.diffusionRateBrush
) * (1.0 - settings.decayRateBrush);
return clamp(vec4(mixedTrails, mixedBrush), vec4(0), vec4(1));
current.a
) - vec4(vec3(settings.decayRateTrails), settings.decayRateBrush) * settings.deltaTime * ((noise1 - 0.5) * 0.25 + 1);
return clamp(decayed, vec4(0), vec4(1));
}

View file

@ -1,6 +1,7 @@
import { setUpFullScreenQuad } from '../../utils/graphics/full-screen-quad/full-screen-quad';
import { generateNoise } from '../../utils/graphics/noise/noise';
import { smartCompile } from '../../utils/webgpu/smart-compile';
import random from '../../utils/graphics/random.wgsl';
import { smartCompile } from '../../utils/graphics/smart-compile';
import { CommonParameters } from '../common-parameters';
import shader from './diffuse.wgsl';
import { DiffusionSettings } from './diffusion-settings';
@ -34,7 +35,7 @@ export class DiffusionPipeline {
layout: 'auto',
vertex,
fragment: {
module: smartCompile(device, shader),
module: smartCompile(device, random, shader),
entryPoint: 'fragment',
targets: [
{