More proper HDR.

This commit is contained in:
Joshua Yanovski 2020-04-27 13:13:23 +02:00
parent 4c6da3ed16
commit e0452e895c
12 changed files with 254 additions and 109 deletions

View File

@ -77,11 +77,13 @@ void main() {
// vec3 point_light = light_at(f_pos, f_norm);
// vec3 light, diffuse_light, ambient_light;
//get_sun_diffuse(f_norm, time_of_day.x, view_dir, k_a * point_shadow * (shade_frac * 0.5 + light_frac * 0.5), k_d * point_shadow * shade_frac, k_s * point_shadow * shade_frac, alpha, emitted_light, reflected_light);
get_sun_diffuse2(f_norm, sun_dir, moon_dir, view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, emitted_light, reflected_light);
float max_light = 0.0;
max_light += get_sun_diffuse2(f_norm, sun_dir, moon_dir, view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, emitted_light, reflected_light);
reflected_light *= point_shadow * shade_frac;
emitted_light *= point_shadow;
max_light *= point_shadow * shade_frac;
lights_at(f_pos, f_norm, view_dir, k_a, k_d, k_s, alpha, emitted_light, reflected_light);
max_light += lights_at(f_pos, f_norm, view_dir, k_a, k_d, k_s, alpha, emitted_light, reflected_light);
float ao = /*pow(f_ao, 0.5)*/f_ao * 0.85 + 0.15;
@ -100,7 +102,7 @@ void main() {
// diffuse_light += point_light;
// reflected_light += point_light;
// vec3 surf_color = illuminate(srgb_to_linear(model_col.rgb * f_col), light, diffuse_light, ambient_light);
surf_color = illuminate(surf_color * emitted_light, surf_color * reflected_light);
surf_color = illuminate(max_light, surf_color * emitted_light, surf_color * reflected_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec4 clouds;

View File

@ -76,9 +76,11 @@ void main() {
// vec3 light_frac = /*vec3(1.0);*/light_reflection_factor(f_norm/*vec3(0, 0, 1.0)*/, view_dir, vec3(0, 0, -1.0), vec3(1.0), vec3(R_s), alpha);
// vec3 surf_color = /*srgb_to_linear*/(vec3(0.4, 0.7, 2.0));
get_sun_diffuse2(f_norm, /*time_of_day.x*/sun_dir, moon_dir, /*-cam_to_frag*/view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, vec3(0.0), k_s, alpha, emitted_light, reflected_light);
float max_light = 0.0;
max_light += get_sun_diffuse2(f_norm, /*time_of_day.x*/sun_dir, moon_dir, /*-cam_to_frag*/view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, vec3(0.0), k_s, alpha, emitted_light, reflected_light);
reflected_light *= f_light * point_shadow * shade_frac;
emitted_light *= f_light * point_shadow;
max_light *= f_light * point_shadow * shade_frac;
// get_sun_diffuse(f_norm, time_of_day.x, light, diffuse_light, ambient_light, 0.0);
// diffuse_light *= f_light * point_shadow;
// ambient_light *= f_light, point_shadow;
@ -94,15 +96,15 @@ void main() {
reflected_light += point_light; */
vec3 diffuse_light_point = vec3(0.0);
lights_at(f_pos, f_norm, view_dir, k_a, vec3(1.0), vec3(k_s), alpha, emitted_light, diffuse_light_point);
max_light += lights_at(f_pos, f_norm, view_dir, k_a, vec3(1.0), k_s, alpha, emitted_light, diffuse_light_point);
vec3 dump_light = vec3(0.0);
vec3 specular_light_point = vec3(0.0);
lights_at(f_pos, f_norm, view_dir, vec3(0.0), vec3(0.0), /*vec3(1.0)*/k_s, alpha, dump_light, specular_light_point);
diffuse_light_point -= specular_light_point;
float reflected_light_point = length(diffuse_light_point) + f_light * point_shadow;
reflected_light += water_color * k_d * (diffuse_light_point + f_light * point_shadow * shade_frac) + k_s * specular_light_point;
float reflected_light_point = /*length*/(diffuse_light_point.r) + f_light * point_shadow;
reflected_light += k_d * (diffuse_light_point + f_light * point_shadow * shade_frac) + specular_light_point;
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec4 clouds;
@ -110,8 +112,8 @@ void main() {
float passthrough = /*pow(*/dot(faceforward(f_norm, f_norm, cam_to_frag/*view_dir*/), -cam_to_frag/*view_dir*/)/*, 0.5)*/;
vec3 surf_color = illuminate(water_color * emitted_light, /*surf_color * */reflected_light);
vec3 surf_color = illuminate(max_light, water_color * emitted_light, /*surf_color * */fog_color * reflected_light);
vec4 color = mix(vec4(surf_color, 1.0), vec4(surf_color, 1.0 / (1.0 + /*diffuse_light*//*(f_light * point_shadow + point_light)*/reflected_light_point/* * 0.25*/)), passthrough);
tgt_color = mix(color, vec4(fog_color, 0.0), 0.0);
tgt_color = mix(mix(color, vec4(fog_color, 0.0), fog_level), vec4(clouds.rgb, 0.0), clouds.a);
}

View File

@ -141,12 +141,14 @@ void main() {
// 0 = 100% reflection, 1 = translucent water
float passthrough = /*pow(*/dot(faceforward(f_norm, f_norm, cam_to_frag/*view_dir*/), -cam_to_frag/*view_dir*/)/*, 0.5)*/;
get_sun_diffuse2(norm, /*time_of_day.x*/sun_dir, moon_dir, view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, vec3(0.0), /*vec3(f_light * point_shadow)*//*reflect_color*/k_s, alpha, emitted_light, reflected_light);
float max_light = 0.0;
max_light += get_sun_diffuse2(norm, /*time_of_day.x*/sun_dir, moon_dir, view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, vec3(0.0), /*vec3(f_light * point_shadow)*//*reflect_color*/k_s, alpha, emitted_light, reflected_light);
reflected_light *= reflect_color * f_light * point_shadow * shade_frac;
emitted_light *= f_light * point_shadow;
max_light *= f_light * point_shadow * shade_frac;
vec3 diffuse_light_point = vec3(0.0);
lights_at(f_pos, norm, view_dir, k_a, vec3(1.0), /*vec3(0.0)*/k_s, alpha, emitted_light, diffuse_light_point);
max_light += lights_at(f_pos, norm, view_dir, k_a, vec3(1.0), /*vec3(0.0)*/k_s, alpha, emitted_light, diffuse_light_point);
vec3 dump_light = vec3(0.0);
vec3 specular_light_point = vec3(0.0);
@ -167,7 +169,7 @@ void main() {
// diffuse_light += point_light;
// reflected_light += point_light;
// vec3 surf_color = srgb_to_linear(vec3(0.2, 0.5, 1.0)) * light * diffuse_light * ambient_light;
vec3 surf_color = illuminate(water_color * emitted_light, reflected_light);
vec3 surf_color = illuminate(max_light, water_color * emitted_light, reflected_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec4 clouds;

View File

@ -78,6 +78,8 @@ float shadow_at(vec3 wpos, vec3 wnorm) {
float lights_at(vec3 wpos, vec3 wnorm, vec3 cam_to_frag, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, inout vec3 emitted_light, inout vec3 reflected_light/*, out float shadow*/) {
// shadow = 0.0;
vec3 ambient_light = vec3(0.0);
vec3 directed_light = vec3(0.0);
vec3 max_light = vec3(0.0);
const float LIGHT_AMBIENCE = 0.5;
@ -124,8 +126,9 @@ float lights_at(vec3 wpos, vec3 wnorm, vec3 cam_to_frag, vec3 k_a, vec3 k_d, vec
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));
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);
reflected_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;
max_light += square_factor * square_factor * color;
// light += color * (max(0, max(dot(normalize(difference), wnorm), 0.15)) + LIGHT_AMBIENCE);
// Compute emiittance.
// float ambient_sides = clamp(mix(0.15, 0.0, abs(dot(wnorm, light_dir)) * 10000.0), 0.0, 0.15);
@ -136,6 +139,7 @@ float lights_at(vec3 wpos, vec3 wnorm, vec3 cam_to_frag, vec3 k_a, vec3 k_d, vec
// shadow = shadow_at(wpos, wnorm);
// float shadow = shadow_at(wpos, wnorm);
reflected_light += directed_light;
emitted_light += k_a * ambient_light/* * shadow*/;// min(shadow, 1.0);
return 1.0;//ambient_light;
return /*rel_luminance(ambient_light + directed_light)*/rel_luminance(max_light);//ambient_light;
}

View File

@ -188,8 +188,26 @@ float horizon_at(vec3 pos, /*float time_of_day*/vec3 light_dir) {
}
vec2 splay(vec2 pos) {
const float SPLAY_MULT = 1048576.0;
return pos * pow(length(pos) * 0.5, 3.0) * SPLAY_MULT;
// const float SPLAY_MULT = 1048576.0;
float len = length(pos);
// vec2 splayed = pos * pow(len * 0.5, 3.0) * SPLAY_MULT;
const float SQRT_2 = sqrt(2.0) / 2.0;
// /const float CBRT_2 = cbrt(2.0) / 2.0;
// vec2 splayed = pos * (view_distance.x * SQRT_2 + pow(len * 0.5, 3.0) * (SPLAY_MULT - view_distance.x));
vec2 splayed = pos * (view_distance.x * SQRT_2 + pow(len/* * SQRT_2*//* * 0.5*/, 3.0) * (textureSize(t_map, 0) * 32.0 - view_distance.x));
return splayed;
// Radial: pos.x = r - view_distance.x from focus_pos, pos.y = θ from cam_pos to focus_pos on xy plane.
// const float PI_2 = 3.1415926535897932384626433832795;
// float squared = pos.x * pos.x;
// // // vec2 splayed2 = pos * vec2(squared * (SPLAY_MULT - view_distance.x), PI);
// vec2 splayed2 = pos * vec2(squared * (textureSize(t_map, 0).x * 32.0 - view_distance.x), PI);
// float r = splayed2.x + view_distance.x;
// vec2 theta = vec2(cos(splayed2.y), sin(splayed2.y));
// return r * theta;
// // mat2 rot_mat = mat2(vec2(theta.x, -theta.y), theta.yx);
// // return r * /*normalize(normalize(focus_pos.xy - cam_pos.xy) + theta);*/rot_mat * normalize(focus_pos.xy - cam_pos.xy);
// return splayed;
}
vec3 lod_norm(vec2 f_pos/*vec3 pos*/, vec4 square) {
@ -250,6 +268,7 @@ vec3 lod_pos(vec2 pos, vec2 focus_pos) {
vec3 lod_col(vec2 pos) {
//return vec3(0, 0.5, 0);
return /*linear_to_srgb*/(textureBicubic(t_map, pos_to_tex(pos)).rgb);
return /*linear_to_srgb*/(textureBicubic(t_map, pos_to_tex(pos)).rgb)
+ (texture(t_noise, pos * 0.04 + texture(t_noise, pos * 0.005).xy * 2.0 + texture(t_noise, pos * 0.06).xy * 0.6).x - 0.5) * 0.1;
//+ (texture(t_noise, pos * 0.04 + texture(t_noise, pos * 0.005).xy * 2.0 + texture(t_noise, pos * 0.06).xy * 0.6).x - 0.5) * 0.1;
}

View File

@ -20,6 +20,7 @@ const vec3 SKY_NIGHT_TOP = vec3(0.001, 0.001, 0.0025);
const vec3 SKY_NIGHT_MID = vec3(0.001, 0.005, 0.02);
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.0, 0.0, 0.0);
const float UNDERWATER_MIST_DIST = 100.0;
@ -45,14 +46,14 @@ vec3 get_moon_dir(float time_of_day) {
return normalize(-vec3(sin(moon_angle_rad), 0.0, cos(moon_angle_rad) - 0.5));
}
const float PERSISTENT_AMBIANCE = 0.0125; // 0.1;// 0.025; // 0.1;
const float PERSISTENT_AMBIANCE = 0.00125; // 0.1;// 0.025; // 0.1;
float get_sun_brightness(vec3 sun_dir) {
return max(-sun_dir.z + 0.6, 0.0) * 0.9;
}
float get_moon_brightness(vec3 moon_dir) {
return max(-moon_dir.z + 0.6, 0.0) * 0.07;
return max(-moon_dir.z + 0.6, 0.0) * 0.007;
}
vec3 get_sun_color(vec3 sun_dir) {
@ -71,59 +72,59 @@ vec3 get_moon_color(vec3 moon_dir) {
return vec3(0.05, 0.05, 0.6);
}
// Calculates extra emission and reflectance (due to sunlight / moonlight).
// // Calculates extra emission and reflectance (due to sunlight / moonlight).
// //
// // reflectence = k_a * i_a + i_a,persistent
// // emittence = Σ { m ∈ lights } i_m * shadow_m * get_light_reflected(light_m)
// //
// // Note that any shadowing to be done that would block the sun and moon, aside from heightmap shadowing (that will be
// // implemented sooon), should be implicitly provided via k_a, k_d, and k_s. For instance, shadowing via ambient occlusion.
// //
// // Also note that the emitted light calculation is kind of lame... we probabbly need something a bit nicer if we ever want to do
// // 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, 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 / 2.0;// 0.1 / 3.0;
//
// reflectence = k_a * i_a + i_a,persistent
// emittence = Σ { m ∈ lights } i_m * shadow_m * get_light_reflected(light_m)
// vec3 sun_dir = get_sun_dir(time_of_day);
// vec3 moon_dir = get_moon_dir(time_of_day);
//
// Note that any shadowing to be done that would block the sun and moon, aside from heightmap shadowing (that will be
// implemented sooon), should be implicitly provided via k_a, k_d, and k_s. For instance, shadowing via ambient occlusion.
// float sun_light = get_sun_brightness(sun_dir);
// float moon_light = get_moon_brightness(moon_dir);
//
// Also note that the emitted light calculation is kind of lame... we probabbly need something a bit nicer if we ever want to do
// 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, 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 / 2.0;// 0.1 / 3.0;
vec3 sun_dir = get_sun_dir(time_of_day);
vec3 moon_dir = get_moon_dir(time_of_day);
float sun_light = get_sun_brightness(sun_dir);
float moon_light = get_moon_brightness(moon_dir);
vec3 sun_color = get_sun_color(sun_dir);
vec3 moon_color = get_moon_color(moon_dir);
vec3 sun_chroma = sun_color * sun_light;
vec3 moon_chroma = moon_color * moon_light;
/* float NLsun = max(dot(-norm, sun_dir), 0);
float NLmoon = max(dot(-norm, moon_dir), 0);
vec3 E = -dir; */
// 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).
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 = 0.5 - 0.5 * abs(dot(-norm, sun_dir));
emitted_light = k_a * (ambient_sides + vec3(SUN_AMBIANCE * sun_light + moon_light)) + PERSISTENT_AMBIANCE;
// TODO: Add shadows.
reflected_light =
sun_chroma * light_reflection_factor(norm, dir, sun_dir, k_d, k_s, alpha) +
moon_chroma * 1.0 * /*4.0 * */light_reflection_factor(norm, dir, moon_dir, k_d, k_s, alpha);
/* light = sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
diffuse_light =
sun_chroma * mix(1.0, max(dot(-norm, sun_dir) * 0.5 + 0.5, 0.0), diffusion) +
moon_chroma * mix(1.0, pow(dot(-norm, moon_dir) * 2.0, 2.0), diffusion) +
PERSISTENT_AMBIANCE;
ambient_light = vec3(SUN_AMBIANCE * sun_light + moon_light); */
}
// vec3 sun_color = get_sun_color(sun_dir);
// vec3 moon_color = get_moon_color(moon_dir);
//
// vec3 sun_chroma = sun_color * sun_light;
// vec3 moon_chroma = moon_color * moon_light;
//
// /* float NLsun = max(dot(-norm, sun_dir), 0);
// float NLmoon = max(dot(-norm, moon_dir), 0);
// vec3 E = -dir; */
//
// // 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).
// 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 = 0.5 - 0.5 * abs(dot(-norm, sun_dir));
//
// emitted_light = k_a * (ambient_sides + vec3(SUN_AMBIANCE * sun_light + moon_light)) + PERSISTENT_AMBIANCE;
// // TODO: Add shadows.
// reflected_light =
// sun_chroma * light_reflection_factor(norm, dir, sun_dir, k_d, k_s, alpha) +
// moon_chroma * 1.0 * /*4.0 * */light_reflection_factor(norm, dir, moon_dir, k_d, k_s, alpha);
//
// /* light = sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
// diffuse_light =
// sun_chroma * mix(1.0, max(dot(-norm, sun_dir) * 0.5 + 0.5, 0.0), diffusion) +
// moon_chroma * mix(1.0, pow(dot(-norm, moon_dir) * 2.0, 2.0), diffusion) +
// PERSISTENT_AMBIANCE;
// ambient_light = vec3(SUN_AMBIANCE * sun_light + moon_light); */
// }
// 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) {
const float SUN_AMBIANCE = 0.23 / 3.0;/* / 1.8*/;// 0.1 / 3.0;
const float SUN_COLOR_FACTOR = 6.0;
const float SUN_COLOR_FACTOR = 6.0;//1.8;
const float MOON_AMBIANCE = 0.23;//0.1;
float sun_light = get_sun_brightness(sun_dir);
@ -222,7 +223,7 @@ float get_sun_diffuse2(vec3 norm, vec3 sun_dir, vec3 moon_dir, vec3 dir, vec3 k_
vec3 R_t_r = R_d + R_r;
// vec3 half_vec = normalize(-norm + dir);
vec3 light_frac = R_t_b * (sun_chroma * SUN_AMBIANCE + moon_chroma * MOON_AMBIANCE) * light_reflection_factor(norm, /*norm*/dir, /*-norm*/-dir, /*k_d*/k_d * (1.0 - k_s), /*k_s*/vec3(0.0), alpha);
vec3 light_frac = R_t_b * (sun_chroma * SUN_AMBIANCE + moon_chroma * MOON_AMBIANCE) * light_reflection_factor(norm, /*norm*//*dir*/dir, /*-norm*/-dir, /*k_d*/k_d/* * (1.0 - k_s)*/, /*k_s*/vec3(0.0), alpha);
// vec3 light_frac = /*vec3(1.0)*//*H_d * */
// SUN_AMBIANCE * /*sun_light*/sun_chroma * light_reflection_factor(norm, dir, /*vec3(0, 0, -1.0)*/-norm, vec3((1.0 + cos_sun) * 0.5), vec3(k_s * (1.0 - cos_sun) * 0.5), alpha) +
// MOON_AMBIANCE * /*sun_light*/moon_chroma * light_reflection_factor(norm, dir, /*vec3(0, 0, -1.0)*/-norm, vec3((1.0 + cos_moon) * 0.5), vec3(k_s * (1.0 - cos_moon) * 0.5), alpha);
@ -254,7 +255,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) +
PERSISTENT_AMBIANCE;
ambient_light = vec3(SUN_AMBIANCE * sun_light + moon_light); */
return 1.0;//sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
return rel_luminance(emitted_light + reflected_light);//sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
}
@ -388,18 +389,19 @@ float fog(vec3 f_pos, vec3 focus_pos, uint medium) {
return pow(clamp((max(fog, mist) - min_fog) / (max_fog - min_fog), 0.0, 1.0), 1.7);
}
float rel_luminance(vec3 rgb)
{
// https://en.wikipedia.org/wiki/Relative_luminance
const vec3 W = vec3(0.2126, 0.7152, 0.0722);
return dot(rgb, W);
}
/* vec3 illuminate(vec3 color, vec3 light, vec3 diffuse, vec3 ambience) {
float avg_col = (color.r + color.g + color.b) / 3.0;
return ((color - avg_col) * light + (diffuse + ambience) * avg_col) * (diffuse + ambience);
} */
vec3 illuminate(/*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 DUSK_EXPOSURE = 1.0;//0.8;
const float DAY_EXPOSURE = 2.0;//0.7;
const float DAY_SATURATION = 1.0;
const float DUSK_SATURATION = 1.0;
const float NIGHT_SATURATION = 0.1;
const float gamma = /*0.5*//*1.*0*/1.0;//1.0;
/* float light = length(emitted + reflected);
float color = srgb_to_linear(emitted + reflected);
@ -411,17 +413,59 @@ vec3 illuminate(/*vec3 max_light, */vec3 emitted, vec3 reflected) {
// Tone mapped value.
// vec3 T = /*color*//*lum*/color;//normalize(color) * lum / (1.0 + lum);
float alpha = 2.0;
// float alpha = 0.5;//2.0;
vec3 sun_dir = get_sun_dir(time_of_day.x);
float alpha = mix(
mix(
DUSK_EXPOSURE,
NIGHT_EXPOSURE,
max(sun_dir.z, 0)
),
DAY_EXPOSURE,
max(-sun_dir.z, 0)
);
alpha = min(alpha, alpha / /*(1.0 + max_light)*/(1.0 + lum));
vec3 col_adjusted = lum == 0.0 ? vec3(0.0) : color / lum;
// float L = lum == 0.0 ? 0.0 : log(lum);
// // float B = T;
// // float B = L + log(alpha);
// float B = lum;
// float D = L - B;
// float o = 0.0;//log(PERSISTENT_AMBIANCE);
// float scale = /*-alpha*/-alpha;//1.0;
// float B_ = (B - o) * scale;
// // float T = lum;
// float O = exp(B_ + D);
float T = 1.0 - exp(-alpha * lum);//lum / (1.0 + lum);
// float T = lum;
// Heuristic desaturation
const float s = 0.8;
vec3 col_adjusted = lum == 0.0 ? vec3(0.0) : color / lum;
// vec3 c = pow(col_adjusted, vec3(s)) * T;
// const float s = 0.8;
float s = mix(
mix(
DUSK_SATURATION,
NIGHT_SATURATION,
max(sun_dir.z, 0)
),
DAY_SATURATION,
max(-sun_dir.z, 0)
);
// s = max(s, (max_light) / (1.0 + s));
s = max(s, max_light / (1.0 + max_light));
vec3 c = pow(col_adjusted, vec3(s)) * T;
// vec3 c = col_adjusted * T;
// vec3 c = sqrt(col_adjusted) * T;
vec3 c = /*col_adjusted * */col_adjusted * T;
// vec3 c = /*col_adjusted * */col_adjusted * T;
return c;
// float sum_col = color.r + color.g + color.b;

View File

@ -109,8 +109,11 @@ vec3 FresnelBlend_f(vec3 norm, vec3 dir, vec3 light_dir, vec3 R_d, vec3 R_s, flo
// Vector3f wh = wi + wo;
vec3 wh = -light_dir + dir;
if (cos_wi <= 0.0 || cos_wo <= 0.0) {
return vec3(0.0);
return vec3(/*diffuse*/0.0);
}
// if (cos_wo < 0.0) {
// return /*vec3(0.0)*/diffuse;
// }
/* if (cos_wi == 0.0 || cos_wo == 0.0) {
return vec3(0.0);
} */
@ -197,3 +200,10 @@ vec3 light_reflection_factor(vec3 norm, vec3 dir, vec3 light_dir, vec3 k_d, vec3
// // }
// // return vec3(0.0);
}
float rel_luminance(vec3 rgb)
{
// https://en.wikipedia.org/wiki/Relative_luminance
const vec3 W = vec3(0.2126, 0.7152, 0.0722);
return dot(rgb, W);
}

View File

@ -16,7 +16,7 @@ out vec4 tgt_color;
void main() {
// vec3 f_pos = lod_pos(f_pos.xy);
vec3 f_col = lod_col(f_pos.xy);
// vec3 f_col = lod_col(f_pos.xy);
// vec4 vert_pos4 = view_mat * vec4(f_pos, 1.0);
// vec3 view_dir = normalize(-vec3(vert_pos4)/* / vert_pos4.w*/);
@ -35,8 +35,53 @@ void main() {
// which_norm = 0.5 + which_norm * 0.5;
which_norm = pow(max(0.0, which_norm), /*0.03125*/1 / 8.0);// * 0.5;
// which_norm = mix(0.0, 1.0, which_norm > 0.0);
// vec3 normals[6] = vec3[](vec3(-1,0,0), vec3(1,0,0), vec3(0,-1,0), vec3(0,1,0), vec3(0,0,-1), vec3(0,0,1));
vec3 f_norm = mix(faceforward(f_norm, cam_pos.xyz - f_pos, -f_norm), my_norm, which_norm);
vec3 f_pos = mix(f_pos, my_pos, which_norm);
// vec3 fract_pos = fract(f_pos);
/* if (length(f_pos - cam_pos.xyz) <= view_distance.x + 32.0) {
vec4 new_f_pos;
float depth = 10000000.0;
vec4 old_coord = all_mat * vec4(f_pos.xyz, 1.0);
for (int i = 0; i < 6; i ++) {
// vec4 square = focus_pos.xy + vec4(splay(pos - vec2(1.0, 1.0), splay(pos + vec2(1.0, 1.0))));
vec3 my_f_norm = normals[i];
vec3 my_f_tan = normals[(i + 2) % 6];
vec3 my_f_bitan = normals[(i + 4) % 6];
mat4 foo = mat4(vec4(my_f_tan, 0), vec4(my_f_bitan, 0), vec4(my_f_norm, 0), vec4(0, 0, 0, 1));
mat4 invfoo = foo * inverse(foo * all_mat);
vec4 my_f_pos = invfoo * (old_coord);//vec4(f_pos, 1.0);
vec4 my_f_proj = all_mat * my_f_pos;
if (my_f_proj.z <= depth) {
new_f_pos = my_f_pos;
f_norm = my_f_norm;
depth = my_f_proj.z;
}
}
// f_pos = new_f_pos.xyz;
} */
// Test for distance to all 6 sides of the enclosing cube.
// if (/*any(lessThan(fract(f_pos.xy), 0.01))*/fract_pos.x <= 0.1) {
// f_norm = faceforward(vec3(-1, 0, 0), f_norm, vec3(1, 0, 0));
// f_tan = vec3(0, 1, 0);
// } else if (fract_pos.y <= 0.1) {
// f_norm = faceforward(vec3(0, -1, 0), f_norm, vec3(0, 1, 0));
// f_tan = vec3(0, 0, 1);
// } else {
// f_norm = faceforward(vec3(0, 0, -1), f_norm, vec3(0, 0, 1));
// f_tan = vec3(1, 0, 0);
// }
// vec3 f_bitan = cross(f_norm, f_tan);
// mat4 foo = mat4(vec4(f_tan, 0), vec4(f_bitan, 0), vec4(f_norm, 0), vec4(0, 0, 0, 1));
// mat4 invfoo = foo * inverse(foo * all_mat);
// vec3 old_coord = all_mat * vec4(f_pos.xyz, 1.0);
// vec4 new_f_pos = invfoo * (old_coord);//vec4(f_pos, 1.0);
vec3 f_col = lod_col(f_pos.xy);
// vec3 f_norm = faceforward(f_norm, cam_pos.xyz - f_pos, -f_norm);
// vec3 f_up = faceforward(cam_pos.xyz - f_pos, vec3(0.0, 0.0, -1.0), cam_pos.xyz - f_pos);
// vec3 f_norm = faceforward(f_norm, /*vec3(cam_pos.xyz - f_pos.xyz)*/vec3(0.0, 0.0, -1.0), f_norm);
vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
vec3 view_dir = -cam_to_frag;
@ -90,7 +135,7 @@ void main() {
float shade_frac = sun_shade_frac + moon_shade_frac;
// float brightness_denominator = (ambient_sides + vec3(SUN_AMBIANCE * sun_light + moon_light);
float alpha = 1.0;//sqrt(2.0);
float alpha = 1.0;//0.1;//0.2;///1.0;//sqrt(2.0);
const float n2 = 1.01;
const float R_s2s0 = pow((1.0 - n2) / (1.0 + n2), 2);
const float R_s1s0 = pow((1.3325 - n2) / (1.3325 + n2), 2);
@ -104,9 +149,11 @@ void main() {
// vec3 light, diffuse_light, ambient_light;
// get_sun_diffuse(f_norm, time_of_day.x, cam_to_frag, (0.25 * shade_frac + 0.25 * light_frac) * f_col, 0.5 * shade_frac * f_col, 0.5 * shade_frac * /*vec3(1.0)*/f_col, 2.0, emitted_light, reflected_light);
get_sun_diffuse2(f_norm/*l_norm*/, sun_dir, moon_dir, view_dir, vec3(1.0)/* * (0.5 * light_frac + vec3(0.5 * shade_frac))*/, vec3(1.0), /*0.5 * shade_frac * *//*vec3(1.0)*//*f_col*/vec3(R_s), alpha, emitted_light, reflected_light);
float max_light = 0.0;
max_light += get_sun_diffuse2(f_norm/*l_norm*/, sun_dir, moon_dir, view_dir, vec3(1.0)/* * (0.5 * light_frac + vec3(0.5 * shade_frac))*/, vec3(1.0), /*0.5 * shade_frac * *//*vec3(1.0)*//*f_col*/vec3(R_s), alpha, emitted_light, reflected_light);
// emitted_light = vec3(1.0);
reflected_light *= shade_frac;
max_light *= shade_frac;
// reflected_light = vec3(0.0);
// emitted_light += 0.5 * vec3(SUN_AMBIANCE * sun_shade_frac * sun_light + moon_shade_frac * moon_light) * f_col * (ambient_sides + 1.0);
@ -117,8 +164,8 @@ void main() {
// vec3 light, diffuse_light, ambient_light;
// get_sun_diffuse(f_norm, time_of_day.x, light, diffuse_light, ambient_light, 1.0);
// vec3 surf_color = illuminate(f_col, light, diffuse_light, ambient_light);
f_col = f_col + hash(vec4(floor(vec3(focus_pos.xy + splay(v_pos_orig), f_pos.z)) * 3.0/* - round(f_norm) * 0.5*/, 0)) * 0.05; // Small-scale noise
vec3 surf_color = /*illuminate(emitted_light, reflected_light)*/illuminate(f_col * emitted_light, f_col * reflected_light);
// f_col = f_col + (hash(vec4(floor(vec3(focus_pos.xy + splay(v_pos_orig), f_pos.z)) * 3.0 - round(f_norm) * 0.5, 0)) - 0.5) * 0.05; // Small-scale noise
vec3 surf_color = /*illuminate(emitted_light, reflected_light)*/illuminate(max_light, f_col * emitted_light, f_col * reflected_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);

View File

@ -70,9 +70,11 @@ void main() {
// vec3 point_light = light_at(f_pos, f_norm);
// vec3 surf_color = srgb_to_linear(vec3(0.2, 0.5, 1.0));
// vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
get_sun_diffuse2(f_norm, /*time_of_day.x, */sun_dir, moon_dir, /*cam_to_frag*/view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, emitted_light, reflected_light);
float max_light = 0.0;
max_light += get_sun_diffuse2(f_norm, /*time_of_day.x, */sun_dir, moon_dir, /*cam_to_frag*/view_dir, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, emitted_light, reflected_light);
reflected_light *= vert_light * point_shadow * shade_frac;
emitted_light *= vert_light * point_shadow;
max_light *= vert_light * point_shadow * shade_frac;
// get_sun_diffuse(f_norm, time_of_day.x, light, diffuse_light, ambient_light, 1.0);
// float point_shadow = shadow_at(f_pos, f_norm);
// diffuse_light *= f_light * point_shadow;
@ -81,7 +83,7 @@ void main() {
// diffuse_light += point_light;
// reflected_light += point_light;
lights_at(f_pos, f_norm, cam_to_frag, k_a, k_d, k_s, alpha, emitted_light, reflected_light);
max_light += lights_at(f_pos, f_norm, cam_to_frag, k_a, k_d, k_s, alpha, emitted_light, reflected_light);
/* vec3 point_light = light_at(f_pos, f_norm);
emitted_light += point_light;
reflected_light += point_light; */
@ -90,7 +92,7 @@ void main() {
emitted_light *= ao;
reflected_light *= ao;
surf_color = illuminate(surf_color * emitted_light, surf_color * reflected_light);
surf_color = illuminate(max_light, surf_color * emitted_light, surf_color * reflected_light);
// vec3 surf_color = illuminate(f_col, light, diffuse_light, ambient_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);

View File

@ -75,6 +75,7 @@ void main() {
emitted_light *= f_light * point_shadow;
reflected_light *= f_light * point_shadow * shade_frac;
max_light *= f_light * point_shadow * shade_frac;
max_light += lights_at(f_pos, f_norm, view_dir, k_a, k_d, k_s, alpha, emitted_light, reflected_light);
@ -104,7 +105,7 @@ void main() {
// vec3 surf_color = illuminate(srgb_to_linear(f_col), light, diffuse_light, ambient_light);
vec3 col = srgb_to_linear(f_col + hash(vec4(floor(f_chunk_pos * 3.0 - f_norm * 0.5), 0)) * 0.02); // Small-scale noise
surf_color = illuminate(col * emitted_light, col * reflected_light);
surf_color = illuminate(max_light, col * emitted_light, col * reflected_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec4 clouds;

View File

@ -94,13 +94,13 @@ impl Scene {
settings: &Settings,
backdrop: Option<&str>,
) -> Self {
let start_angle = 90.0f32.to_radians();
let resolution = renderer.get_resolution().map(|e| e as f32);
let mut camera = Camera::new(resolution.x / resolution.y, CameraMode::ThirdPerson);
camera.set_focus_pos(Vec3::unit_z() * 1.5);
camera.set_distance(3.0); // 4.2
camera.set_orientation(Vec3::new(0.0, 0.0, 0.0));
camera.set_orientation(Vec3::new(start_angle, 0.0, 0.0));
Self {
globals: renderer.create_consts(&[Globals::default()]).unwrap(),
lights: renderer.create_consts(&[Light::default(); 32]).unwrap(),
@ -124,6 +124,19 @@ impl Scene {
figure_state: FigureState::new(renderer, CharacterSkeleton::new()),
backdrop: backdrop.map(|specifier| {
let mut state = FigureState::new(renderer, FixtureSkeleton::new());
state.update(
renderer,
Vec3::zero(),
Vec3::new(start_angle.sin(), -start_angle.cos(), 0.0),
1.0,
Rgba::broadcast(1.0),
15.0, // Want to get there immediately.
1.0,
0,
true,
false,
);
(
renderer
.create_model(&load_mesh(
@ -132,12 +145,12 @@ impl Scene {
generate_mesh,
))
.unwrap(),
FigureState::new(renderer, FixtureSkeleton::new()),
state
)
}),
turning: false,
char_ori: 0.0,
char_ori: /*0.0*/-start_angle,
}
}
@ -170,8 +183,11 @@ impl Scene {
}
pub fn maintain(&mut self, renderer: &mut Renderer, scene_data: SceneData) {
self.camera
.update(scene_data.time, 1.0 / 60.0, scene_data.mouse_smoothing);
self.camera.update(
scene_data.time,
/* 1.0 / 60.0 */ scene_data.delta_time,
scene_data.mouse_smoothing,
);
self.camera.compute_dependents(&VoidVol);
let camera::Dependents {
@ -180,16 +196,16 @@ impl Scene {
cam_pos,
} = self.camera.dependents();
const VD: f32 = 115.0; // View Distance
const LOD: f32 = 100.0; // LOD detail
// const MAP_BOUNDS: Vec2<f32> = Vec2::new(140.0, 2048.0);
const TIME: f64 = 43200.0; // 12 hours*3600 seconds
const TIME: f64 = 10.0 * 60.0 * 60.0; //43200.0; // 12 hours*3600 seconds
if let Err(err) = renderer.update_consts(&mut self.globals, &[Globals::new(
view_mat,
proj_mat,
cam_pos,
self.camera.get_focus_pos(),
VD,
LOD,
self.lod.tgt_detail as f32,
self.map_bounds, //MAP_BOUNDS,
TIME,
scene_data.time,
@ -226,7 +242,7 @@ impl Scene {
Vec3::new(self.char_ori.sin(), -self.char_ori.cos(), 0.0),
1.0,
Rgba::broadcast(1.0),
1.0 / 60.0, // TODO: Use actual deltatime here?
scene_data.delta_time,
1.0,
0,
true,

View File

@ -7,7 +7,6 @@ use crate::{
};
use common::{
terrain::{structure::StructureBlock, Block, BlockKind, Structure},
util::{linear_to_srgb, srgb_to_linear},
vol::{ReadVol, Vox},
};
use std::ops::{Add, Div, Mul, Neg};
@ -566,7 +565,7 @@ pub fn block_from_structure(
let saturate_leaves = |col: Rgb<f32>| {
// /*saturate_srgb(col / 255.0, 0.65)*/
let rgb = srgb_to_linear(col / 255.0);
/* let rgb = srgb_to_linear(col / 255.0);
/* let mut xyy = rgb_to_xyy(rgb);
xyy.x *= xyy.x;
xyy.y *= xyy.y;
@ -582,7 +581,8 @@ pub fn block_from_structure(
/* let mut hsv = rgb_to_hsv(rgb);
hsv.y *= hsv.y;
linear_to_srgb(hsv_to_rgb(hsv).map(|e| e.min(1.0).max(0.0))).map(|e| e * 255.0) */
linear_to_srgb(rgb * rgb).map(|e| e * 255.0)
linear_to_srgb(rgb * rgb).map(|e| e * 255.0) */
col
};
match sblock {
@ -598,12 +598,8 @@ pub fn block_from_structure(
)),
StructureBlock::PineLeaves => Some(Block::new(
BlockKind::Leaves,
Lerp::lerp(
Rgb::new(0.0, 60.0, 50.0),
Rgb::new(30.0, 100.0, 10.0),
lerp,
)
.map(|e| e as u8),
Lerp::lerp(Rgb::new(0.0, 60.0, 50.0), Rgb::new(30.0, 100.0, 10.0), lerp)
.map(|e| e as u8),
)),
StructureBlock::PalmLeavesInner => Some(Block::new(
BlockKind::Leaves,