Skip to content

Commit 910fb54

Browse files
Add quantize time nodes (#3703)
add quantize time nodes
1 parent 5ace2c9 commit 910fb54

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

node-graph/nodes/gcore/src/animation.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
use core_types::{Ctx, ExtractAnimationTime, ExtractPointerPosition, ExtractRealTime};
2-
use glam::DVec2;
1+
use core_types::table::Table;
2+
use core_types::transform::Footprint;
3+
use core_types::uuid::NodeId;
4+
use core_types::{CloneVarArgs, Color, Context, Ctx, ExtractAll, ExtractAnimationTime, ExtractPointerPosition, ExtractRealTime, OwnedContextImpl};
5+
use glam::{DAffine2, DVec2};
6+
use graphic_types::{Artboard, Graphic, Vector, vector_types::GradientStops};
7+
use raster_types::{CPU, GPU, Raster};
38

49
const DAY: f64 = 1000. * 3600. * 24.;
510

@@ -54,6 +59,92 @@ fn animation_time(
5459
ctx.try_animation_time().unwrap_or_default() * rate
5560
}
5661

62+
#[node_macro::node(category("Animation"))]
63+
async fn quantize_real_time<T>(
64+
ctx: impl Ctx + ExtractAll + CloneVarArgs,
65+
#[implementations(
66+
Context -> bool,
67+
Context -> u32,
68+
Context -> u64,
69+
Context -> f32,
70+
Context -> f64,
71+
Context -> String,
72+
Context -> DAffine2,
73+
Context -> Footprint,
74+
Context -> DVec2,
75+
Context -> Vec<DVec2>,
76+
Context -> Vec<NodeId>,
77+
Context -> Vec<f64>,
78+
Context -> Vec<f32>,
79+
Context -> Vec<String>,
80+
Context -> Table<Vector>,
81+
Context -> Table<Graphic>,
82+
Context -> Table<Raster<CPU>>,
83+
Context -> Table<Raster<GPU>>,
84+
Context -> Table<Color>,
85+
Context -> Table<Artboard>,
86+
Context -> Table<GradientStops>,
87+
Context -> GradientStops,
88+
Context -> (),
89+
)]
90+
value: impl Node<'n, Context<'static>, Output = T>,
91+
#[default(1)]
92+
#[unit("sec")]
93+
quantum: f64,
94+
) -> T {
95+
let time = ctx.try_real_time().unwrap_or_default();
96+
let time = time / 1000.;
97+
let mut quantized_time = (time * quantum.recip()).round() / quantum.recip();
98+
if !quantized_time.is_finite() {
99+
quantized_time = time;
100+
}
101+
let quantized_time = quantized_time * 1000.;
102+
let new_context = OwnedContextImpl::from(ctx).with_real_time(quantized_time);
103+
value.eval(Some(new_context.into())).await
104+
}
105+
106+
#[node_macro::node(category("Animation"))]
107+
async fn quantize_animation_time<T>(
108+
ctx: impl Ctx + ExtractAll + CloneVarArgs,
109+
#[implementations(
110+
Context -> bool,
111+
Context -> u32,
112+
Context -> u64,
113+
Context -> f32,
114+
Context -> f64,
115+
Context -> String,
116+
Context -> DAffine2,
117+
Context -> Footprint,
118+
Context -> DVec2,
119+
Context -> Vec<DVec2>,
120+
Context -> Vec<NodeId>,
121+
Context -> Vec<f64>,
122+
Context -> Vec<f32>,
123+
Context -> Vec<String>,
124+
Context -> Table<Vector>,
125+
Context -> Table<Graphic>,
126+
Context -> Table<Raster<CPU>>,
127+
Context -> Table<Raster<GPU>>,
128+
Context -> Table<Color>,
129+
Context -> Table<Artboard>,
130+
Context -> Table<GradientStops>,
131+
Context -> GradientStops,
132+
Context -> (),
133+
)]
134+
value: impl Node<'n, Context<'static>, Output = T>,
135+
#[default(1)]
136+
#[unit("sec")]
137+
quantum: f64,
138+
) -> T {
139+
let time = ctx.try_animation_time().unwrap_or_default();
140+
let mut quantized_time = (time * quantum.recip()).round() / quantum.recip();
141+
if !quantized_time.is_finite() {
142+
quantized_time = time;
143+
}
144+
let new_context = OwnedContextImpl::from(ctx).with_animation_time(quantized_time);
145+
value.eval(Some(new_context.into())).await
146+
}
147+
57148
/// Produces the current position of the user's pointer within the document canvas.
58149
#[node_macro::node(category("Animation"))]
59150
fn pointer_position(ctx: impl Ctx + ExtractPointerPosition) -> DVec2 {

0 commit comments

Comments
 (0)