Combine color and opacity on push to scene

This commit is contained in:
Aaron Fenyes 2025-05-19 00:21:05 -07:00
parent 592f327e62
commit 2aa1adcd07

View file

@ -20,15 +20,23 @@ use crate::{
assembly::{Element, ElementColor, ElementMotion, Point, Sphere} assembly::{Element, ElementColor, ElementMotion, Point, Sphere}
}; };
// --- scene data --- // --- color ---
const COLOR_SIZE: usize = 3; const COLOR_SIZE: usize = 3;
type ColorWithOpacity = [f32; COLOR_SIZE + 1]; type ColorWithOpacity = [f32; COLOR_SIZE + 1];
fn combine_channels(color: ElementColor, opacity: f32) -> ColorWithOpacity {
let mut color_with_opacity = [0.0; COLOR_SIZE + 1];
color_with_opacity[..COLOR_SIZE].copy_from_slice(&color);
color_with_opacity[COLOR_SIZE] = opacity;
color_with_opacity
}
// --- scene data ---
struct SceneSpheres { struct SceneSpheres {
representations: Vec<DVector<f64>>, representations: Vec<DVector<f64>>,
colors: Vec<ElementColor>, colors_with_opacity: Vec<ColorWithOpacity>,
opacities: Vec<f32>,
highlights: Vec<f32> highlights: Vec<f32>
} }
@ -36,8 +44,7 @@ impl SceneSpheres {
fn new() -> SceneSpheres{ fn new() -> SceneSpheres{
SceneSpheres { SceneSpheres {
representations: Vec::new(), representations: Vec::new(),
colors: Vec::new(), colors_with_opacity: Vec::new(),
opacities: Vec::new(),
highlights: Vec::new() highlights: Vec::new()
} }
} }
@ -48,8 +55,7 @@ impl SceneSpheres {
fn push(&mut self, representation: DVector<f64>, color: ElementColor, opacity: f32, highlight: f32) { fn push(&mut self, representation: DVector<f64>, color: ElementColor, opacity: f32, highlight: f32) {
self.representations.push(representation); self.representations.push(representation);
self.colors.push(color); self.colors_with_opacity.push(combine_channels(color, opacity));
self.opacities.push(opacity);
self.highlights.push(highlight); self.highlights.push(highlight);
} }
} }
@ -72,12 +78,8 @@ impl ScenePoints {
} }
fn push(&mut self, representation: DVector<f64>, color: ElementColor, opacity: f32, highlight: f32, selected: bool) { fn push(&mut self, representation: DVector<f64>, color: ElementColor, opacity: f32, highlight: f32, selected: bool) {
let mut color_with_opacity = [0.0; COLOR_SIZE + 1];
color_with_opacity[..COLOR_SIZE].copy_from_slice(&color);
color_with_opacity[COLOR_SIZE] = opacity;
self.representations.push(representation); self.representations.push(representation);
self.colors_with_opacity.push(color_with_opacity); self.colors_with_opacity.push(combine_channels(color, opacity));
self.highlights.push(highlight); self.highlights.push(highlight);
self.selections.push(if selected { 1.0 } else { 0.0 }); self.selections.push(if selected { 1.0 } else { 0.0 });
} }
@ -664,11 +666,6 @@ pub fn Display() -> View {
ctx.uniform1i(sphere_cnt_loc.as_ref(), sphere_cnt); ctx.uniform1i(sphere_cnt_loc.as_ref(), sphere_cnt);
for n in 0..sphere_reps_world.len() { for n in 0..sphere_reps_world.len() {
let v = &sphere_reps_world[n]; let v = &sphere_reps_world[n];
let sphere_color = &mut [0.0; 4];
sphere_color[..3].copy_from_slice(&scene.spheres.colors[n]);
sphere_color[3] = scene.spheres.opacities[n];
ctx.uniform3fv_with_f32_array( ctx.uniform3fv_with_f32_array(
sphere_sp_locs[n].as_ref(), sphere_sp_locs[n].as_ref(),
v.rows(0, 3).as_slice() v.rows(0, 3).as_slice()
@ -679,7 +676,7 @@ pub fn Display() -> View {
); );
ctx.uniform4fv_with_f32_array( ctx.uniform4fv_with_f32_array(
sphere_color_locs[n].as_ref(), sphere_color_locs[n].as_ref(),
sphere_color &scene.spheres.colors_with_opacity[n]
); );
ctx.uniform1f( ctx.uniform1f(
sphere_highlight_locs[n].as_ref(), sphere_highlight_locs[n].as_ref(),