move sun/moon direction to common

This commit is contained in:
laundmo 2023-06-18 15:18:51 +02:00 committed by juliancoffee
parent 4e37430189
commit 2ae7c68dde
3 changed files with 27 additions and 23 deletions

View File

@ -2,10 +2,31 @@ use crate::comp::Pos;
use serde::{Deserialize, Serialize};
use specs::Entity;
use std::ops::{Mul, MulAssign};
use vek::Vec3;
/// A resource that stores the time of day.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, Default)]
pub struct TimeOfDay(pub f64);
impl TimeOfDay {
pub fn new(t: f64) -> Self { TimeOfDay(t) }
fn get_angle_rad(self) -> f32 {
const TIME_FACTOR: f64 = (std::f64::consts::PI * 2.0) / (3600.0 * 24.0);
((self.0 as f32 * TIME_FACTOR) % (std::f64::consts::PI * 2.0)) as f32
}
/// Computes the direction of light from the sun based on the time of day.
pub fn get_sun_dir(self) -> Vec3<f32> {
let angle_rad = self.get_angle_rad();
Vec3::new(-angle_rad.sin(), 0.0, angle_rad.cos())
}
/// Computes the direction of light from the moon based on the time of day.
pub fn get_moon_dir(self) -> Vec3<f32> {
let angle_rad = self.get_angle_rad();
-Vec3::new(-angle_rad.sin(), 0.0, angle_rad.cos() - 0.5).normalized()
}
}
impl TimeOfDay {
pub fn day(&self) -> f64 { self.0.rem_euclid(24.0 * 3600.0) }

View File

@ -20,7 +20,7 @@ pub mod ui;
use super::{Consts, Renderer, Texture};
use crate::scene::camera::CameraMode;
use bytemuck::{Pod, Zeroable};
use common::{terrain::BlockKind, util::srgb_to_linear};
use common::{resources::TimeOfDay, terrain::BlockKind, util::srgb_to_linear};
use std::marker::PhantomData;
use vek::*;
@ -144,8 +144,8 @@ impl Globals {
0.0,
0.0,
],
sun_dir: Vec4::from_direction(Self::get_sun_dir(time_of_day)).into_array(),
moon_dir: Vec4::from_direction(Self::get_moon_dir(time_of_day)).into_array(),
sun_dir: Vec4::from_direction(TimeOfDay::new(time_of_day).get_sun_dir()).into_array(),
moon_dir: Vec4::from_direction(TimeOfDay::new(time_of_day).get_moon_dir()).into_array(),
tick: [
(tick % TIME_OVERFLOW) as f32,
(tick / TIME_OVERFLOW).floor() as f32,
@ -195,23 +195,6 @@ impl Globals {
globals_dummy: [0.0; 3],
}
}
fn get_angle_rad(time_of_day: f64) -> f32 {
const TIME_FACTOR: f64 = (std::f64::consts::PI * 2.0) / (3600.0 * 24.0);
((time_of_day * TIME_FACTOR) % (std::f64::consts::PI * 2.0)) as f32
}
/// Computes the direction of light from the sun based on the time of day.
pub fn get_sun_dir(time_of_day: f64) -> Vec3<f32> {
let angle_rad = Self::get_angle_rad(time_of_day);
Vec3::new(-angle_rad.sin(), 0.0, angle_rad.cos())
}
/// Computes the direction of light from the moon based on the time of day.
pub fn get_moon_dir(time_of_day: f64) -> Vec3<f32> {
let angle_rad = Self::get_angle_rad(time_of_day);
-Vec3::new(-angle_rad.sin(), 0.0, angle_rad.cos() - 0.5).normalized()
}
}
impl Default for Globals {

View File

@ -39,7 +39,7 @@ use common::{
tool::ToolKind,
},
outcome::Outcome,
resources::{DeltaTime, TimeScale},
resources::{DeltaTime, TimeScale, TimeOfDay},
terrain::{BlockKind, CoordinateConversions, TerrainChunk, TerrainGrid, NEIGHBOR_DELTA},
vol::ReadVol,
weather::WeatherGrid,
@ -150,11 +150,11 @@ pub struct SceneData<'a> {
impl<'a> SceneData<'a> {
pub fn get_sun_dir(&self) -> Vec3<f32> {
Globals::get_sun_dir(self.interpolated_time_of_day.unwrap_or(0.0))
TimeOfDay::new(self.interpolated_time_of_day.unwrap_or(0.0)).get_sun_dir()
}
pub fn get_moon_dir(&self) -> Vec3<f32> {
Globals::get_moon_dir(self.interpolated_time_of_day.unwrap_or(0.0))
TimeOfDay::new(self.interpolated_time_of_day.unwrap_or(0.0)).get_moon_dir()
}
}