work on raster tools
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
//! Gradient stop editor widget.
|
||||
//!
|
||||
//! Call [`gradient_stop_editor`] inside any egui layout; it returns `true` when
|
||||
//! `gradient` was modified.
|
||||
|
||||
use eframe::egui::{self, Color32, DragValue, Painter, Rect, Sense, Stroke, Vec2};
|
||||
use lightningbeam_core::gradient::{GradientExtend, GradientStop, GradientType, ShapeGradient};
|
||||
use lightningbeam_core::shape::ShapeColor;
|
||||
|
||||
// ── Public entry point ───────────────────────────────────────────────────────
|
||||
|
||||
/// Render an inline gradient editor.
|
||||
///
|
||||
/// * `gradient` – the gradient being edited (mutated in place).
|
||||
/// * `selected_stop` – index of the currently selected stop (persisted by caller).
|
||||
///
|
||||
/// Returns `true` if anything changed.
|
||||
pub fn gradient_stop_editor(
|
||||
ui: &mut egui::Ui,
|
||||
gradient: &mut ShapeGradient,
|
||||
selected_stop: &mut Option<usize>,
|
||||
) -> bool {
|
||||
let mut changed = false;
|
||||
|
||||
// ── Row 1: Kind + angle ───────────────────────────────────────────────
|
||||
ui.horizontal(|ui| {
|
||||
let was_linear = gradient.kind == GradientType::Linear;
|
||||
if ui.selectable_label(was_linear, "Linear").clicked() && !was_linear {
|
||||
gradient.kind = GradientType::Linear;
|
||||
changed = true;
|
||||
}
|
||||
if ui.selectable_label(!was_linear, "Radial").clicked() && was_linear {
|
||||
gradient.kind = GradientType::Radial;
|
||||
changed = true;
|
||||
}
|
||||
if gradient.kind == GradientType::Linear {
|
||||
ui.add_space(8.0);
|
||||
ui.label("Angle:");
|
||||
if ui.add(
|
||||
DragValue::new(&mut gradient.angle)
|
||||
.speed(1.0)
|
||||
.range(-360.0..=360.0)
|
||||
.suffix("°"),
|
||||
).changed() {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// ── Gradient bar + handles ────────────────────────────────────────────
|
||||
let bar_height = 22.0_f32;
|
||||
let handle_h = 14.0_f32;
|
||||
let total_height = bar_height + handle_h + 4.0;
|
||||
let avail_w = ui.available_width();
|
||||
|
||||
let (bar_rect, bar_resp) = ui.allocate_exact_size(
|
||||
Vec2::new(avail_w, total_height),
|
||||
Sense::click(),
|
||||
);
|
||||
let painter = ui.painter_at(bar_rect);
|
||||
|
||||
let bar = Rect::from_min_size(bar_rect.min, Vec2::new(avail_w, bar_height));
|
||||
let track = Rect::from_min_size(
|
||||
egui::pos2(bar_rect.min.x, bar_rect.min.y + bar_height + 2.0),
|
||||
Vec2::new(avail_w, handle_h),
|
||||
);
|
||||
|
||||
// Draw checkerboard background (transparent indicator).
|
||||
draw_checker(&painter, bar);
|
||||
|
||||
// Draw gradient bar as N segments.
|
||||
let seg = 128_usize;
|
||||
for i in 0..seg {
|
||||
let t0 = i as f32 / seg as f32;
|
||||
let t1 = (i + 1) as f32 / seg as f32;
|
||||
let t = (t0 + t1) * 0.5;
|
||||
let [r, g, b, a] = gradient.eval(t);
|
||||
let col = Color32::from_rgba_unmultiplied(r, g, b, a);
|
||||
let x0 = bar.min.x + t0 * bar.width();
|
||||
let x1 = bar.min.x + t1 * bar.width();
|
||||
let seg_rect = Rect::from_min_max(
|
||||
egui::pos2(x0, bar.min.y),
|
||||
egui::pos2(x1, bar.max.y),
|
||||
);
|
||||
painter.rect_filled(seg_rect, 0.0, col);
|
||||
}
|
||||
// Outline.
|
||||
painter.rect_stroke(bar, 2.0, Stroke::new(1.0, Color32::from_gray(60)), eframe::egui::StrokeKind::Middle);
|
||||
|
||||
// Click on bar → add stop.
|
||||
if bar_resp.clicked() {
|
||||
if let Some(pos) = bar_resp.interact_pointer_pos() {
|
||||
if bar.contains(pos) {
|
||||
let t = ((pos.x - bar.min.x) / bar.width()).clamp(0.0, 1.0);
|
||||
let [r, g, b, a] = gradient.eval(t);
|
||||
gradient.stops.push(GradientStop {
|
||||
position: t,
|
||||
color: ShapeColor::rgba(r, g, b, a),
|
||||
});
|
||||
gradient.stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
|
||||
*selected_stop = gradient.stops.iter().position(|s| s.position == t);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw stop handles.
|
||||
// We need to detect drags per-handle, so allocate individual rects with the
|
||||
// regular egui input model. To avoid borrow conflicts we collect interactions
|
||||
// before mutating.
|
||||
let handle_w = 10.0_f32;
|
||||
let n_stops = gradient.stops.len();
|
||||
|
||||
let mut drag_idx: Option<usize> = None;
|
||||
let mut drag_delta: f32 = 0.0;
|
||||
let mut click_idx: Option<usize> = None;
|
||||
|
||||
// To render handles after collecting, remember their rects.
|
||||
let handle_rects: Vec<Rect> = (0..n_stops).map(|i| {
|
||||
let cx = track.min.x + gradient.stops[i].position * track.width();
|
||||
Rect::from_center_size(
|
||||
egui::pos2(cx, track.center().y),
|
||||
Vec2::new(handle_w, handle_h),
|
||||
)
|
||||
}).collect();
|
||||
|
||||
for (i, &h_rect) in handle_rects.iter().enumerate() {
|
||||
let resp = ui.interact(h_rect, ui.id().with(("grad_handle", i)), Sense::click_and_drag());
|
||||
if resp.dragged() {
|
||||
drag_idx = Some(i);
|
||||
drag_delta = resp.drag_delta().x / track.width();
|
||||
}
|
||||
if resp.clicked() {
|
||||
click_idx = Some(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply drag.
|
||||
if let (Some(i), delta) = (drag_idx, drag_delta) {
|
||||
if delta != 0.0 {
|
||||
let new_pos = (gradient.stops[i].position + delta).clamp(0.0, 1.0);
|
||||
gradient.stops[i].position = new_pos;
|
||||
// Re-sort and track the moved stop.
|
||||
gradient.stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
|
||||
// Find new index of the moved stop (closest position match).
|
||||
if let Some(ref mut sel) = *selected_stop {
|
||||
// Re-find by position proximity.
|
||||
*sel = gradient.stops.iter().enumerate()
|
||||
.min_by(|(_, a), (_, b)| {
|
||||
let pa = (a.position - (gradient.stops.get(i).map_or(0.0, |s| s.position))).abs();
|
||||
let pb = (b.position - (gradient.stops.get(i).map_or(0.0, |s| s.position))).abs();
|
||||
pa.partial_cmp(&pb).unwrap()
|
||||
})
|
||||
.map(|(idx, _)| idx)
|
||||
.unwrap_or(0);
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(i) = click_idx {
|
||||
*selected_stop = Some(i);
|
||||
}
|
||||
|
||||
// Paint handles on top (after interaction so they visually react).
|
||||
for (i, h_rect) in handle_rects.iter().enumerate() {
|
||||
let col = ShapeColor_to_Color32(gradient.stops[i].color);
|
||||
let is_selected = *selected_stop == Some(i);
|
||||
|
||||
// Draw a downward-pointing triangle.
|
||||
let cx = h_rect.center().x;
|
||||
let top = h_rect.min.y;
|
||||
let bot = h_rect.max.y;
|
||||
let hw = h_rect.width() * 0.5;
|
||||
let tri = vec![
|
||||
egui::pos2(cx, bot),
|
||||
egui::pos2(cx - hw, top),
|
||||
egui::pos2(cx + hw, top),
|
||||
];
|
||||
painter.add(egui::Shape::convex_polygon(
|
||||
tri,
|
||||
col,
|
||||
Stroke::new(if is_selected { 2.0 } else { 1.0 },
|
||||
if is_selected { Color32::WHITE } else { Color32::from_gray(100) }),
|
||||
));
|
||||
}
|
||||
|
||||
// ── Selected stop detail ──────────────────────────────────────────────
|
||||
if let Some(i) = *selected_stop {
|
||||
if i < gradient.stops.len() {
|
||||
ui.separator();
|
||||
ui.horizontal(|ui| {
|
||||
let stop = &mut gradient.stops[i];
|
||||
let mut rgba = [stop.color.r, stop.color.g, stop.color.b, stop.color.a];
|
||||
if ui.color_edit_button_srgba_unmultiplied(&mut rgba).changed() {
|
||||
stop.color = ShapeColor::rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
|
||||
changed = true;
|
||||
}
|
||||
ui.label("Position:");
|
||||
if ui.add(
|
||||
DragValue::new(&mut stop.position)
|
||||
.speed(0.005)
|
||||
.range(0.0..=1.0),
|
||||
).changed() {
|
||||
gradient.stops.sort_by(|a, b| a.position.partial_cmp(&b.position).unwrap());
|
||||
changed = true;
|
||||
}
|
||||
let can_remove = gradient.stops.len() > 2;
|
||||
if ui.add_enabled(can_remove, egui::Button::new("− Remove")).clicked() {
|
||||
gradient.stops.remove(i);
|
||||
*selected_stop = None;
|
||||
changed = true;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
*selected_stop = None;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Extend mode ───────────────────────────────────────────────────────
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Extend:");
|
||||
if ui.selectable_label(gradient.extend == GradientExtend::Pad, "Pad").clicked() {
|
||||
gradient.extend = GradientExtend::Pad; changed = true;
|
||||
}
|
||||
if ui.selectable_label(gradient.extend == GradientExtend::Reflect, "Reflect").clicked() {
|
||||
gradient.extend = GradientExtend::Reflect; changed = true;
|
||||
}
|
||||
if ui.selectable_label(gradient.extend == GradientExtend::Repeat, "Repeat").clicked() {
|
||||
gradient.extend = GradientExtend::Repeat; changed = true;
|
||||
}
|
||||
});
|
||||
|
||||
changed
|
||||
}
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
fn ShapeColor_to_Color32(c: ShapeColor) -> Color32 {
|
||||
Color32::from_rgba_unmultiplied(c.r, c.g, c.b, c.a)
|
||||
}
|
||||
|
||||
/// Draw a small grey/white checkerboard inside `rect`.
|
||||
fn draw_checker(painter: &Painter, rect: Rect) {
|
||||
let cell = 6.0_f32;
|
||||
let cols = ((rect.width() / cell).ceil() as u32).max(1);
|
||||
let rows = ((rect.height() / cell).ceil() as u32).max(1);
|
||||
for row in 0..rows {
|
||||
for col in 0..cols {
|
||||
let light = (row + col) % 2 == 0;
|
||||
let col32 = if light { Color32::from_gray(200) } else { Color32::from_gray(140) };
|
||||
let x = rect.min.x + col as f32 * cell;
|
||||
let y = rect.min.y + row as f32 * cell;
|
||||
let r = Rect::from_min_size(
|
||||
egui::pos2(x, y),
|
||||
Vec2::splat(cell),
|
||||
).intersect(rect);
|
||||
painter.rect_filled(r, 0.0, col32);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,14 @@
|
||||
|
||||
use eframe::egui::{self, DragValue, Ui};
|
||||
use lightningbeam_core::brush_settings::{bundled_brushes, BrushSettings};
|
||||
use lightningbeam_core::actions::{SetDocumentPropertiesAction, SetShapePropertiesAction};
|
||||
use lightningbeam_core::actions::{SetDocumentPropertiesAction, SetShapePropertiesAction, SetFillPaintAction};
|
||||
use lightningbeam_core::gradient::ShapeGradient;
|
||||
use lightningbeam_core::layer::{AnyLayer, LayerTrait};
|
||||
use lightningbeam_core::selection::FocusSelection;
|
||||
use lightningbeam_core::shape::ShapeColor;
|
||||
use lightningbeam_core::tool::{SimplifyMode, Tool};
|
||||
use super::{NodePath, PaneRenderer, SharedPaneState};
|
||||
use super::gradient_editor::gradient_stop_editor;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Info panel pane state
|
||||
@@ -36,6 +38,10 @@ pub struct InfopanelPane {
|
||||
eraser_picker_expanded: bool,
|
||||
/// Cached preview textures, one per preset (populated lazily).
|
||||
brush_preview_textures: Vec<egui::TextureHandle>,
|
||||
/// Selected stop index for gradient editor in shape section.
|
||||
selected_shape_gradient_stop: Option<usize>,
|
||||
/// Selected stop index for gradient editor in tool section (gradient tool).
|
||||
selected_tool_gradient_stop: Option<usize>,
|
||||
}
|
||||
|
||||
impl InfopanelPane {
|
||||
@@ -50,6 +56,8 @@ impl InfopanelPane {
|
||||
selected_eraser_preset: default_eraser_idx,
|
||||
eraser_picker_expanded: false,
|
||||
brush_preview_textures: Vec::new(),
|
||||
selected_shape_gradient_stop: None,
|
||||
selected_tool_gradient_stop: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,6 +73,8 @@ struct SelectionInfo {
|
||||
|
||||
// Shape property values (None = mixed)
|
||||
fill_color: Option<Option<ShapeColor>>,
|
||||
/// None = mixed across selection; Some(None) = no gradient; Some(Some(g)) = all same gradient
|
||||
fill_gradient: Option<Option<ShapeGradient>>,
|
||||
stroke_color: Option<Option<ShapeColor>>,
|
||||
stroke_width: Option<f64>,
|
||||
}
|
||||
@@ -76,6 +86,7 @@ impl Default for SelectionInfo {
|
||||
dcel_count: 0,
|
||||
layer_id: None,
|
||||
fill_color: None,
|
||||
fill_gradient: None,
|
||||
stroke_color: None,
|
||||
stroke_width: None,
|
||||
}
|
||||
@@ -138,21 +149,32 @@ impl InfopanelPane {
|
||||
// Gather fill properties from selected faces
|
||||
let mut first_fill_color: Option<Option<ShapeColor>> = None;
|
||||
let mut fill_color_mixed = false;
|
||||
let mut first_fill_gradient: Option<Option<ShapeGradient>> = None;
|
||||
let mut fill_gradient_mixed = false;
|
||||
|
||||
for &fid in shared.selection.selected_faces() {
|
||||
let face = dcel.face(fid);
|
||||
let fc = face.fill_color;
|
||||
let fg = face.gradient_fill.clone();
|
||||
|
||||
match first_fill_color {
|
||||
None => first_fill_color = Some(fc),
|
||||
Some(prev) if prev != fc => fill_color_mixed = true,
|
||||
_ => {}
|
||||
}
|
||||
match &first_fill_gradient {
|
||||
None => first_fill_gradient = Some(fg),
|
||||
Some(prev) if *prev != fg => fill_gradient_mixed = true,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if !fill_color_mixed {
|
||||
info.fill_color = first_fill_color;
|
||||
}
|
||||
if !fill_gradient_mixed {
|
||||
info.fill_gradient = first_fill_gradient;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,6 +213,7 @@ impl InfopanelPane {
|
||||
|| is_raster_select || is_raster_shape || matches!(
|
||||
tool,
|
||||
Tool::PaintBucket | Tool::RegionSelect | Tool::MagicWand | Tool::QuickSelect
|
||||
| Tool::Warp | Tool::Liquify | Tool::Gradient
|
||||
);
|
||||
|
||||
if !has_options {
|
||||
@@ -414,6 +437,62 @@ impl InfopanelPane {
|
||||
});
|
||||
}
|
||||
|
||||
Tool::Warp => {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Grid:");
|
||||
let cols = shared.raster_settings.warp_grid_cols;
|
||||
let rows = shared.raster_settings.warp_grid_rows;
|
||||
for (label, c, r) in [("3×3", 3u32, 3u32), ("4×4", 4, 4), ("5×5", 5, 5), ("8×8", 8, 8)] {
|
||||
let selected = cols == c && rows == r;
|
||||
if ui.selectable_label(selected, label).clicked() {
|
||||
shared.raster_settings.warp_grid_cols = c;
|
||||
shared.raster_settings.warp_grid_rows = r;
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.small("Enter to commit · Escape to cancel");
|
||||
}
|
||||
|
||||
Tool::Liquify => {
|
||||
use crate::tools::LiquifyMode;
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Mode:");
|
||||
for (label, mode) in [
|
||||
("Push", LiquifyMode::Push),
|
||||
("Pucker", LiquifyMode::Pucker),
|
||||
("Bloat", LiquifyMode::Bloat),
|
||||
("Smooth", LiquifyMode::Smooth),
|
||||
("Reconstruct", LiquifyMode::Reconstruct),
|
||||
] {
|
||||
let selected = shared.raster_settings.liquify_mode == mode;
|
||||
if ui.selectable_label(selected, label).clicked() {
|
||||
shared.raster_settings.liquify_mode = mode;
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Radius:");
|
||||
ui.add(
|
||||
egui::Slider::new(
|
||||
&mut shared.raster_settings.liquify_radius,
|
||||
5.0_f32..=500.0,
|
||||
)
|
||||
.step_by(1.0),
|
||||
);
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Strength:");
|
||||
ui.add(
|
||||
egui::Slider::new(
|
||||
&mut shared.raster_settings.liquify_strength,
|
||||
0.01_f32..=1.0,
|
||||
)
|
||||
.step_by(0.01),
|
||||
);
|
||||
});
|
||||
ui.small("Enter to commit · Escape to cancel");
|
||||
}
|
||||
|
||||
Tool::Polygon => {
|
||||
// Number of sides
|
||||
ui.horizontal(|ui| {
|
||||
@@ -461,6 +540,22 @@ impl InfopanelPane {
|
||||
});
|
||||
}
|
||||
|
||||
Tool::Gradient if active_is_raster => {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Opacity:");
|
||||
ui.add(egui::Slider::new(
|
||||
&mut shared.raster_settings.gradient_opacity,
|
||||
0.0_f32..=1.0,
|
||||
).custom_formatter(|v, _| format!("{:.0}%", v * 100.0)));
|
||||
});
|
||||
ui.add_space(4.0);
|
||||
gradient_stop_editor(
|
||||
ui,
|
||||
&mut shared.raster_settings.gradient,
|
||||
&mut self.selected_tool_gradient_stop,
|
||||
);
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -680,28 +775,72 @@ impl InfopanelPane {
|
||||
self.shape_section_open = true;
|
||||
ui.add_space(4.0);
|
||||
|
||||
// Fill color
|
||||
// Fill — determine current fill type
|
||||
let has_gradient = matches!(&info.fill_gradient, Some(Some(_)));
|
||||
let has_solid = matches!(&info.fill_color, Some(Some(_)));
|
||||
let fill_is_none = matches!(&info.fill_color, Some(None))
|
||||
&& matches!(&info.fill_gradient, Some(None));
|
||||
let fill_mixed = info.fill_color.is_none() && info.fill_gradient.is_none();
|
||||
|
||||
// Fill type toggle row
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Fill:");
|
||||
match info.fill_color {
|
||||
Some(Some(color)) => {
|
||||
if fill_mixed {
|
||||
ui.label("--");
|
||||
} else {
|
||||
if ui.selectable_label(fill_is_none, "None").clicked() && !fill_is_none {
|
||||
let action = SetFillPaintAction::solid(
|
||||
layer_id, time, face_ids.clone(), None,
|
||||
);
|
||||
shared.pending_actions.push(Box::new(action));
|
||||
}
|
||||
if ui.selectable_label(has_solid || (!has_gradient && !fill_is_none), "Solid").clicked() && !has_solid {
|
||||
// Switch to solid: use existing color or default to black
|
||||
let color = info.fill_color.flatten()
|
||||
.unwrap_or(ShapeColor::rgba(0, 0, 0, 255));
|
||||
let action = SetFillPaintAction::solid(
|
||||
layer_id, time, face_ids.clone(), Some(color),
|
||||
);
|
||||
shared.pending_actions.push(Box::new(action));
|
||||
}
|
||||
if ui.selectable_label(has_gradient, "Gradient").clicked() && !has_gradient {
|
||||
let grad = info.fill_gradient.clone().flatten()
|
||||
.unwrap_or_default();
|
||||
let action = SetFillPaintAction::gradient(
|
||||
layer_id, time, face_ids.clone(), Some(grad),
|
||||
);
|
||||
shared.pending_actions.push(Box::new(action));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Solid fill color editor
|
||||
if !fill_mixed && has_solid {
|
||||
if let Some(Some(color)) = info.fill_color {
|
||||
ui.horizontal(|ui| {
|
||||
let mut rgba = [color.r, color.g, color.b, color.a];
|
||||
if ui.color_edit_button_srgba_unmultiplied(&mut rgba).changed() {
|
||||
let new_color = ShapeColor::rgba(rgba[0], rgba[1], rgba[2], rgba[3]);
|
||||
let action = SetShapePropertiesAction::set_fill_color(
|
||||
let action = SetFillPaintAction::solid(
|
||||
layer_id, time, face_ids.clone(), Some(new_color),
|
||||
);
|
||||
shared.pending_actions.push(Box::new(action));
|
||||
}
|
||||
}
|
||||
Some(None) => {
|
||||
ui.label("None");
|
||||
}
|
||||
None => {
|
||||
ui.label("--");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Gradient fill editor
|
||||
if !fill_mixed && has_gradient {
|
||||
if let Some(Some(mut grad)) = info.fill_gradient.clone() {
|
||||
if gradient_stop_editor(ui, &mut grad, &mut self.selected_shape_gradient_stop) {
|
||||
let action = SetFillPaintAction::gradient(
|
||||
layer_id, time, face_ids.clone(), Some(grad),
|
||||
);
|
||||
shared.pending_actions.push(Box::new(action));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Stroke color
|
||||
ui.horizontal(|ui| {
|
||||
|
||||
@@ -68,6 +68,7 @@ pub enum WebcamRecordCommand {
|
||||
|
||||
pub mod toolbar;
|
||||
pub mod stage;
|
||||
pub mod gradient_editor;
|
||||
pub mod timeline;
|
||||
pub mod infopanel;
|
||||
pub mod outliner;
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
// GPU gradient fill shader.
|
||||
//
|
||||
// Reads the anchor canvas (before_pixels), composites a gradient over it, and
|
||||
// writes the result to the display canvas. All color values in the canvas are
|
||||
// linear premultiplied RGBA. The stop colors passed via `stops` are linear
|
||||
// straight-alpha [0..1] (sRGB→linear conversion is done on the CPU).
|
||||
//
|
||||
// Dispatch: ceil(canvas_w / 8) × ceil(canvas_h / 8) × 1
|
||||
|
||||
struct Params {
|
||||
canvas_w: u32,
|
||||
canvas_h: u32,
|
||||
start_x: f32,
|
||||
start_y: f32,
|
||||
end_x: f32,
|
||||
end_y: f32,
|
||||
opacity: f32,
|
||||
extend_mode: u32, // 0 = Pad, 1 = Reflect, 2 = Repeat
|
||||
num_stops: u32,
|
||||
kind: u32, // 0 = Linear, 1 = Radial
|
||||
_pad1: u32,
|
||||
_pad2: u32,
|
||||
}
|
||||
|
||||
// 32 bytes per stop (8 × f32), matching `GpuGradientStop` on the Rust side.
|
||||
struct GradientStop {
|
||||
position: f32,
|
||||
r: f32, // linear [0..1], straight-alpha
|
||||
g: f32,
|
||||
b: f32,
|
||||
a: f32,
|
||||
_pad0: f32,
|
||||
_pad1: f32,
|
||||
_pad2: f32,
|
||||
}
|
||||
|
||||
@group(0) @binding(0) var<uniform> params: Params;
|
||||
@group(0) @binding(1) var src: texture_2d<f32>;
|
||||
@group(0) @binding(2) var<storage, read> stops: array<GradientStop>;
|
||||
@group(0) @binding(3) var dst: texture_storage_2d<rgba8unorm, write>;
|
||||
|
||||
fn apply_extend(t: f32) -> f32 {
|
||||
if params.extend_mode == 0u {
|
||||
// Pad: clamp to [0, 1]
|
||||
return clamp(t, 0.0, 1.0);
|
||||
} else if params.extend_mode == 1u {
|
||||
// Reflect: 0→1→0→1→...
|
||||
let t_abs = abs(t);
|
||||
let period = floor(t_abs);
|
||||
let frac = t_abs - period;
|
||||
if (u32(period) & 1u) == 0u {
|
||||
return frac;
|
||||
} else {
|
||||
return 1.0 - frac;
|
||||
}
|
||||
} else {
|
||||
// Repeat: tile [0, 1)
|
||||
return t - floor(t);
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_gradient(t: f32) -> vec4<f32> {
|
||||
let n = params.num_stops;
|
||||
if n == 0u { return vec4<f32>(0.0); }
|
||||
|
||||
let s0 = stops[0];
|
||||
if t <= s0.position {
|
||||
return vec4<f32>(s0.r, s0.g, s0.b, s0.a);
|
||||
}
|
||||
|
||||
let sn = stops[n - 1u];
|
||||
if t >= sn.position {
|
||||
return vec4<f32>(sn.r, sn.g, sn.b, sn.a);
|
||||
}
|
||||
|
||||
for (var i = 0u; i < n - 1u; i++) {
|
||||
let sa = stops[i];
|
||||
let sb = stops[i + 1u];
|
||||
if t >= sa.position && t <= sb.position {
|
||||
let span = sb.position - sa.position;
|
||||
let f = select(0.0, (t - sa.position) / span, span > 0.0001);
|
||||
return mix(
|
||||
vec4<f32>(sa.r, sa.g, sa.b, sa.a),
|
||||
vec4<f32>(sb.r, sb.g, sb.b, sb.a),
|
||||
f,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return vec4<f32>(sn.r, sn.g, sn.b, sn.a);
|
||||
}
|
||||
|
||||
@compute @workgroup_size(8, 8)
|
||||
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
||||
if gid.x >= params.canvas_w || gid.y >= params.canvas_h { return; }
|
||||
|
||||
// Anchor pixel (linear premultiplied RGBA).
|
||||
let src_px = textureLoad(src, vec2<i32>(i32(gid.x), i32(gid.y)), 0);
|
||||
|
||||
let dx = params.end_x - params.start_x;
|
||||
let dy = params.end_y - params.start_y;
|
||||
let px = f32(gid.x) + 0.5;
|
||||
let py = f32(gid.y) + 0.5;
|
||||
|
||||
var t_raw: f32 = 0.0;
|
||||
if params.kind == 1u {
|
||||
// Radial: center at start point, radius = |end-start|.
|
||||
let radius = sqrt(dx * dx + dy * dy);
|
||||
if radius >= 0.5 {
|
||||
let pdx = px - params.start_x;
|
||||
let pdy = py - params.start_y;
|
||||
t_raw = sqrt(pdx * pdx + pdy * pdy) / radius;
|
||||
}
|
||||
} else {
|
||||
// Linear: project pixel centre onto gradient axis (start → end).
|
||||
let len2 = dx * dx + dy * dy;
|
||||
if len2 >= 1.0 {
|
||||
let fx = px - params.start_x;
|
||||
let fy = py - params.start_y;
|
||||
t_raw = (fx * dx + fy * dy) / len2;
|
||||
}
|
||||
}
|
||||
|
||||
let t = apply_extend(t_raw);
|
||||
let grad = eval_gradient(t); // straight-alpha linear RGBA
|
||||
|
||||
// Effective alpha: gradient alpha × tool opacity.
|
||||
let a = grad.a * params.opacity;
|
||||
|
||||
// Alpha-over composite.
|
||||
// src_px.rgb is premultiplied (= straight_rgb * src_a).
|
||||
// Output is also premultiplied.
|
||||
let out_a = a + src_px.a * (1.0 - a);
|
||||
let out_rgb = grad.rgb * a + src_px.rgb * (1.0 - a);
|
||||
|
||||
textureStore(dst, vec2<i32>(i32(gid.x), i32(gid.y)), vec4<f32>(out_rgb, out_a));
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// GPU liquify-brush shader.
|
||||
//
|
||||
// Updates a per-pixel displacement map (array of vec2f) for one brush step.
|
||||
// Each pixel within the brush radius receives a displacement contribution
|
||||
// weighted by a Gaussian falloff.
|
||||
//
|
||||
// Modes:
|
||||
// 0 = Push — displace in brush-drag direction (dx, dy)
|
||||
// 1 = Pucker — pull toward brush center
|
||||
// 2 = Bloat — push away from brush center
|
||||
// 3 = Smooth — blend toward average of 4 cardinal neighbours
|
||||
// 4 = Reconstruct — blend toward zero (gradually undo)
|
||||
//
|
||||
// Dispatch: ceil((2*radius+1) / 8) × ceil((2*radius+1) / 8) × 1
|
||||
// The CPU clips invocation IDs to the valid map range.
|
||||
|
||||
struct Params {
|
||||
cx: f32, // brush center x (canvas pixels)
|
||||
cy: f32, // brush center y
|
||||
radius: f32, // brush radius (canvas pixels)
|
||||
strength: f32, // effect strength [0..1]
|
||||
dx: f32, // push direction x (normalised by caller, Push mode only)
|
||||
dy: f32, // push direction y
|
||||
mode: u32, // 0=Push 1=Pucker 2=Bloat 3=Smooth 4=Reconstruct
|
||||
map_w: u32,
|
||||
map_h: u32,
|
||||
_pad0: u32,
|
||||
_pad1: u32,
|
||||
}
|
||||
|
||||
@group(0) @binding(0) var<uniform> params: Params;
|
||||
@group(0) @binding(1) var<storage, read_write> disp: array<vec2f>;
|
||||
|
||||
@compute @workgroup_size(8, 8)
|
||||
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
||||
// Offset invocation into the brush bounding box so gid(0,0) = (cx-r, cy-r).
|
||||
let base_x = floor(params.cx - params.radius);
|
||||
let base_y = floor(params.cy - params.radius);
|
||||
let px = base_x + f32(gid.x);
|
||||
let py = base_y + f32(gid.y);
|
||||
|
||||
// Clip to displacement map bounds.
|
||||
if px < 0.0 || py < 0.0 { return; }
|
||||
let map_x = u32(px);
|
||||
let map_y = u32(py);
|
||||
if map_x >= params.map_w || map_y >= params.map_h { return; }
|
||||
|
||||
let ddx = px - params.cx;
|
||||
let ddy = py - params.cy;
|
||||
let dist2 = ddx * ddx + ddy * ddy;
|
||||
let r2 = params.radius * params.radius;
|
||||
|
||||
if dist2 > r2 { return; }
|
||||
|
||||
// Gaussian influence: 1 at center, ~0.01 at edge (sigma = radius/2.15)
|
||||
let influence = params.strength * exp(-dist2 / (r2 * 0.2));
|
||||
|
||||
let idx = map_y * params.map_w + map_x;
|
||||
var d = disp[idx];
|
||||
|
||||
switch params.mode {
|
||||
case 0u: { // Push
|
||||
d = d + vec2f(params.dx, params.dy) * influence * params.radius;
|
||||
}
|
||||
case 1u: { // Pucker — toward center
|
||||
let len = sqrt(dist2) + 0.0001;
|
||||
d = d + vec2f(-ddx / len, -ddy / len) * influence * params.radius;
|
||||
}
|
||||
case 2u: { // Bloat — away from center
|
||||
let len = sqrt(dist2) + 0.0001;
|
||||
d = d + vec2f(ddx / len, ddy / len) * influence * params.radius;
|
||||
}
|
||||
case 3u: { // Smooth — blend toward average of 4 neighbours
|
||||
let xi = i32(map_x);
|
||||
let yi = i32(map_y);
|
||||
let w = i32(params.map_w);
|
||||
let h = i32(params.map_h);
|
||||
let l = disp[u32(clamp(yi, 0, h-1)) * params.map_w + u32(clamp(xi - 1, 0, w-1))];
|
||||
let r = disp[u32(clamp(yi, 0, h-1)) * params.map_w + u32(clamp(xi + 1, 0, w-1))];
|
||||
let u = disp[u32(clamp(yi - 1, 0, h-1)) * params.map_w + u32(clamp(xi, 0, w-1))];
|
||||
let dn = disp[u32(clamp(yi + 1, 0, h-1)) * params.map_w + u32(clamp(xi, 0, w-1))];
|
||||
let avg = (l + r + u + dn) * 0.25;
|
||||
d = mix(d, avg, influence * 0.5);
|
||||
}
|
||||
case 4u: { // Reconstruct — blend toward zero
|
||||
d = mix(d, vec2f(0.0), influence * 0.5);
|
||||
}
|
||||
default: {}
|
||||
}
|
||||
|
||||
disp[idx] = d;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// GPU warp-apply shader.
|
||||
//
|
||||
// Two modes selected by grid_cols / grid_rows:
|
||||
//
|
||||
// grid_cols == 0 (Liquify / per-pixel mode)
|
||||
// disp[] is a full canvas-sized array<vec2f>. Each pixel reads its own entry.
|
||||
//
|
||||
// grid_cols > 0 (Warp control-point mode)
|
||||
// disp[] contains only grid_cols * grid_rows vec2f displacements (one per
|
||||
// control point). The shader bilinearly interpolates them so the CPU never
|
||||
// needs to build or upload the full per-pixel buffer.
|
||||
//
|
||||
// Dispatch: ceil(dst_w / 8) × ceil(dst_h / 8) × 1
|
||||
|
||||
struct Params {
|
||||
src_w: u32,
|
||||
src_h: u32,
|
||||
dst_w: u32,
|
||||
dst_h: u32,
|
||||
grid_cols: u32, // 0 = per-pixel mode
|
||||
grid_rows: u32,
|
||||
_pad0: u32,
|
||||
_pad1: u32,
|
||||
}
|
||||
|
||||
@group(0) @binding(0) var<uniform> params: Params;
|
||||
@group(0) @binding(1) var src: texture_2d<f32>;
|
||||
@group(0) @binding(2) var<storage, read> disp: array<vec2f>;
|
||||
@group(0) @binding(3) var dst: texture_storage_2d<rgba8unorm, write>;
|
||||
|
||||
// Manual bilinear sample with clamp-to-edge (textureSample forbidden in compute shaders).
|
||||
fn bilinear_sample(px: f32, py: f32) -> vec4<f32> {
|
||||
let sw = i32(params.src_w);
|
||||
let sh = i32(params.src_h);
|
||||
|
||||
let ix = i32(floor(px - 0.5));
|
||||
let iy = i32(floor(py - 0.5));
|
||||
let fx = fract(px - 0.5);
|
||||
let fy = fract(py - 0.5);
|
||||
|
||||
let x0 = clamp(ix, 0, sw - 1);
|
||||
let x1 = clamp(ix + 1, 0, sw - 1);
|
||||
let y0 = clamp(iy, 0, sh - 1);
|
||||
let y1 = clamp(iy + 1, 0, sh - 1);
|
||||
|
||||
let s00 = textureLoad(src, vec2<i32>(x0, y0), 0);
|
||||
let s10 = textureLoad(src, vec2<i32>(x1, y0), 0);
|
||||
let s01 = textureLoad(src, vec2<i32>(x0, y1), 0);
|
||||
let s11 = textureLoad(src, vec2<i32>(x1, y1), 0);
|
||||
|
||||
return mix(mix(s00, s10, fx), mix(s01, s11, fx), fy);
|
||||
}
|
||||
|
||||
// Bilinearly interpolate the control-point displacement grid.
|
||||
fn grid_displacement(px: u32, py: u32) -> vec2f {
|
||||
let cols = params.grid_cols;
|
||||
let rows = params.grid_rows;
|
||||
|
||||
// Normalised position in grid space [0 .. cols-1] × [0 .. rows-1].
|
||||
let gx = f32(px) / f32(params.dst_w - 1u) * f32(cols - 1u);
|
||||
let gy = f32(py) / f32(params.dst_h - 1u) * f32(rows - 1u);
|
||||
|
||||
let col0 = u32(floor(gx));
|
||||
let row0 = u32(floor(gy));
|
||||
let col1 = min(col0 + 1u, cols - 1u);
|
||||
let row1 = min(row0 + 1u, rows - 1u);
|
||||
let fx = gx - floor(gx);
|
||||
let fy = gy - floor(gy);
|
||||
|
||||
let d00 = disp[row0 * cols + col0];
|
||||
let d10 = disp[row0 * cols + col1];
|
||||
let d01 = disp[row1 * cols + col0];
|
||||
let d11 = disp[row1 * cols + col1];
|
||||
|
||||
return d00 * (1.0 - fx) * (1.0 - fy)
|
||||
+ d10 * fx * (1.0 - fy)
|
||||
+ d01 * (1.0 - fx) * fy
|
||||
+ d11 * fx * fy;
|
||||
}
|
||||
|
||||
@compute @workgroup_size(8, 8)
|
||||
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
|
||||
if gid.x >= params.dst_w || gid.y >= params.dst_h { return; }
|
||||
|
||||
var d: vec2f;
|
||||
if params.grid_cols > 0u {
|
||||
d = grid_displacement(gid.x, gid.y);
|
||||
} else {
|
||||
d = disp[gid.y * params.dst_w + gid.x];
|
||||
}
|
||||
|
||||
let sx = f32(gid.x) + d.x;
|
||||
let sy = f32(gid.y) + d.y;
|
||||
|
||||
var color: vec4<f32>;
|
||||
if sx < 0.0 || sy < 0.0 || sx >= f32(params.src_w) || sy >= f32(params.src_h) {
|
||||
color = vec4<f32>(0.0);
|
||||
} else {
|
||||
color = bilinear_sample(sx + 0.5, sy + 0.5);
|
||||
}
|
||||
|
||||
textureStore(dst, vec2<i32>(i32(gid.x), i32(gid.y)), color);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user