Proper HDR from point lights.

This commit is contained in:
Joshua Yanovski 2020-04-27 18:14:10 +02:00
parent 48c93d2b41
commit 0594238004
2 changed files with 39 additions and 9 deletions

View File

@ -125,10 +125,31 @@ float lights_at(vec3 wpos, vec3 wnorm, vec3 cam_to_frag, vec3 k_a, vec3 k_d, vec
// light_dir = faceforward(light_dir, wnorm, light_dir); // light_dir = faceforward(light_dir, wnorm, light_dir);
bool is_direct = dot(-light_dir, wnorm) > 0.0; bool is_direct = dot(-light_dir, wnorm) > 0.0;
// reflected_light += color * (distance_2 == 0.0 ? vec3(1.0) : light_reflection_factor(wnorm, cam_to_frag, light_dir, k_d, k_s, alpha)); // reflected_light += color * (distance_2 == 0.0 ? vec3(1.0) : light_reflection_factor(wnorm, cam_to_frag, light_dir, k_d, k_s, alpha));
vec3 direct_light = color * strength * square_factor * light_reflection_factor(wnorm, cam_to_frag, is_direct ? light_dir : -light_dir, k_d, k_s, alpha); vec3 direct_light = PI * color * strength * square_factor * light_reflection_factor(wnorm, cam_to_frag, is_direct ? light_dir : -light_dir, k_d, k_s, alpha);
directed_light += is_direct ? direct_light * square_factor : vec3(0.0); directed_light += is_direct ? direct_light * square_factor : vec3(0.0);
ambient_light += is_direct ? vec3(0.0) : direct_light * LIGHT_AMBIENCE; ambient_light += is_direct ? vec3(0.0) : direct_light * LIGHT_AMBIENCE;
max_light += square_factor * square_factor * color;
vec3 cam_light_diff = light_pos - cam_pos.xyz;
float cam_distance_2 = dot(cam_light_diff, cam_light_diff);// + 0.0001;
float cam_strength = 1.0 / (/*4.0 * *//*PI * *//*1.0 + */cam_distance_2);
vec3 cam_pos_diff = cam_to_frag.xyz - wpos;
float pos_distance_2 = dot(cam_pos_diff, cam_pos_diff);// + 0.0001;
float cam_distance = sqrt(cam_distance_2);
float distance = sqrt(distance_2);
float both_strength = cam_distance_2 == 0.0 ? distance_2 == 0.0 ? 0.0 : strength/* * strength*//*1.0*/ : distance_2 == 0.0 ? cam_strength/* * cam_strength*//*1.0*/ :
// 1.0 / (cam_distance * distance);
// sqrt(cam_strength * strength);
cam_strength + strength;
// (cam_strength * strength);
// max(cam_strength, strength);
// mix(cam_strength, strength, distance_2 / (cam_distance_2 + distance_2));
// mix(cam_strength, strength, cam_distance_2 / (cam_distance_2 + distance_2));
// max(cam_strength, strength);//mix(cam_strength, strength, clamp(distance_2 / /*pos_distance_2*/cam_distance_2, 0.0, 1.0));
// float both_strength = mix(cam_strength, strength, cam_distance_2 / sqrt(cam_distance_2 + distance_2));
max_light += /*max(1.0, cam_strength)*//*min(cam_strength, 1.0)*//*max*//*max(both_strength, 1.0) * *//*cam_strength*/both_strength * square_factor * square_factor * PI * color;
// max_light += /*max(1.0, cam_strength)*//*min(cam_strength, 1.0)*//*max*/max(cam_strength, 1.0/*, strength*//*1.0*/) * square_factor * square_factor * PI * color;
// light += color * (max(0, max(dot(normalize(difference), wnorm), 0.15)) + LIGHT_AMBIENCE); // light += color * (max(0, max(dot(normalize(difference), wnorm), 0.15)) + LIGHT_AMBIENCE);
// Compute emiittance. // Compute emiittance.
// float ambient_sides = clamp(mix(0.15, 0.0, abs(dot(wnorm, light_dir)) * 10000.0), 0.0, 0.15); // float ambient_sides = clamp(mix(0.15, 0.0, abs(dot(wnorm, light_dir)) * 10000.0), 0.0, 0.15);

View File

@ -22,6 +22,8 @@ const vec3 SKY_NIGHT_BOT = vec3(0.002, 0.004, 0.004);
const vec3 NIGHT_LIGHT = vec3(0.002, 0.01, 0.03); const vec3 NIGHT_LIGHT = vec3(0.002, 0.01, 0.03);
// const vec3 NIGHT_LIGHT = vec3(0.0, 0.0, 0.0); // const vec3 NIGHT_LIGHT = vec3(0.0, 0.0, 0.0);
const float SUN_COLOR_FACTOR = 6.0;//1.8;
const float UNDERWATER_MIST_DIST = 100.0; const float UNDERWATER_MIST_DIST = 100.0;
vec3 get_sun_dir(float time_of_day) { vec3 get_sun_dir(float time_of_day) {
@ -124,7 +126,6 @@ vec3 get_moon_color(vec3 moon_dir) {
// Returns computed maximum intensity. // Returns computed maximum intensity.
float get_sun_diffuse2(vec3 norm, vec3 sun_dir, vec3 moon_dir, vec3 dir, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, out vec3 emitted_light, out vec3 reflected_light) { float get_sun_diffuse2(vec3 norm, vec3 sun_dir, vec3 moon_dir, vec3 dir, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, out vec3 emitted_light, out vec3 reflected_light) {
const float SUN_AMBIANCE = 0.23;/* / 1.8*/;// 0.1 / 3.0; const float SUN_AMBIANCE = 0.23;/* / 1.8*/;// 0.1 / 3.0;
const float SUN_COLOR_FACTOR = 6.0;//1.8;
const float MOON_AMBIANCE = 0.23;//0.1; const float MOON_AMBIANCE = 0.23;//0.1;
float sun_light = get_sun_brightness(sun_dir); float sun_light = get_sun_brightness(sun_dir);
@ -255,7 +256,7 @@ float get_sun_diffuse2(vec3 norm, vec3 sun_dir, vec3 moon_dir, vec3 dir, vec3 k_
moon_chroma * mix(1.0, pow(dot(-norm, moon_dir) * 2.0, 2.0), diffusion) + moon_chroma * mix(1.0, pow(dot(-norm, moon_dir) * 2.0, 2.0), diffusion) +
PERSISTENT_AMBIANCE; PERSISTENT_AMBIANCE;
ambient_light = vec3(SUN_AMBIANCE * sun_light + moon_light); */ ambient_light = vec3(SUN_AMBIANCE * sun_light + moon_light); */
return rel_luminance(emitted_light + reflected_light);//sun_chroma + moon_chroma + PERSISTENT_AMBIANCE; return 0.0;//rel_luminance(emitted_light + reflected_light);//sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
} }
@ -395,11 +396,11 @@ float fog(vec3 f_pos, vec3 focus_pos, uint medium) {
} */ } */
vec3 illuminate(float max_light, /*vec3 max_light, */vec3 emitted, vec3 reflected) { vec3 illuminate(float max_light, /*vec3 max_light, */vec3 emitted, vec3 reflected) {
const float NIGHT_EXPOSURE = 10.0; const float NIGHT_EXPOSURE = 10.0;
const float DUSK_EXPOSURE = 1.0;//0.8; const float DUSK_EXPOSURE = 2.0;//0.8;
const float DAY_EXPOSURE = 2.0;//0.7; const float DAY_EXPOSURE = 1.0;//0.7;
const float DAY_SATURATION = 1.0; const float DAY_SATURATION = 1.0;
const float DUSK_SATURATION = 1.0; const float DUSK_SATURATION = 0.5;
const float NIGHT_SATURATION = 0.1; const float NIGHT_SATURATION = 0.1;
const float gamma = /*0.5*//*1.*0*/1.0;//1.0; const float gamma = /*0.5*//*1.*0*/1.0;//1.0;
@ -410,11 +411,17 @@ vec3 illuminate(float max_light, /*vec3 max_light, */vec3 emitted, vec3 reflecte
// float max_intensity = vec3(1.0); // float max_intensity = vec3(1.0);
vec3 color = emitted + reflected; vec3 color = emitted + reflected;
float lum = rel_luminance(color); float lum = rel_luminance(color);
// float lum_sky = lum - max_light;
vec3 sun_dir = get_sun_dir(time_of_day.x);
vec3 moon_dir = get_moon_dir(time_of_day.x);
float sky_light = rel_luminance(
get_sun_color(sun_dir) * get_sun_brightness(sun_dir) * SUN_COLOR_FACTOR +
get_moon_color(moon_dir) * get_moon_brightness(moon_dir));
// Tone mapped value. // Tone mapped value.
// vec3 T = /*color*//*lum*/color;//normalize(color) * lum / (1.0 + lum); // vec3 T = /*color*//*lum*/color;//normalize(color) * lum / (1.0 + lum);
// float alpha = 0.5;//2.0; // float alpha = 0.5;//2.0;
vec3 sun_dir = get_sun_dir(time_of_day.x);
float alpha = mix( float alpha = mix(
mix( mix(
DUSK_EXPOSURE, DUSK_EXPOSURE,
@ -424,7 +431,9 @@ vec3 illuminate(float max_light, /*vec3 max_light, */vec3 emitted, vec3 reflecte
DAY_EXPOSURE, DAY_EXPOSURE,
max(-sun_dir.z, 0) max(-sun_dir.z, 0)
); );
alpha = min(alpha, alpha / /*(1.0 + max_light)*/(1.0 + lum)); float alph = sky_light > 0.0 && max_light > 0.0 ? mix(1.0 / log(/*1.0*//*1.0 + *//*lum_sky + */1.0 + max_light / (0.0 + sky_light)), 1.0, clamp(max_light - sky_light, 0.0, 1.0)) : 1.0;
alpha = alpha * min(alph, 1.0);//((max_light > 0.0 && max_light > sky_light /* && sky_light > 0.0*/) ? /*1.0*/1.0 / log(/*1.0*//*1.0 + *//*lum_sky + */1.0 + max_light - (0.0 + sky_light)) : 1.0);
// alpha = alpha * min(1.0, (max_light == 0.0 ? 1.0 : (1.0 + abs(lum_sky)) / /*(1.0 + max_light)*/max_light));
vec3 col_adjusted = lum == 0.0 ? vec3(0.0) : color / lum; vec3 col_adjusted = lum == 0.0 ? vec3(0.0) : color / lum;