From 2ae7c68dde05dd5992ff07a060b9ec1664b52e0e Mon Sep 17 00:00:00 2001 From: laundmo Date: Sun, 18 Jun 2023 15:18:51 +0200 Subject: [PATCH] move sun/moon direction to common --- common/src/resources.rs | 21 +++++++++++++++++++++ voxygen/src/render/pipelines/mod.rs | 23 +++-------------------- voxygen/src/scene/mod.rs | 6 +++--- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/common/src/resources.rs b/common/src/resources.rs index 007f34d0bd..1d3c968e89 100644 --- a/common/src/resources.rs +++ b/common/src/resources.rs @@ -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 { + 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 { + 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) } diff --git a/voxygen/src/render/pipelines/mod.rs b/voxygen/src/render/pipelines/mod.rs index fc87440c25..da20cd7d0c 100644 --- a/voxygen/src/render/pipelines/mod.rs +++ b/voxygen/src/render/pipelines/mod.rs @@ -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 { - 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 { - 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 { diff --git a/voxygen/src/scene/mod.rs b/voxygen/src/scene/mod.rs index af2eb453b2..5d79ce5c58 100644 --- a/voxygen/src/scene/mod.rs +++ b/voxygen/src/scene/mod.rs @@ -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 { - 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 { - 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() } }