diff options
Diffstat (limited to 'src/shaders')
| -rw-r--r-- | src/shaders/debug/debug_print.wgsl | 9 | ||||
| -rw-r--r-- | src/shaders/math/noise.wgsl | 7 | ||||
| -rw-r--r-- | src/shaders/render/scratch_lines.wgsl | 36 |
3 files changed, 29 insertions, 23 deletions
diff --git a/src/shaders/debug/debug_print.wgsl b/src/shaders/debug/debug_print.wgsl index 22b5838..7e6debe 100644 --- a/src/shaders/debug/debug_print.wgsl +++ b/src/shaders/debug/debug_print.wgsl @@ -11,6 +11,8 @@ // each component holds 4 chars (MSB = leftmost char) // e.g. "Hi!" -> vec4u(0x48692100u, 0u, 0u, 0u), 3u +const ink_color = vec4f(1.0, 1.0, 0.0, 1.0); // yellow + // Returns lit pixel (0 or 1) for ASCII code [0x20-0x7E], row r [0-7], col c [0-7]. // Encoding: 2 u32s per glyph; hi covers rows 0-3, lo covers rows 4-7. // Within each u32, row r occupies bits [(3-r%4)*8+7 : (3-r%4)*8]. @@ -217,8 +219,7 @@ fn _dbg_char(ascii: u32, r: u32, c: u32) -> u32 { // pixel_pos : @builtin(position).xy // origin : top-left corner of text in screen pixels // value : f32 to display (format: [-]DDD.DDD, 64×8 px) -fn debug_f32(col: vec3f, pixel_pos: vec2f, origin: vec2f, value: f32) -> vec3f { - let ink_color = vec3f(1.0, 1.0, 0.0); // yellow +fn debug_f32(col: vec4f, pixel_pos: vec2f, origin: vec2f, value: f32) -> vec4f { let lp = pixel_pos - origin; if (lp.x < 0.0 || lp.x >= 64.0 || lp.y < 0.0 || lp.y >= 8.0) { return col; @@ -256,8 +257,8 @@ fn debug_f32(col: vec3f, pixel_pos: vec2f, origin: vec2f, value: f32) -> vec3f { // // Example — "Hello" (5 chars, ASCII 0x48 0x65 0x6C 0x6C 0x6F): // debug_str(col, pos.xy, origin, vec4u(0x48656C6Cu, 0x6F000000u, 0u, 0u), 5u) -fn debug_str(col: vec3f, pixel_pos: vec2f, origin: vec2f, s: vec4u, len: u32) -> vec3f { - let ink_color = vec3f(1.0, 1.0, 0.0); // yellow +fn debug_str(col: vec4f, pixel_pos: vec2f, origin: vec2f, s: vec4u, len: u32) -> +vec4f { let lp = pixel_pos - origin; let max_w = f32(len * 8u); if (lp.x < 0.0 || lp.x >= max_w || lp.y < 0.0 || lp.y >= 8.0) { diff --git a/src/shaders/math/noise.wgsl b/src/shaders/math/noise.wgsl index dd97e02..dff6619 100644 --- a/src/shaders/math/noise.wgsl +++ b/src/shaders/math/noise.wgsl @@ -21,6 +21,13 @@ fn hash_2f(p: vec2f) -> f32 { return fract(sin(h) * 43758.5453123); } +// variant for vec2f -> f32 hashing +fn hash_2f_alt(p: vec2f) -> f32 { + var p3 = fract(p.xyx * .1376); + p3 += dot(p3, p3.yzx + 33.33); + return fract((p3.x + p3.y) * p3.z); +} + // Hash: vec2f -> vec2f // 2D coordinate to 2D hash (from Shadertoy 4djSRW) fn hash_2f_2f(p: vec2f) -> vec2f { diff --git a/src/shaders/render/scratch_lines.wgsl b/src/shaders/render/scratch_lines.wgsl index 04ed6f8..6123486 100644 --- a/src/shaders/render/scratch_lines.wgsl +++ b/src/shaders/render/scratch_lines.wgsl @@ -12,24 +12,22 @@ #include "math/noise" fn scratch_lines(uv: vec2f, resolution: vec2f, time: f32) -> f32 { - // Quantize to ~24fps to simulate film frame rate - let t = floor(time * 24.0); + // Quantize to ~24fps to simulate film frame rate + let t = floor(time * 24.0); - var intensity = 0.0; - for (var i = 0; i < 8; i++) { - let seed = f32(i) * 17.3 + t; - // ~25% chance this scratch is visible this frame - let visible = step(0.75, hash_1f(seed + 3.1)); - // Random vertical position [0, 1] - let y = hash_1f(seed + 7.9); - // Thickness in pixels [1, 4] - let thickness = hash_1f(seed + 13.5) * 3.0 + 1.0; - // Brightness [0.5, 1.0] - let brightness = hash_1f(seed + 21.7) * 0.5 + 0.5; - // Soft linear falloff from line center - let dy = abs(uv.y - y) * resolution.y; - let line = clamp(1.0 - dy / thickness, 0.0, 1.0); - intensity += line * brightness * visible; - } - return clamp(intensity, 0.0, 1.0); + // Scanlines + let scan = abs(sin(uv.y * uniforms.resolution.y * 3.14159265)); + var intensity = 0.2 * scan; + + // Per-pixel temporal noise + let pixel = floor(uv * uniforms.resolution * .4); + let n = hash_2f(pixel + vec2f(time * 47.3, time * 31.7)) * 0.04 - 0.02; + intensity += n; + + // Horizontal jitter line (random scanline rolling artifact) + let jitter_y = hash_1f(fract(t * 0.37)); + let jitter_band = abs(uv.y - jitter_y); + let scratch = smoothstep(5. / uniforms.resolution.y, 0., jitter_band); + intensity += scratch * 0.11 * hash_1f(uv.x * 100.0 + t); + return clamp(intensity, 0.0, 1.0); } |
