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

@ -1,5 +1,5 @@
import { generateNoise } from '../../utils/graphics/noise/noise';
import { smartCompile } from '../../utils/webgpu/smart-compile';
import { smartCompile } from '../../utils/graphics/smart-compile';
import { CommonParameters } from '../common-parameters';
import { BrushSettings } from './brush-settings';
import shader from './brush.wgsl';
@ -24,10 +24,12 @@ export class BrushPipeline {
public constructor(private readonly device: GPUDevice) {
this.noise = generateNoise({
device,
octaves: 4,
amplitude: 0.7,
gain: 0.6,
lacunarity: 4,
width: 512,
height: 512,
octaves: 16,
amplitude: 0.5,
gain: 0.8,
lacunarity: 80,
});
this.vertexBuffer = device.createBuffer({
@ -138,12 +140,18 @@ export class BrushPipeline {
deltaTime,
time,
brushWidth,
brushBlurWidth,
brushWidthRandomness,
}: CommonParameters & BrushSettings) {
this.device.queue.writeBuffer(
this.uniforms,
0,
new Float32Array([...canvasSize, deltaTime, time, brushWidth / 2, brushBlurWidth])
new Float32Array([
...canvasSize,
deltaTime,
time,
brushWidth / 2,
brushWidthRandomness,
])
);
// this.linePoints = [

View file

@ -1,4 +1,4 @@
export interface BrushSettings {
brushWidth: number;
brushBlurWidth: number;
brushWidthRandomness: number;
}

View file

@ -3,7 +3,7 @@ struct Settings {
deltaTime: f32,
time: f32,
brushWidth: f32,
brushBlurWidth: f32
brushWidthRandomness: f32
};
@group(0) @binding(0) var<uniform> settings: Settings;
@ -34,17 +34,20 @@ fn fragment(
@location(1) start: vec2<f32>,
@location(2) end: vec2<f32>
) -> @location(0) vec4<f32> {
let pa = (screenPosition - start);
let direction = (end - start);
let q = clamp(dot(pa, direction) / dot(direction, direction), 0, 1);
var distance = distanceFromLine(screenPosition, start, end);
let noise = textureSample(noise, Sampler, screenPosition / settings.size);
let distance = length(pa - direction * q) + noise.r * 5;
distance += noise.r * settings.brushWidthRandomness;
if(distance > settings.brushWidth) {
discard;
}
let strength = clamp((settings.brushWidth - distance) / settings.brushBlurWidth, 0, 1);
return vec4(0, 0, 0, strength);
return vec4(0, 0, 0, 1);
}
fn distanceFromLine(position: vec2<f32>, start: vec2<f32>, end: vec2<f32>) -> f32 {
let pa = position - start;
let direction = end - start;
let q = clamp(dot(pa, direction) / dot(direction, direction), 0, 1);
return length(pa - direction * q);
}