Skip to content

Commit

Permalink
[editor] fix dashed move preview artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed Oct 14, 2024
1 parent 8de4bce commit 2024475
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
18 changes: 11 additions & 7 deletions crates/ctl-core/src/interpolation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ impl<T: 'static + Interpolatable> Interpolation<T> {

/// Get the full path of the interpolation with the given `resolution` per segment.
pub fn get_path(&self, resolution: usize) -> impl Iterator<Item = T> + '_ {
self.segments.iter().flat_map(move |segment| {
(0..segment.num_intervals()).flat_map(move |interval| {
(0..resolution).flat_map(move |i| {
let t = i as f32 / resolution as f32;
segment.get(interval, r32(t))
self.segments
.first()
.and_then(|segment| segment.get(0, Time::ZERO))
.into_iter()
.chain(self.segments.iter().flat_map(move |segment| {
(0..segment.num_intervals()).flat_map(move |interval| {
(1..=resolution).flat_map(move |i| {
let t = i as f32 / resolution as f32;
segment.get(interval, r32(t))
})
})
})
})
}))
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/render/editor/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,37 +197,42 @@ impl EditorRender {
};

// A dashed line moving through the waypoints to show general direction
const RESOLUTION: usize = 10;
const RESOLUTION: usize = 5;
// TODO: cache curve
let curve = event.light.movement.bake();
let mut positions: Vec<draw2d::ColoredVertex> = curve
.get_path(RESOLUTION)
.enumerate()
.map(|(i, transform)| {
.filter_map(|(i, transform)| {
let movement = &event.light.movement;
let segment = i / RESOLUTION;
let t = (i % RESOLUTION) as f32 / RESOLUTION.saturating_sub(1) as f32;
let t = (i % RESOLUTION) as f32 / RESOLUTION as f32;
let a = movement
.get_time(WaypointId::Frame(segment).prev().unwrap())
.unwrap_or(Time::ZERO);
let b = movement
.get_time(WaypointId::Frame(segment))
.unwrap_or(Time::ZERO);
let beat = a + (b - a) * r32(t);
draw2d::ColoredVertex {
let alpha = visibility(beat);
(alpha > 0.01).then_some(draw2d::ColoredVertex {
a_pos: transform.translation.as_f32(),
a_color: crate::util::with_alpha(color, visibility(beat)),
}
a_color: crate::util::with_alpha(color, alpha),
})
})
.collect();

positions.dedup_by_key(|vertex| vertex.a_pos);
let options = util::DashRenderOptions {
width: 0.15,
color,
dash_length: 0.1,
space_length: 0.2,
};

positions.dedup_by(|a, b| {
(a.a_pos - b.a_pos).len_sqr()
< (options.dash_length + options.space_length).sqr()
});

if let Some(&to) = positions.get(1) {
let pos = positions.first_mut().unwrap();
let period = options.dash_length + options.space_length;
Expand Down
1 change: 0 additions & 1 deletion src/render/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub struct TextRenderOptions {
#[derive(Debug, Clone, Copy)]
pub struct DashRenderOptions {
pub width: f32,
pub color: Color,
pub dash_length: f32,
pub space_length: f32,
}
Expand Down

0 comments on commit 2024475

Please sign in to comment.