From 15c3bac67a4c17b125acfac4dcbc0501ebde1699 Mon Sep 17 00:00:00 2001 From: Timon Date: Wed, 28 Jan 2026 12:24:43 +0000 Subject: [PATCH] add quantize time nodes --- node-graph/nodes/gcore/src/animation.rs | 95 ++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/node-graph/nodes/gcore/src/animation.rs b/node-graph/nodes/gcore/src/animation.rs index 506b0581c4..4d004bb748 100644 --- a/node-graph/nodes/gcore/src/animation.rs +++ b/node-graph/nodes/gcore/src/animation.rs @@ -1,5 +1,10 @@ -use core_types::{Ctx, ExtractAnimationTime, ExtractPointerPosition, ExtractRealTime}; -use glam::DVec2; +use core_types::table::Table; +use core_types::transform::Footprint; +use core_types::uuid::NodeId; +use core_types::{CloneVarArgs, Color, Context, Ctx, ExtractAll, ExtractAnimationTime, ExtractPointerPosition, ExtractRealTime, OwnedContextImpl}; +use glam::{DAffine2, DVec2}; +use graphic_types::{Artboard, Graphic, Vector, vector_types::GradientStops}; +use raster_types::{CPU, GPU, Raster}; const DAY: f64 = 1000. * 3600. * 24.; @@ -54,6 +59,92 @@ fn animation_time( ctx.try_animation_time().unwrap_or_default() * rate } +#[node_macro::node(category("Animation"))] +async fn quantize_real_time( + ctx: impl Ctx + ExtractAll + CloneVarArgs, + #[implementations( + Context -> bool, + Context -> u32, + Context -> u64, + Context -> f32, + Context -> f64, + Context -> String, + Context -> DAffine2, + Context -> Footprint, + Context -> DVec2, + Context -> Vec, + Context -> Vec, + Context -> Vec, + Context -> Vec, + Context -> Vec, + Context -> Table, + Context -> Table, + Context -> Table>, + Context -> Table>, + Context -> Table, + Context -> Table, + Context -> Table, + Context -> GradientStops, + Context -> (), + )] + value: impl Node<'n, Context<'static>, Output = T>, + #[default(1)] + #[unit("sec")] + quantum: f64, +) -> T { + let time = ctx.try_real_time().unwrap_or_default(); + let time = time / 1000.; + let mut quantized_time = (time * quantum.recip()).round() / quantum.recip(); + if !quantized_time.is_finite() { + quantized_time = time; + } + let quantized_time = quantized_time * 1000.; + let new_context = OwnedContextImpl::from(ctx).with_real_time(quantized_time); + value.eval(Some(new_context.into())).await +} + +#[node_macro::node(category("Animation"))] +async fn quantize_animation_time( + ctx: impl Ctx + ExtractAll + CloneVarArgs, + #[implementations( + Context -> bool, + Context -> u32, + Context -> u64, + Context -> f32, + Context -> f64, + Context -> String, + Context -> DAffine2, + Context -> Footprint, + Context -> DVec2, + Context -> Vec, + Context -> Vec, + Context -> Vec, + Context -> Vec, + Context -> Vec, + Context -> Table, + Context -> Table, + Context -> Table>, + Context -> Table>, + Context -> Table, + Context -> Table, + Context -> Table, + Context -> GradientStops, + Context -> (), + )] + value: impl Node<'n, Context<'static>, Output = T>, + #[default(1)] + #[unit("sec")] + quantum: f64, +) -> T { + let time = ctx.try_animation_time().unwrap_or_default(); + let mut quantized_time = (time * quantum.recip()).round() / quantum.recip(); + if !quantized_time.is_finite() { + quantized_time = time; + } + let new_context = OwnedContextImpl::from(ctx).with_animation_time(quantized_time); + value.eval(Some(new_context.into())).await +} + /// Produces the current position of the user's pointer within the document canvas. #[node_macro::node(category("Animation"))] fn pointer_position(ctx: impl Ctx + ExtractPointerPosition) -> DVec2 {