From eb8605a0b3cd2a03895f250daa2ed21c1a8fbcab Mon Sep 17 00:00:00 2001 From: Firestar99 Date: Thu, 5 Jun 2025 14:21:26 +0200 Subject: [PATCH] Instances extension --- node-graph/gcore/src/instances.rs | 55 ++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/node-graph/gcore/src/instances.rs b/node-graph/gcore/src/instances.rs index 8c90777aa8..a65a0c102c 100644 --- a/node-graph/gcore/src/instances.rs +++ b/node-graph/gcore/src/instances.rs @@ -18,11 +18,24 @@ pub struct Instances { impl Instances { pub fn new(instance: T) -> Self { + Self::from(Instance::new(instance)) + } + + pub fn empty() -> Self { Self { - instance: vec![instance], - transform: vec![DAffine2::IDENTITY], - alpha_blending: vec![AlphaBlending::default()], - source_node_id: vec![None], + instance: Vec::new(), + transform: Vec::new(), + alpha_blending: Vec::new(), + source_node_id: Vec::new(), + } + } + + pub fn with_capacity(capacity: usize) -> Self { + Self { + instance: Vec::with_capacity(capacity), + transform: Vec::with_capacity(capacity), + alpha_blending: Vec::with_capacity(capacity), + source_node_id: Vec::with_capacity(capacity), } } @@ -121,7 +134,7 @@ impl Default for Instances { } } -impl core::hash::Hash for Instances { +impl Hash for Instances { fn hash(&self, state: &mut H) { for instance in &self.instance { instance.hash(state); @@ -140,6 +153,29 @@ unsafe impl StaticType for Instances { type Static = Instances; } +impl From> for Instances { + fn from(instance: Instance) -> Self { + Self { + instance: vec![instance.instance], + transform: vec![instance.transform], + alpha_blending: vec![instance.alpha_blending], + source_node_id: vec![instance.source_node_id], + } + } +} + +impl FromIterator> for Instances { + fn from_iter>>(iter: I) -> Self { + let iter = iter.into_iter(); + let (lower, _) = iter.size_hint(); + let mut instances = Self::with_capacity(lower); + for instance in iter { + instances.push(instance); + } + instances + } +} + fn one_daffine2_default() -> Vec { vec![DAffine2::IDENTITY] } @@ -189,6 +225,15 @@ pub struct Instance { } impl Instance { + pub fn new(instance: T) -> Self { + Self { + instance, + transform: DAffine2::IDENTITY, + alpha_blending: AlphaBlending::default(), + source_node_id: None, + } + } + pub fn to_graphic_element(self) -> Instance where T: Into,