Skip to content

Commit

Permalink
Feat: Add toggle to render in realistic lighting and support for part…
Browse files Browse the repository at this point in the history
…ial data files
  • Loading branch information
ndavd committed Sep 16, 2023
1 parent 416e58a commit 1072e41
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"
description = "Generalized hypercube visualizer"
readme = ".github/README.md"
repository = "https://github.com/ndavd/ncube"
exclude = [".github/demo.gif"]
exclude = [".github/demo.gif", ".github/workflows"]
keywords = ["simulation", "hypercube", "bevy", "mathematics", "tesseract"]
categories = ["simulation", "mathematics", "visualization", "graphics"]

Expand Down
46 changes: 36 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn main() {
.init_resource::<NCubeEdgeColor>()
.init_resource::<NCubeFaceColor>()
.init_resource::<NCubeEdgeThickness>()
.init_resource::<NCubeUnlit>()
.add_plugins((
DefaultPlugins,
camera::CameraPlugin,
Expand Down Expand Up @@ -135,6 +136,14 @@ impl std::default::Default for NCubeEdgeThickness {
}
}

#[derive(Resource, Deref, DerefMut)]
pub struct NCubeUnlit(bool);
impl std::default::Default for NCubeUnlit {
fn default() -> Self {
Self(false)
}
}

fn spawn_hypercube(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
Expand All @@ -144,6 +153,7 @@ fn spawn_hypercube(
mut ncube_rotations: ResMut<NCubeRotations>,
mut ncube_planes_of_rotation: ResMut<NCubePlanesOfRotation>,
mut ncube_vertices_3d: ResMut<NCubeVertices3D>,
ncube_unlit: Res<NCubeUnlit>,
ncube_edge_color: Res<NCubeEdgeColor>,
ncube_face_color: Res<NCubeFaceColor>,
q_ncube_entities: Query<Entity, With<NCubeMesh>>,
Expand Down Expand Up @@ -186,6 +196,7 @@ fn spawn_hypercube(
base_color: **ncube_edge_color,
double_sided: true,
cull_mode: None,
unlit: **ncube_unlit,
..default()
}),
transform: edge::Edge::transform(
Expand All @@ -209,8 +220,11 @@ fn spawn_hypercube(
ncube_vertices_3d[*k],
],
);
let normal = ncube_vertices_3d[*i].normal(&ncube_vertices_3d[*j], &ncube_vertices_3d[*k]);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vec![normal; 3]);
if !**ncube_unlit {
let normal =
ncube_vertices_3d[*i].normal(&ncube_vertices_3d[*j], &ncube_vertices_3d[*k]);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vec![normal; 3]);
}
commands.spawn((
MaterialMeshBundle {
mesh: meshes.add(mesh.into()).into(),
Expand All @@ -219,6 +233,7 @@ fn spawn_hypercube(
alpha_mode: AlphaMode::Add,
double_sided: true,
cull_mode: None,
unlit: **ncube_unlit,
..default()
}),
..default()
Expand All @@ -235,6 +250,7 @@ fn update_ncube_meshes(
ncube_face_color: Res<NCubeFaceColor>,
ncube_edge_thickness: Res<NCubeEdgeThickness>,
ncube_vertices_3d: Res<NCubeVertices3D>,
ncube_unlit: Res<NCubeUnlit>,
mut q_edges: Query<(&mut Transform, &Handle<StandardMaterial>), With<Edge>>,
q_face_handles: Query<(&Handle<Mesh>, &Handle<StandardMaterial>), With<Face>>,
mut meshes: ResMut<Assets<Mesh>>,
Expand All @@ -250,6 +266,14 @@ fn update_ncube_meshes(
materials.get_mut(material_handle).unwrap().base_color = **ncube_face_color;
});
}
if ncube_unlit.is_changed() {
q_edges.iter().for_each(|(_, material_handle)| {
materials.get_mut(material_handle).unwrap().unlit = **ncube_unlit;
});
q_face_handles.iter().for_each(|(_, material_handle)| {
materials.get_mut(material_handle).unwrap().unlit = **ncube_unlit;
});
}

if !ncube.is_changed() {
return;
Expand All @@ -273,14 +297,16 @@ fn update_ncube_meshes(
.for_each(|(i, (mesh_handle, _))| {
if let Some(face) = ncube.faces.0.get(i) {
let mesh = meshes.get_mut(mesh_handle).unwrap();
mesh.insert_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![
ncube_vertices_3d[face.0]
.normal(&ncube_vertices_3d[face.1], &ncube_vertices_3d[face.2]);
3
],
);
if !**ncube_unlit {
mesh.insert_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![
ncube_vertices_3d[face.0]
.normal(&ncube_vertices_3d[face.1], &ncube_vertices_3d[face.2]);
3
],
);
}
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
Expand Down
79 changes: 54 additions & 25 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::camera::get_default_camera_transform;
use crate::ncube::NCube as InnerNCube;
use crate::NCube;
use crate::NCubeDimension;
Expand All @@ -7,6 +8,7 @@ use crate::NCubeFaceColor;
use crate::NCubeIsPaused;
use crate::NCubePlanesOfRotation;
use crate::NCubeRotations;
use crate::NCubeUnlit;
use crate::NCubeVertices3D;
use crate::S;
use bevy::prelude::*;
Expand All @@ -27,15 +29,31 @@ struct CameraTransform {
rotation: Quat,
scale: Vec3,
}
impl std::default::Default for CameraTransform {
fn default() -> Self {
let transform = get_default_camera_transform();
Self {
translation: transform.translation,
rotation: transform.rotation,
scale: transform.scale,
}
}
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct NCubeData {
dimension: usize,
rotations: Vec<(usize, usize, f32, f32)>,
#[serde(default)]
camera_transform: CameraTransform,
#[serde(default)]
edge_thickness: f32,
#[serde(default)]
edge_color: Color,
#[serde(default)]
face_color: Color,
camera_transform: CameraTransform,
#[serde(default)]
unlit: bool,
}

#[derive(Resource, Deref, DerefMut, Default)]
Expand All @@ -50,6 +68,7 @@ fn info_panel(
mut ncube_face_color: ResMut<NCubeFaceColor>,
mut ncube_edge_thickness: ResMut<NCubeEdgeThickness>,
mut ncube_vertices_3d: ResMut<NCubeVertices3D>,
mut ncube_unlit: ResMut<NCubeUnlit>,
mut ncube_is_paused: ResMut<NCubeIsPaused>,
mut contexts: EguiContexts,
mut q_camera_transform: Query<&mut Transform, With<Camera>>,
Expand Down Expand Up @@ -85,6 +104,12 @@ fn info_panel(
ui.label(format!("{}", ncube.faces.0.len() / 2));
ui.end_row();

ui.label("realistic lighting:");
let mut lit = !**ncube_unlit;
ui.add(egui::Checkbox::new(&mut lit, ""));
**ncube_unlit = !lit;
ui.end_row();

ui.label("edge thickness:");
ui.add(egui::Slider::new(&mut **ncube_edge_thickness, 0.0..=0.025));
ui.end_row();
Expand Down Expand Up @@ -165,6 +190,7 @@ fn info_panel(
rotation: camera_transform.rotation,
scale: camera_transform.scale,
},
unlit: **ncube_unlit,
};
serde_json::to_writer_pretty(&mut file, &ncube_data)
.unwrap_or_else(|_| {});
Expand All @@ -188,31 +214,34 @@ fn info_panel(
FileDragAndDrop::DroppedFile { path_buf, .. } => {
let file = std::fs::File::open(&path_buf).unwrap();
let reader = std::io::BufReader::new(file);
if let Ok(data) =
serde_json::from_reader::<_, NCubeData>(reader)
{
*q_camera_transform.get_single_mut().unwrap() = Transform {
translation: data.camera_transform.translation,
scale: data.camera_transform.scale,
rotation: data.camera_transform.rotation,
};
**ncube_is_paused = true;
**ncube_edge_thickness = data.edge_thickness;
**ncube_edge_color = data.edge_color;
**ncube_face_color = data.face_color;
**ncube_dimension = data.dimension;
**ncube = InnerNCube::new(**ncube_dimension, S);
**ncube_rotations = std::collections::HashMap::new();
**ncube_planes_of_rotation = Vec::new();
let mut angles = Vec::new();
for (d1, d2, angle, vel) in data.rotations {
ncube_rotations.insert((d1, d2), (angle, vel));
ncube_planes_of_rotation.push((d1, d2));
angles.push(angle);
match serde_json::from_reader::<_, NCubeData>(reader) {
Ok(data) => {
*q_camera_transform.get_single_mut().unwrap() =
Transform {
translation: data.camera_transform.translation,
scale: data.camera_transform.scale,
rotation: data.camera_transform.rotation,
};
**ncube_is_paused = true;
**ncube_edge_thickness = data.edge_thickness;
**ncube_edge_color = data.edge_color;
**ncube_face_color = data.face_color;
**ncube_unlit = data.unlit;
**ncube_dimension = data.dimension;
**ncube = InnerNCube::new(**ncube_dimension, S);
**ncube_rotations = std::collections::HashMap::new();
**ncube_planes_of_rotation = Vec::new();
let mut angles = Vec::new();
for (d1, d2, angle, vel) in data.rotations {
ncube_rotations.insert((d1, d2), (angle, vel));
ncube_planes_of_rotation.push((d1, d2));
angles.push(angle);
}
**ncube_vertices_3d = ncube
.rotate(&ncube_planes_of_rotation, &angles)
.perspective_project_vertices();
}
**ncube_vertices_3d = ncube
.rotate(&ncube_planes_of_rotation, &angles)
.perspective_project_vertices();
Err(e) => println!("ERR {e}"),
}
**is_hovering_file = false;
}
Expand Down

0 comments on commit 1072e41

Please sign in to comment.