A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
//! WGSL shaders for the Phase 1 wgpu (DX12) engine:
//! 1. Viewport compositor (canvas matte, artboard 1px border + drop shadow,
//! 16px document-space checkerboard, document texture composite).
//! 2. Instanced GPU brush dab rasterizer (soft/hard anti-aliased circular dabs).
//! 3. Analytic SDF dual-tone GPU brush outline cursor ring.
pub const VIEWPORT_SHADER_WGSL: &str = r#"
struct ViewportUniforms {
viewport_size_phys: vec2<f32>,
doc_size: vec2<f32>,
pan_css: vec2<f32>,
zoom: f32,
rotation_rad: f32,
flip: vec2<f32>,
dpr: f32,
show_checkerboard: u32,
matte_color: vec4<f32>,
};
@group(0) @binding(0) var<uniform> u: ViewportUniforms;
@group(0) @binding(1) var doc_tex: texture_2d<f32>;
@group(0) @binding(2) var doc_sampler_linear: sampler;
@group(0) @binding(3) var doc_sampler_nearest: sampler;
struct VsOut {
@builtin(position) pos: vec4<f32>,
};
@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {
// Fullscreen triangle covering NDC [-1, 1] x [-1, 1]
let x = f32((vid << 1u) & 2u) * 2.0 - 1.0;
let y = 1.0 - f32(vid & 2u) * 2.0;
var out: VsOut;
out.pos = vec4<f32>(x, y, 0.0, 1.0);
return out;
}
fn phys_to_doc(phys: vec2<f32>) -> vec2<f32> {
let safe_dpr = max(0.05, u.dpr);
let safe_zoom = max(0.01, u.zoom);
let css = phys / safe_dpr;
let rel = css - u.pan_css;
let c = cos(u.rotation_rad);
let s = sin(u.rotation_rad);
let unrot = vec2<f32>(
rel.x * c + rel.y * s,
-rel.x * s + rel.y * c,
);
return u.doc_size * 0.5 + (unrot * u.flip) / safe_zoom;
}
@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
// in.pos.xy is in physical swapchain pixel centers (top-left origin)
let doc_coord = phys_to_doc(in.pos.xy);
let safe_zoom = max(0.01, u.zoom);
// Distance outside [0, doc_size] in document pixels
let outside_doc = max(vec2<f32>(0.0), max(-doc_coord, doc_coord - u.doc_size));
if (outside_doc.x > 0.0 || outside_doc.y > 0.0) {
let outside_css_px = outside_doc * safe_zoom;
let border_dist = max(outside_css_px.x, outside_css_px.y);
if (border_dist <= 1.0) {
// Crisp 1px dark artboard border matching coreShaders.ts
return vec4<f32>(0.086, 0.086, 0.086, 1.0);
}
// Soft drop shadow offset 10 CSS px downward in viewport space
let shadow_doc = phys_to_doc(in.pos.xy - vec2<f32>(0.0, 10.0 * max(0.05, u.dpr)));
let shadow_outside = max(vec2<f32>(0.0), max(-shadow_doc, shadow_doc - u.doc_size));
let shadow_dist = length(shadow_outside * safe_zoom);
let shadow_a = 0.55 * exp(-(shadow_dist * shadow_dist) / 520.0) * smoothstep(48.0, 0.0, shadow_dist);
let matte = u.matte_color.rgb;
let shaded = matte * (1.0 - shadow_a);
return vec4<f32>(shaded, 1.0);
}
// Artboard background: 16px document-space checkerboard or crisp white paper
var bg_rgb: vec3<f32>;
if (u.show_checkerboard != 0u) {
let check_coord = floor(doc_coord / 16.0);
let check = abs((check_coord.x + check_coord.y) - 2.0 * floor((check_coord.x + check_coord.y) * 0.5));
bg_rgb = select(vec3<f32>(0.125), vec3<f32>(0.157), check > 0.5);
} else {
bg_rgb = vec3<f32>(1.0, 1.0, 1.0);
}
let uv = clamp(doc_coord / max(vec2<f32>(1.0), u.doc_size), vec2<f32>(0.0), vec2<f32>(1.0));
let sample_linear = textureSample(doc_tex, doc_sampler_linear, uv);
let sample_nearest = textureSample(doc_tex, doc_sampler_nearest, uv);
// Use crisp nearest-neighbor pixel grid when zoomed in >= 400% (manga/pixel precision)
let layer_premul = select(sample_linear, sample_nearest, safe_zoom >= 4.0);
// Premultiplied source-over onto opaque artboard background
let out_rgb = clamp(layer_premul.rgb + bg_rgb * (1.0 - layer_premul.a), vec3<f32>(0.0), vec3<f32>(1.0));
return vec4<f32>(out_rgb, 1.0);
}
"#;
pub const DAB_SHADER_WGSL: &str = r#"
struct DabUniforms {
doc_size: vec2<f32>,
_pad: vec2<f32>,
};
@group(0) @binding(0) var<uniform> u: DabUniforms;
struct DabInstance {
@location(0) center_doc: vec2<f32>,
@location(1) radius_doc: f32,
@location(2) hardness: f32,
@location(3) color_rgba: vec4<f32>,
};
struct VsOut {
@builtin(position) pos: vec4<f32>,
@location(0) local_pos: vec2<f32>,
@location(1) radius_doc: f32,
@location(2) hardness: f32,
@location(3) color_rgba: vec4<f32>,
};
@vertex
fn vs_main(@builtin(vertex_index) vid: u32, inst: DabInstance) -> VsOut {
// 6-vertex quad covering [-1, 1] x [-1, 1] padded by 1.5px for anti-aliasing
var corner: vec2<f32>;
switch (vid) {
case 0u: { corner = vec2<f32>(-1.0, -1.0); }
case 1u: { corner = vec2<f32>( 1.0, -1.0); }
case 2u: { corner = vec2<f32>(-1.0, 1.0); }
case 3u: { corner = vec2<f32>(-1.0, 1.0); }
case 4u: { corner = vec2<f32>( 1.0, -1.0); }
default: { corner = vec2<f32>( 1.0, 1.0); }
}
let extent = inst.radius_doc + 1.5;
let local_px = corner * extent;
let doc_px = inst.center_doc + local_px;
let ndc = vec2<f32>(
(doc_px.x / max(1.0, u.doc_size.x)) * 2.0 - 1.0,
1.0 - (doc_px.y / max(1.0, u.doc_size.y)) * 2.0,
);
var out: VsOut;
out.pos = vec4<f32>(ndc, 0.0, 1.0);
out.local_pos = local_px;
out.radius_doc = inst.radius_doc;
out.hardness = inst.hardness;
out.color_rgba = inst.color_rgba;
return out;
}
@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
let dist = length(in.local_pos);
let r = max(0.35, in.radius_doc);
if (dist > r + 1.0) {
discard;
}
let h = clamp(in.hardness, 0.0, 1.0);
// Inner solid plateau + smooth radial feather + 1px anti-aliased edge
let inner_r = r * h * 0.96;
let aa_edge = max(0.75, r - inner_r);
let falloff = 1.0 - smoothstep(max(0.0, r - aa_edge), r + 0.5, dist);
let alpha = clamp(in.color_rgba.a * falloff, 0.0, 1.0);
if (alpha < 0.002) {
discard;
}
// Premultiplied RGBA output for clean alpha compositing & destination-out erasing
return vec4<f32>(in.color_rgba.rgb * alpha, alpha);
}
"#;
pub const CURSOR_SHADER_WGSL: &str = r#"
struct CursorUniforms {
viewport_size_phys: vec2<f32>,
center_phys: vec2<f32>,
half_extents_phys: vec2<f32>,
angle_rad: f32,
dpr: f32,
is_square: u32,
visible: u32,
_pad: vec2<u32>,
};
@group(0) @binding(0) var<uniform> u: CursorUniforms;
struct VsOut {
@builtin(position) pos: vec4<f32>,
@location(0) local_phys: vec2<f32>,
};
@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {
var corner: vec2<f32>;
switch (vid) {
case 0u: { corner = vec2<f32>(-1.0, -1.0); }
case 1u: { corner = vec2<f32>( 1.0, -1.0); }
case 2u: { corner = vec2<f32>(-1.0, 1.0); }
case 3u: { corner = vec2<f32>(-1.0, 1.0); }
case 4u: { corner = vec2<f32>( 1.0, -1.0); }
default: { corner = vec2<f32>( 1.0, 1.0); }
}
var out: VsOut;
if (u.visible == 0u) {
out.pos = vec4<f32>(-2.0, -2.0, 0.0, 1.0);
out.local_phys = vec2<f32>(0.0);
return out;
}
let pad = 6.0 * max(1.0, u.dpr);
let box_half = u.half_extents_phys + vec2<f32>(pad, pad);
let local_p = corner * box_half;
let c = cos(u.angle_rad);
let s = sin(u.angle_rad);
let rot_p = vec2<f32>(
local_p.x * c - local_p.y * s,
local_p.x * s + local_p.y * c,
);
let screen_p = u.center_phys + rot_p;
let ndc = vec2<f32>(
(screen_p.x / max(1.0, u.viewport_size_phys.x)) * 2.0 - 1.0,
1.0 - (screen_p.y / max(1.0, u.viewport_size_phys.y)) * 2.0,
);
out.pos = vec4<f32>(ndc, 0.0, 1.0);
out.local_phys = local_p;
return out;
}
@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
if (u.visible == 0u) {
discard;
}
let dpr = max(1.0, u.dpr);
let half_ext = max(vec2<f32>(1.5 * dpr), u.half_extents_phys);
var sd: f32;
if (u.is_square != 0u) {
let q = abs(in.local_phys) - half_ext;
sd = length(max(q, vec2<f32>(0.0))) + min(max(q.x, q.y), 0.0);
} else {
// Gradient-normalized ellipse SDF for uniform stroke width across eccentricities
let p = in.local_phys;
let inv = vec2<f32>(1.0) / half_ext;
let k0 = length(p * inv);
let k1 = length(p * inv * inv);
if (k1 < 1e-6) {
sd = -min(half_ext.x, half_ext.y);
} else {
sd = k0 * (k0 - 1.0) / k1;
}
}
let abs_d = abs(sd);
let aa = 0.85;
let white_half_w = 1.35 * dpr;
let black_half_w = 0.55 * dpr;
let white_cov = 1.0 - smoothstep(white_half_w - aa, white_half_w + aa, abs_d);
let black_cov = 1.0 - smoothstep(black_half_w - aa, black_half_w + aa, abs_d);
let total_alpha = clamp(white_cov * 0.88, 0.0, 1.0);
if (total_alpha < 0.005) {
discard;
}
// Crisp black inner line over white outer halo (high visibility on light & dark art)
let rgb = mix(vec3<f32>(1.0, 1.0, 1.0), vec3<f32>(0.05, 0.05, 0.06), black_cov);
return vec4<f32>(rgb * total_alpha, total_alpha);
}
"#;