Implement configurable lighting

This commit is contained in:
Camden Dixie O'Brien committed 2025-10-05 13:51:14 +01:00
1 parent 070758b6cc
commit b5bda150d2
6 files changed
+148 -49

No files matched your search

+66 -10
View File
@@ -1,8 +1,28 @@
#version 450
float ambient = 0.1;
vec3 light_dir = normalize(vec3(2, 2, -1));
vec3 base_colour = vec3(1.0, 0.94, 0.8);
#define PI 3.14159265358979
float floor_light_coeff = 0.01;
float sunny_sky_light_coeff = 0.5;
float cloudy_sky_light_coeff = 0.4;
float sunny_sun_light_coeff = 0.8;
float cloudy_sun_light_coeff = 0.3;
float sunny_shadow_pcf_radius = 1;
float cloudy_shadow_pcf_radius = 24;
vec3 sunny_sky_colour = vec3(0.5, 0.7, 1.0);
vec3 cloudy_sky_colour = vec3(0.7, 0.7, 1.0);
vec3 sunny_sun_colour = vec3(1.0, 0.95, 0.8);
vec3 cloudy_sun_colour = vec3(0.7, 0.7, 1.0);
vec3 material_colour = vec3(1.0, 0.94, 0.8);
float shadow_bias = 0.005;
float shadow_map_size = 4096;
layout(location = 0) in vec3 frag_normal;
layout(location = 1) in vec4 light_pos;
@@ -11,18 +31,54 @@ layout(location = 0) out vec4 out_colour;
layout(binding = 2) uniform sampler2DShadow shadow_map;
layout(push_constant) uniform params_t {
layout(offset = 128) vec3 sun_dir;
layout(offset = 140) float cloudiness;
} params;
void main()
{
const float lambertian = max(dot(frag_normal, light_dir), 0.0);
const float sky_light_coeff
= mix(sunny_sky_light_coeff, cloudy_sky_light_coeff,
params.cloudiness);
const float sun_light_coeff
= mix(sunny_sun_light_coeff, cloudy_sun_light_coeff,
params.cloudiness);
const float shadow_pcf_radius
= mix(sunny_shadow_pcf_radius, cloudy_shadow_pcf_radius,
params.cloudiness);
const vec3 sky_colour
= mix(sunny_sky_colour, cloudy_sky_colour,
params.cloudiness);
const vec3 sun_colour
= mix(sunny_sun_colour, cloudy_sun_colour,
params.cloudiness);
const float lambertian = max(dot(frag_normal, params.sun_dir), 0.0);
const float shadow_bias = 0.005;
const vec3 proj_coords = light_pos.xyz / light_pos.w;
const vec3 shadow_map_pos
= vec3(0.5 * proj_coords.xy + 0.5, proj_coords.z);
float shadow = texture(
shadow_map,
vec3(shadow_map_pos.xy, shadow_map_pos.z - shadow_bias));
float shadow = 0.0;
for (int x = -2; x <= 2; ++x) {
for (int y = -2; y < 3; ++y) {
const vec2 offset
= shadow_pcf_radius * vec2(x, y) / shadow_map_size;
shadow += texture(
shadow_map,
vec3(shadow_map_pos.xy + offset,
shadow_map_pos.z - shadow_bias));
}
}
shadow /= 25;
const vec3 sun_light = shadow * lambertian * sun_colour;
const float intensity = min(shadow * lambertian + ambient, 1.0);
out_colour = vec4(intensity * base_colour, 1.0);
const vec3 floor_colour = floor_light_coeff * material_colour;
const float t = 0.5 * clamp(frag_normal.y, -1, 1) + 0.5;
const vec3 sky_light = mix(floor_colour, sky_colour, t);
const vec3 lighting
= sun_light_coeff * sun_light
+ sky_light_coeff * sky_light;
out_colour = vec4(lighting * material_colour, 1.0);
}