ak_vis/render/
render_cell.rs

1use crate::components::FrameCell;
2use crate::visuals::CellVisual;
3
4use bevy::{
5    light::{NotShadowCaster, NotShadowReceiver},
6    prelude::*,
7};
8
9pub fn render_cell(
10    visuals: Vec<CellVisual>,
11    commands: &mut Commands,
12    materials: &mut ResMut<Assets<StandardMaterial>>,
13    meshes: &mut ResMut<Assets<Mesh>>,
14) {
15    let cyl_mesh = Cylinder::new(1.0, 2.0);
16
17    for visual in visuals {
18        commands.spawn((
19            Mesh3d(meshes.add(cyl_mesh)),
20            MeshMaterial3d(materials.add(visual.color)),
21            transform_cylinder_between(visual.corner_1, visual.corner_2, 0.025),
22            FrameCell,
23            NotShadowCaster,
24            NotShadowReceiver,
25        ));
26    }
27}
28
29fn transform_cylinder_between(p0: Vec3, p1: Vec3, radius: f32) -> Transform {
30    let d = p1 - p0;
31    let len = d.length();
32    let mid = (p0 + p1) * 0.5;
33
34    // Bevy's Cylinder is aligned along +Y by default (in most setups).
35    // So rotate +Y to match the direction d.
36    let dir = d / len;
37    let rot = Quat::from_rotation_arc(Vec3::Y, dir);
38
39    Transform {
40        translation: mid,
41        rotation: rot,
42        scale: Vec3::new(radius, len * 0.5, radius), // y scale = half-length if cylinder height is 2.0; adjust if yours differs
43    }
44}