Skip to content

Commit

Permalink
feat: implement environment effects in a more correct way
Browse files Browse the repository at this point in the history
SSAO and skylight are affected by cloud-coverage.

Clouds are not as strongly affected by a bright skylight (change in shader), therefore lights are stronger in general (bluer sky).

Refs: #312
  • Loading branch information
Mathias Baumgartinger~ committed Aug 8, 2023
1 parent 2588866 commit e9067d5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
36 changes: 24 additions & 12 deletions World/Environment/WorldEnvironment.gd
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
extends WorldEnvironment

@onready var light = get_node("DirectionalLight3D")
# Diffuse and specular
@onready var light: DirectionalLight3D = get_node("DirectionalLight")
# Ambient and sky
@onready var sky_light: DirectionalLight3D = get_node("DirectionalLight/SkyLight")

var wind_speed = 0
var wind_direction = 0

var brightest_light_energy = 0.75
var brightest_light_energy = 1.5
var light_darken_begin_altitude = 15.0
var light_disabled_altitude = 3.0


func apply_visibility(new_visibility):
environment.fog_density = new_visibility * 0.000008
environment.volumetric_fog_enabled = new_visibility > 40
environment.volumetric_fog_density = new_visibility * 0.0003
environment.fog_density = remap(new_visibility, 0., 100., 0, 0.00015)
# Enable volumetric fog only above a certain threshold
environment.volumetric_fog_density = remap(new_visibility, 30., 100., 0.0, 0.045)


func apply_rain_enabled(enabled: bool):
Expand Down Expand Up @@ -62,20 +65,29 @@ func apply_datetime(datetime: Dictionary):


func apply_light_energy():
var altitude = rad_to_deg(-light.rotation.x)
# Directional light energy is 0 when cloud coverage is maximized
var cloud_coverage = environment.sky.get_material().get_shader_parameter("cloud_coverage")
var directional_energy = brightest_light_energy - remap(cloud_coverage, 0, 1, 0, brightest_light_energy)

# Light energy is halved when it is maximally cloudy
var brightest = brightest_light_energy - environment.sky.get_material().get_shader_parameter("cloud_coverage") * 0.000025
# Lower light quickly in the beginning when coverage/density are higher
# and lower light slower in the end (sqrt-curve-function), vice versa for ssao
var sqrt_cloud_cov = sqrt(cloud_coverage)
sky_light.light_energy = 4.0 - remap(sqrt_cloud_cov, 0, 1, 0, 2)
environment.ssao_intensity = 3.0 + remap(sqrt_cloud_cov, 0, 1, 0, 5)

var altitude = rad_to_deg(-light.rotation.x)
# Sunrise/sunset
if altitude > light_disabled_altitude and altitude < light_darken_begin_altitude:
_set_light_energy(inverse_lerp(light_disabled_altitude, light_darken_begin_altitude, altitude) * brightest)
_set_directional_light_energy(directional_energy *
inverse_lerp(light_disabled_altitude, light_darken_begin_altitude, altitude))
# Night
elif altitude <= light_disabled_altitude:
_set_light_energy(0.0)
_set_directional_light_energy(0.0)
else:
_set_light_energy(brightest)
_set_directional_light_energy(directional_energy)


func _set_light_energy(new_energy):
func _set_directional_light_energy(new_energy):
light.light_energy = new_energy


Expand Down
30 changes: 21 additions & 9 deletions World/Environment/WorldEnvironment.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=12 format=3 uid="uid://cejf5c8oyb02b"]
[gd_scene load_steps=13 format=3 uid="uid://cejf5c8oyb02b"]

[ext_resource type="Script" path="res://World/Environment/WorldEnvironment.gd" id="1"]
[ext_resource type="Shader" path="res://addons/clouds/clouds.gdshader" id="1_p31xn"]
Expand All @@ -10,10 +10,10 @@

[sub_resource type="ShaderMaterial" id="ShaderMaterial_ywyjs"]
shader = ExtResource("1_p31xn")
shader_parameter/_density = 0.05
shader_parameter/_density = 0.069
shader_parameter/cloud_coverage = 0.25
shader_parameter/_time_scale = 1.0
shader_parameter/_time_offset = 0.0
shader_parameter/_time_offset = 80.0
shader_parameter/rayleigh = 2.0
shader_parameter/rayleigh_color = Color(0.26, 0.41, 0.58, 1)
shader_parameter/mie = 0.005
Expand All @@ -32,40 +32,49 @@ sky_material = SubResource("ShaderMaterial_ywyjs")

[sub_resource type="Environment" id="2"]
background_mode = 2
background_energy_multiplier = 1.5
sky = SubResource("Sky_gjwmw")
ambient_light_source = 3
ambient_light_color = Color(1, 1, 1, 1)
reflected_light_source = 2
tonemap_white = 6.0
ssao_enabled = true
ssao_radius = 5.0
ssao_intensity = 6.15
sdfgi_min_cell_size = 1.0
sdfgi_cascade0_distance = 64.0
sdfgi_max_distance = 1024.0
glow_enabled = true
fog_enabled = true
fog_light_color = Color(0.517647, 0.552941, 0.607843, 1)
fog_density = 0.001
fog_density = 0.0119
fog_aerial_perspective = 1.0
fog_sky_affect = 0.0
fog_height_density = 2.0
volumetric_fog_enabled = true
volumetric_fog_density = 0.0
adjustment_contrast = 1.15
adjustment_saturation = 1.2

[sub_resource type="SphereMesh" id="SphereMesh_v0nf7"]

[sub_resource type="QuadMesh" id="QuadMesh_l7xtl"]
size = Vector2(2, 2)

[node name="WorldEnvironment" type="WorldEnvironment"]
environment = SubResource("2")
script = ExtResource("1")

[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(-2.91845e-08, -0.744463, 0.667664, -3.25415e-08, 0.667664, 0.744463, -1, 1.24345e-14, -4.37114e-08, 0, 0, 0)
[node name="DirectionalLight" type="DirectionalLight3D" parent="."]
transform = Transform3D(0.551073, -0.830715, 0.0789348, -0.498894, -0.252165, 0.829167, -0.668897, -0.496311, -0.5534, 0, 0, 0)
layers = 15
light_energy = 1.5
shadow_enabled = true
directional_shadow_blend_splits = true
directional_shadow_max_distance = 1000.0
sky_mode = 1

[node name="DirectionalLight3D" type="DirectionalLight3D" parent="DirectionalLight3D"]
[node name="SkyLight" type="DirectionalLight3D" parent="DirectionalLight"]
transform = Transform3D(0.998675, 0.0390908, -0.0334572, -0.0374992, 0.998193, 0.0469456, 0.0352319, -0.0456288, 0.998337, 0, 0, 0)
light_energy = 4.0
sky_mode = 2

[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
Expand All @@ -76,3 +85,6 @@ enabled = false

[node name="Lightning" parent="." instance=ExtResource("7_canqr")]
enabled = false

[node name="MeshInstance3D2" type="MeshInstance3D" parent="."]
mesh = SubResource("QuadMesh_l7xtl")
2 changes: 1 addition & 1 deletion addons/clouds/clouds.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ vec4 march(vec3 pos, vec3 end, vec3 dir, int depth) {
vec3 p = pos + hash(pos * 10.0) * ss;
const float t_dist = sky_t_radius-sky_b_radius;
float lss = (t_dist / 36.0);
vec3 ldir = normalize(LIGHT0_DIRECTION);
vec3 ldir = normalize(LIGHT0_DIRECTION) * 0.6;
vec3 L = vec3(0.0);
int count=0;
float t = 1.0;
Expand Down

0 comments on commit e9067d5

Please sign in to comment.