Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mirror node #1632

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,19 @@ fn static_nodes() -> Vec<DocumentNodeDefinition> {
properties: node_properties::image_color_palette,
..Default::default()
},
DocumentNodeDefinition {
name: "Mirror",
category: "Vector",
implementation: DocumentNodeImplementation::proto("graphene_core::vector::MirrorNode<_, _>"),
inputs: vec![
DocumentInputType::value("Vector Data", TaggedValue::VectorData(graphene_core::vector::VectorData::empty()), true),
DocumentInputType::value("Mirror X", TaggedValue::Bool(true), false),
DocumentInputType::value("Mirror Y", TaggedValue::Bool(false), false),
],
outputs: vec![DocumentOutputType::new("Vector", FrontendGraphDataType::Subpath)],
properties: node_properties::mirror_properties,
..Default::default()
},
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2299,3 +2299,10 @@ pub fn image_color_palette(document_node: &DocumentNode, node_id: NodeId, _conte

vec![LayoutGroup::Row { widgets: size }]
}

pub fn mirror_properties(document_node: &DocumentNode, node_id: NodeId, _context: &mut NodePropertiesContext) -> Vec<LayoutGroup> {
let mirror_x = bool_widget(document_node, node_id, 1, "Mirror X", true);
let mirror_y = bool_widget(document_node, node_id, 2, "Mirror Y", true);

vec![LayoutGroup::Row { widgets: mirror_x }, LayoutGroup::Row { widgets: mirror_y }]
}
13 changes: 13 additions & 0 deletions node-graph/gcore/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ pub trait Transform {
self.transform().transform_vector2((0., 1.).into()).length(),
)
}
fn pivot_mut(&mut self) -> Option<&mut Option<DVec2>> {
None
}
fn set_pivot(&mut self, pivot: DVec2) -> () {
if let Some(pivot_ref) = self.pivot_mut() {
*pivot_ref = Some(pivot);
}
}
}

pub trait TransformMut: Transform {
Expand Down Expand Up @@ -118,6 +126,9 @@ impl Transform for VectorData {
fn local_pivot(&self, pivot: DVec2) -> DVec2 {
self.local_pivot(pivot)
}
fn pivot_mut(&mut self) -> Option<&mut Option<DVec2>> {
Some(&mut self.pivot)
}
}
impl TransformMut for VectorData {
fn transform_mut(&mut self) -> &mut DAffine2 {
Expand Down Expand Up @@ -259,6 +270,8 @@ where
let data_transform = data.transform_mut();
*data_transform = modification * (*data_transform);

data.set_pivot(pivot);

data
}
#[derive(Debug, Clone, Copy)]
Expand Down
2 changes: 2 additions & 0 deletions node-graph/gcore/src/vector/vector_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct VectorData {
/// A list of all manipulator groups (referenced in `subpaths`) that have smooth handles (where their handles are colinear, or locked to 180° angles from one another)
/// This gets read in `graph_operation_message_handler.rs` by calling `inputs.as_mut_slice()` (search for the string `"Shape does not have subpath and mirror angle inputs"` to find it).
pub mirror_angle: Vec<ManipulatorGroupId>,
pub pivot: Option<DVec2>,
}

impl core::hash::Hash for VectorData {
Expand All @@ -40,6 +41,7 @@ impl VectorData {
style: PathStyle::new(Some(Stroke::new(Some(Color::BLACK), 0.)), super::style::Fill::None),
alpha_blending: AlphaBlending::new(),
mirror_angle: Vec::new(),
pivot: None,
}
}

Expand Down
39 changes: 39 additions & 0 deletions node-graph/gcore/src/vector/vector_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,42 @@ async fn morph<SourceFuture: Future<Output = VectorData>, TargetFuture: Future<O

current
}

#[derive(Debug, Clone, Copy)]
pub struct MirrorNode<MirrorX, MirrorY> {
mirror_x: MirrorX,
mirror_y: MirrorY,
}

#[node_macro::node_fn(MirrorNode)]
fn mirror_node(vector_data: VectorData, mirror_x: bool, mirror_y: bool) -> VectorData {
if !mirror_x && !mirror_y {
return vector_data;
}

let pivot = vector_data.layerspace_pivot(vector_data.pivot.unwrap_or(DVec2::splat(0.5)));
let translate = DAffine2::from_translation(pivot);
let mut new_vector_data = vector_data.clone();

let mirror = |x: f64, y: f64| {
let mirror_transform = DAffine2::from_scale(DVec2::new(x, y));

let mut subpaths = vector_data.subpaths.clone();
subpaths.iter_mut().for_each(|subpath| {
subpath.apply_transform(translate * mirror_transform * translate.inverse());
});
subpaths
};

let mut final_subpaths = vector_data.subpaths.clone();
if mirror_x && mirror_y {
final_subpaths.extend(mirror(-1., 1.));
final_subpaths.extend(mirror(1., -1.));
final_subpaths.extend(mirror(-1., -1.));
} else {
final_subpaths.extend(mirror(if mirror_x { -1. } else { 1. }, if mirror_y { -1. } else { 1. }));
};

new_vector_data.subpaths = final_subpaths;
new_vector_data
}
1 change: 1 addition & 0 deletions node-graph/interpreted-executor/src/node_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
register_node!(graphene_core::vector::RepeatNode<_, _>, input: VectorData, params: [DVec2, u32]),
register_node!(graphene_core::vector::BoundingBoxNode, input: VectorData, params: []),
register_node!(graphene_core::vector::CircularRepeatNode<_, _, _>, input: VectorData, params: [f64, f64, u32]),
register_node!(graphene_core::vector::MirrorNode<_, _>, input: VectorData, params: [bool, bool]),
vec![(
ProtoNodeIdentifier::new("graphene_core::transform::CullNode<_>"),
|args| {
Expand Down