use super::EntityId;
use crate::{
core::transform::components::local_to_world,
entity::get_component,
internal::{conversion::FromBindgen, wit},
};
use glam::{vec3, Vec3};
#[derive(Debug, Clone, Copy, Default)]
pub struct Ray {
pub origin: Vec3,
pub dir: Vec3,
}
impl Ray {
pub fn from_camera_view_matrix(camera: EntityId) -> Option<Ray> {
let mat4 = get_component(camera, local_to_world())?;
let origin = mat4.project_point3(Vec3::ZERO);
let end = mat4.project_point3(vec3(0., 0., 1.));
let dir = (end - origin).normalize();
Some(Ray { origin, dir })
}
}
impl FromBindgen for wit::types::Ray {
type Item = Ray;
fn from_bindgen(self) -> Self::Item {
Ray {
origin: self.origin.from_bindgen(),
dir: self.dir.from_bindgen(),
}
}
}