Apply clouds more correctly

This commit is contained in:
Joshua Barretto 2022-10-10 21:27:19 +01:00
parent d4c81cf8b8
commit d40be0ea5f

View File

@ -76,16 +76,6 @@ float depth_at(vec2 uv) {
} }
} }
vec3 dir_at(vec2 uv) {
vec4 view_space = all_mat_inv * vec4((uv * 2.0 - 1.0) * vec2(1, -1), 0.0, 1.0);
view_space /= view_space.w;
return normalize(view_space.xyz);
}
float invlerp(float a, float b, float v) {
return (v - a) / (b - a);
}
void main() { void main() {
vec4 color = texture(sampler2D(t_src_color, s_src_color), uv); vec4 color = texture(sampler2D(t_src_color, s_src_color), uv);
@ -98,27 +88,11 @@ void main() {
float dist = distance(wpos, cam_pos.xyz); float dist = distance(wpos, cam_pos.xyz);
vec3 dir = (wpos - cam_pos.xyz) / dist; vec3 dir = (wpos - cam_pos.xyz) / dist;
/* vec4 clip2 = all_mat * vec4(wpos, 1.0); */
/* vec2 test_uv = (clip2.xy / clip2.w).xy * 0.5 * vec2(1, -1) + 0.5; */
/* color = texture(sampler2D(t_src_color, s_src_color), test_uv); */
// Apply clouds // Apply clouds
float cloud_blend = 1.0; float cloud_blend = 1.0;
bool is_reflection = false; bool is_reflection = false;
if (color.a < 1.0) { if (color.a < 1.0 && medium.x != MEDIUM_WATER) {
cloud_blend = 1.0 - color.a; cloud_blend = 1.0 - color.a;
//color.rgb = vec3(1, 0, 0);
vec2 uv_refl = uv;
vec3 wpos_refl = wpos;
/* for (int i = 0; i < 10; i ++) { */
/* uv_refl.y -= 0.1; */
/* wpos_refl += reflect(dir, vec3(0, 0, 1)) * dist * 0.5; */
/* if (distance(wpos_at(uv_refl), cam_pos.xyz) < distance(wpos_refl, cam_pos.xyz)) { */
/* color.rgb = mix(color.rgb, texture(sampler2D(t_src_color, s_src_color), uv_refl).rgb, 0.9); */
/* break; */
/* } */
/* } */
#ifdef EXPERIMENTAL_SCREENSPACEREFLECTIONS #ifdef EXPERIMENTAL_SCREENSPACEREFLECTIONS
if (dir.z < 0.0) { if (dir.z < 0.0) {
@ -139,7 +113,7 @@ void main() {
#ifdef EXPERIMENTAL_SCREENSPACEREFLECTIONSCASTING #ifdef EXPERIMENTAL_SCREENSPACEREFLECTIONSCASTING
vec3 ray_end = wpos + refl_dir * 5.0 * dist; vec3 ray_end = wpos + refl_dir * 5.0 * dist;
float t = 0.0; float t = 0.0;
float lastd = 0.0; // Trace through the screen-space depth buffer to find the ray intersection
const int MAIN_ITERS = 64; const int MAIN_ITERS = 64;
for (int i = 0; i < MAIN_ITERS; i ++) { for (int i = 0; i < MAIN_ITERS; i ++) {
vec3 swpos = mix(wpos, ray_end, t); vec3 swpos = mix(wpos, ray_end, t);
@ -149,6 +123,7 @@ void main() {
float d = -depth_at(suv); float d = -depth_at(suv);
if (d < svpos.z * 0.8 && d > svpos.z * 0.999) { if (d < svpos.z * 0.8 && d > svpos.z * 0.999) {
t -= 1.0 / float(MAIN_ITERS); t -= 1.0 / float(MAIN_ITERS);
// Do a bit of extra iteration to try to refine the estimate
const int ITERS = 8; const int ITERS = 8;
float diff = 1.0 / float(MAIN_ITERS); float diff = 1.0 / float(MAIN_ITERS);
for (int i = 0; i < ITERS; i ++) { for (int i = 0; i < ITERS; i ++) {
@ -164,9 +139,7 @@ void main() {
} }
break; break;
} }
lastd = d;
t += 1.0 / float(MAIN_ITERS); t += 1.0 / float(MAIN_ITERS);
lastd = d;
} }
#endif #endif
@ -176,7 +149,7 @@ void main() {
float new_dist = distance(new_wpos, cam_pos.xyz); float new_dist = distance(new_wpos, cam_pos.xyz);
float merge = min( float merge = min(
// Off-screen merge factor // Off-screen merge factor
clamp((1.0 - abs(new_uv.y - 0.5) * 2) * 3.0, 0, 0.75), clamp((1.0 - abs(new_uv.y - 0.5) * 2) * 3.0, 0, 1),
// Depth merge factor // Depth merge factor
min( min(
clamp((new_dist - dist * 0.5) / (dist * 0.5), 0.0, 1.0), clamp((new_dist - dist * 0.5) / (dist * 0.5), 0.0, 1.0),
@ -185,17 +158,17 @@ void main() {
); );
if (merge > 0.0) { if (merge > 0.0) {
color.rgb = mix(color.rgb, texture(sampler2D(t_src_color, s_src_color), new_uv).rgb, merge); vec3 new_col = texture(sampler2D(t_src_color, s_src_color), new_uv).rgb;
wpos = new_wpos; new_col = get_cloud_color(new_col.rgb, refl_dir, wpos, time_of_day.x, distance(new_wpos, wpos.xyz), 1.0);
dist = distance(wpos, cam_pos.xyz); color.rgb = mix(color.rgb, new_col, merge);
dir = (wpos - cam_pos.xyz) / dist; cloud_blend = 1;
cloud_blend = merge;
is_reflection = true; is_reflection = true;
} }
} else { } else {
#else #else
{ {
#endif #endif
cloud_blend = 1;
dist = DIST_CAP; dist = DIST_CAP;
} }
} }