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
//! Non-destructive Adjustment Layer WGSL shader (`curves`, `levels`, `gradient-map`, `color-balance`).
//!
//! - Mode 0 (`curves`, `levels`): per-channel 256×1 RGBA8 LUT lookup
//! - Mode 1 (`gradient-map`): BT.601 luminance 256×1 RGBA8 LUT lookup
//! - Mode 2 (`color-balance`): exact analytic shadows/midtones/highlights shifts + `preserveLuminosity`
//! - Honors all 28 `layer.meta.blend_mode`s on adjustment output.
use super::blend_modes::BLEND_MODES_WGSL;
pub fn build_adjustment_shader_wgsl() -> String {
format!(
r#"
struct AdjustmentUniforms {{
viewport_size_phys: vec2<f32>,
doc_size: vec2<f32>,
pan_css: vec2<f32>,
zoom: f32,
rotation_rad: f32,
flip: vec2<f32>,
dpr: f32,
opacity: f32,
// flags = [adj_mode (0: per-channel LUT, 1: gradient-map, 2: color-balance), mask_enabled, clip_enabled, preserve_luminosity]
flags: vec4<u32>,
// Color balance [-100..100] shifts packed as xyz
cb_shadows: vec4<f32>,
cb_midtones: vec4<f32>,
cb_highlights: vec4<f32>,
// blend_flags = [blend_mode, 0, 0, 0]
blend_flags: vec4<u32>,
}};
@group(0) @binding(0) var<uniform> u: AdjustmentUniforms;
@group(0) @binding(1) var backdrop_tex: texture_2d<f32>;
@group(0) @binding(2) var lut_tex: texture_2d<f32>;
@group(0) @binding(3) var doc_mask_tex: texture_2d<f32>;
@group(0) @binding(4) var clip_mask_tex: texture_2d<f32>;
@group(0) @binding(5) var linear_sampler: sampler;
struct VsOut {{
@builtin(position) pos: vec4<f32>,
@location(0) uv: vec2<f32>,
}};
@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {{
let x = f32((vid << 1u) & 2u);
let y = f32(vid & 2u);
var out: VsOut;
out.uv = vec2<f32>(x, y);
out.pos = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
return out;
}}
{BLEND_MODES_WGSL}
fn phys_to_doc(phys: vec2<f32>) -> vec2<f32> {{
let safe_dpr = max(0.05, u.dpr);
let safe_zoom = max(0.0001, u.zoom);
let css = phys / safe_dpr;
let rel = (css - u.pan_css) / safe_zoom;
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 unrot * u.flip + u.doc_size * 0.5;
}}
fn sample_lut_u(val: f32) -> f32 {{
return (clamp(val, 0.0, 1.0) * 255.0 + 0.5) / 256.0;
}}
fn evaluate_color_balance(rgb: vec3<f32>) -> vec3<f32> {{
let lum = dot(rgb, vec3<f32>(0.299, 0.587, 0.114));
let w_shad = pow(max(0.0, 1.0 - lum * 2.0), 2.0);
let w_high = pow(max(0.0, lum * 2.0 - 1.0), 2.0);
let w_mid = max(0.0, 1.0 - w_shad - w_high);
let delta = (u.cb_shadows.rgb * w_shad + u.cb_midtones.rgb * w_mid + u.cb_highlights.rgb * w_high)
* (1.28 / 255.0);
var out_rgb = clamp(rgb + delta, vec3<f32>(0.0), vec3<f32>(1.0));
if (u.flags.w != 0u) {{
let new_lum = dot(out_rgb, vec3<f32>(0.299, 0.587, 0.114));
if (new_lum > 0.0001) {{
out_rgb = clamp(out_rgb * (lum / new_lum), vec3<f32>(0.0), vec3<f32>(1.0));
}}
}}
return out_rgb;
}}
@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {{
let px_coord = clamp(
vec2<i32>(in.pos.xy),
vec2<i32>(0),
max(vec2<i32>(0), vec2<i32>(u.viewport_size_phys) - vec2<i32>(1)),
);
let base_color = textureLoad(backdrop_tex, px_coord, 0);
if (base_color.a < 0.0005) {{
return base_color;
}}
let doc_coord = phys_to_doc(in.pos.xy);
if (
doc_coord.x < 0.0 ||
doc_coord.y < 0.0 ||
doc_coord.x > u.doc_size.x ||
doc_coord.y > u.doc_size.y
) {{
return base_color;
}}
let unpremult = clamp(base_color.rgb / max(0.0005, base_color.a), vec3<f32>(0.0), vec3<f32>(1.0));
var adjusted: vec3<f32>;
if (u.flags.x == 1u) {{
// Gradient map: map BT.601 luminance to 256x1 RGBA LUT
let lum = dot(unpremult, vec3<f32>(0.299, 0.587, 0.114));
adjusted = textureSampleLevel(lut_tex, linear_sampler, vec2<f32>(sample_lut_u(lum), 0.5), 0.0).rgb;
}} else if (u.flags.x == 2u) {{
// Analytic Color Balance (shadows/midtones/highlights + optional preserveLuminosity)
adjusted = evaluate_color_balance(unpremult);
}} else {{
// Curves / Levels: per-channel LUT lookup
let r = textureSampleLevel(lut_tex, linear_sampler, vec2<f32>(sample_lut_u(unpremult.r), 0.5), 0.0).r;
let g = textureSampleLevel(lut_tex, linear_sampler, vec2<f32>(sample_lut_u(unpremult.g), 0.5), 0.0).g;
let b = textureSampleLevel(lut_tex, linear_sampler, vec2<f32>(sample_lut_u(unpremult.b), 0.5), 0.0).b;
adjusted = vec3<f32>(r, g, b);
}}
let blend_mode = u.blend_flags.x;
if (blend_mode != 0u && blend_mode != 12u) {{
adjusted = evaluate_blend_mode(unpremult, adjusted, blend_mode);
}}
var mask_factor = clamp(u.opacity, 0.0, 1.0);
if (u.flags.y != 0u) {{
let mask_uv = clamp(in.pos.xy / max(vec2<f32>(1.0), u.viewport_size_phys), vec2<f32>(0.0), vec2<f32>(1.0));
let mask_val = textureSampleLevel(doc_mask_tex, linear_sampler, mask_uv, 0.0).r;
mask_factor *= mask_val;
}}
if (u.flags.z != 0u) {{
let clip_a = textureLoad(clip_mask_tex, px_coord, 0).a;
mask_factor *= clamp(clip_a, 0.0, 1.0);
}}
let final_rgb = mix(unpremult, adjusted, mask_factor);
return vec4<f32>(final_rgb * base_color.a, base_color.a);
}}
"#
)
}