2020-07-17 01:26:21 +00:00
|
|
|
use bevy_core::Byteable;
|
2020-05-26 04:57:48 +00:00
|
|
|
use bevy_property::Properties;
|
2020-07-17 01:26:21 +00:00
|
|
|
use bevy_render::{
|
|
|
|
camera::{CameraProjection, PerspectiveProjection},
|
|
|
|
color::Color,
|
|
|
|
};
|
2020-09-14 21:00:32 +00:00
|
|
|
use bevy_transform::components::GlobalTransform;
|
2019-12-02 04:03:04 +00:00
|
|
|
use std::ops::Range;
|
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// A point light
|
2020-05-26 04:57:48 +00:00
|
|
|
#[derive(Properties)]
|
2019-12-02 04:03:04 +00:00
|
|
|
pub struct Light {
|
2020-03-10 06:43:40 +00:00
|
|
|
pub color: Color,
|
2019-12-02 04:03:04 +00:00
|
|
|
pub fov: f32,
|
|
|
|
pub depth: Range<f32>,
|
2020-02-18 03:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Light {
|
|
|
|
fn default() -> Self {
|
|
|
|
Light {
|
2020-03-10 06:43:40 +00:00
|
|
|
color: Color::rgb(1.0, 1.0, 1.0),
|
2020-02-18 03:06:12 +00:00
|
|
|
depth: 0.1..50.0,
|
|
|
|
fov: f32::to_radians(60.0),
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 04:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
2020-06-02 02:23:11 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2020-08-09 23:13:04 +00:00
|
|
|
pub(crate) struct LightRaw {
|
2019-12-02 04:03:04 +00:00
|
|
|
pub proj: [[f32; 4]; 4],
|
|
|
|
pub pos: [f32; 4],
|
|
|
|
pub color: [f32; 4],
|
|
|
|
}
|
|
|
|
|
2020-06-02 02:23:11 +00:00
|
|
|
unsafe impl Byteable for LightRaw {}
|
|
|
|
|
2019-12-03 17:01:15 +00:00
|
|
|
impl LightRaw {
|
2020-09-14 21:00:32 +00:00
|
|
|
pub fn from(light: &Light, global_transform: &GlobalTransform) -> LightRaw {
|
2020-05-30 19:31:04 +00:00
|
|
|
let perspective = PerspectiveProjection {
|
2020-03-22 04:55:33 +00:00
|
|
|
fov: light.fov,
|
|
|
|
aspect_ratio: 1.0,
|
|
|
|
near: light.depth.start,
|
|
|
|
far: light.depth.end,
|
|
|
|
};
|
|
|
|
|
2020-09-14 21:00:32 +00:00
|
|
|
let proj = perspective.get_projection_matrix() * *global_transform.value();
|
|
|
|
let (x, y, z) = global_transform.translation().into();
|
2019-12-02 04:03:04 +00:00
|
|
|
LightRaw {
|
2019-12-04 08:11:14 +00:00
|
|
|
proj: proj.to_cols_array_2d(),
|
|
|
|
pos: [x, y, z, 1.0],
|
2020-02-18 03:06:12 +00:00
|
|
|
color: light.color.into(),
|
2019-12-02 04:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 10:11:27 +00:00
|
|
|
}
|