HDR fixes.

This commit is contained in:
Joshua Yanovski 2020-04-22 22:56:12 +02:00
parent 1015e60dee
commit ddd4a67a97
3 changed files with 61 additions and 8 deletions

View File

@ -46,7 +46,7 @@ void main() {
float moon_shade_frac = horizon_at(/*f_shadow, f_pos.z, */f_pos, moon_dir); float moon_shade_frac = horizon_at(/*f_shadow, f_pos.z, */f_pos, moon_dir);
float shade_frac = /*1.0;*/sun_shade_frac + moon_shade_frac; float shade_frac = /*1.0;*/sun_shade_frac + moon_shade_frac;
const float alpha = 0.255 / 4.0 / sqrt(2.0); const float alpha = 0.255/* / 4.0 / sqrt(2.0)*/;
const float n2 = 1.3325; const float n2 = 1.3325;
const float R_s = pow((1.0 - n2) / (1.0 + n2), 2); const float R_s = pow((1.0 - n2) / (1.0 + n2), 2);

View File

@ -37,7 +37,7 @@ vec3 get_moon_dir(float time_of_day) {
return normalize(-vec3(sin(moon_angle_rad), 0.0, cos(moon_angle_rad) - 0.5)); return normalize(-vec3(sin(moon_angle_rad), 0.0, cos(moon_angle_rad) - 0.5));
} }
const float PERSISTENT_AMBIANCE = 0.025; // 0.1; // 0.025; // 0.1; const float PERSISTENT_AMBIANCE = 0.0125; // 0.1;// 0.025; // 0.1;
float get_sun_brightness(vec3 sun_dir) { float get_sun_brightness(vec3 sun_dir) {
return max(-sun_dir.z + 0.6, 0.0) * 0.9; return max(-sun_dir.z + 0.6, 0.0) * 0.9;
@ -75,7 +75,7 @@ vec3 get_moon_color(vec3 moon_dir) {
// anything interesting here. // anything interesting here.
// void get_sun_diffuse(vec3 norm, float time_of_day, out vec3 light, out vec3 diffuse_light, out vec3 ambient_light, float diffusion // void get_sun_diffuse(vec3 norm, float time_of_day, out vec3 light, out vec3 diffuse_light, out vec3 ambient_light, float diffusion
void get_sun_diffuse(vec3 norm, float time_of_day, vec3 dir, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, out vec3 emitted_light, out vec3 reflected_light) { void get_sun_diffuse(vec3 norm, float time_of_day, 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.1; const float SUN_AMBIANCE = 0.1 / 3.0;
vec3 sun_dir = get_sun_dir(time_of_day); vec3 sun_dir = get_sun_dir(time_of_day);
vec3 moon_dir = get_moon_dir(time_of_day); vec3 moon_dir = get_moon_dir(time_of_day);
@ -114,7 +114,8 @@ void get_sun_diffuse(vec3 norm, float time_of_day, vec3 dir, vec3 k_a, vec3 k_d,
// 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.1; const float SUN_AMBIANCE = 0.1 / 3.0;
const float MOON_AMBIANCE = 0.1;
float sun_light = get_sun_brightness(sun_dir); float sun_light = get_sun_brightness(sun_dir);
float moon_light = get_moon_brightness(moon_dir); float moon_light = get_moon_brightness(moon_dir);
@ -131,12 +132,13 @@ float get_sun_diffuse2(vec3 norm, vec3 sun_dir, vec3 moon_dir, vec3 dir, vec3 k_
// Globbal illumination "estimate" used to light the faces of voxels which are parallel to the sun or moon (which is a very common occurrence). // Globbal illumination "estimate" used to light the faces of voxels which are parallel to the sun or moon (which is a very common occurrence).
// Will be attenuated by k_d, which is assumed to carry any additional ambient occlusion information (e.g. about shadowing). // Will be attenuated by k_d, which is assumed to carry any additional ambient occlusion information (e.g. about shadowing).
float ambient_sides = 0.0; // float ambient_sides = 0.0;
// float ambient_sides = 0.5 - 0.5 * min(abs(dot(-norm, sun_dir)), abs(dot(-norm, moon_dir))); // float ambient_sides = 0.5 - 0.5 * min(abs(dot(-norm, sun_dir)), abs(dot(-norm, moon_dir)));
// float ambient_sides = clamp(mix(0.5, 0.0, abs(dot(-norm, sun_dir)) * mix(0.0, 1.0, abs(sun_dir.z) * 10000.0) * 10000.0), 0.0, 0.5); // float ambient_sides = clamp(mix(0.5, 0.0, abs(dot(-norm, sun_dir)) * mix(0.0, 1.0, abs(sun_dir.z) * 10000.0) * 10000.0), 0.0, 0.5);
// float ambient_sides = clamp(mix(0.5, 0.0, abs(dot(-norm, sun_dir)) * mix(0.0, 1.0, abs(sun_dir.z) * 10000.0) * 10000.0), 0.0, 0.5); // float ambient_sides = clamp(mix(0.5, 0.0, abs(dot(-norm, sun_dir)) * mix(0.0, 1.0, abs(sun_dir.z) * 10000.0) * 10000.0), 0.0, 0.5);
emitted_light = k_a * (ambient_sides + vec3(SUN_AMBIANCE * sun_light + moon_light)) + PERSISTENT_AMBIANCE; emitted_light = k_a * (/*ambient_sides + */SUN_AMBIANCE * /*sun_light*/sun_chroma + /*vec3(moon_light)*/MOON_AMBIANCE * moon_chroma) + PERSISTENT_AMBIANCE;
// TODO: Add shadows. // TODO: Add shadows.
reflected_light = reflected_light =
sun_chroma * light_reflection_factor(norm, dir, sun_dir, k_d, k_s, alpha) + sun_chroma * light_reflection_factor(norm, dir, sun_dir, k_d, k_s, alpha) +
@ -303,7 +305,20 @@ vec3 illuminate(/*vec3 max_light, */vec3 emitted, vec3 reflected) {
vec3 color = emitted + reflected; vec3 color = emitted + reflected;
float lum = rel_luminance(color); float lum = rel_luminance(color);
return color;//normalize(color) * lum / (1.0 + lum); // Tone mapped value.
// vec3 T = /*color*//*lum*/color;//normalize(color) * lum / (1.0 + lum);
float alpha = 2.0;// 2.0;
float T = 1.0 - exp(-alpha * lum);//lum / (1.0 + lum);
// float T = lum;
// Heuristic desaturation
// float s = 0.5;
vec3 col_adjusted = (color / lum);
// vec3 c = pow(color / lum, vec3(s)) * T;
// vec3 c = sqrt(col_adjusted) * T;
vec3 c = col_adjusted * col_adjusted * T;
return c;
// float sum_col = color.r + color.g + color.b; // float sum_col = color.r + color.g + color.b;
// return /*srgb_to_linear*/(/*0.5*//*0.125 * */vec3(pow(color.x, gamma), pow(color.y, gamma), pow(color.z, gamma))); // return /*srgb_to_linear*/(/*0.5*//*0.125 * */vec3(pow(color.x, gamma), pow(color.y, gamma), pow(color.z, gamma)));
} }

View File

@ -126,13 +126,51 @@ vec3 FresnelBlend_f(vec3 norm, vec3 dir, vec3 light_dir, vec3 R_d, vec3 R_s, flo
// (4 * AbsDot(wi, wh) * // (4 * AbsDot(wi, wh) *
// std::max(AbsCosTheta(wi), AbsCosTheta(wo))) * // std::max(AbsCosTheta(wi), AbsCosTheta(wo))) *
// SchlickFresnel(Dot(wi, wh)); // SchlickFresnel(Dot(wi, wh));
return mix(diffuse + specular, vec3(0.0), bvec3(all(equal(light_dir, dir)))); return mix(/*diffuse*//* + specular*/diffuse + specular, vec3(0.0), bvec3(all(equal(light_dir, dir))));
} }
// Phong reflection. // Phong reflection.
// //
// Note: norm, dir, light_dir must all be normalizd. // Note: norm, dir, light_dir must all be normalizd.
vec3 light_reflection_factor(vec3 norm, vec3 dir, vec3 light_dir, vec3 k_d, vec3 k_s, float alpha) { vec3 light_reflection_factor(vec3 norm, vec3 dir, vec3 light_dir, vec3 k_d, vec3 k_s, float alpha) {
// TODO: These are supposed to be the differential changes in the point location p, in tangent space.
// That is, assuming we can parameterize a 2D surface by some function p : R² → R³, mapping from
// points in a plane to 3D points on the surface, we can define
// ∂p(u,v)/∂u and ∂p(u,v)/∂v representing the changes in the pont location as we move along these
// coordinates.
//
// Then we can define the normal at a point, n(u,v) = ∂p(u,v)/∂u × ∂p(u,v)/∂v.
//
// Additionally, we can define the change in *normals* at each point using the
// Weingarten equations (see http://www.pbr-book.org/3ed-2018/Shapes/Spheres.html):
//
// ∂n/∂u = (fF - eG) / (EG - F²) ∂p/∂u + (eF - fE) / (EG - F²) ∂p/∂v
// ∂n/∂v = (gF - fG) / (EG - F²) ∂p/∂u + (fF - gE) / (EG - F²) ∂p/∂v
//
// where
//
// E = |∂p/∂u ⋅ ∂p/∂u|
// F = ∂p/∂u ⋅ ∂p/∂u
// G = |∂p/∂v ⋅ ∂p/∂v|
//
// and
//
// e = n ⋅ ∂²p/∂u²
// f = n ⋅ ∂²p/(∂u∂v)
// g = n ⋅ ∂²p/∂v²
//
// For planes (see http://www.pbr-book.org/3ed-2018/Shapes/Triangle_Meshes.html) we have
// e = f = g = 0 (since the plane has no curvature of any sort) so we get:
//
// ∂n/∂u = (0, 0, 0)
// ∂n/∂v = (0, 0, 0)
//
// To find ∂p/∂u and ∂p/∂v, we first write p and u parametrically:
// p(u, v) = p0 + u ∂p/∂u + v ∂p/∂v
//
// ( u₀ - u₂ v₀ - v₂
// u₁ - u₂ v₁ - v₂ )
//
// Basis: plane norm = norm = (0, 0, 1), x vector = any orthgonal vector on the plane. // Basis: plane norm = norm = (0, 0, 1), x vector = any orthgonal vector on the plane.
// vec3 w_i = // vec3 w_i =
// vec3 w_i = vec3(view_mat * vec4(-light_dir, 1.0)); // vec3 w_i = vec3(view_mat * vec4(-light_dir, 1.0));