From 6590b249d1adcbc214aad845b471c76d28877ce7 Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Fri, 6 Mar 2026 09:05:14 -0500 Subject: [PATCH] Add dodge/burn tool --- .../lightningbeam-core/src/brush_engine.rs | 15 ++++++++- .../lightningbeam-core/src/raster_layer.rs | 5 +++ .../lightningbeam-editor/src/main.rs | 16 +++++++++ .../src/panes/infopanel.rs | 33 +++++++++++++++++++ .../lightningbeam-editor/src/panes/mod.rs | 7 ++++ .../src/panes/shaders/brush_dab.wgsl | 20 +++++++++++ .../lightningbeam-editor/src/panes/stage.rs | 17 +++++++++- 7 files changed, 111 insertions(+), 2 deletions(-) diff --git a/lightningbeam-ui/lightningbeam-core/src/brush_engine.rs b/lightningbeam-ui/lightningbeam-core/src/brush_engine.rs index 5f7b585..6d647c2 100644 --- a/lightningbeam-ui/lightningbeam-core/src/brush_engine.rs +++ b/lightningbeam-ui/lightningbeam-core/src/brush_engine.rs @@ -305,6 +305,7 @@ impl BrushEngine { RasterBlendMode::CloneStamp => 3u32, RasterBlendMode::Healing => 4u32, RasterBlendMode::PatternStamp => 5u32, + RasterBlendMode::DodgeBurn => 6u32, }; let push_dab = |dabs: &mut Vec, @@ -327,7 +328,7 @@ impl BrushEngine { color_b: cb, // Clone stamp: color_r/color_g hold source canvas X/Y, so color_a = 1.0 // (blend strength is opa_weight × opacity × 1.0 in the shader). - color_a: if blend_mode_u == 3 || blend_mode_u == 4 { 1.0 } else { stroke.color[3] }, + color_a: if blend_mode_u == 3 || blend_mode_u == 4 || blend_mode_u == 6 { 1.0 } else { stroke.color[3] }, ndx, ndy, smudge_dist, blend_mode: blend_mode_u, elliptical_dab_ratio: bs.elliptical_dab_ratio.max(1.0), @@ -368,6 +369,9 @@ impl BrushEngine { } else if matches!(base_blend, RasterBlendMode::PatternStamp) { // ndx = pattern_type, ndy = pattern_scale (cr, cg, cb, stroke.pattern_type as f32, stroke.pattern_scale) + } else if matches!(base_blend, RasterBlendMode::DodgeBurn) { + // color_r = mode (0=dodge, 1=burn); strength comes from opacity + (stroke.dodge_burn_mode as f32, 0.0, 0.0, 0.0, 0.0) } else { (cr, cg, cb, 0.0, 0.0) }; @@ -495,6 +499,12 @@ impl BrushEngine { push_dab(&mut dabs, &mut bbox, ex, ey, radius2, opacity2, cr, cg, cb, stroke.pattern_type as f32, stroke.pattern_scale, 0.0); + } else if matches!(base_blend, RasterBlendMode::DodgeBurn) { + // color_r = mode (0=dodge, 1=burn) + push_dab(&mut dabs, &mut bbox, + ex, ey, radius2, opacity2, + stroke.dodge_burn_mode as f32, 0.0, 0.0, + 0.0, 0.0, 0.0); } else { push_dab(&mut dabs, &mut bbox, ex, ey, radius2, opacity2, cr, cg, cb, @@ -534,6 +544,9 @@ impl BrushEngine { } else if matches!(base_blend, RasterBlendMode::PatternStamp) { // ndx = pattern_type, ndy = pattern_scale (cr, cg, cb, stroke.pattern_type as f32, stroke.pattern_scale) + } else if matches!(base_blend, RasterBlendMode::DodgeBurn) { + // color_r = mode (0=dodge, 1=burn) + (stroke.dodge_burn_mode as f32, 0.0, 0.0, 0.0, 0.0) } else { (cr, cg, cb, 0.0, 0.0) }; diff --git a/lightningbeam-ui/lightningbeam-core/src/raster_layer.rs b/lightningbeam-ui/lightningbeam-core/src/raster_layer.rs index d144a40..5fa7006 100644 --- a/lightningbeam-ui/lightningbeam-core/src/raster_layer.rs +++ b/lightningbeam-ui/lightningbeam-core/src/raster_layer.rs @@ -24,6 +24,8 @@ pub enum RasterBlendMode { Healing, /// Pattern stamp: paint with a repeating procedural tile pattern PatternStamp, + /// Dodge / Burn: lighten (dodge) or darken (burn) existing pixels + DodgeBurn, } impl Default for RasterBlendMode { @@ -65,6 +67,9 @@ pub struct StrokeRecord { /// Pattern stamp: tile size in pixels #[serde(default = "default_pattern_scale")] pub pattern_scale: f32, + /// Dodge/Burn mode: 0 = dodge (lighten), 1 = burn (darken) + #[serde(default)] + pub dodge_burn_mode: u32, pub points: Vec, } diff --git a/lightningbeam-ui/lightningbeam-editor/src/main.rs b/lightningbeam-ui/lightningbeam-editor/src/main.rs index ce363a9..8e948aa 100644 --- a/lightningbeam-ui/lightningbeam-editor/src/main.rs +++ b/lightningbeam-ui/lightningbeam-editor/src/main.rs @@ -806,6 +806,12 @@ struct EditorApp { /// Pattern stamp settings pattern_type: u32, pattern_scale: f32, + /// Dodge/Burn tool settings + dodge_burn_radius: f32, + dodge_burn_hardness: f32, + dodge_burn_spacing: f32, + dodge_burn_exposure: f32, + dodge_burn_mode: u32, /// GPU-rendered brush preview pixel buffers, shared with VelloCallback::prepare(). brush_preview_pixels: std::sync::Arc)>>>, // Audio engine integration @@ -1107,6 +1113,11 @@ impl EditorApp { smudge_strength: 1.0, pattern_type: 0, pattern_scale: 32.0, + dodge_burn_radius: 30.0, + dodge_burn_hardness: 0.5, + dodge_burn_spacing: 3.0, + dodge_burn_exposure: 0.5, + dodge_burn_mode: 0, brush_preview_pixels: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())), audio_stream, audio_controller, @@ -5577,6 +5588,11 @@ impl eframe::App for EditorApp { smudge_strength: &mut self.smudge_strength, pattern_type: &mut self.pattern_type, pattern_scale: &mut self.pattern_scale, + dodge_burn_radius: &mut self.dodge_burn_radius, + dodge_burn_hardness: &mut self.dodge_burn_hardness, + dodge_burn_spacing: &mut self.dodge_burn_spacing, + dodge_burn_exposure: &mut self.dodge_burn_exposure, + dodge_burn_mode: &mut self.dodge_burn_mode, audio_controller: self.audio_controller.as_ref(), video_manager: &self.video_manager, playback_time: &mut self.playback_time, diff --git a/lightningbeam-ui/lightningbeam-editor/src/panes/infopanel.rs b/lightningbeam-ui/lightningbeam-editor/src/panes/infopanel.rs index 1ab9283..c726e84 100644 --- a/lightningbeam-ui/lightningbeam-editor/src/panes/infopanel.rs +++ b/lightningbeam-ui/lightningbeam-editor/src/panes/infopanel.rs @@ -173,6 +173,7 @@ impl InfopanelPane { tool, Tool::Draw | Tool::Pencil | Tool::Pen | Tool::Airbrush | Tool::Erase | Tool::Smudge | Tool::CloneStamp | Tool::HealingBrush | Tool::PatternStamp + | Tool::DodgeBurn ); // Only show tool options for tools that have options @@ -197,6 +198,7 @@ impl InfopanelPane { Tool::CloneStamp => "Clone Stamp", Tool::HealingBrush => "Healing Brush", Tool::PatternStamp => "Pattern Stamp", + Tool::DodgeBurn => "Dodge / Burn", _ => "Brush", } } else { @@ -358,6 +360,37 @@ impl InfopanelPane { self.render_raster_tool_options(ui, shared, false); } + Tool::DodgeBurn if is_raster_paint_tool => { + ui.horizontal(|ui| { + if ui.selectable_label(*shared.dodge_burn_mode == 0, "Dodge").clicked() { + *shared.dodge_burn_mode = 0; + } + if ui.selectable_label(*shared.dodge_burn_mode == 1, "Burn").clicked() { + *shared.dodge_burn_mode = 1; + } + }); + ui.horizontal(|ui| { + ui.label("Size:"); + ui.add(egui::Slider::new(shared.dodge_burn_radius, 1.0_f32..=500.0).logarithmic(true).suffix(" px")); + }); + ui.horizontal(|ui| { + ui.label("Exposure:"); + ui.add(egui::Slider::new(shared.dodge_burn_exposure, 0.0_f32..=1.0) + .custom_formatter(|v, _| format!("{:.0}%", v * 100.0))); + }); + ui.horizontal(|ui| { + ui.label("Hardness:"); + ui.add(egui::Slider::new(shared.dodge_burn_hardness, 0.0_f32..=1.0) + .custom_formatter(|v, _| format!("{:.0}%", v * 100.0))); + }); + ui.horizontal(|ui| { + ui.label("Spacing:"); + ui.add(egui::Slider::new(shared.dodge_burn_spacing, 0.5_f32..=20.0) + .logarithmic(true) + .custom_formatter(|v, _| format!("{:.1}", v))); + }); + } + Tool::Smudge if is_raster_paint_tool => { ui.horizontal(|ui| { ui.label("Size:"); diff --git a/lightningbeam-ui/lightningbeam-editor/src/panes/mod.rs b/lightningbeam-ui/lightningbeam-editor/src/panes/mod.rs index 1f58e71..1951793 100644 --- a/lightningbeam-ui/lightningbeam-editor/src/panes/mod.rs +++ b/lightningbeam-ui/lightningbeam-editor/src/panes/mod.rs @@ -212,6 +212,13 @@ pub struct SharedPaneState<'a> { pub pattern_type: &'a mut u32, /// Pattern stamp: tile size in pixels pub pattern_scale: &'a mut f32, + /// Dodge/Burn tool settings + pub dodge_burn_radius: &'a mut f32, + pub dodge_burn_hardness: &'a mut f32, + pub dodge_burn_spacing: &'a mut f32, + pub dodge_burn_exposure: &'a mut f32, + /// 0 = dodge (lighten), 1 = burn (darken) + pub dodge_burn_mode: &'a mut u32, /// Audio engine controller for playback control (wrapped in Arc> for thread safety) pub audio_controller: Option<&'a std::sync::Arc>>, /// Video manager for video decoding and frame caching diff --git a/lightningbeam-ui/lightningbeam-editor/src/panes/shaders/brush_dab.wgsl b/lightningbeam-ui/lightningbeam-editor/src/panes/shaders/brush_dab.wgsl index 9beb0bc..8b6e6c6 100644 --- a/lightningbeam-ui/lightningbeam-editor/src/panes/shaders/brush_dab.wgsl +++ b/lightningbeam-ui/lightningbeam-editor/src/panes/shaders/brush_dab.wgsl @@ -240,6 +240,26 @@ fn apply_dab(current: vec4, dab: GpuDab, px: i32, py: i32) -> vec4 { alpha * corrected.b + ba * current.b, alpha * corrected.a + ba * current.a, ); + } else if dab.blend_mode == 6u { + // Dodge / Burn: power-curve exposure adjustment. + // color_r: 0.0 = dodge, 1.0 = burn + // Uses pow(channel, gamma) which is asymmetric across channels: + // burn (gamma > 1): low channels compressed toward 0 faster than high ones → saturation increases + // dodge (gamma < 1): low channels lifted faster than high ones → saturation decreases + // This matches the behaviour of GIMP / Photoshop dodge-burn tools. + let s = opa_weight * dab.opacity; + if s <= 0.0 { return current; } + + let rgb = max(current.rgb, vec3(0.0)); + var adjusted: vec3; + if dab.color_r < 0.5 { + // Dodge: gamma < 1 → brightens + adjusted = pow(rgb, vec3(max(1.0 - s, 0.001))); + } else { + // Burn: gamma > 1 → darkens and increases saturation + adjusted = pow(rgb, vec3(1.0 + s)); + } + return vec4(clamp(adjusted, vec3(0.0), vec3(1.0)), current.a); } else { return current; } diff --git a/lightningbeam-ui/lightningbeam-editor/src/panes/stage.rs b/lightningbeam-ui/lightningbeam-editor/src/panes/stage.rs index 7b0afa6..50128c3 100644 --- a/lightningbeam-ui/lightningbeam-editor/src/panes/stage.rs +++ b/lightningbeam-ui/lightningbeam-editor/src/panes/stage.rs @@ -617,6 +617,7 @@ impl egui_wgpu::CallbackTrait for VelloCallback { clone_src_offset: None, pattern_type: 0, pattern_scale: 32.0, + dodge_burn_mode: 0, points: vec![ StrokePoint { x: x0, y: y_lo, pressure: 1.0, tilt_x: 0.0, tilt_y: 0.0, timestamp: 0.0 }, StrokePoint { x: mid_x, y: mid_y, pressure: 1.0, tilt_x: 0.0, tilt_y: 0.0, timestamp: 0.0 }, @@ -4885,6 +4886,13 @@ impl StagePane { *shared.smudge_hardness, *shared.smudge_spacing, ), + RasterBlendMode::DodgeBurn => ( + lightningbeam_core::brush_settings::BrushSettings::default(), + *shared.dodge_burn_radius, + *shared.dodge_burn_exposure, + *shared.dodge_burn_hardness, + *shared.dodge_burn_spacing, + ), _ => ( shared.active_brush_settings.clone(), *shared.brush_radius, @@ -4978,6 +4986,7 @@ impl StagePane { clone_src_offset: self.clone_stroke_offset, pattern_type: *shared.pattern_type, pattern_scale: *shared.pattern_scale, + dodge_burn_mode: *shared.dodge_burn_mode, points: vec![first_pt.clone()], }; let (dabs, dab_bbox) = BrushEngine::compute_dabs(&single, &mut stroke_state, 0.0); @@ -5069,6 +5078,7 @@ impl StagePane { clone_src_offset: self.clone_stroke_offset, pattern_type: *shared.pattern_type, pattern_scale: *shared.pattern_scale, + dodge_burn_mode: *shared.dodge_burn_mode, points: vec![first_pt.clone()], }; let (dabs, dab_bbox) = BrushEngine::compute_dabs(&single, &mut stroke_state, 0.0); @@ -5156,6 +5166,7 @@ impl StagePane { clone_src_offset, pattern_type: *shared.pattern_type, pattern_scale: *shared.pattern_scale, + dodge_burn_mode: *shared.dodge_burn_mode, points: vec![prev_pt, curr_local], }; let current_time = ui.input(|i| i.time); @@ -5220,6 +5231,7 @@ impl StagePane { clone_src_offset: self.clone_stroke_offset, pattern_type: *shared.pattern_type, pattern_scale: *shared.pattern_scale, + dodge_burn_mode: *shared.dodge_burn_mode, points: vec![pt], }; let (dabs, dab_bbox) = BrushEngine::compute_dabs(&single, stroke_state, dt); @@ -7586,6 +7598,9 @@ impl StagePane { Tool::PatternStamp => { self.handle_raster_stroke_tool(ui, &response, world_pos, lightningbeam_core::raster_layer::RasterBlendMode::PatternStamp, shared); } + Tool::DodgeBurn => { + self.handle_raster_stroke_tool(ui, &response, world_pos, lightningbeam_core::raster_layer::RasterBlendMode::DodgeBurn, shared); + } Tool::SelectLasso => { self.handle_raster_lasso_tool(ui, &response, world_pos, shared); } @@ -8066,8 +8081,8 @@ impl StagePane { Tool::Erase => (*shared.eraser_radius, *shared.eraser_radius, 0.0_f32), Tool::Smudge | Tool::BlurSharpen - | Tool::DodgeBurn | Tool::Sponge => (*shared.smudge_radius, *shared.smudge_radius, 0.0_f32), + Tool::DodgeBurn => (*shared.dodge_burn_radius, *shared.dodge_burn_radius, 0.0_f32), _ => { let bs = &shared.active_brush_settings; let r = *shared.brush_radius;