Unified and fixed attenuation

This commit is contained in:
Joshua Barretto 2022-01-23 16:25:15 +00:00 committed by Imbris
parent b94d560a20
commit a3fcd7f568
14 changed files with 82 additions and 71 deletions

View File

@ -73,7 +73,7 @@ void main() {
float cloud_blend = 1.0;
if (color.a < 1.0) {
cloud_blend = 1.0 - color.a;
dist = 50000;
dist = DIST_CAP;
}
color.rgb = mix(color.rgb, get_cloud_color(color.rgb, dir, cam_pos.xyz, time_of_day.x, dist, 1.0), cloud_blend);

View File

@ -8,7 +8,11 @@
#define LIGHTING_REFLECTION_KIND LIGHTING_REFLECTION_KIND_GLOSSY
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#if (FLUID_MODE == FLUID_MODE_CHEAP)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#elif (FLUID_MODE == FLUID_MODE_SHINY)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_RADIANCE
#endif
#define LIGHTING_DISTRIBUTION_SCHEME LIGHTING_DISTRIBUTION_SCHEME_MICROFACET
@ -183,7 +187,6 @@ void main() {
// 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);
float max_light = 0.0;
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, 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(shade_frac, MIN_SHADOW);
// max_light *= point_shadow * shade_frac;
@ -191,20 +194,18 @@ void main() {
// emitted_light *= point_shadow;
// max_light *= point_shadow;
vec3 cam_attenuation = vec3(1);
float fluid_alt = max(f_pos.z + 1, floor(f_alt + 1));
vec3 mu = medium.x == MEDIUM_WATER ? MU_WATER : vec3(0.0);
#if (FLUID_MODE == FLUID_MODE_SHINY)
// Attenuate sunlight
if (medium.x == 1) {
float fluid_alt = max(f_pos.z + 1, floor(f_alt + 1));
float water_dist = max(fluid_alt - f_pos.z, 0);
vec3 attenuate = pow(vec3(0.5, 0.98, 0.99), vec3(water_dist * 0.1));
emitted_light *= attenuate;
reflected_light *= attenuate;
surf_color *= attenuate;
}
cam_attenuation =
medium.x == MEDIUM_WATER ? compute_attenuation_point(cam_pos.xyz, view_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/f_pos)
: compute_attenuation_point(f_pos, -view_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/cam_pos.xyz);
#endif
max_light += lights_at(f_pos, f_norm, view_dir, k_a, k_d, k_s, alpha, emitted_light, reflected_light);
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
max_light += lights_at(f_pos, f_norm, view_dir, mu, cam_attenuation, fluid_alt, k_a, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
float ao = f_ao * sqrt(f_ao);//0.25 + f_ao * 0.75; ///*pow(f_ao, 0.5)*/f_ao * 0.85 + 0.15;
@ -219,7 +220,7 @@ void main() {
* glow_light(f_pos)
* (max(dot(f_norm, model_glow.xyz / glow_mag) * 0.5 + 0.5, 0.0) + max(1.0 - glow_mag, 0.0));
emitted_light += glow;
emitted_light += glow * cam_attenuation;
reflected_light *= ao;
emitted_light *= ao;

View File

@ -146,6 +146,7 @@ void main() {
// vec3 surf_color = /*srgb_to_linear*/(vec3(0.4, 0.7, 2.0));
float max_light = 0.0;
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, /*time_of_day.x*//*-cam_to_frag*/sun_view_dir/*view_dir*/, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, /*vec3(0.0)*/k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
emitted_light *= not_underground;
reflected_light *= not_underground;
@ -185,9 +186,9 @@ void main() {
// 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 passthrough = clamp(dot(f_norm, -cam_to_frag), 0, 1);
float passthrough = max(dot(f_norm, -cam_to_frag), 0);
float min_refl = 0.0;
if (medium.x != 1) {
if (medium.x != MEDIUM_WATER) {
min_refl = min(emitted_light.r, min(emitted_light.g, emitted_light.b));
}

View File

@ -176,14 +176,14 @@ void main() {
// Squared to account for prior saturation.
float f_light = 1.0;// pow(f_light, 1.5);
vec3 ray_dir;
if (medium.x == 1) {
if (medium.x == MEDIUM_WATER) {
ray_dir = refract(cam_to_frag, -norm, 1.33);
} else {
ray_dir = reflect_ray_dir;
}
vec3 reflect_color = get_sky_color(/*reflect_ray_dir*/ray_dir, time_of_day.x, f_pos, vec3(-100000), 0.125, true);
reflect_color = get_cloud_color(reflect_color, ray_dir, f_pos.xyz, time_of_day.x, 100000.0, 0.1);
reflect_color = get_cloud_color(reflect_color, ray_dir, f_pos.xyz, time_of_day.x, 100000.0, 0.2);
reflect_color *= f_light;
// Prevent the sky affecting light when underground
@ -331,7 +331,7 @@ void main() {
// float log_cam = log(min(cam_attenuation.r, min(cam_attenuation.g, cam_attenuation.b)));
float min_refl = 0.0;
if (medium.x != 1) {
if (medium.x != MEDIUM_WATER) {
min_refl = min(emitted_light.r, min(emitted_light.g, emitted_light.b));
}
vec4 color = vec4(surf_color, (1.0 - passthrough) * 1.0 / (1.0 + min_refl));// * (1.0 - /*log(1.0 + cam_attenuation)*//*cam_attenuation*/1.0 / (2.0 - log_cam)));

View File

@ -178,7 +178,6 @@ float atan2(in float y, in float x) {
return mix(PI/2.0 - atan(x,y), atan(y,x), s);
}
const float DIST_CAP = 50000;
#if (CLOUD_MODE == CLOUD_MODE_ULTRA)
const uint QUALITY = 200u;
#elif (CLOUD_MODE == CLOUD_MODE_HIGH)

View File

@ -44,6 +44,13 @@
#define LIGHTING_DISTRIBUTION_BECKMANN 0
#define LIGHTING_DISTRIBUTION_TROWBRIDGE 1
#define MEDIUM_AIR 0
#define MEDIUM_WATER 1
// An arbitrary value that represents a very far distance (at least as far as the player should be able to see) without
// being too far that we end up with precision issues (used in clouds and elsewhere).
#define DIST_CAP 50000
/* Constants expected to be defined automatically by configuration: */
/*

View File

@ -418,14 +418,6 @@ float get_sun_diffuse2(DirectionalLight sun_info, DirectionalLight moon_info, ve
return rel_luminance(emitted_light + reflected_light);//rel_luminance(emitted_light + reflected_light);//sun_chroma + moon_chroma + PERSISTENT_AMBIANCE;
}
float get_sun_diffuse2(DirectionalLight sun_info, DirectionalLight moon_info, vec3 norm, vec3 dir, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, vec3 voxel_norm, float voxel_lighting, out vec3 emitted_light, out vec3 reflected_light) {
return get_sun_diffuse2(sun_info, moon_info, norm, dir, vec3(0.0), vec3(0.0), vec3(1.0), 0.0, k_a, k_d, k_s, alpha, voxel_norm, voxel_lighting, emitted_light, reflected_light);
}
float get_sun_diffuse2(DirectionalLight sun_info, DirectionalLight moon_info, vec3 norm, vec3 dir, vec3 k_a, vec3 k_d, vec3 k_s, float alpha, out vec3 emitted_light, out vec3 reflected_light) {
return get_sun_diffuse2(sun_info, moon_info, norm, dir, vec3(0.0), vec3(0.0), vec3(1.0), 0.0, k_a, k_d, k_s, alpha, norm, 1.0, emitted_light, reflected_light);
}
// This has been extracted into a function to allow quick exit when detecting a star.
float is_star_at(vec3 dir) {
@ -560,7 +552,7 @@ vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float q
vec3 sky_color = get_sky_light(dir, time_of_day, true);
#else
vec3 sky_color;
if (medium.x == 1) {
if (medium.x == MEDIUM_WATER) {
sky_color = get_sky_light(dir, time_of_day, true);
} else {
vec3 star_dir = normalize(sun_dir.xyz * dir.z + cross(sun_dir.xyz, vec3(0, 1, 0)) * dir.x + vec3(0, 1, 0) * dir.y);
@ -585,7 +577,7 @@ float fog(vec3 f_pos, vec3 focus_pos, uint medium) {
// float min_fog = 0.5;
// float max_fog = 1.0;
// if (medium == 1u) {
// if (medium == MEDIUM_WATER) {
// mist_radius = UNDERWATER_MIST_DIST;
// min_fog = 0.0;
// }

View File

@ -492,7 +492,7 @@ vec3 compute_attenuation(vec3 wpos, vec3 ray_dir, vec3 mu, float surface_alt, ve
return vec3(1.0);
#elif (LIGHTING_TRANSPORT_MODE == LIGHTING_TRANSPORT_MODE_RADIANCE)
#if (LIGHTING_TYPE & LIGHTING_TYPE_TRANSMISSION) != 0
return vec3(1.0);
return vec3(1.0);
#else
// return vec3(1.0);
/*if (mu == vec3(0.0)) {

View File

@ -565,12 +565,12 @@ void main() {
const float R_s2s1 = pow((1.0 - 1.3325) / (1.0 + 1.3325), 2);
const float R_s1s2 = pow((1.3325 - 1.0) / (1.3325 + 1.0), 2);
float cam_alt = alt_at(cam_pos.xy);
float fluid_alt = medium.x == 1u ? max(cam_alt + 1, floor(shadow_alt)) : view_distance.w;
float fluid_alt = medium.x == MEDIUM_WATER ? max(cam_alt + 1, floor(shadow_alt)) : view_distance.w;
float R_s = (f_pos.z < my_alt) ? mix(R_s2s1 * R_s1s0, R_s1s0, medium.x) : mix(R_s2s0, R_s1s2 * R_s2s0, medium.x);
vec3 emitted_light, reflected_light;
vec3 mu = medium.x == 1u/* && f_pos.z <= fluid_alt*/ ? MU_WATER : vec3(0.0);
vec3 mu = medium.x == MEDIUM_WATER/* && f_pos.z <= fluid_alt*/ ? MU_WATER : vec3(0.0);
// NOTE: Default intersection point is camera position, meaning if we fail to intersect we assume the whole camera is in water.
vec3 cam_attenuation = compute_attenuation_point(cam_pos.xyz, view_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/f_pos);
// Use f_norm here for better shadows.

View File

@ -6,7 +6,7 @@
#define LIGHTING_REFLECTION_KIND LIGHTING_REFLECTION_KIND_GLOSSY
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_RADIANCE
#define LIGHTING_DISTRIBUTION_SCHEME LIGHTING_DISTRIBUTION_SCHEME_MICROFACET
@ -75,9 +75,19 @@ void main() {
// To account for prior saturation.
float max_light = 0.0;
max_light += get_sun_diffuse2(sun_info, moon_info, 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);
vec3 cam_attenuation = vec3(1);
float fluid_alt = max(f_pos.z + 1, floor(f_alt + 1));
vec3 mu = medium.x == MEDIUM_WATER ? MU_WATER : vec3(0.0);
#if (FLUID_MODE == FLUID_MODE_SHINY)
cam_attenuation =
medium.x == MEDIUM_WATER ? compute_attenuation_point(cam_pos.xyz, view_dir, MU_WATER, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/f_pos)
: compute_attenuation_point(f_pos, -view_dir, vec3(0), fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/cam_pos.xyz);
#endif
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, view_dir, f_pos, MU_WATER, cam_attenuation, fluid_alt, k_a, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
max_light += lights_at(f_pos, f_norm, view_dir, mu, cam_attenuation, fluid_alt, k_a, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
// Allow particles to glow at night
// TODO: Not this

View File

@ -193,7 +193,7 @@ void main() {
#ifdef EXPERIMENTAL_UNDERWARPER
vec2 uv = uv;
if (medium.x == 1) {
if (medium.x == MEDIUM_WATER) {
uv += sin(uv.yx * 40 + tick.xx * 1.0) * 0.003;
}
#endif
@ -255,7 +255,7 @@ void main() {
vec4 final_color = aa_color;
#if (FLUID_MODE == FLUID_MODE_CHEAP)
if (medium.x == 1u) {
if (medium.x == MEDIUM_WATER) {
final_color *= vec4(0.2, 0.2, 0.8, 1.0);
}
#endif

View File

@ -32,9 +32,9 @@ void main() {
float cam_alt = alt_at(cam_pos.xy);
// float f_alt = alt_at(f_pos.xy);
float fluid_alt = medium.x == 1u ? floor(cam_alt + 1) : view_distance.w;
float fluid_alt = medium.x == MEDIUM_WATER ? floor(cam_alt + 1) : view_distance.w;
// float fluid_alt = max(f_pos.z + 1, floor(f_alt));
vec3 mu = medium.x == 1u /* && f_pos.z <= fluid_alt*/ ? MU_WATER : vec3(0.0);
vec3 mu = medium.x == MEDIUM_WATER /* && f_pos.z <= fluid_alt*/ ? MU_WATER : vec3(0.0);
// vec3 sun_attenuation = compute_attenuation(wpos, -sun_dir, mu, surface_alt, wpos);
vec3 cam_attenuation = compute_attenuation(cam_pos.xyz, -cam_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*//*f_pos*//*vec3(f_pos.xy, fluid_alt)*/cam_pos.xyz);
// vec3 cam_attenuation = compute_attenuation_point(f_pos, -view_dir, mu, fluid_alt, cam_pos.xyz);
@ -47,7 +47,7 @@ void main() {
float dist = 100000.0;
float refractionIndex = medium.x == 1u ? 1.0 / 1.3325 : 1.0;
float refractionIndex = medium.x == MEDIUM_WATER ? 1.0 / 1.3325 : 1.0;
/* if (medium.x == 1u) {
dist = UNDERWATER_MIST_DIST;
} */

View File

@ -6,7 +6,11 @@
#define LIGHTING_REFLECTION_KIND LIGHTING_REFLECTION_KIND_GLOSSY
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#if (FLUID_MODE == FLUID_MODE_CHEAP)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#elif (FLUID_MODE == FLUID_MODE_SHINY)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_RADIANCE
#endif
#define LIGHTING_DISTRIBUTION_SCHEME LIGHTING_DISTRIBUTION_SCHEME_MICROFACET
@ -73,34 +77,33 @@ void main() {
vec3 k_d = vec3(1.0);
vec3 k_s = vec3(R_s);
vec3 emitted_light, reflected_light;
vec3 emitted_light = vec3(1);
vec3 reflected_light = vec3(1);
// Make voxel shadows block the sun and moon
sun_info.block = f_inst_light.x;
moon_info.block = f_inst_light.x;
float max_light = 0.0;
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, view_dir, k_a, k_d, k_s, alpha, emitted_light, reflected_light);
vec3 cam_attenuation = vec3(1);
float fluid_alt = max(f_pos.z + 1, floor(f_alt + 1));
vec3 mu = medium.x == MEDIUM_WATER ? MU_WATER : vec3(0.0);
#if (FLUID_MODE == FLUID_MODE_SHINY)
cam_attenuation =
medium.x == MEDIUM_WATER ? compute_attenuation_point(cam_pos.xyz, view_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/f_pos)
: compute_attenuation_point(f_pos, -view_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/cam_pos.xyz);
#endif
max_light += get_sun_diffuse2(sun_info, moon_info, f_norm, view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
emitted_light *= sun_info.block;
reflected_light *= sun_info.block;
#if (FLUID_MODE == FLUID_MODE_SHINY)
// Attenuate sunlight
if (medium.x == 1) {
float fluid_alt = max(f_pos.z + 1, floor(f_alt + 1));
float water_dist = max(fluid_alt - f_pos.z, 0);
vec3 attenuate = pow(vec3(0.5, 0.98, 0.99), vec3(water_dist * 0.1));
emitted_light *= attenuate;
reflected_light *= attenuate;
}
#endif
max_light += 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, mu, cam_attenuation, fluid_alt, k_a, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
vec3 glow = pow(f_inst_light.y, 3) * 4 * glow_light(f_pos);
emitted_light += glow;
emitted_light += glow * cam_attenuation;
float ao = f_ao;
emitted_light *= ao;

View File

@ -8,9 +8,9 @@
#define LIGHTING_REFLECTION_KIND LIGHTING_REFLECTION_KIND_GLOSSY
#if (FLUID_MODE == FLUID_MODE_CHEAP)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_IMPORTANCE
#elif (FLUID_MODE == FLUID_MODE_SHINY)
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_RADIANCE
#define LIGHTING_TRANSPORT_MODE LIGHTING_TRANSPORT_MODE_RADIANCE
#endif
#define LIGHTING_DISTRIBUTION_SCHEME LIGHTING_DISTRIBUTION_SCHEME_MICROFACET
@ -260,21 +260,19 @@ void main() {
// Compute attenuation due to water from the camera.
vec3 mu = faces_fluid/* && f_pos.z <= fluid_alt*/ ? MU_WATER : vec3(0.0);
// NOTE: Default intersection point is camera position, meaning if we fail to intersect we assume the whole camera is in water.
vec3 cam_attenuation =
medium.x == 1u ? compute_attenuation_point(cam_pos.xyz, view_dir, MU_WATER, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/f_pos)
: compute_attenuation_point(f_pos, -view_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/cam_pos.xyz);
// Computing light attenuation from water.
vec3 emitted_light, reflected_light;
vec3 cam_attenuation =
medium.x == MEDIUM_WATER ? compute_attenuation_point(cam_pos.xyz, view_dir, MU_WATER, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/f_pos)
: compute_attenuation_point(f_pos, -view_dir, mu, fluid_alt, /*cam_pos.z <= fluid_alt ? cam_pos.xyz : f_pos*/cam_pos.xyz);
// Prevent the sky affecting light when underground
float not_underground = clamp((f_pos.z - f_alt) / 128.0 + 1.0, 0.0, 1.0);
// To account for prior saturation
/*float */f_light = faces_fluid ? not_underground * f_light : f_light * sqrt(f_light);
/*float */f_light = faces_fluid ? not_underground : f_light * sqrt(f_light);
emitted_light = vec3(1.0);
reflected_light = vec3(1.0);
vec3 emitted_light = vec3(1.0);
vec3 reflected_light = vec3(1.0);
float sun_diffuse = get_sun_diffuse2(/*time_of_day.x, */sun_info, moon_info, f_norm, view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
max_light += sun_diffuse;
@ -287,8 +285,8 @@ void main() {
max_light *= f_light;
// TODO: Apply AO after this
vec3 glow = glow_light(f_pos) * (pow(f_glow, 3) * 5 + pow(f_glow, 2.0) * 2);
reflected_light += glow * pow(max(dot(face_norm, f_norm), 0), 2);
vec3 glow = glow_light(f_pos) * (pow(f_glow, 3) * 5 + pow(f_glow, 2.0) * 2) * pow(max(dot(face_norm, f_norm), 0), 2);
reflected_light += glow * cam_attenuation;
max_light += lights_at(f_pos, f_norm, view_dir, mu, cam_attenuation, fluid_alt, k_a, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);