Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

32.4 KB
Raw Blame
//! Native Rust procedural brush tip & seamless 512×512 surface heightfield generators.
//!
//! Ports the mathematical primitives (`sfc32`, periodic bilinear noise grids, Worley
//! cellular dome fields, smoothstep) and built-in brush tips & paper/canvas textures from
//! `src/utils/proceduralBrushTips.ts` and `src/utils/proProceduralBrushTips.ts`.

#[inline]
fn clamp01(v: f32) -> f32 {
    v.clamp(0.0, 1.0)
}

#[inline]
fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
    let t = clamp01((x - edge0) / (edge1 - edge0).max(1e-6));
    t * t * (3.0 - 2.0 * t)
}

/// Deterministic `sfc32` PRNG matching `createPrng` in `proceduralBrushTips.ts`.
#[derive(Debug, Clone)]
pub struct Sfc32 {
    a: u32,
    b: u32,
    c: u32,
    d: u32,
}

impl Sfc32 {
    pub fn new(seed: u32) -> Self {
        let mut rng = Self {
            a: seed,
            b: 0x9E37_79B9,
            c: 0x243F_6A88,
            d: 0xB7E1_5162,
        };
        for _ in 0..12 {
            rng.next_f32();
        }
        rng
    }

    #[inline]
    pub fn next_u32(&mut self) -> u32 {
        let t = self.a.wrapping_add(self.b).wrapping_add(self.d);
        self.d = self.d.wrapping_add(1);
        self.a = self.b ^ (self.b >> 9);
        self.b = self.c.wrapping_add(self.c << 3);
        self.c = self.c.rotate_left(21).wrapping_add(t);
        t
    }

    #[inline]
    pub fn next_f32(&mut self) -> f32 {
        self.next_u32() as f32 / 4_294_967_296.0
    }
}

fn make_periodic_grid(grid_size: usize, rng: &mut Sfc32) -> Vec<f32> {
    let mut g = vec![0.0f32; grid_size * grid_size];
    for v in &mut g {
        *v = rng.next_f32();
    }
    g
}

#[inline]
fn sample_periodic(grid: &[f32], grid_size: usize, x: f32, y: f32) -> f32 {
    let gs = grid_size as f32;
    let gx = x.rem_euclid(gs);
    let gy = y.rem_euclid(gs);
    let x0 = gx.floor() as usize % grid_size;
    let y0 = gy.floor() as usize % grid_size;
    let x1 = (x0 + 1) % grid_size;
    let y1 = (y0 + 1) % grid_size;
    let fx = gx - gx.floor();
    let fy = gy - gy.floor();
    let ux = fx * fx * (3.0 - 2.0 * fx);
    let uy = fy * fy * (3.0 - 2.0 * fy);

    let v00 = grid[y0 * grid_size + x0];
    let v10 = grid[y0 * grid_size + x1];
    let v01 = grid[y1 * grid_size + x0];
    let v11 = grid[y1 * grid_size + x1];
    let a = v00 + (v10 - v00) * ux;
    let b = v01 + (v11 - v01) * ux;
    a + (b - a) * uy
}

/// Periodic Worley cellular dome field for authentic mineral chalk / charcoal / cold-press peaks.
fn make_cellular_dome_field(
    out_size: usize,
    cells: usize,
    rng: &mut Sfc32,
    dome_power: f32,
) -> Vec<f32> {
    let mut fx = vec![0.0f32; cells * cells];
    let mut fy = vec![0.0f32; cells * cells];
    for i in 0..(cells * cells) {
        fx[i] = 0.15 + rng.next_f32() * 0.70;
        fy[i] = 0.15 + rng.next_f32() * 0.70;
    }

    let mut out = vec![0.0f32; out_size * out_size];
    let inv = cells as f32 / out_size as f32;
    for y in 0..out_size {
        let cy_f = y as f32 * inv;
        let cy_i = cy_f.floor() as isize;
        for x in 0..out_size {
            let cx_f = x as f32 * inv;
            let cx_i = cx_f.floor() as isize;
            let mut min_d2 = 10.0f32;
            for oy in -1..=1 {
                let ny = (cy_i + oy).rem_euclid(cells as isize) as usize;
                for ox in -1..=1 {
                    let nx = (cx_i + ox).rem_euclid(cells as isize) as usize;
                    let idx = ny * cells + nx;
                    let px = (cx_i + ox) as f32 + fx[idx];
                    let py = (cy_i + oy) as f32 + fy[idx];
                    let dx = px - cx_f;
                    let dy = py - cy_f;
                    let d2 = dx * dx + dy * dy;
                    if d2 < min_d2 {
                        min_d2 = d2;
                    }
                }
            }
            let d = min_d2.sqrt();
            let dome = clamp01(1.0 - d * 1.18).powf(dome_power);
            out[y * out_size + x] = dome;
        }
    }
    out
}

#[inline]
fn write_alpha_pixel(rgba: &mut [u8], off: usize, alpha: f32) {
    let a = (clamp01(alpha) * 255.0 + 0.5) as u8;
    rgba[off] = 255;
    rgba[off + 1] = 255;
    rgba[off + 2] = 255;
    rgba[off + 3] = a;
}

#[inline]
fn write_gray_height_pixel(rgba: &mut [u8], off: usize, height: f32) {
    let g = (clamp01(height) * 255.0 + 0.5) as u8;
    rgba[off] = g;
    rgba[off + 1] = g;
    rgba[off + 2] = g;
    rgba[off + 3] = 255;
}

// =============================================================================
// 1. SEAMLESS 1024×1024 HIGH-DPI (2×) SURFACE & PAPER HEIGHTFIELDS
// =============================================================================

/// Generates any of InkStudio's 7 built-in seamless `SurfaceTextureId` heightfields
/// or 6 procedural paper textures natively in Rust as a 1024×1024 high-DPI (2× oversampled)
/// RGBA8 grayscale tile matching `buildSurfaceHeightField` in `src/utils/proceduralBrushTips.ts`.
pub fn generate_builtin_paper_texture(id: &str) -> Option<(u32, u32, Vec<u8>)> {
    let size = 1024usize;
    let inv_size = 1.0f32 / size as f32;
    let mut rgba = vec![0u8; size * size * 4];
    match id {
        "default" | "cold-press-default" => {
            let mut rng = Sfc32::new(0xC01D_94E5);
            let n_coarse = make_periodic_grid(16, &mut rng);
            let n_med = make_periodic_grid(48, &mut rng);
            let n_fine = make_periodic_grid(128, &mut rng);
            for y in 0..size {
                let ny = y as f32 * inv_size;
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let c = sample_periodic(&n_coarse, 16, nx * 16.0, ny * 16.0);
                    let m = sample_periodic(&n_med, 48, nx * 48.0, ny * 48.0);
                    let f = sample_periodic(&n_fine, 128, nx * 128.0, ny * 128.0);
                    let h = clamp01(c * 0.45 + m * 0.35 + f * 0.20);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "linen-canvas" | "canvas-weave" => {
            // 80 Belgian linen threads across 1024 high-DPI texels (12.8 texels/thread -> ~5.5px at scale=0.85)
            // Purely continuous, band-limited cylindrical yarn geometry (ZERO per-texel white noise -> silky 3D normals!)
            let mut rng = Sfc32::new(0x07A1_1F01);
            let threads = 80usize;
            let threads_f = threads as f32;
            let mut warp_slub = vec![0.0f32; threads];
            let mut weft_slub = vec![0.0f32; threads];
            let mut warp_jitter = vec![0.0f32; threads];
            let mut weft_jitter = vec![0.0f32; threads];
            let mut warp_height = vec![0.0f32; threads];
            let mut weft_height = vec![0.0f32; threads];
            for i in 0..threads {
                warp_slub[i] = 0.82 + rng.next_f32() * 0.30;
                weft_slub[i] = 0.82 + rng.next_f32() * 0.30;
                warp_jitter[i] = (rng.next_f32() - 0.5) * 0.18;
                weft_jitter[i] = (rng.next_f32() - 0.5) * 0.18;
                warp_height[i] = 0.90 + rng.next_f32() * 0.15;
                weft_height[i] = 0.88 + rng.next_f32() * 0.15;
            }
            let g_fiber = make_periodic_grid(128, &mut rng);
            let g_undul = make_periodic_grid(32, &mut rng);
            let g_drift = make_periodic_grid(16, &mut rng);
            let pi = std::f32::consts::PI;
            let half_pi = std::f32::consts::FRAC_PI_2;

            for y in 0..size {
                let ny = y as f32 * inv_size;
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let drift_x = (sample_periodic(&g_drift, 16, nx * 16.0, ny * 16.0) - 0.5) * 0.24;
                    let drift_y =
                        (sample_periodic(&g_drift, 16, nx * 16.0 + 7.3, ny * 16.0 + 4.9) - 0.5)
                            * 0.24;

                    let tx = (nx * threads_f + drift_x).rem_euclid(threads_f);
                    let ty = (ny * threads_f + drift_y).rem_euclid(threads_f);
                    let col_cell = (tx.floor() as usize) % threads;
                    let row_cell = (ty.floor() as usize) % threads;
                    let frac_x = tx - tx.floor();
                    let frac_y = ty - ty.floor();

                    let parity = ((row_cell + col_cell) & 1) == 0;
                    let dx = (frac_x - 0.5 + warp_jitter[col_cell] * 0.15) * 2.0;
                    let dy = (frac_y - 0.5 + weft_jitter[row_cell] * 0.15) * 2.0;

                    let warp_cyl =
                        ((dx.abs() / warp_slub[col_cell].max(0.60)).min(1.0) * half_pi).cos();
                    let weft_cyl =
                        ((dy.abs() / weft_slub[row_cell].max(0.60)).min(1.0) * half_pi).cos();

                    let warp_twist =
                        0.93 + 0.07 * ((frac_x * 2.2 + ny * threads_f * 2.5) * pi).sin();
                    let weft_twist =
                        0.93 + 0.07 * ((frac_y * 2.2 - nx * threads_f * 2.5) * pi).sin();

                    let warp_arch = warp_cyl
                        * (0.64 + 0.36 * (dy * half_pi).cos())
                        * warp_twist
                        * warp_height[col_cell];
                    let weft_arch = weft_cyl
                        * (0.64 + 0.36 * (dx * half_pi).cos())
                        * weft_twist
                        * weft_height[row_cell];

                    let over = if parity { warp_arch } else { weft_arch };
                    let under = if parity {
                        weft_arch * 0.44
                    } else {
                        warp_arch * 0.44
                    };
                    let base_weave = over.max(under);

                    let fiber = sample_periodic(&g_fiber, 128, nx * 128.0, ny * 128.0);
                    let undul = sample_periodic(&g_undul, 32, nx * 32.0, ny * 32.0);
                    let h = clamp01(base_weave * 0.82 + fiber * 0.11 + undul * 0.07).powf(0.95);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "coarse-burlap" => {
            // 64 heavy twisted jute/burlap threads across 1024 high-DPI texels (16 texels/thread!)
            let mut rng = Sfc32::new(0x07A2_2B02);
            let threads = 64usize;
            let threads_f = threads as f32;
            let mut warp_thick = vec![0.0f32; threads];
            let mut weft_thick = vec![0.0f32; threads];
            for i in 0..threads {
                warp_thick[i] = 0.72 + rng.next_f32() * 0.42;
                weft_thick[i] = 0.72 + rng.next_f32() * 0.42;
            }
            let g_rough = make_periodic_grid(256, &mut rng);
            let g_drift = make_periodic_grid(32, &mut rng);
            let pi = std::f32::consts::PI;
            let half_pi = std::f32::consts::FRAC_PI_2;

            for y in 0..size {
                let ny = y as f32 * inv_size;
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let drift_x = (sample_periodic(&g_drift, 32, nx * 32.0, ny * 32.0) - 0.5) * 0.25;
                    let drift_y =
                        (sample_periodic(&g_drift, 32, nx * 32.0 + 13.0, ny * 32.0 + 5.0) - 0.5)
                            * 0.25;
                    let tx = (nx * threads_f + drift_x).rem_euclid(threads_f);
                    let ty = (ny * threads_f + drift_y).rem_euclid(threads_f);
                    let c_cell = (tx.floor() as usize) % threads;
                    let r_cell = (ty.floor() as usize) % threads;
                    let fx = (tx - tx.floor() - 0.5) * 2.0;
                    let fy = (ty - ty.floor() - 0.5) * 2.0;
                    let parity = ((r_cell + c_cell) & 1) == 0;
                    let twist_w = 0.84 + 0.16 * ((ny * threads_f * 4.0 + fx * 2.2) * pi).sin();
                    let twist_h = 0.84 + 0.16 * ((nx * threads_f * 4.0 - fy * 2.2) * pi).sin();
                    let warp_c =
                        ((fx.abs() / warp_thick[c_cell].max(0.55)).min(1.0) * half_pi).cos()
                            * twist_w;
                    let weft_c =
                        ((fy.abs() / weft_thick[r_cell].max(0.55)).min(1.0) * half_pi).cos()
                            * twist_h;
                    let weave = if parity {
                        warp_c.max(weft_c * 0.40)
                    } else {
                        weft_c.max(warp_c * 0.40)
                    };
                    let rough = sample_periodic(&g_rough, 256, nx * 256.0, ny * 256.0);
                    let h = clamp01(weave * 0.82 + rough * 0.18 - 0.04).powf(1.04);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "fine-cotton" | "fine-paper" => {
            let mut rng = Sfc32::new(0x07A3_3C03);
            let threads = 160.0f32;
            let tau = std::f32::consts::TAU;
            let g_gesso = make_periodic_grid(128, &mut rng);
            let g_micro = make_periodic_grid(256, &mut rng);
            for y in 0..size {
                let ny = y as f32 * inv_size;
                let wy = (ny * tau * threads).sin();
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let wx = (nx * tau * threads).sin();
                    let checker = ((nx + ny) * tau * (threads * 0.5)).sin() * 0.16;
                    let weave = (wx * 0.5 + 0.5) * 0.36 + (wy * 0.5 + 0.5) * 0.36 + 0.14 + checker;
                    let gesso = sample_periodic(&g_gesso, 128, nx * 128.0, ny * 128.0);
                    let micro = sample_periodic(&g_micro, 256, nx * 256.0, ny * 256.0);
                    let h = clamp01(weave * 0.58 + gesso * 0.24 + micro * 0.14 + rng.next_f32() * 0.04);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "cold-press-watercolor" | "rough-cold-press" => {
            let mut rng = Sfc32::new(0x07A4_4D04);
            let coarse_domes = make_cellular_dome_field(256, 48, &mut rng, 1.15);
            let fine_domes = make_cellular_dome_field(256, 96, &mut rng, 1.15);
            let g_fiber = make_periodic_grid(256, &mut rng);
            let g_undul = make_periodic_grid(64, &mut rng);
            for y in 0..size {
                let ny = y as f32 * inv_size;
                let sy = ny * 256.0;
                let y0 = (sy.floor() as usize) & 255;
                let y1 = (y0 + 1) & 255;
                let ty = sy - sy.floor();
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let sx = nx * 256.0;
                    let x0 = (sx.floor() as usize) & 255;
                    let x1 = (x0 + 1) & 255;
                    let tx = sx - sx.floor();
                    let c_top = coarse_domes[y0 * 256 + x0] * (1.0 - tx)
                        + coarse_domes[y0 * 256 + x1] * tx;
                    let c_bot = coarse_domes[y1 * 256 + x0] * (1.0 - tx)
                        + coarse_domes[y1 * 256 + x1] * tx;
                    let d1 = c_top * (1.0 - ty) + c_bot * ty;
                    let f_top =
                        fine_domes[y0 * 256 + x0] * (1.0 - tx) + fine_domes[y0 * 256 + x1] * tx;
                    let f_bot =
                        fine_domes[y1 * 256 + x0] * (1.0 - tx) + fine_domes[y1 * 256 + x1] * tx;
                    let d2 = f_top * (1.0 - ty) + f_bot * ty;
                    let fiber = sample_periodic(&g_fiber, 256, nx * 256.0, ny * 256.0);
                    let undul = sample_periodic(&g_undul, 64, nx * 64.0, ny * 64.0);
                    let raw = d1 * 0.48 + d2 * 0.26 + fiber * 0.14 + undul * 0.08 + rng.next_f32() * 0.04;
                    let h = clamp01((raw - 0.12) / 0.74).powf(1.06);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "rough-pastel" | "charcoal-paper" | "painterly-chalk-grain" => {
            let mut rng = Sfc32::new(0x07A5_5E05);
            let domes = make_cellular_dome_field(256, 60, &mut rng, 1.35);
            let g_grit = make_periodic_grid(256, &mut rng);
            let tau = std::f32::consts::TAU;
            for y in 0..size {
                let ny = y as f32 * inv_size;
                let rib = (ny * tau * 96.0).sin() * 0.15;
                let sy = ny * 256.0;
                let y0 = (sy.floor() as usize) & 255;
                let y1 = (y0 + 1) & 255;
                let ty = sy - sy.floor();
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let chain = (nx * tau * 16.0).sin() * 0.06;
                    let sx = nx * 256.0;
                    let x0 = (sx.floor() as usize) & 255;
                    let x1 = (x0 + 1) & 255;
                    let tx = sx - sx.floor();
                    let top = domes[y0 * 256 + x0] * (1.0 - tx) + domes[y0 * 256 + x1] * tx;
                    let bot = domes[y1 * 256 + x0] * (1.0 - tx) + domes[y1 * 256 + x1] * tx;
                    let d = top * (1.0 - ty) + bot * ty;
                    let grit = sample_periodic(&g_grit, 256, nx * 256.0, ny * 256.0);
                    let raw = d * 0.48 + grit * 0.30 + rng.next_f32() * 0.14 + rib + chain;
                    let h = clamp01((raw - 0.14) / 0.72).powf(1.15);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "gesso-board" | "sketch-paper" => {
            let mut rng = Sfc32::new(0x07A6_6F06);
            let g_brush_h = make_periodic_grid(128, &mut rng);
            let g_brush_v = make_periodic_grid(128, &mut rng);
            let g_micro = make_periodic_grid(256, &mut rng);
            for y in 0..size {
                let ny = y as f32 * inv_size;
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let h_streak = sample_periodic(&g_brush_h, 128, nx * 28.0, ny * 220.0);
                    let v_streak = sample_periodic(&g_brush_v, 128, nx * 220.0, ny * 28.0);
                    let micro = sample_periodic(&g_micro, 256, nx * 256.0, ny * 256.0);
                    let raw = h_streak * 0.38 + v_streak * 0.30 + micro * 0.24 + rng.next_f32() * 0.08;
                    let h = clamp01((raw - 0.12) / 0.76);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "stipple-grain" | "noise-grain" => {
            let mut rng = Sfc32::new(0x07A7_7007);
            let g_med = make_periodic_grid(128, &mut rng);
            let g_fine = make_periodic_grid(256, &mut rng);
            for y in 0..size {
                let ny = y as f32 * inv_size;
                let row = y * size * 4;
                for x in 0..size {
                    let nx = x as f32 * inv_size;
                    let m = sample_periodic(&g_med, 128, nx * 128.0, ny * 128.0);
                    let f = sample_periodic(&g_fine, 256, nx * 256.0, ny * 256.0);
                    let raw = m * 0.28 + f * 0.36 + rng.next_f32() * 0.36;
                    let h = clamp01((raw - 0.14) / 0.72);
                    write_gray_height_pixel(&mut rgba, row + x * 4, h);
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        _ => None,
    }
}

// =============================================================================
// 2. BUILT-IN PROCEDURAL BRUSH TIPS (256×256 RGBA8)
// =============================================================================

/// Generates a built-in procedural brush tip natively in Rust when referenced by
/// canonical key (e.g., `"tip:real-g-pen"`, `"tip:pencil-hb-2b"`, `"pro:sumi-fude-brush"`,
/// `"tip:mineral-grain-dual"`, etc.). Returns `(width, height, rgba8)`.
pub fn generate_builtin_tip(key: &str) -> Option<(u32, u32, Vec<u8>)> {
    let size = 256usize;
    let mut rgba = vec![0u8; size * size * 4];
    let half = size as f32 * 0.5;

    match key {
        "tip:real-g-pen" | "tip:real-g-pen-csp-v2" => {
            let mut rng = Sfc32::new(0x9E41_69E0);
            let n_med = make_periodic_grid(32, &mut rng);
            let n_fine = make_periodic_grid(64, &mut rng);
            let r_base = size as f32 * 0.44;
            for y in 0..size {
                let dy = y as f32 - half + 0.5;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = x as f32 - half + 0.5;
                    let r = dx.hypot(dy) / r_base;
                    if r >= 1.08 {
                        continue;
                    }
                    let wob = (sample_periodic(&n_med, 32, x as f32 * 0.16, y as f32 * 0.16) - 0.5) * 0.08
                        + (sample_periodic(&n_fine, 64, x as f32 * 0.38, y as f32 * 0.38) - 0.5) * 0.05;
                    let a = 1.0 - smoothstep(0.84 + wob, 1.0 + wob, r);
                    if a > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "tip:calligraphy" => {
            let rx = size as f32 * 0.46;
            let ry = size as f32 * 0.13;
            for y in 0..size {
                let dy = (y as f32 - half + 0.5) / ry;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = (x as f32 - half + 0.5) / rx;
                    let d = (dx.abs().powf(3.0) + dy.abs().powf(3.0)).powf(1.0 / 3.0);
                    if d < 1.02 {
                        let a = 1.0 - smoothstep(0.88, 1.0, d);
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "tip:mechanical-pencil" | "tip:pencil-hb-2b" | "tip:pencil-6b-8b" => {
            let seed = match key {
                "tip:mechanical-pencil" => 0x51A0_0001,
                "tip:pencil-hb-2b" => 0x51A0_0002,
                _ => 0x51A0_0003,
            };
            let softness = if key == "tip:pencil-6b-8b" { 0.42 } else { 0.72 };
            let mut rng = Sfc32::new(seed);
            let domes = make_cellular_dome_field(size, 28, &mut rng, 1.25);
            let n_fine = make_periodic_grid(64, &mut rng);
            let rx = size as f32 * 0.44;
            let ry = size as f32 * if key == "tip:pencil-6b-8b" { 0.34 } else { 0.42 };
            for y in 0..size {
                let dy = (y as f32 - half + 0.5) / ry;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = (x as f32 - half + 0.5) / rx;
                    let r = dx.hypot(dy);
                    if r >= 1.0 {
                        continue;
                    }
                    let env = 1.0 - smoothstep(softness, 1.0, r);
                    let grain = domes[y * size + x] * 0.65
                        + sample_periodic(&n_fine, 64, x as f32 * 0.4, y as f32 * 0.4) * 0.35;
                    let a = clamp01(env * (0.42 + 0.78 * grain));
                    if a > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "tip:charcoal-pencil" | "tip:standard-chalk" | "pro:vine-charcoal-256" => {
            let mut rng = Sfc32::new(0xC4A0_C0A1);
            let domes = make_cellular_dome_field(size, 20, &mut rng, 1.3);
            let n_med = make_periodic_grid(32, &mut rng);
            let n_fine = make_periodic_grid(64, &mut rng);
            let rx = size as f32 * 0.45;
            let ry = size as f32 * 0.38;
            for y in 0..size {
                let dy = (y as f32 - half + 0.5) / ry;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = (x as f32 - half + 0.5) / rx;
                    let r = dx.hypot(dy);
                    if r >= 1.05 {
                        continue;
                    }
                    let warp = (sample_periodic(&n_med, 32, x as f32 * 0.14, y as f32 * 0.14) - 0.5) * 0.24;
                    let env = 1.0 - smoothstep(0.35 + warp, 0.98 + warp, r);
                    let tooth = domes[y * size + x] * 0.68
                        + sample_periodic(&n_fine, 64, x as f32 * 0.32, y as f32 * 0.32) * 0.32;
                    let a = clamp01(env * smoothstep(0.16, 0.72, tooth + (1.0 - r) * 0.32));
                    if a > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "tip:mineral-grain-dual" | "tip:mineral-grain-dual-256" | "pro:smoke-dual-mask-256" => {
            let mut rng = Sfc32::new(0xD0A1_64A1);
            let domes = make_cellular_dome_field(size, 18, &mut rng, 1.15);
            let n_fine = make_periodic_grid(64, &mut rng);
            let r_max = size as f32 * 0.48;
            for y in 0..size {
                let dy = (y as f32 - half + 0.5) / r_max;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = (x as f32 - half + 0.5) / r_max;
                    let r = dx.hypot(dy);
                    if r >= 1.0 {
                        continue;
                    }
                    let env = 1.0 - smoothstep(0.28, 1.0, r);
                    let d = domes[y * size + x];
                    let f = sample_periodic(&n_fine, 64, x as f32 * 0.25, y as f32 * 0.25);
                    let a = clamp01(env * (0.28 + 0.72 * (d * 0.65 + f * 0.35)));
                    if a > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "pro:sumi-fude-brush" | "pro:sumi-fude-brush-256" => {
            let mut rng = Sfc32::new(0x0002_1B02);
            let n_med = make_periodic_grid(32, &mut rng);
            let n_fine = make_periodic_grid(64, &mut rng);
            let rx = size as f32 * 0.46;
            let ry = size as f32 * 0.39;
            let num_strands = 18usize;
            let mut s_y = [0.0f32; 18];
            let mut s_w = [0.0f32; 18];
            let mut s_reach = [0.0f32; 18];
            for i in 0..num_strands {
                let u = ((i as f32 + 0.5) / num_strands as f32) * 2.0 - 1.0;
                s_y[i] = u * 0.92 + (rng.next_f32() - 0.5) * 0.06;
                s_w[i] = 0.065 + rng.next_f32() * 0.06;
                s_reach[i] = 0.78 + rng.next_f32() * 0.24;
            }
            for y in 0..size {
                let ny_base = (y as f32 - half + 0.5) / ry;
                let row = y * size * 4;
                for x in 0..size {
                    let nx = (x as f32 - half + 0.5) / rx;
                    if !(-1.04..=1.02).contains(&nx) {
                        continue;
                    }
                    let profile_half_h = if nx >= 0.0 {
                        (1.0 - nx).max(0.0).powf(0.72) * (0.96 + 0.14 * (1.0 - nx))
                    } else {
                        (1.0 - ((nx + 0.15) / 0.92).powi(2)).max(0.0).sqrt() * 1.04
                    };
                    if profile_half_h <= 0.01 {
                        continue;
                    }
                    let abs_ny = (ny_base / profile_half_h).abs();
                    if abs_ny >= 1.08 {
                        continue;
                    }
                    let warp = (sample_periodic(&n_med, 32, x as f32 * 0.12, y as f32 * 0.22) - 0.5) * 0.09;
                    let env = 1.0 - smoothstep(0.84 + warp, 1.01 + warp, abs_ny);
                    let mut alpha = env;
                    if nx < -0.18 {
                        let heel = smoothstep(-0.18, -0.88, nx);
                        let mut strand = 0.0f32;
                        let mut reach = 0.85f32;
                        for i in 0..num_strands {
                            let d = (ny_base - s_y[i]).abs() / s_w[i];
                            if d < 1.0 {
                                let v = 1.0 - d * d;
                                if v > strand {
                                    strand = v;
                                    reach = s_reach[i];
                                }
                            }
                        }
                        let heel_cut = 1.0 - smoothstep(reach * 0.75, reach * 1.02, -nx);
                        let fine = sample_periodic(&n_fine, 64, x as f32 * 0.22, y as f32 * 0.54);
                        let split = smoothstep(0.14 + heel * 0.26, 0.56, strand * 0.75 + fine * 0.25);
                        alpha = env * (1.0 - heel * 0.85 + heel * 0.85 * split * heel_cut);
                    }
                    if alpha > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, alpha);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "pro:soft-tapered-liner" | "pro:soft-tapered-liner-256" => {
            let mut rng = Sfc32::new(0x0002_1A01);
            let n_fine = make_periodic_grid(64, &mut rng);
            let rx = size as f32 * 0.45;
            let ry = size as f32 * 0.42;
            for y in 0..size {
                let dy = y as f32 - half + 0.5;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = x as f32 - half + 0.5;
                    let nx = dx / rx;
                    let taper = 1.0 - nx.max(0.0) * 0.14;
                    let ny = dy / (ry * taper);
                    let r = nx.hypot(ny);
                    if r >= 1.0 {
                        continue;
                    }
                    let mut a = 1.0 - smoothstep(0.74, 0.995, r);
                    if r > 0.68 {
                        let rim = smoothstep(0.68, 0.98, r);
                        let tooth = sample_periodic(&n_fine, 64, x as f32 * 0.36, y as f32 * 0.36);
                        a *= 1.0 - rim * 0.14 * (1.0 - tooth);
                    }
                    if a > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "pro:watercolor-wash" | "pro:watercolor-wash-256" | "tip:atmospheric-grain-256" => {
            let mut rng = Sfc32::new(0x7A7E_2C01);
            let n_med = make_periodic_grid(24, &mut rng);
            let n_fine = make_periodic_grid(64, &mut rng);
            let r_max = size as f32 * 0.46;
            for y in 0..size {
                let dy = (y as f32 - half + 0.5) / r_max;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = (x as f32 - half + 0.5) / r_max;
                    let r = dx.hypot(dy);
                    if r >= 1.04 {
                        continue;
                    }
                    let cloud = sample_periodic(&n_med, 24, x as f32 * 0.12, y as f32 * 0.12);
                    let fine = sample_periodic(&n_fine, 64, x as f32 * 0.28, y as f32 * 0.28);
                    let warp = (cloud - 0.5) * 0.26;
                    let a = (1.0 - smoothstep(0.25 + warp, 0.98 + warp, r)) * (0.62 + 0.38 * fine);
                    if a > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        "pro:luminous-bloom" | "pro:luminous-bloom-256" => {
            let r_max = size as f32 * 0.48;
            for y in 0..size {
                let dy = (y as f32 - half + 0.5) / r_max;
                let row = y * size * 4;
                for x in 0..size {
                    let dx = (x as f32 - half + 0.5) / r_max;
                    let r = dx.hypot(dy);
                    if r >= 1.0 {
                        continue;
                    }
                    let a = (1.0 - r).powf(2.4);
                    if a > 0.004 {
                        write_alpha_pixel(&mut rgba, row + x * 4, a);
                    }
                }
            }
            Some((size as u32, size as u32, rgba))
        }
        _ => None,
    }
}