Improve rendering
This commit is contained in:
parent
f6c7abf8dc
commit
afe2a67ba0
20 changed files with 147 additions and 95 deletions
|
|
@ -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 = [
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
export interface BrushSettings {
|
||||
brushWidth: number;
|
||||
brushBlurWidth: number;
|
||||
brushWidthRandomness: number;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue