Better lightning effect

This commit is contained in:
Joshua Barretto 2022-07-09 00:39:18 +01:00
parent 6d1b46956d
commit edc2720416
2 changed files with 72 additions and 55 deletions

View File

@ -1,6 +1,47 @@
#ifndef POINT_GLOW_GLSL
#define POINT_GLOW_GLSL
#include "sky.glsl"
void apply_point_glow_light(Light L, vec3 wpos, vec3 dir, float max_dist, inout vec3 color) {
vec3 light_pos = L.light_pos.xyz;
// Project light_pos to dir line
float t = max(dot(light_pos - wpos, dir), 0);
vec3 nearest = wpos + dir * min(t, max_dist);
vec3 difference = light_pos - nearest;
float distance_2 = dot(difference, difference);
//if (distance_2 > 100000.0) {
// return;
//}
#if (CLOUD_MODE >= CLOUD_MODE_HIGH)
vec3 _unused;
float unused2;
float spread = 1.0 / (1.0 + cloud_at(nearest, 0.0, _unused, unused2).z * 0.005);
#else
const float spread = 1.0;
#endif
float strength = pow(attenuation_strength_real(difference), spread);
#ifdef EXPERIMENTAL_LOWGLOWNEARCAMERA
vec3 cam_wpos = cam_pos.xyz + focus_pos.xyz + focus_off.xyz;
vec3 cam_diff = light_pos - cam_wpos;
float cam_dist_2 = dot(cam_diff, cam_diff);
// 3 meters away glow returns to the maximum strength.
strength *= clamp(cam_dist_2 / 9.0, 0.25, 1.0);
#endif
vec3 light_color = srgb_to_linear(L.light_col.rgb) * strength;
const float LIGHT_AMBIANCE = 0.025;
color += light_color
* 0.002
// Constant, *should* const fold
* POINT_GLOW_FACTOR;
}
vec3 apply_point_glow(vec3 wpos, vec3 dir, float max_dist, vec3 color) {
#ifndef POINT_GLOW_FACTOR
return color;
@ -9,44 +50,11 @@ vec3 apply_point_glow(vec3 wpos, vec3 dir, float max_dist, vec3 color) {
// Only access the array once
Light L = lights[i];
vec3 light_pos = L.light_pos.xyz;
// Project light_pos to dir line
float t = max(dot(light_pos - wpos, dir), 0);
vec3 nearest = wpos + dir * min(t, max_dist);
vec3 difference = light_pos - nearest;
float distance_2 = dot(difference, difference);
if (distance_2 > 100000.0) {
continue;
}
#if (CLOUD_MODE >= CLOUD_MODE_HIGH)
vec3 _unused;
float unused2;
float spread = 1.0 / (1.0 + cloud_at(nearest, 0.0, _unused, unused2).z * 0.005);
#else
const float spread = 1.0;
#endif
float strength = pow(attenuation_strength_real(difference), spread);
#ifdef EXPERIMENTAL_LOWGLOWNEARCAMERA
vec3 cam_wpos = cam_pos.xyz + focus_pos.xyz + focus_off.xyz;
vec3 cam_diff = light_pos - cam_wpos;
float cam_dist_2 = dot(cam_diff, cam_diff);
// 3 meters away glow returns to the maximum strength.
strength *= clamp(cam_dist_2 / 9.0, 0.25, 1.0);
#endif
vec3 light_color = srgb_to_linear(L.light_col.rgb) * strength;
const float LIGHT_AMBIANCE = 0.025;
color += light_color
* 0.002
// Constant, *should* const fold
* POINT_GLOW_FACTOR;
apply_point_glow_light(L, wpos, dir, max_dist, color);
}
#endif
// Apply lightning
apply_point_glow_light(Light(last_lightning.xyzw + vec4(0, 0, LIGHTNING_HEIGHT, 0), vec4(vec3(0.5, 0.8, 1) * lightning_intensity() * 0.003, 1)), wpos, dir, max_dist, color);
return color;
}

View File

@ -232,6 +232,33 @@ DirectionalLight get_moon_info(vec4 _dir, float shade_frac/*, vec4 light_pos[2]*
return DirectionalLight(/*dir, */shadow, block/*, get_moon_color(dir), get_moon_brightness(dir)*/);
}
const float LIGHTNING_HEIGHT = 350.0;
float lightning_intensity() {
float time_since_lightning = tick.x - last_lightning.w;
return
// Strength
1000000
// Flash
* max(0.0, 1.0 - time_since_lightning * 1.0)
// Reverb
* max(sin(time_of_day.x * 0.4), 0.0);
}
vec3 lightning_at(vec3 wpos) {
float time_since_lightning = tick.x - last_lightning.w;
if (time_since_lightning < 5.0) {
vec3 diff = wpos + focus_off.xyz - (last_lightning.xyz + vec3(0, 0, LIGHTNING_HEIGHT));
float dist = length(diff);
return vec3(0.5, 0.8, 1.0)
* lightning_intensity()
// Attenuation
/ pow(50.0 + dist, 2);
} else {
return vec3(0.0);
}
}
// // Calculates extra emission and reflectance (due to sunlight / moonlight).
// //
// // reflectence = k_a * i_a + i_a,persistent
@ -431,31 +458,13 @@ float get_sun_diffuse2(DirectionalLight sun_info, DirectionalLight moon_info, ve
}
#endif
float time_since_lightning = tick.x - last_lightning.w;
vec3 lightning = vec3(0.0);
if (time_since_lightning < 5.0) {
vec3 diff = wpos + focus_off.xyz - (last_lightning.xyz + vec3(0, 0, 250));
float dist = length(diff);
lightning = vec3(0.5, 0.8, 1.0)
// Strength
* 1000000
// Flash
* max(0.0, 1.0 - time_since_lightning * 1.0)
// Reverb
* max(sin(time_of_day.x * 0.4), 0.0)
// Direction
* (dot(norm, diff / dist) + 1.0)
// Attenuation
/ pow(50.0 + dist, 2);
}
reflected_light = R_t_r * (
(1.0 - SUN_AMBIANCE) * sun_chroma * sun_shadow * (light_reflection_factor(norm, dir, sun_dir, k_d, k_s, alpha, voxel_norm, voxel_lighting) /*+
light_reflection_factor(norm, dir, normalize(sun_dir + vec3(0.0, 0.1, 0.0)), k_d, k_s, alpha) +
light_reflection_factor(norm, dir, normalize(sun_dir - vec3(0.0, 0.1, 0.0)), k_d, k_s, alpha)*/) +
(1.0 - MOON_AMBIANCE) * moon_chroma * moon_shadow * 1.0 * /*4.0 * */light_reflection_factor(norm, dir, moon_dir, k_d, k_s, alpha, voxel_norm, voxel_lighting) +
emission
) + lightning;
) + lightning_at(wpos);
/* light = sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
diffuse_light =