Skip to content

Commit

Permalink
Implement day night cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano committed Mar 1, 2024
1 parent ca40bdf commit 78aae78
Show file tree
Hide file tree
Showing 12 changed files with 613 additions and 23 deletions.
1 change: 1 addition & 0 deletions native/src/scripts/world.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod buildings;
mod solar_setup;
55 changes: 55 additions & 0 deletions native/src/scripts/world/solar_setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use godot::builtin::{NodePath, Vector3};
use godot::engine::{light_3d, DirectionalLight3D, Node3D, Time};
use godot::obj::Gd;
use godot_rust_script::{godot_script_impl, GodotScript};

use crate::util::logger;

#[derive(GodotScript, Debug)]
#[script(base = Node3D)]
struct SolarSetup {
/// Reference to the sun child node.
#[export(node_path = ["DirectionalLight3D"])]
pub sun: NodePath,

/// Reference to the moon child node.
#[export(node_path = ["DirectionalLight3D"])]
pub moon: NodePath,

#[export(range(min = 1.0, max = 120.0, step = 1.0))]
pub day_length: u64,

base: Gd<Node3D>,
}

#[godot_script_impl]
impl SolarSetup {
fn sun(&self) -> Option<Gd<DirectionalLight3D>> {
self
.base
.get_window()
.and_then(|root| root.get_node(self.sun.clone()))
.map(|node| node.cast())
}

pub fn _physics_process(&mut self, _delta: f64) {
let day_length = self.day_length * 60 * 1000;
let time = Time::singleton().get_ticks_msec() % (day_length * 2);

let sun_pos = time as f32 * (360.0 / (day_length * 2) as f32);

let Some(mut sun) = self.sun() else {
logger::error!("no sun is assigned to solar setup!");

return;
};

self.base
.set_rotation_degrees(Vector3::new(sun_pos, 0.0, 0.0));

sun.set_param(
light_3d::Param::ENERGY,
if sun_pos > 190.0 { 0.0 } else { 1.0 },
);
}
}
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ common/physics_interpolation=true
[rendering]

textures/vram_compression/import_s3tc_bptc=true
lights_and_shadows/directional_shadow/size=8192
lights_and_shadows/directional_shadow/soft_shadow_filter_quality=4
lights_and_shadows/directional_shadow/16_bits=false
lights_and_shadows/positional_shadow/atlas_16_bits=false
Expand Down
11 changes: 4 additions & 7 deletions resources/Environments/WorldEnv.tres
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
[gd_resource type="Environment" load_steps=3 format=3 uid="uid://bl607wqoa882d"]

[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_c6t7k"]
ground_bottom_color = Color(0.501961, 0.6, 0.701961, 1)
ground_horizon_color = Color(0.501961, 0.6, 0.701961, 1)
[ext_resource type="Material" uid="uid://c26tq15swq4at" path="res://resources/Materials/sky_material.tres" id="1_inrw2"]

[sub_resource type="Sky" id="1"]
sky_material = SubResource("ProceduralSkyMaterial_c6t7k")
[sub_resource type="Sky" id="Sky_gwtol"]
sky_material = ExtResource("1_inrw2")

[resource]
background_mode = 2
sky = SubResource("1")
sky = SubResource("Sky_gwtol")
tonemap_mode = 3
tonemap_exposure = 0.7
ssr_enabled = true
Expand All @@ -18,6 +16,5 @@ ssr_fade_out = 1.0
ssr_depth_tolerance = 0.51
ssao_enabled = true
sdfgi_enabled = true
glow_enabled = true
fog_light_color = Color(0.65, 0.84, 0.95, 1)
fog_density = 0.001
1 change: 1 addition & 0 deletions resources/Materials/ocean_backdrop.tres
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ noise = SubResource("FastNoiseLite_ifgtm")
render_priority = 0
shader = ExtResource("2")
shader_parameter/Wave_Ratio = 0.5
shader_parameter/noise_scale = 0.02
shader_parameter/Wave_Dir = 0.124
shader_parameter/speed = 0.1
shader_parameter/Wave2_Dir = 0.569
Expand Down
26 changes: 26 additions & 0 deletions resources/Materials/sky_material.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[gd_resource type="ShaderMaterial" load_steps=6 format=3 uid="uid://c26tq15swq4at"]

[ext_resource type="Shader" uid="uid://dadtbq4ym2chm" path="res://resources/Shaders/sky.tres" id="1_gixr0"]

[sub_resource type="Gradient" id="Gradient_klkl5"]
offsets = PackedFloat32Array(0.354386, 0.421053, 0.610526)
colors = PackedColorArray(0, 0, 0, 1, 0.137255, 0.32549, 0.639216, 1, 0.137255, 0.32549, 0.639216, 1)

[sub_resource type="GradientTexture2D" id="GradientTexture2D_scq8q"]
gradient = SubResource("Gradient_klkl5")
fill_from = Vector2(0, 1)
fill_to = Vector2(0, 0)

[sub_resource type="Gradient" id="Gradient_0sht1"]
interpolation_color_space = 1
offsets = PackedFloat32Array(0.0350877, 0.463158)
colors = PackedColorArray(1, 0.466667, 0, 1, 0.375132, 0.588795, 0.748625, 1)

[sub_resource type="GradientTexture1D" id="GradientTexture1D_lmpjt"]
gradient = SubResource("Gradient_0sht1")

[resource]
shader = ExtResource("1_gixr0")
shader_parameter/moon_size = 0.06
shader_parameter/Texture2DParameter = SubResource("GradientTexture2D_scq8q")
shader_parameter/sun_rise_gradient = SubResource("GradientTexture1D_lmpjt")
3 changes: 1 addition & 2 deletions resources/Materials/terrain_material.tres
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ shader_parameter/albedo = Color(0.482353, 0.356863, 0.27451, 1)
shader_parameter/specular = 0.5
shader_parameter/metallic = 0.0
shader_parameter/roughness = 1.0
shader_parameter/ao_texture_channel = Plane(1, 0, 0, 0)
shader_parameter/ao_texture_channel = null
shader_parameter/ao_light_affect = 0.0
shader_parameter/transmission = Color(0.517647, 0.823529, 0.262745, 1)
shader_parameter/uv1_scale = Vector3(1, 1, 1)
shader_parameter/uv1_offset = Vector3(0, 0, 0)
1 change: 0 additions & 1 deletion resources/Shaders/ocean_visualshader.tres
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ void fragment() {

}
"
graph_offset = Vector2(-914.285, -256.322)
varyings/vary_world_uv = "0,3"
varyings/vary_world_uv2 = "0,3"
nodes/vertex/0/position = Vector2(3340, -540)
Expand Down
Loading

0 comments on commit 78aae78

Please sign in to comment.