2022-01-14 06:05:51 +00:00
|
|
|
use bevy::{
|
|
|
|
ecs::schedule::StageLabel,
|
2022-01-17 21:40:31 +00:00
|
|
|
prelude::{ResMut, SystemLabel, Vec3, Windows},
|
2022-01-14 06:05:51 +00:00
|
|
|
};
|
2022-01-12 08:18:13 +00:00
|
|
|
|
2022-01-14 06:05:51 +00:00
|
|
|
pub mod action;
|
2022-01-12 08:18:13 +00:00
|
|
|
pub mod camera;
|
|
|
|
pub mod geometry;
|
2022-01-14 06:05:51 +00:00
|
|
|
pub mod glamor;
|
2022-01-12 08:18:13 +00:00
|
|
|
pub mod input;
|
|
|
|
pub mod lights;
|
|
|
|
pub mod ui;
|
|
|
|
|
2022-01-14 06:05:51 +00:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq, Eq, SystemLabel, StageLabel)]
|
|
|
|
pub enum Label {
|
|
|
|
Geometry,
|
|
|
|
Glamor,
|
|
|
|
Input,
|
|
|
|
Action,
|
|
|
|
}
|
|
|
|
|
2022-01-12 08:18:13 +00:00
|
|
|
pub fn disable_mouse_trap(mut windows: ResMut<Windows>) {
|
|
|
|
let window = windows.get_primary_mut().unwrap();
|
|
|
|
window.set_cursor_lock_mode(false);
|
|
|
|
window.set_cursor_visibility(true);
|
|
|
|
}
|
2022-01-17 21:40:31 +00:00
|
|
|
|
2022-01-20 00:02:17 +00:00
|
|
|
pub fn random_unit_vec(r: &mut impl rand::prelude::Rng) -> Vec3 {
|
2022-01-17 21:40:31 +00:00
|
|
|
// https://mathworld.wolfram.com/SpherePointPicking.html
|
|
|
|
// Marsaglia (1972) for picking x1 and x2 from (-1, 1) and generating surface
|
|
|
|
// points directly if their sum is less than 1.
|
|
|
|
|
2022-01-19 23:11:48 +00:00
|
|
|
let mut x1: f32 = 0.0;
|
|
|
|
let mut x2: f32 = 0.0;
|
|
|
|
let mut ssum = std::f32::MAX;
|
2022-01-17 21:40:31 +00:00
|
|
|
while ssum >= 1.0 {
|
|
|
|
x1 = r.gen_range(-1.0..=1.0);
|
|
|
|
x2 = r.gen_range(-1.0..=1.0);
|
|
|
|
ssum = x1.powi(2) + x2.powi(2);
|
|
|
|
}
|
2022-01-20 00:02:17 +00:00
|
|
|
let sqrt = (1.0 - ssum).sqrt();
|
2022-01-17 21:40:31 +00:00
|
|
|
let x = 2.0 * x1 * sqrt;
|
|
|
|
let y = 2.0 * x2 * sqrt;
|
|
|
|
let z = 1.0 - 2.0 * ssum;
|
2022-01-19 04:08:47 +00:00
|
|
|
Vec3::new(x, y, z).normalize()
|
2022-01-17 21:40:31 +00:00
|
|
|
}
|