diff --git a/node-graph/gcore/src/context.rs b/node-graph/gcore/src/context.rs index bbe1c243e..ba4836d04 100644 --- a/node-graph/gcore/src/context.rs +++ b/node-graph/gcore/src/context.rs @@ -27,7 +27,7 @@ pub trait ExtractAnimationTime { } pub trait ExtractIndex { - fn try_index(&self) -> Option>; + fn try_index(&self) -> Option>; } // Consider returning a slice or something like that @@ -175,7 +175,7 @@ impl ExtractAnimationTime for Option { } } impl ExtractIndex for Option { - fn try_index(&self) -> Option> { + fn try_index(&self) -> Option> { self.as_ref().and_then(|x| x.try_index()) } } @@ -212,7 +212,7 @@ impl ExtractAnimationTime for Arc { } } impl ExtractIndex for Arc { - fn try_index(&self) -> Option> { + fn try_index(&self) -> Option> { (**self).try_index() } } @@ -268,8 +268,8 @@ impl ExtractRealTime for ContextImpl<'_> { } } impl ExtractIndex for ContextImpl<'_> { - fn try_index(&self) -> Option> { - self.index.clone() + fn try_index(&self) -> Option> { + self.index.clone().map(|x| x.into_iter()) } } impl ExtractVarArgs for ContextImpl<'_> { @@ -304,8 +304,8 @@ impl ExtractAnimationTime for OwnedContextImpl { } } impl ExtractIndex for OwnedContextImpl { - fn try_index(&self) -> Option> { - self.index.clone() + fn try_index(&self) -> Option> { + self.index.clone().map(|x| x.into_iter()) } } impl ExtractVarArgs for OwnedContextImpl { @@ -418,7 +418,7 @@ impl OwnedContextImpl { footprint, varargs: None, parent, - index, + index: index.map(|x| x.collect()), real_time, animation_time, } diff --git a/node-graph/gcore/src/vector/algorithms/instance.rs b/node-graph/gcore/src/vector/algorithms/instance.rs index b5586203b..9ddb290e9 100644 --- a/node-graph/gcore/src/vector/algorithms/instance.rs +++ b/node-graph/gcore/src/vector/algorithms/instance.rs @@ -2,7 +2,7 @@ use crate::gradient::GradientStops; use crate::raster_types::{CPU, Raster}; use crate::table::{Table, TableRowRef}; use crate::vector::Vector; -use crate::{CloneVarArgs, Context, Ctx, ExtractAll, ExtractIndex, ExtractVarArgs, Graphic, InjectIndex, InjectVarArgs, OwnedContextImpl}; +use crate::{CloneVarArgs, Context, Ctx, ExtractAll, ExtractIndex, ExtractVarArgs, Graphic, InjectVarArgs, OwnedContextImpl}; use glam::DVec2; use graphene_core_shaders::color::Color; @@ -19,7 +19,7 @@ impl std::hash::Hash for HashableDVec2 { #[node_macro::node(name("Instance on Points"), category("Instancing"), path(graphene_core::vector))] async fn instance_on_points + Default + Send + Clone + 'static>( - ctx: impl ExtractAll + CloneVarArgs + Sync + Ctx + InjectIndex + InjectVarArgs, + ctx: impl ExtractAll + CloneVarArgs + Sync + Ctx + InjectVarArgs, points: Table, #[implementations( Context -> Table, @@ -63,7 +63,7 @@ async fn instance_on_points + Default + Send + Clone + 'static> #[node_macro::node(category("Instancing"), path(graphene_core::vector))] async fn instance_repeat + Default + Send + Clone + 'static>( - ctx: impl ExtractAll + CloneVarArgs + Ctx + InjectIndex, + ctx: impl ExtractAll + CloneVarArgs + Ctx, #[implementations( Context -> Table, Context -> Table, @@ -106,9 +106,15 @@ async fn instance_position(ctx: impl Ctx + ExtractVarArgs) -> DVec2 { // TODO: Make this return a u32 instead of an f64, but we ned to improve math-related compatibility with integer types first. #[node_macro::node(category("Instancing"), path(graphene_core::vector))] async fn instance_index(ctx: impl Ctx + ExtractIndex, _primary: (), loop_level: u32) -> f64 { - ctx.try_index() - .and_then(|indexes| indexes.get(indexes.len().wrapping_sub(1).wrapping_sub(loop_level as usize)).copied()) - .unwrap_or_default() as f64 + let Some(index_iter) = ctx.try_index() else { return 0. }; + let mut last = 0; + for (i, index) in index_iter.enumerate() { + if i == loop_level as usize { + return index as f64; + } + last = index; + } + last as f64 } #[cfg(test)]