diff --git a/document/migrations/nodes/src/raster_nodes/make_opaque_v1.rs b/document/migrations/nodes/src/raster_nodes/make_opaque_v1.rs new file mode 100644 index 0000000000..8344443077 --- /dev/null +++ b/document/migrations/nodes/src/raster_nodes/make_opaque_v1.rs @@ -0,0 +1,43 @@ +use migration_core::{Inputs, literal, migration}; +use serde::{Deserialize, Serialize}; + +/// Frozen mirror of the `TaggedValue` variant this migration writes. +#[derive(Serialize, Deserialize)] +enum Tagged { + Number(f64), +} + +/// 'Make Opaque' gained a Strength percentage, set to 100% to keep making the content fully opaque. +#[migration("raster_nodes::adjustments::MakeOpaqueNode"@1 => @2)] +pub fn make_opaque_v1(inputs: &mut Inputs) { + inputs.push(literal(Tagged::Number(100.))); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::decl; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(1, |random, _slot| literal(random.below(10) as f64)) + } + + #[test] + fn rewrites_history() { + check_commutation(&MakeOpaqueV1, &contract()); + check_undo(&MakeOpaqueV1, &contract()); + } + + #[test] + fn keeps_the_content_and_adds_full_strength() { + let content = literal(1.); + + let mut fixture = Fixture::new(&MakeOpaqueV1); + let make_opaque = fixture.add(decl!("raster_nodes::adjustments::MakeOpaqueNode"@1), vec![content.clone()]); + let migrated = fixture.migrate(&MakeOpaqueV1).unwrap(); + + assert_eq!(fixture.declaration_of(&migrated, make_opaque), Some(decl!("raster_nodes::adjustments::MakeOpaqueNode"@2))); + assert_eq!(fixture.inputs(&migrated, make_opaque), [content, literal(Tagged::Number(100.))]); + } +} diff --git a/document/migrations/nodes/src/raster_nodes/mod.rs b/document/migrations/nodes/src/raster_nodes/mod.rs index f34f268d28..a75e50a945 100644 --- a/document/migrations/nodes/src/raster_nodes/mod.rs +++ b/document/migrations/nodes/src/raster_nodes/mod.rs @@ -1,5 +1,6 @@ //! Migrations of the nodes in `raster_nodes`. +mod make_opaque_v1; mod mandelbrot_v1; mod mandelbrot_v2; mod mix_colors_from_mix_node; diff --git a/editor/src/messages/portfolio/document_migration.rs b/editor/src/messages/portfolio/document_migration.rs index cf570f06de..38da367e8e 100644 --- a/editor/src/messages/portfolio/document_migration.rs +++ b/editor/src/messages/portfolio/document_migration.rs @@ -3045,6 +3045,18 @@ fn migrate_node(node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId], inputs_count = 3; } + // Make Opaque gained a Strength input, set to 100% to keep making the content fully opaque + if reference == DefinitionIdentifier::ProtoNode(graphene_std::raster_nodes::adjustments::make_opaque::IDENTIFIER) && inputs_count == 1 { + let mut node_template = resolve_document_node_type(&reference)?.default_node_template(); + document.network_interface.replace_implementation(node_id, network_path, &mut node_template); + let old_inputs = document.network_interface.replace_inputs(node_id, network_path, &mut node_template)?; + document.network_interface.set_input(&InputConnector::node_at_index(*node_id, 0), old_inputs[0].clone(), network_path); + document + .network_interface + .set_input(&InputConnector::node_at_index(*node_id, 1), NodeInput::value(TaggedValue::Number(100.), false), network_path); + inputs_count = 2; + } + // The removed "Brightness/Contrast Classic" node had no Use Classic input, so its three inputs become the unified node with the toggle on if reference == DefinitionIdentifier::ProtoNode(graphene_std::raster::brightness_contrast::IDENTIFIER) && inputs_count == 3 { let mut node_template = resolve_document_node_type(&reference)?.default_node_template(); @@ -5915,6 +5927,33 @@ mod tests { } } + #[test] + fn make_opaques_from_before_their_strength_open_fully_opaque() { + use crate::messages::portfolio::document_storage_io::load_legacy_document; + use graph_craft::application_io::resource::HashMapResourceStorage; + use graphene_std::raster_nodes::adjustments::make_opaque; + + let definition = resolve_document_node_type(&DefinitionIdentifier::ProtoNode(make_opaque::IDENTIFIER)).expect("the Make Opaque definition should exist"); + let mut inputs = definition.default_node_template().inputs; + inputs.truncate(1); + let content = inputs.clone(); + + // Saved with its content alone, before its strength + let mut document = DocumentMessageHandler::default(); + let node_template = NodeTemplate { + implementation: NodeTemplateImplementation::ProtoNode(make_opaque::IDENTIFIER), + inputs, + ..Default::default() + }; + document.network_interface.insert_node(NodeId(1), node_template, &[]); + + let opened = load_legacy_document(document.serialize_document(), &HashMapResourceStorage::new(), false).expect("the document should open"); + + let node = opened.network_interface.document_node(&NodeId(1), &[]).expect("the node should still exist"); + assert_eq!(node.inputs[..1], content, "Make Opaque should keep its content"); + assert_eq!(node.inputs[1..], [NodeInput::value(TaggedValue::Number(100.), false)], "Make Opaque should open at full strength"); + } + // Serialize, Sample Image, and Empty Image nodes open as As String, Passthrough, and Image nodes, keeping their wires and Serialize's stored text #[test] fn removed_nodes_open_as_their_stand_ins() { diff --git a/node-graph/libraries/graphic-types/src/adjustment.rs b/node-graph/libraries/graphic-types/src/adjustment.rs index 602d3b4b2a..2c7913274b 100644 --- a/node-graph/libraries/graphic-types/src/adjustment.rs +++ b/node-graph/libraries/graphic-types/src/adjustment.rs @@ -23,7 +23,10 @@ pub enum Adjustment { gamma: f64, inverse: bool, }, - MakeOpaque, + MakeOpaque { + /// Percent. + strength: f64, + }, BrightnessContrast { /// Percent. brightness: f64, @@ -90,7 +93,7 @@ impl Adjustment { match self { Self::Desaturate { .. } => EffectKind::Desaturate, Self::GammaCorrection { .. } => EffectKind::GammaCorrection, - Self::MakeOpaque => EffectKind::MakeOpaque, + Self::MakeOpaque { .. } => EffectKind::MakeOpaque, Self::BrightnessContrast { .. } => EffectKind::BrightnessContrast, Self::Levels(_) => EffectKind::Levels, Self::Curves(_) => EffectKind::Curves, @@ -117,7 +120,7 @@ impl Adjustment { matches!( self, Self::GammaCorrection { .. } - | Self::MakeOpaque + | Self::MakeOpaque { .. } | Self::BrightnessContrast { .. } | Self::Levels(_) | Self::Curves(_) @@ -137,7 +140,8 @@ impl Adjustment { visitor.parameter("gamma", gamma, 2.2); visitor.parameter("inverse", inverse, false); } - Self::MakeOpaque | Self::Invert | Self::Solarize => {} + Self::MakeOpaque { strength } => visitor.parameter("strength", strength, 100.), + Self::Invert | Self::Solarize => {} Self::BrightnessContrast { brightness, contrast, diff --git a/node-graph/libraries/graphic-types/src/effect_layers.rs b/node-graph/libraries/graphic-types/src/effect_layers.rs index 771c0f532b..85545d2bbf 100644 --- a/node-graph/libraries/graphic-types/src/effect_layers.rs +++ b/node-graph/libraries/graphic-types/src/effect_layers.rs @@ -636,6 +636,13 @@ mod tests { let layers = EffectLayers::plan(&effects, BlendMode::Normal, 0.5, true); assert_eq!(layers.flattened, 0); assert_eq!(parts(&layers), [[Some((0, 0, PiecePlace::Beneath)), None, Some((2, 0, PiecePlace::Within))]]); + + // Making the content opaque at no strength leaves the multiplying shadow before it blending with what lies beneath, which any strength flattens + let make_opaque = |strength| Effects::from_operations([drop_shadow(RED, BlendMode::Multiply), EffectOperation::Adjustment(Adjustment::MakeOpaque { strength })]); + let layers = EffectLayers::plan(&make_opaque(0.), BlendMode::Normal, 1., true); + assert_eq!(layers.flattened, 0); + assert_eq!(modes(&layers), [BlendMode::Multiply, BlendMode::Normal]); + assert_eq!(EffectLayers::plan(&make_opaque(1.), BlendMode::Normal, 1., true).flattened, 2); } fn mask() -> EffectOperation { diff --git a/node-graph/libraries/graphic-types/src/effects.rs b/node-graph/libraries/graphic-types/src/effects.rs index c44ffc0e3f..7aed6a45ce 100644 --- a/node-graph/libraries/graphic-types/src/effects.rs +++ b/node-graph/libraries/graphic-types/src/effects.rs @@ -1224,6 +1224,7 @@ impl EffectOperation { DisplaceMode::XAndY => *offset == DVec2::ZERO, }, Self::Adjustment(Adjustment::ColorOverlay { color, opacity, .. }) => color.a() <= 0. || *opacity <= 0., + Self::Adjustment(Adjustment::MakeOpaque { strength }) => *strength <= 0., Self::Mask { density, .. } => *density <= 0., Self::Feather { distance } => *distance <= 0., Self::ConvolutionKernel { .. } => self.convolution().is_none(), @@ -1636,7 +1637,7 @@ impl EffectOperation { }, EffectKind::Desaturate => Self::Adjustment(Adjustment::Desaturate { method: Default::default() }), EffectKind::GammaCorrection => Self::Adjustment(Adjustment::GammaCorrection { gamma: 0., inverse: false }), - EffectKind::MakeOpaque => Self::Adjustment(Adjustment::MakeOpaque), + EffectKind::MakeOpaque => Self::Adjustment(Adjustment::MakeOpaque { strength: 0. }), EffectKind::BrightnessContrast => Self::Adjustment(Adjustment::BrightnessContrast { brightness: 0., contrast: 0., diff --git a/node-graph/libraries/raster-kernels/src/adjustment_steps.rs b/node-graph/libraries/raster-kernels/src/adjustment_steps.rs index 072a65704f..2f93378429 100644 --- a/node-graph/libraries/raster-kernels/src/adjustment_steps.rs +++ b/node-graph/libraries/raster-kernels/src/adjustment_steps.rs @@ -221,7 +221,7 @@ mod tests { let run = [ Adjustment::Desaturate { method: Default::default() }, Adjustment::GammaCorrection { gamma: 2.2, inverse: false }, - Adjustment::MakeOpaque, + Adjustment::MakeOpaque { strength: 100. }, Adjustment::BrightnessContrast { brightness: 10., contrast: 10., diff --git a/node-graph/libraries/raster-kernels/src/adjustments.rs b/node-graph/libraries/raster-kernels/src/adjustments.rs index 9cdf2a239a..0fce87a8a5 100644 --- a/node-graph/libraries/raster-kernels/src/adjustments.rs +++ b/node-graph/libraries/raster-kernels/src/adjustments.rs @@ -131,7 +131,9 @@ pub fn visit_kernel(adjustment: &Adjustment, visitor: V) - let exponent = if *inverse { 1. / gamma } else { gamma }; visitor.visit(GammaCorrection { exponent: exponent as f32 }) } - Adjustment::MakeOpaque => visitor.visit(MakeOpaque), + Adjustment::MakeOpaque { strength } => visitor.visit(MakeOpaque { + strength: (strength / 100.).clamp(0., 1.) as f32, + }), Adjustment::BrightnessContrast { brightness, contrast, @@ -438,15 +440,23 @@ impl MapColor for GammaCorrection { } } -struct MakeOpaque; +struct MakeOpaque { + strength: f32, +} impl MapColor for MakeOpaque { fn map_cpu(&self, color: Color) -> Color { - color.with_alpha(1.) + // At full strength, even an alpha that isn't finite becomes opaque + if self.strength >= 1. { + return color.with_alpha(1.); + } + + // Alpha moves toward 1 by the strength, in a form exact at both ends, keeping the straight color + color.with_alpha(color.a() * (1. - self.strength) + self.strength) } fn encode_step(&self, steps: &mut AdjustmentSteps) { - steps.push(StepKind::MakeOpaque, []); + steps.push(StepKind::MakeOpaque, [texel(&[self.strength])]); } } @@ -1978,4 +1988,23 @@ mod tests { assert_eq!((mapped(&ramp, 0), mapped(&ramp, 255)), (0., 1.), "{spread:?}"); } } + + #[test] + fn making_opaque_lifts_alpha_by_its_strength_keeping_the_straight_color() { + let make_opaque = |strength| Adjustment::MakeOpaque { strength }; + + // Even a fully transparent pixel's hidden color shows, as the strength lifts its alpha + for color in [COLORS[1], Color::from_rgbaf32_unchecked(0.8, 0.4, 0.2, 0.), Color::from_rgbaf32_unchecked(0.3, 0.6, 0.1, 0.1)] { + assert_eq!(adjust_color(&make_opaque(0.), color), color); + assert_eq!(adjust_color(&make_opaque(100.), color), color.with_alpha(1.)); + + let half = adjust_color(&make_opaque(50.), color); + assert_eq!(half.with_alpha(color.a()), color); + assert!((half.a() - (color.a() + 1.) / 2.).abs() < 1e-6, "{half:?}"); + } + + for alpha in [f32::NAN, f32::INFINITY] { + assert_eq!(adjust_color(&make_opaque(100.), Color::from_rgbaf32_unchecked(0.8, 0.4, 0.2, alpha)).a(), 1.); + } + } } diff --git a/node-graph/libraries/raster-kernels/src/raster_effects.rs b/node-graph/libraries/raster-kernels/src/raster_effects.rs index a66750cbb5..fdca484817 100644 --- a/node-graph/libraries/raster-kernels/src/raster_effects.rs +++ b/node-graph/libraries/raster-kernels/src/raster_effects.rs @@ -1949,7 +1949,8 @@ mod tests { blue: identity, alpha: LevelsRecord { output_minimums: 50., ..identity }, })); - for adjustment in [Adjustment::MakeOpaque, raised_alpha] { + let make_opaque = Adjustment::MakeOpaque { strength: 100. }; + for adjustment in [make_opaque.clone(), raised_alpha] { let [empty, translucent, colored_without_alpha] = adjusted(std::slice::from_ref(&adjustment)); assert_eq!((empty, colored_without_alpha), ([0.; 4], [0.; 4]), "{adjustment:?}"); assert!(translucent[3] > 0.5, "{adjustment:?}"); @@ -1960,7 +1961,7 @@ mod tests { alpha: TransferCurve::new(vec![DVec2::ZERO, DVec2::new(1., 0.)]), ..Default::default() })); - assert_eq!(adjusted(&[emptied, Adjustment::MakeOpaque]), [[0.; 4]; 3]); + assert_eq!(adjusted(&[emptied, make_opaque]), [[0.; 4]; 3]); } #[test] diff --git a/node-graph/libraries/rendering/src/renderer.rs b/node-graph/libraries/rendering/src/renderer.rs index 11d357c364..1f9e0fa529 100644 --- a/node-graph/libraries/rendering/src/renderer.rs +++ b/node-graph/libraries/rendering/src/renderer.rs @@ -6839,9 +6839,14 @@ mod tests { assert!(invert.contains(r#"