mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 07:24:56 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6eb5ad1059 |
No files matched your search
@@ -4,5 +4,6 @@ mod mandelbrot_v1;
|
||||
mod mandelbrot_v2;
|
||||
mod mix_colors_from_mix_node;
|
||||
mod noise_pattern_v1;
|
||||
mod noise_pattern_v2;
|
||||
mod pattern_square;
|
||||
mod renames;
|
||||
@@ -0,0 +1,185 @@
|
||||
use migration_core::attr::node::VISIBLE;
|
||||
use migration_core::attr::node::ui::POSITION;
|
||||
use migration_core::{Attributes, CHAIN_WIDTH, Downstream, NewNode, Node, NodeInput, OutputConnector, Position, decl, literal, migration};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Frozen mirror of the `TaggedValue` variants this migration writes.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
enum Tagged {
|
||||
Number(f64),
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
/// 'Noise Pattern' writes its value as the gray at that gamma-encoded level rather than as linear light, so whatever read it reads it through a
|
||||
/// 'Gamma Correction' at its default gamma of 2.2, which turns the new gray back into about the old one.
|
||||
#[migration("raster_nodes::std_nodes::NoisePatternNode"@2 => @3)]
|
||||
pub fn noise_pattern_v2(node: &Node, downstream: &mut Downstream, attributes: &mut Attributes) {
|
||||
let consumers = downstream.consumers(0);
|
||||
if consumers.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let gamma_correction = downstream.add_node(NewNode {
|
||||
declaration: decl!("raster_nodes::adjustments::GammaCorrectionNode"@1),
|
||||
inputs: vec![NodeInput::Node { id: node.id(), index: 0 }, literal(Tagged::Number(2.2)), literal(Tagged::Bool(false))],
|
||||
});
|
||||
for consumer in consumers {
|
||||
downstream.connect(OutputConnector { node: gamma_correction, output: 0 }, consumer);
|
||||
}
|
||||
|
||||
// A disabled pattern passes its primary input through, so its Gamma Correction is disabled too
|
||||
if !node.attributes().get_or(VISIBLE, true) {
|
||||
attributes.set(gamma_correction, VISIBLE, &false);
|
||||
}
|
||||
|
||||
// The Gamma Correction takes the pattern's place, with the pattern before it
|
||||
if let Some(stored) = node.attributes().get_typed::<Position>(POSITION) {
|
||||
attributes.set(gamma_correction, POSITION, &stored);
|
||||
}
|
||||
attributes.set(node.id(), POSITION, &Position::Relative([-CHAIN_WIDTH, 0]));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use document_graph_storage::{Implementation, Network, NetworkId, NodeId, ROOT_NETWORK, TimeStamp};
|
||||
use migration_core::Declaration;
|
||||
use migration_core::attr::node::ui::IS_LAYER;
|
||||
use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo};
|
||||
|
||||
const NOISE_PATTERN: Declaration = decl!("raster_nodes::std_nodes::NoisePatternNode"@2);
|
||||
const GAMMA_CORRECTION: Declaration = decl!("raster_nodes::adjustments::GammaCorrectionNode"@1);
|
||||
const CONSUMER: Declaration = decl!("migration_testing::consumer"@1);
|
||||
|
||||
fn contract() -> SourceContract {
|
||||
SourceContract::new(15, |random, _| literal(Tagged::Number(random.below(10) as f64)))
|
||||
}
|
||||
|
||||
fn noise_inputs() -> Vec<NodeInput> {
|
||||
(0..15).map(|slot| literal(Tagged::Number(slot as f64))).collect()
|
||||
}
|
||||
|
||||
fn wire(id: NodeId) -> NodeInput {
|
||||
NodeInput::Node { id, index: 0 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewrites_history() {
|
||||
check_commutation(&NoisePatternV2, &contract());
|
||||
check_undo(&NoisePatternV2, &contract());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_reader_reads_the_pattern_through_one_gamma_correction() {
|
||||
let mut fixture = Fixture::new(&NoisePatternV2);
|
||||
let noise = fixture.add(NOISE_PATTERN, noise_inputs());
|
||||
let both_slots = fixture.add(CONSUMER, vec![wire(noise), wire(noise)]);
|
||||
let second_slot = fixture.add(CONSUMER, vec![literal(Tagged::Number(0.)), wire(noise)]);
|
||||
fixture.export(wire(noise));
|
||||
let migrated = fixture.migrate(&NoisePatternV2).unwrap();
|
||||
|
||||
assert_eq!(fixture.declaration_of(&migrated, noise), Some(decl!("raster_nodes::std_nodes::NoisePatternNode"@3)));
|
||||
assert_eq!(fixture.inputs(&migrated, noise), noise_inputs());
|
||||
|
||||
let NodeInput::Node { id: gamma_correction, .. } = fixture.inputs(&migrated, second_slot)[1] else {
|
||||
panic!("the consumer should read a node")
|
||||
};
|
||||
assert_eq!(fixture.declaration_of(&migrated, gamma_correction), Some(GAMMA_CORRECTION));
|
||||
assert_eq!(fixture.inputs(&migrated, gamma_correction), [wire(noise), literal(Tagged::Number(2.2)), literal(Tagged::Bool(false))]);
|
||||
|
||||
assert_eq!(fixture.inputs(&migrated, both_slots), [wire(gamma_correction), wire(gamma_correction)]);
|
||||
assert_eq!(fixture.inputs(&migrated, second_slot)[0], literal(Tagged::Number(0.)));
|
||||
assert_eq!(migrated.networks[&ROOT_NETWORK].exports[0].target, Some(wire(gamma_correction)));
|
||||
assert_eq!(migrated.node_instances.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_disabled_pattern_is_read_through_a_disabled_gamma_correction() {
|
||||
let visibility_of_gamma_correction = |visible: bool| {
|
||||
let mut fixture = Fixture::new(&NoisePatternV2);
|
||||
let noise = fixture.add(NOISE_PATTERN, noise_inputs());
|
||||
if !visible {
|
||||
let attributes = fixture.registry.node_instances.get_mut(&noise).unwrap().attributes_mut();
|
||||
attributes.set_serialized(VISIBLE, &false, TimeStamp::ORIGIN).unwrap();
|
||||
}
|
||||
let consumer = fixture.add(CONSUMER, vec![wire(noise)]);
|
||||
let migrated = fixture.migrate(&NoisePatternV2).unwrap();
|
||||
|
||||
let NodeInput::Node { id: gamma_correction, .. } = fixture.inputs(&migrated, consumer)[0] else {
|
||||
panic!("the consumer should read a node")
|
||||
};
|
||||
migrated.node_instances[&gamma_correction].attributes().get_typed::<bool>(VISIBLE)
|
||||
};
|
||||
|
||||
// Visible is the default, so only a disabled pattern writes it
|
||||
assert_eq!(visibility_of_gamma_correction(false), Some(false));
|
||||
assert_eq!(visibility_of_gamma_correction(true), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unread_pattern_gains_no_gamma_correction() {
|
||||
let mut fixture = Fixture::new(&NoisePatternV2);
|
||||
let noise = fixture.add(NOISE_PATTERN, noise_inputs());
|
||||
let migrated = fixture.migrate(&NoisePatternV2).unwrap();
|
||||
|
||||
assert_eq!(fixture.declaration_of(&migrated, noise), Some(decl!("raster_nodes::std_nodes::NoisePatternNode"@3)));
|
||||
assert_eq!(migrated.node_instances.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pattern_in_a_subgraph_is_read_through_a_gamma_correction_inside_it() {
|
||||
let mut fixture = Fixture::new(&NoisePatternV2);
|
||||
|
||||
// A custom subgraph exporting the pattern, read by the root network's export
|
||||
let (subgraph, instance) = (NetworkId(100), NodeId(100));
|
||||
fixture.registry.networks.insert(subgraph, Network::default());
|
||||
let instance_node = document_graph_storage::Node::new(ROOT_NETWORK, Implementation::Network(subgraph), 0);
|
||||
fixture.registry.node_instances.insert(instance, instance_node);
|
||||
let noise = fixture.add_in(subgraph, NOISE_PATTERN, noise_inputs());
|
||||
fixture.export_from(subgraph, wire(noise));
|
||||
fixture.export(wire(instance));
|
||||
|
||||
let migrated = fixture.migrate(&NoisePatternV2).unwrap();
|
||||
|
||||
let Some(NodeInput::Node { id: gamma_correction, .. }) = migrated.networks[&subgraph].exports[0].target else {
|
||||
panic!("the subgraph's export should read a node")
|
||||
};
|
||||
assert_eq!(fixture.declaration_of(&migrated, gamma_correction), Some(GAMMA_CORRECTION));
|
||||
assert_eq!(migrated.node_instances[&gamma_correction].network(), subgraph);
|
||||
assert_eq!(fixture.inputs(&migrated, gamma_correction)[0], wire(noise));
|
||||
assert_eq!(migrated.networks[&ROOT_NETWORK].exports[0].target, Some(wire(instance)));
|
||||
}
|
||||
|
||||
/// The positions of the pattern and its Gamma Correction after migrating a pattern at `position`, read by a layer's chain or by another node.
|
||||
fn placed_gamma_correction(position: Position, read_by_layer: bool) -> [Option<Position>; 2] {
|
||||
let mut fixture = Fixture::new(&NoisePatternV2);
|
||||
let noise = fixture.add(NOISE_PATTERN, noise_inputs());
|
||||
let attributes = fixture.registry.node_instances.get_mut(&noise).unwrap().attributes_mut();
|
||||
attributes.set_serialized(POSITION, &position, TimeStamp::ORIGIN).unwrap();
|
||||
|
||||
let (reader, slot) = if read_by_layer {
|
||||
let layer = fixture.add(decl!("graphic_nodes::layer"@1), vec![literal(Tagged::Number(0.)), wire(noise)]);
|
||||
let attributes = fixture.registry.node_instances.get_mut(&layer).unwrap().attributes_mut();
|
||||
attributes.set_serialized(IS_LAYER, &true, TimeStamp::ORIGIN).unwrap();
|
||||
(layer, 1)
|
||||
} else {
|
||||
(fixture.add(CONSUMER, vec![wire(noise)]), 0)
|
||||
};
|
||||
|
||||
let migrated = fixture.migrate(&NoisePatternV2).unwrap();
|
||||
let NodeInput::Node { id: gamma_correction, .. } = fixture.inputs(&migrated, reader)[slot] else {
|
||||
panic!("slot {slot} of the reader should read a node")
|
||||
};
|
||||
assert_eq!(fixture.declaration_of(&migrated, gamma_correction), Some(GAMMA_CORRECTION));
|
||||
|
||||
[noise, gamma_correction].map(|node| migrated.node_instances[&node].attributes().get_typed::<Position>(POSITION))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_gamma_correction_takes_the_place_of_a_chained_or_placed_pattern() {
|
||||
let chained = Position::Relative([-CHAIN_WIDTH, 0]);
|
||||
for (position, read_by_layer) in [(chained, true), (Position::Absolute([5, 5]), false)] {
|
||||
assert_eq!(placed_gamma_correction(position, read_by_layer), [Some(chained), Some(position)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1901,8 +1901,13 @@ pub fn document_migration_upgrades(document: &mut DocumentMessageHandler, reset_
|
||||
continue;
|
||||
};
|
||||
|
||||
// The noise settings follow the Clip toggle, in the same order as the current inputs after the primary one
|
||||
// The primary input comes before the Clip toggle where there is one, and the noise settings follow it in the current inputs' order
|
||||
if let Some(clip_index) = clip_index {
|
||||
if *clip_index == 1
|
||||
&& let Some(primary) = old_inputs.first()
|
||||
{
|
||||
document.network_interface.set_input(&InputConnector::node_at_index(*node_id, 0), primary.clone(), network_path);
|
||||
}
|
||||
for (offset, input) in old_inputs.iter().skip(clip_index + 1).enumerate() {
|
||||
document.network_interface.set_input(&InputConnector::node_at_index(*node_id, 1 + offset), input.clone(), network_path);
|
||||
}
|
||||
@@ -1914,6 +1919,11 @@ pub fn document_migration_upgrades(document: &mut DocumentMessageHandler, reset_
|
||||
if *reference == DefinitionIdentifier::ProtoNode(graphene_std::raster_nodes::std_nodes::mandelbrot::IDENTIFIER) {
|
||||
move_mandelbrot_to_old_origin(document, node_id, network_path);
|
||||
}
|
||||
|
||||
// Only a Noise Pattern old enough to have the Clip toggle is surely from before it wrote gamma-encoded grays
|
||||
if *reference == DefinitionIdentifier::ProtoNode(graphene_std::raster_nodes::std_nodes::noise_pattern::IDENTIFIER) {
|
||||
read_noise_through_gamma_correction(document, node_id, network_path);
|
||||
}
|
||||
}
|
||||
|
||||
// Parameters that historically accepted fractional numbers are now integers (the polygon and star "Sides", the index,
|
||||
@@ -1986,6 +1996,9 @@ fn fill_square_with_pattern(document: &mut DocumentMessageHandler, pattern_node_
|
||||
.and_then(|wires| wires.get(&OutputConnector::node(*pattern_node_id, 0)))
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if downstream_consumers.is_empty() {
|
||||
return Some(());
|
||||
}
|
||||
let was_in_chain = document.network_interface.is_chain(pattern_node_id, network_path);
|
||||
|
||||
let template = |identifier| resolve_document_node_type(&DefinitionIdentifier::ProtoNode(identifier)).map(|definition| definition.default_node_template());
|
||||
@@ -2087,6 +2100,48 @@ fn move_mandelbrot_to_old_origin(document: &mut DocumentMessageHandler, mandelbr
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Keeps an old Noise Pattern's look now that it writes its value as the gray at that gamma-encoded level rather than as linear light,
|
||||
/// with a Gamma Correction at its default gamma of 2.2 turning the new gray back into about the old one wherever the pattern was read.
|
||||
fn read_noise_through_gamma_correction(document: &mut DocumentMessageHandler, noise_node_id: &NodeId, network_path: &[NodeId]) -> Option<()> {
|
||||
let downstream_consumers: Vec<InputConnector> = document
|
||||
.network_interface
|
||||
.outward_wires(network_path)
|
||||
.and_then(|wires| wires.get(&OutputConnector::node(*noise_node_id, 0)))
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if downstream_consumers.is_empty() {
|
||||
return Some(());
|
||||
}
|
||||
let was_in_chain = document.network_interface.is_chain(noise_node_id, network_path);
|
||||
let position = document.network_interface.position(noise_node_id, network_path);
|
||||
|
||||
let gamma_correction_template = resolve_document_node_type(&DefinitionIdentifier::ProtoNode(graphene_std::raster_nodes::adjustments::gamma_correction::IDENTIFIER))?.default_node_template();
|
||||
let gamma_correction_id = NodeId::new();
|
||||
document.network_interface.insert_node(gamma_correction_id, gamma_correction_template, network_path);
|
||||
|
||||
document
|
||||
.network_interface
|
||||
.set_input(&InputConnector::node_at_index(gamma_correction_id, 0), NodeInput::node(*noise_node_id, 0), network_path);
|
||||
for consumer in &downstream_consumers {
|
||||
document.network_interface.set_input(consumer, NodeInput::node(gamma_correction_id, 0), network_path);
|
||||
}
|
||||
|
||||
// A disabled pattern passes its primary input through, so its Gamma Correction is disabled too
|
||||
if !document.network_interface.is_visible(noise_node_id, network_path) {
|
||||
document.network_interface.set_visibility(&gamma_correction_id, network_path, false);
|
||||
}
|
||||
|
||||
// The Gamma Correction takes the pattern's place, and the pattern moves before it
|
||||
if was_in_chain {
|
||||
document.network_interface.force_set_upstream_to_chain(&gamma_correction_id, network_path);
|
||||
} else if let Some(position) = position {
|
||||
document.network_interface.set_absolute_position(&gamma_correction_id, position, network_path);
|
||||
document.network_interface.set_absolute_position(noise_node_id, position + IVec2::new(-7, 0), network_path);
|
||||
}
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// Converts a legacy stroke dash input (a `List<f64>`, single `f64`, or comma/space separated `String`) to the `DashPattern` value type.
|
||||
fn migrate_dash_input(input: &NodeInput) -> Option<NodeInput> {
|
||||
let NodeInput::Value { tagged_value, exposed } = input else { return None };
|
||||
@@ -6411,6 +6466,9 @@ mod tests {
|
||||
assert_eq!(value(read, 1), Some(TaggedValue::DVec2(DVec2::new(2. / 3., 0.5))));
|
||||
read = upstream(read, 0).expect("the origin transform should read the pattern");
|
||||
}
|
||||
if pattern == noise {
|
||||
read = upstream(read, 0).expect("the Gamma Correction should read the pattern");
|
||||
}
|
||||
assert_eq!(read, pattern);
|
||||
}
|
||||
|
||||
@@ -6424,6 +6482,130 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// Noise Pattern once wrote its value as linear light, so whatever read an old one reads it through a Gamma Correction giving back about the old gray
|
||||
#[tokio::test]
|
||||
async fn old_noise_patterns_are_read_through_a_gamma_correction() {
|
||||
use crate::messages::portfolio::document_storage_io::load_legacy_document;
|
||||
use crate::test_utils::test_prelude::*;
|
||||
use graph_craft::application_io::resource::HashMapResourceStorage;
|
||||
use graphene_std::raster_nodes::adjustments::gamma_correction;
|
||||
use graphene_std::raster_nodes::std_nodes::noise_pattern;
|
||||
use graphene_std::transform_nodes::transform;
|
||||
|
||||
let proto = |identifier| DefinitionIdentifier::ProtoNode(identifier);
|
||||
let mut editor = EditorTestUtils::create();
|
||||
editor.new_document().await;
|
||||
|
||||
// Old patterns read by two nodes and the export, in a layer's chain, disabled, and unread, plus a current one in the new look
|
||||
let read_thrice = editor.create_node_by_name_at(proto(noise_pattern::IDENTIFIER), 0, 0).await;
|
||||
let readers = [
|
||||
editor.create_node_by_name_at(proto(transform::IDENTIFIER), 10, 0).await,
|
||||
editor.create_node_by_name_at(proto(transform::IDENTIFIER), 10, 5).await,
|
||||
];
|
||||
let chained = editor.create_node_by_name_at(proto(noise_pattern::IDENTIFIER), 15, 20).await;
|
||||
let layer = editor.create_node_by_name_at(DefinitionIdentifier::Network("Merge".into()), 20, 20).await;
|
||||
let disabled = editor.create_node_by_name_at(proto(noise_pattern::IDENTIFIER), 0, 30).await;
|
||||
let disabled_reader = editor.create_node_by_name_at(proto(transform::IDENTIFIER), 10, 30).await;
|
||||
let disabled_primary = editor.create_node_by_name_at(proto(transform::IDENTIFIER), -10, 30).await;
|
||||
let unread = editor.create_node_by_name_at(proto(noise_pattern::IDENTIFIER), 0, 40).await;
|
||||
let unread_clipped = editor.create_node_by_name_at(proto(noise_pattern::IDENTIFIER), 0, 45).await;
|
||||
let current = editor.create_node_by_name_at(proto(noise_pattern::IDENTIFIER), 0, 50).await;
|
||||
let current_reader = editor.create_node_by_name_at(proto(transform::IDENTIFIER), 10, 50).await;
|
||||
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
for reader in readers {
|
||||
interface.set_input(&InputConnector::node_at_index(reader, 0), NodeInput::node(read_thrice, 0), &[]);
|
||||
}
|
||||
interface.create_wire(&OutputConnector::primary_output(read_thrice), &InputConnector::Export(0), &[]);
|
||||
interface.set_to_node_or_layer(&layer, &[], true);
|
||||
interface.create_wire(&OutputConnector::primary_output(chained), &InputConnector::layer_secondary_input(layer), &[]);
|
||||
interface.set_input(&InputConnector::node_at_index(disabled_reader, 0), NodeInput::node(disabled, 0), &[]);
|
||||
interface.set_input(&InputConnector::node_at_index(disabled, 0), NodeInput::node(disabled_primary, 0), &[]);
|
||||
interface.set_visibility(&disabled, &[], false);
|
||||
interface.set_input(&InputConnector::node_at_index(current_reader, 0), NodeInput::node(current, 0), &[]);
|
||||
assert!(interface.is_chain(&chained, &[]), "the pattern should be in the layer's chain: fixture is wrong");
|
||||
|
||||
// A pattern exported from a subgraph
|
||||
let subgraph = editor.create_empty_subgraph_at(0, 60).await;
|
||||
editor.enter_editable_network(subgraph).await;
|
||||
editor.handle_message(NodeGraphMessage::AddPrimaryExport).await;
|
||||
let nested = editor.create_node_by_name_at(proto(noise_pattern::IDENTIFIER), 10, 0).await;
|
||||
editor
|
||||
.handle_message(NodeGraphMessage::CreateWire {
|
||||
output_connector: OutputConnector::primary_output(nested),
|
||||
input_connector: InputConnector::Export(0),
|
||||
})
|
||||
.await;
|
||||
|
||||
// Save the old patterns as an older editor did, with their Clip toggle off except on one of the unread ones
|
||||
let document = editor.active_document_mut();
|
||||
let old_patterns = [
|
||||
(read_thrice, vec![], false),
|
||||
(chained, vec![], false),
|
||||
(disabled, vec![], false),
|
||||
(unread, vec![], false),
|
||||
(unread_clipped, vec![], true),
|
||||
(nested, vec![subgraph], false),
|
||||
];
|
||||
for (node_id, network_path, clip) in old_patterns {
|
||||
let mut inputs = document.network_interface.document_node(&node_id, &network_path).unwrap().inputs.clone();
|
||||
inputs.insert(1, NodeInput::value(TaggedValue::Bool(clip), false));
|
||||
let mut legacy = NodeTemplate { inputs, ..Default::default() };
|
||||
legacy.normalize_input_metadata();
|
||||
document.network_interface.replace_inputs(&node_id, &network_path, &mut legacy);
|
||||
}
|
||||
let serialized = document.serialize_document();
|
||||
|
||||
let opened = load_legacy_document(serialized, &HashMapResourceStorage::new(), false).expect("the document should open");
|
||||
let interface = &opened.network_interface;
|
||||
let upstream = |connector: InputConnector, network_path: &[NodeId]| interface.upstream_output_connector(&connector, network_path).and_then(|output| output.node_id());
|
||||
|
||||
// The Gamma Correction a reader of `pattern` reads instead, at the default gamma and reading the pattern
|
||||
let gamma_correction_between = |pattern: NodeId, reader: InputConnector, network_path: &[NodeId]| {
|
||||
let inserted = upstream(reader, network_path).expect("the reader should read a node");
|
||||
assert_eq!(interface.reference(&inserted, network_path), Some(proto(gamma_correction::IDENTIFIER)));
|
||||
|
||||
let node = interface.document_node(&inserted, network_path).unwrap();
|
||||
assert_eq!(node.input_value(gamma_correction::GammaInput), Some(&TaggedValue::Number(2.2)));
|
||||
assert_eq!(node.input_value(gamma_correction::InverseInput), Some(&TaggedValue::Bool(false)));
|
||||
assert_eq!(upstream(InputConnector::node_at_index(inserted, 0), network_path), Some(pattern));
|
||||
inserted
|
||||
};
|
||||
|
||||
// Every reader of the pattern read three times shares one Gamma Correction, which takes the pattern's place with the pattern before it
|
||||
let shared = gamma_correction_between(read_thrice, InputConnector::Export(0), &[]);
|
||||
for reader in readers {
|
||||
assert_eq!(upstream(InputConnector::node_at_index(reader, 0), &[]), Some(shared));
|
||||
}
|
||||
let position = |node_id: NodeId| interface.position(&node_id, &[]).expect("the node should have a position");
|
||||
assert_eq!(position(read_thrice) + IVec2::new(7, 0), position(shared));
|
||||
|
||||
let in_chain = gamma_correction_between(chained, InputConnector::layer_secondary_input(layer), &[]);
|
||||
assert!(interface.is_chain(&in_chain, &[]) && interface.is_chain(&chained, &[]), "the layer's chain should hold both");
|
||||
|
||||
// Only a disabled pattern's Gamma Correction is disabled too, and the pattern keeps the primary input it passes through
|
||||
let behind_disabled = gamma_correction_between(disabled, InputConnector::node_at_index(disabled_reader, 0), &[]);
|
||||
assert!(!interface.is_visible(&behind_disabled, &[]) && interface.is_visible(&shared, &[]));
|
||||
assert_eq!(upstream(InputConnector::node_at_index(disabled, 0), &[]), Some(disabled_primary));
|
||||
|
||||
gamma_correction_between(nested, InputConnector::Export(0), &[subgraph]);
|
||||
assert_eq!(upstream(InputConnector::node_at_index(current_reader, 0), &[]), Some(current));
|
||||
|
||||
// Unread patterns gain nothing reading them, clipped or not
|
||||
let read = |pattern: NodeId| interface.document_network().nodes.values().any(|node| node.inputs.iter().any(|input| input.as_node() == Some(pattern)));
|
||||
assert!(!read(unread) && !read(unread_clipped));
|
||||
|
||||
// Reopening the upgraded document, as restoring an autosave does, inserts no more
|
||||
let gamma_corrections = |document: &DocumentMessageHandler| {
|
||||
let implementation = DocumentNodeImplementation::ProtoNode(gamma_correction::IDENTIFIER);
|
||||
let nodes = document.network_interface.document_network().recursive_nodes();
|
||||
nodes.filter(|(_, node, _)| node.implementation == implementation).count()
|
||||
};
|
||||
assert_eq!(gamma_corrections(&opened), 4);
|
||||
let reopened = load_legacy_document(opened.serialize_document(), &HashMapResourceStorage::new(), false).expect("the upgraded document should open");
|
||||
assert_eq!(gamma_corrections(&reopened), 4);
|
||||
}
|
||||
|
||||
// The demo draws its patterns through layers and adjustments, which must all take the generators the patterns now are
|
||||
#[tokio::test]
|
||||
async fn marbled_mandelbrot_renders_after_migration() {
|
||||
|
||||
@@ -24,7 +24,7 @@ pub fn render_pattern(pattern: &GeneratorPattern, area: [DVec2; 2], width: u32,
|
||||
let interior_test = setup.has_interior_test() && resolves_in_32_bits(plane_area, plane_texel);
|
||||
fill_rows(&mut image.data, width as usize, |y, row| {
|
||||
row.iter_mut().enumerate().for_each(|(x, pixel)| {
|
||||
*pixel = mandelbrot_gray(setup.sample(setup.plane_point(pixel_center(x, y)), reference.as_ref(), interior_test));
|
||||
*pixel = pattern_gray(setup.sample(setup.plane_point(pixel_center(x, y)), reference.as_ref(), interior_test));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -60,18 +60,18 @@ pub fn pattern_sampler(pattern: &GeneratorPattern) -> impl Fn(DVec2) -> Color +
|
||||
};
|
||||
|
||||
move |point| match &sampler {
|
||||
Sampler::Noise(noise) => Color::from_luminance(noise.sample(point)),
|
||||
Sampler::Mandelbrot(setup) => mandelbrot_gray(setup.sample(setup.plane_point(point), None, false)),
|
||||
Sampler::Noise(noise) => pattern_gray(noise.sample(point)),
|
||||
Sampler::Mandelbrot(setup) => pattern_gray(setup.sample(setup.plane_point(point), None, false)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The Mandelbrot brightness as a gray, gamma-encoded so a Gradient Map takes it as the position along its gradient.
|
||||
fn mandelbrot_gray(brightness: f32) -> Color {
|
||||
let value = brightness.clamp(0., 1.);
|
||||
/// A pattern's value from 0 to 1 as the gray at that gamma-encoded level, so middle values show as middle gray and a Gradient Map takes the value as the position along its gradient.
|
||||
fn pattern_gray(value: f32) -> Color {
|
||||
let value = value.clamp(0., 1.);
|
||||
Color::from_gamma_srgb_channels(value, value, value, 1.)
|
||||
}
|
||||
|
||||
/// Samples a noise pattern's luminance, from 0 to 1, at positions in its space.
|
||||
/// Samples a noise pattern's value, from 0 to 1, at positions in its space.
|
||||
struct NoiseSampler {
|
||||
noise: fastnoise_lite::FastNoiseLite,
|
||||
white_noise_seed: Option<i32>,
|
||||
@@ -221,6 +221,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pattern_values_show_as_the_gray_at_that_level() {
|
||||
for value in [0., 0.25, 0.5, 0.75, 1.] {
|
||||
let [red, green, blue, alpha] = pattern_gray(value).to_gamma_srgb_channels();
|
||||
assert!([red, green, blue].iter().all(|channel| (channel - value).abs() < 1e-5) && alpha == 1., "{value} shows as {red}");
|
||||
}
|
||||
|
||||
// Values out of range clamp
|
||||
assert_eq!(pattern_gray(1.5).to_gamma_srgb_channels()[0], 1.);
|
||||
assert_eq!(pattern_gray(-0.5).to_gamma_srgb_channels()[0], 0.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn white_noise_is_the_same_wherever_and_however_finely_it_is_sampled() {
|
||||
let white_noise = |seed| {
|
||||
|
||||
@@ -8,10 +8,7 @@ fn vertex(@builtin(vertex_index) index: u32) -> @builtin(position) vec4<f32> {
|
||||
return vec4<f32>(corner * 2. - 1., 0., 1.);
|
||||
}
|
||||
|
||||
// Clamped to the range the CPU encodes, which takes NaN as 0
|
||||
fn linear_to_srgb(unclamped: f32) -> f32 {
|
||||
let linear = select(0., min(unclamped, 1.), unclamped >= 0.);
|
||||
let low = linear * 12.92;
|
||||
let high = 1.055 * pow(linear, 1. / 2.4) - 0.055;
|
||||
return select(high, low, linear <= 0.0031308);
|
||||
// A value from 0 to 1 written directly as the gray's gamma-encoded level, clamped to the range the CPU encodes, which takes NaN as 0
|
||||
fn gray_level(unclamped: f32) -> f32 {
|
||||
return select(0., min(unclamped, 1.), unclamped >= 0.);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ fn generate(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> {
|
||||
if parameters.domain_warp != 0u {
|
||||
fnlDomainWarp2D(parameters.noise, &x, &y);
|
||||
}
|
||||
let luminance = (fnlGetNoise2D(parameters.noise, x, y) + 1.) * 0.5;
|
||||
let value = (fnlGetNoise2D(parameters.noise, x, y) + 1.) * 0.5;
|
||||
|
||||
return vec4<f32>(vec3<f32>(linear_to_srgb(luminance)), 1.);
|
||||
return vec4<f32>(vec3<f32>(gray_level(value)), 1.);
|
||||
}
|
||||
@@ -21,9 +21,9 @@ fn generate(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> {
|
||||
let hash = pcg3d(vec3<u32>(bitcast<vec2<u32>>(cell), bitcast<u32>(parameters.noise.seed)));
|
||||
|
||||
// The top 24 bits, which an f32 holds exactly
|
||||
let luminance = f32(hash.x >> 8u) / 16777216.;
|
||||
let value = f32(hash.x >> 8u) / 16777216.;
|
||||
|
||||
return vec4<f32>(vec3<f32>(linear_to_srgb(luminance)), 1.);
|
||||
return vec4<f32>(vec3<f32>(gray_level(value)), 1.);
|
||||
}
|
||||
|
||||
// PCG3D from Jarzynski and Olano's "Hash Functions for GPU Rendering", which spreads every input bit across the result
|
||||
|
||||
@@ -200,7 +200,7 @@ fn image(
|
||||
}
|
||||
|
||||
/// Generates customizable procedural noise patterns.
|
||||
#[node_macro::node(category("Raster"), icon("NodeNoisePattern"), version(2))]
|
||||
#[node_macro::node(category("Raster"), icon("NodeNoisePattern"), version(3))]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn noise_pattern(
|
||||
_: impl Ctx,
|
||||
|
||||
Reference in new issue
Block a user