Skip to content

Commit

Permalink
Add transparency to tracer trails
Browse files Browse the repository at this point in the history
  • Loading branch information
joshburkart committed Jul 2, 2024
1 parent 84be3ed commit 5d8bea4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
1 change: 1 addition & 0 deletions tsunami/src/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{

const TRACER_STEP: usize = 3;
const TRACER_HISTORY_LENGTH: usize = TRACER_STEP.pow(3);
pub const TRACER_TAIL_LENGTH: usize = TRACER_HISTORY_LENGTH / TRACER_STEP;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GeometryType {
Expand Down
29 changes: 26 additions & 3 deletions tsunami/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,20 @@ pub async fn run() {
geometry: InstancedMesh::new(
&context,
&PointCloud {
positions: Positions::F64(rendering_data.tracer_points),
positions: Positions::F64(vec![]),
colors: None,
}
.into(),
&point_mesh,
),
material: ColorMaterial::default(),
material: ColorMaterial::new_transparent(
&context,
&CpuMaterial {
roughness: 1.,
albedo: Srgba::WHITE,
..Default::default()
},
),
};

let mut tsunami_hint_circle_object = Gm {
Expand Down Expand Up @@ -418,6 +425,7 @@ pub async fn run() {
positions: Positions::F32(
rendering_data
.tracer_points
.points
.into_iter()
.map(|point| {
rotation.transform_vector(Vector3 {
Expand All @@ -428,7 +436,22 @@ pub async fn run() {
})
.collect(),
),
colors: None,
colors: Some(
rendering_data
.tracer_points
.positions
.into_iter()
.map(|position| {
Srgba::new(
u8::MAX,
u8::MAX,
u8::MAX,
// Add transparency based on position in tracer's trail.
u8::MAX / geom::TRACER_TAIL_LENGTH as u8 * position as u8,
)
})
.collect(),
),
}
.into(),
&point_mesh,
Expand Down
60 changes: 39 additions & 21 deletions tsunami/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use three_d::{CpuMesh, Indices, Positions, Vector3};
#[derive(Clone)]
pub struct RenderingData {
pub quadrature_points: Vec<Vector3<Float>>,
pub tracer_points: Vec<Vector3<Float>>,
pub tracer_points: TracerPoints,
pub mesh: CpuMesh,
}

Expand Down Expand Up @@ -147,25 +147,31 @@ impl SphereRenderable {
points
}

fn make_tracer_points(&self, height_exaggeration_factor: Float) -> Vec<Vector3<Float>> {
self.tracer_points_history_mu_phi
fn make_tracer_points(&self, height_exaggeration_factor: Float) -> TracerPoints {
let (points, positions) = self
.tracer_points_history_mu_phi
.iter()
.enumerate()
.zip(&self.tracer_heights_history)
.map(|(tracer_points_mu_phi, tracer_heights)| {
.map(|((position, tracer_points_mu_phi), tracer_heights)| {
tracer_points_mu_phi
.axis_iter(nd::Axis(1))
.zip(tracer_heights.iter())
.map(|(point_mu_phi, &height)| {
self.make_point(
point_mu_phi[[0]],
point_mu_phi[[1]],
height,
height_exaggeration_factor,
.map(move |(point_mu_phi, &height)| {
(
self.make_point(
point_mu_phi[[0]],
point_mu_phi[[1]],
height,
height_exaggeration_factor,
),
position,
)
})
})
.flatten()
.collect()
.unzip();
TracerPoints { points, positions }
}

fn make_point(
Expand Down Expand Up @@ -289,25 +295,31 @@ impl TorusRenderable {
points
}

fn make_tracer_points(&self, height_exaggeration_factor: Float) -> Vec<Vector3<Float>> {
self.tracer_points_history_theta_phi
fn make_tracer_points(&self, height_exaggeration_factor: Float) -> TracerPoints {
let (points, positions) = self
.tracer_points_history_theta_phi
.iter()
.enumerate()
.zip(&self.tracer_heights_history)
.map(|(tracer_points_mu_phi, tracer_heights)| {
.map(|((position, tracer_points_mu_phi), tracer_heights)| {
tracer_points_mu_phi
.axis_iter(nd::Axis(1))
.zip(tracer_heights.iter())
.map(|(point_mu_phi, &height)| {
self.make_point(
point_mu_phi[[0]],
point_mu_phi[[1]],
height,
height_exaggeration_factor,
.map(move |(point_mu_phi, &height)| {
(
self.make_point(
point_mu_phi[[0]],
point_mu_phi[[1]],
height,
height_exaggeration_factor,
),
position,
)
})
})
.flatten()
.collect()
.unzip();
TracerPoints { points, positions }
}

fn make_point(
Expand All @@ -326,3 +338,9 @@ impl TorusRenderable {
* (-phi.cos() * radially_out + phi.sin() * Vector3::unit_z()))
}
}

#[derive(Clone)]
pub struct TracerPoints {
pub points: Vec<Vector3<Float>>,
pub positions: Vec<usize>,
}

0 comments on commit 5d8bea4

Please sign in to comment.