aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-fx/src/op5.rs100
1 files changed, 71 insertions, 29 deletions
diff --git a/crates/fparkan-fx/src/op5.rs b/crates/fparkan-fx/src/op5.rs
index 849dd8b4..5ef57368 100644
--- a/crates/fparkan-fx/src/op5.rs
+++ b/crates/fparkan-fx/src/op5.rs
@@ -5,20 +5,23 @@ use crate::{FxCommand, FxOpcode};
/// One constant-width trail layer, as used by the training lasers.
#[derive(Clone, Copy, Debug)]
pub struct TrailLayer {
- pub width: f32,
- pub texture_length: f32,
+ width: f32,
+ texture_length: f32,
+ segment_spacing: f32,
+ pub max_segments: u32,
}
impl TrailLayer {
/// Decodes the constant-width, full-phase form of the command.
pub fn from_command(command: &FxCommand, uniform_scale: f32) -> Option<Self> {
- let read = |offset| {
+ let word = |offset| {
command
.raw_body
.get(offset..offset + 4)
.and_then(|bytes| bytes.try_into().ok())
- .map(f32::from_le_bytes)
+ .map(u32::from_le_bytes)
};
+ let read = |offset| word(offset).map(f32::from_bits);
if command.opcode != FxOpcode::Op5
|| command.native_flag
|| read(0)? != 1.0
@@ -32,19 +35,26 @@ impl TrailLayer {
}
let width = read(20)? * uniform_scale;
let texture_length = read(28)?;
- (width.is_finite() && width > 0.0 && texture_length.is_finite() && texture_length > 0.0)
- .then_some(Self {
- width,
- texture_length,
- })
+ let segment_spacing = read(32)?;
+ let max_segments = word(16)?;
+ ([width, texture_length, segment_spacing]
+ .into_iter()
+ .all(|value| value.is_finite() && value > 0.0)
+ && (1..=u32::MAX / 6).contains(&max_segments))
+ .then_some(Self {
+ width,
+ texture_length,
+ segment_spacing,
+ max_segments,
+ })
}
- /// Builds a ribbon between the two sampled positions in the camera plane.
- pub fn ribbon(
+ /// Builds equal-length trail quads with wrapped longitudinal texture starts.
+ pub fn segments(
self,
[start, end]: [[f32; 3]; 2],
camera_forward: [f32; 3],
- ) -> Option<([[f32; 3]; 4], f32)> {
+ ) -> Option<impl ExactSizeIterator<Item = ([[f32; 3]; 4], [f32; 2])>> {
let [x, y, z]: [f32; 3] = std::array::from_fn(|i| end[i] - start[i]);
let [cx, cy, cz] = camera_forward;
let side = [cy * z - cz * y, cz * x - cx * z, cx * y - cy * x];
@@ -53,16 +63,35 @@ impl TrailLayer {
if !side_length.is_finite() || side_length <= f32::EPSILON || !length.is_finite() {
return None;
}
- let offset = side.map(|value| value * (0.5 * self.width / side_length));
- Some((
- [
- std::array::from_fn(|i| start[i] - offset[i]),
- std::array::from_fn(|i| start[i] + offset[i]),
- std::array::from_fn(|i| end[i] - offset[i]),
- std::array::from_fn(|i| end[i] + offset[i]),
- ],
- length / self.texture_length,
- ))
+ let offset = side.map(|value| (value / side_length) * (0.5 * self.width));
+ let count = ((length / self.segment_spacing) as u32).clamp(1, self.max_segments);
+ let repeat = (length / count as f32) / self.texture_length;
+ if !repeat.is_finite() {
+ return None;
+ }
+ let position = move |index: u32| {
+ if index == count {
+ return end;
+ }
+ let fraction = index as f32 / count as f32;
+ std::array::from_fn(|axis| start[axis] + [x, y, z][axis] * fraction)
+ };
+ let mut phase = 0.0_f32;
+ Some((0..count).map(move |index| {
+ let a = position(index);
+ let b = position(index + 1);
+ let u = phase.fract();
+ phase += repeat;
+ (
+ [
+ std::array::from_fn(|i| a[i] - offset[i]),
+ std::array::from_fn(|i| a[i] + offset[i]),
+ std::array::from_fn(|i| b[i] - offset[i]),
+ std::array::from_fn(|i| b[i] + offset[i]),
+ ],
+ [u, u + repeat],
+ )
+ }))
}
}
@@ -71,14 +100,19 @@ mod tests {
use super::TrailLayer;
#[test]
- fn ribbon_keeps_the_sampled_endpoints_and_width() {
+ fn trail_preserves_width_endpoints_and_uv_across_authored_segments() {
let layer = TrailLayer {
width: 0.5,
- texture_length: 10.0,
+ texture_length: 5.0,
+ segment_spacing: 50.0,
+ max_segments: 20,
+ };
+ let along_x = |length| {
+ layer
+ .segments([[0.0; 3], [length, 0.0, 0.0]], [0.0, 1.0, 0.0])
+ .unwrap()
};
- let (points, repeats) = layer
- .ribbon([[0.0; 3], [20.0, 0.0, 0.0]], [0.0, 1.0, 0.0])
- .unwrap();
+ let (points, uv) = along_x(20.0).next().unwrap();
assert_eq!(
points,
[
@@ -88,7 +122,15 @@ mod tests {
[20.0, 0.0, -0.25]
]
);
- assert_eq!(repeats, 2.0);
- assert!(layer.ribbon([[0.0; 3]; 2], [1.0, 0.0, 0.0]).is_none());
+ assert_eq!(uv, [0.0, 4.0]);
+ assert_eq!(along_x(99.0).len(), 1);
+ assert_eq!(along_x(100.0).len(), 2);
+ let quads = along_x(166.0).collect::<Vec<_>>();
+ assert_eq!(quads.len(), 3);
+ assert_eq!(quads[0].0[2..], quads[1].0[..2]);
+ assert_eq!(quads[1].1[0], quads[0].1[1].fract());
+ assert_eq!(quads[2].0[3], [166.0, 0.0, -0.25]);
+ assert_eq!(along_x(1231.0).len(), 20);
+ assert!(layer.segments([[0.0; 3]; 2], [1.0, 0.0, 0.0]).is_none());
}
}