PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Map the bevel's shading through its gloss contour, not its light (#7360)
Hector Villarreala42e831698eb
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rsindex 5acdd7931..d7bc223b7 100644--- a/crates/compose/src/effects.rs+++ b/crates/compose/src/effects.rs@@ -998,12 +998,7 @@ fn noise(m: &mut Map, amount: f32, x0: i32, y0: i32) {fn apply_contour(m: Map, c: &Contour) -> Map {match contour_lut(c) {None => m,- Some(l) => m.map(|v| {- let x = v.clamp(0.0, 1.0) * (l.len() - 1) as f32;- let i = x.floor() as usize;- let j = (i + 1).min(l.len() - 1);- l[i] + (l[j] - l[i]) * (x - i as f32)- }),+ Some(l) => m.map(|v| lut_at(&l, v)),}}@@ -1623,6 +1618,12 @@ fn bevel_maps(shape: &Map, b: &Bevel, light: &GlobalLight, tex: &TextureCtx, patlet on_bevel = |x: i64, y: i64| {hmap.get(x, y) > BEVEL_H_EPS || (reach_past && [(1, 0), (-1, 0), (0, 1), (0, -1)].iter().any(|(dx, dy)| hmap.get(x + dx, y + dy) > BEVEL_H_EPS))};+ // The gloss contour remaps the shading `n · light` before it splits into highlight (above+ // what it maps a flat face's `sin(altitude)` to) and shadow. Applied to the highlight and shadow amounts+ // instead, a contour that starts high (Rolling Slope - Descending) lit every pixel the light+ // missed, so a bevelled glyph's flat face took the full highlight (#7360) where Photoshop+ // leaves it at the layer's colour. The Linear contour changes nothing either way.+ let gloss = contour_lut(&b.gloss_contour);let shade_into = |depth: f32, region_kind: u8| -> (Map, Map) {let (mut hi, mut sh) = (Map::new(shape.w, shape.h, 0.0), Map::new(shape.w, shape.h, 0.0));for y in 0..shape.h as i64 {@@ -1634,7 +1635,7 @@ fn bevel_maps(shape: &Map, b: &Bevel, light: &GlobalLight, tex: &TextureCtx, patlet len = (n[0] * n[0] + n[1] * n[1] + 1.0).sqrt();let shade = (n[0] * light_v[0] + n[1] * light_v[1] + n[2] * light_v[2]) / len;let region = if region_kind == 0 { shape.v[i] } else { f32::from(shape.v[i] < 1.0 - INSIDE_EPS && on_bevel(x, y)) };- let k = shade - se;+ let k = gloss.as_ref().map_or(shade - se, |l| lut_at(l, shade) - lut_at(l, se));if k > 0.0 {hi.v[i] = (k / (1.0 - se).max(1e-3)).clamp(0.0, 1.0) * region;} else {@@ -1642,7 +1643,7 @@ fn bevel_maps(shape: &Map, b: &Bevel, light: &GlobalLight, tex: &TextureCtx, pat}}}- (apply_contour(hi, &b.gloss_contour), apply_contour(sh, &b.gloss_contour))+ (hi, sh)};let maps = match g.paint {BevelPaint::Inner | BevelPaint::Outer => {diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex ddd487154..d77afda73 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -1138,6 +1138,56 @@ fn satin_and_bevel_stay_inside_shape() {assert!(top[1] > bot[1], "{top:?} {bot:?}");}+/// #7360: a gloss contour that starts high (Rolling Slope - Descending, as the reporter's PSD+/// uses) left a bevelled shape's flat face at the full Overlay highlight, (255, 40, 42) on a+/// (151, 22, 23) fill, where Photoshop leaves the face at the fill colour. The contour maps the+/// shading, so the flat face stays neutral and only the slopes are shaded.+#[test]+fn a_gloss_contour_leaves_the_flat_face_of_a_bevel_unlit() {+ let fill = [151.0 / 255.0, 22.0 / 255.0, 23.0 / 255.0, 1.0];+ let rolling = photocraft_doc::Contour::Custom {+ name: "Rolling Slope - Descending".into(),+ points: [(0.0078, 1.0), (0.2078, 0.3922), (0.3804, 0.4196), (0.7020, 0.6), (0.9882, 0.0)]+ .iter()+ .map(|&(input, output)| CurvePoint { input, output })+ .collect(),+ corners: vec![],+ };+ let render = |gloss_contour: photocraft_doc::Contour| {+ let mut d = doc_white(40, 40);+ let mut l = solid_layer("sq", Rect::new(10, 10, 30, 30), fill);+ l.effects.items = vec![Effect::BevelEmboss(photocraft_doc::Bevel {+ enabled: true,+ style: photocraft_doc::BevelStyle::InnerBevel,+ technique: photocraft_doc::BevelTechnique::ChiselHard,+ depth: 1.47,+ up: false,+ size: 3.0,+ soften: 0.0,+ angle: 90.0,+ altitude: 30.0,+ use_global_light: false,+ gloss_contour,+ highlight: FxCommon::new(photocraft_color::BlendMode::Overlay, 1.0),+ highlight_color: Color::WHITE,+ shadow: FxCommon::new(photocraft_color::BlendMode::Overlay, 0.16),+ shadow_color: Color::BLACK,+ contour: None,+ texture: None,+ })];+ d.layers.push(l);+ d+ };+ let d = render(rolling);+ for (x, y) in [(20, 20), (16, 22), (24, 16)] {+ let p = px(&d, x, y);+ assert!(close4(p, fill), "flat face ({x},{y}) keeps the fill: {:?}", p.map(|v| (v * 255.0).round()));+ }+ let linear = render(photocraft_doc::Contour::Linear);+ assert!(close4(px(&linear, 20, 20), fill), "Linear leaves the flat face too");+ assert!(!close4(px(&d, 20, 11), fill), "the slopes are still shaded");+}+#[test]fn clipped_layer_effects_are_clipped_to_base() {let mut d = doc_white(40, 40);diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgslindex 6457b1192..ee225c75f 100644--- a/crates/gpu/src/compose.wgsl+++ b/crates/gpu/src/compose.wgsl@@ -1329,7 +1329,7 @@ fn fs_mbevelshade(in: VOut) -> @location(0) vec4<f32> {}// One bevel shading value (`bevel_maps`' shade_into): highlight (p1.z = 0) or shadow amount for-// height-gradient scale `depth`, times `region`, through the gloss contour.+// height-gradient scale `depth`, times `region`, with the shading through the gloss contour.fn bevel_part(p: vec2<i32>, depth: f32, region: f32) -> f32 {let gx = (ra(p + vec2(1, 0)) - ra(p - vec2(1, 0))) * 0.5 * depth;let gy = (ra(p + vec2(0, 1)) - ra(p - vec2(0, 1))) * 0.5 * depth;@@ -1337,14 +1337,15 @@ fn bevel_part(p: vec2<i32>, depth: f32, region: f32) -> f32 {let len = sqrt(n.x * n.x + n.y * n.y + 1.0);let shade = (n.x * op.p0.x + n.y * op.p0.y + n.z * op.p0.z) / len;let se = op.p0.w;- let k = shade - se;+ var k = shade - se;+ // effects::bevel_maps: the gloss contour maps the shading, a flat face's as the neutral (#7360).+ if (op.p1.w > 0.5) { k = lut(0, shade) - lut(0, se); }var v = 0.0;if (op.p1.z > 0.5) {if (k <= 0.0) { v = clamp(-k / max(se, 1e-3), 0.0, 1.0) * region; }} else if (k > 0.0) {v = clamp(k / max(1.0 - se, 1e-3), 0.0, 1.0) * region;}- if (op.p1.w > 0.5) { v = lut(0, v); }return v;}