From b0ac9f36f755dd06f7c17c46150568064790864f Mon Sep 17 00:00:00 2001 From: Joshua Yanovski Date: Fri, 3 Apr 2020 07:56:11 +0200 Subject: [PATCH] Use bicubic interpolation for terrain. --- assets/voxygen/shaders/include/lod.glsl | 46 +++++++++++++++++++- assets/voxygen/shaders/lod-terrain-frag.glsl | 5 +-- voxygen/src/hud/settings_window.rs | 7 ++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/assets/voxygen/shaders/include/lod.glsl b/assets/voxygen/shaders/include/lod.glsl index ab39ed72f9..c9a2e64474 100644 --- a/assets/voxygen/shaders/include/lod.glsl +++ b/assets/voxygen/shaders/include/lod.glsl @@ -7,6 +7,50 @@ vec2 pos_to_uv(vec2 pos) { return vec2(uv_pos.x, 1.0 - uv_pos.y); } +// textureBicubic from https://stackoverflow.com/a/42179924 +vec4 cubic(float v) { + vec4 n = vec4(1.0, 2.0, 3.0, 4.0) - v; + vec4 s = n * n * n; + float x = s.x; + float y = s.y - 4.0 * s.x; + float z = s.z - 4.0 * s.y + 6.0 * s.x; + float w = 6.0 - x - y - z; + return vec4(x, y, z, w) * (1.0/6.0); +} + +vec4 textureBicubic(sampler2D sampler, vec2 texCoords) { + vec2 texSize = textureSize(sampler, 0); + vec2 invTexSize = 1.0 / texSize; + + texCoords = texCoords * texSize - 0.5; + + + vec2 fxy = fract(texCoords); + texCoords -= fxy; + + vec4 xcubic = cubic(fxy.x); + vec4 ycubic = cubic(fxy.y); + + vec4 c = texCoords.xxyy + vec2 (-0.5, +1.5).xyxy; + + vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw); + vec4 offset = c + vec4 (xcubic.yw, ycubic.yw) / s; + + offset *= invTexSize.xxyy; + + vec4 sample0 = texture(sampler, offset.xz); + vec4 sample1 = texture(sampler, offset.yz); + vec4 sample2 = texture(sampler, offset.xw); + vec4 sample3 = texture(sampler, offset.yw); + + float sx = s.x / (s.x + s.y); + float sy = s.z / (s.z + s.w); + + return mix( + mix(sample3, sample2, sx), mix(sample1, sample0, sx) + , sy); +} + float alt_at(vec2 pos) { return texture(t_map, pos_to_uv(pos)).a * (1300.0) + 140.0; //+ (texture(t_noise, pos * 0.002).x - 0.5) * 64.0; @@ -52,6 +96,6 @@ vec3 lod_pos(vec2 v_pos, vec2 focus_pos) { vec3 lod_col(vec2 pos) { //return vec3(0, 0.5, 0); - return texture(t_map, pos_to_uv(pos)).rgb; + return textureBicubic(t_map, pos_to_uv(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; } diff --git a/assets/voxygen/shaders/lod-terrain-frag.glsl b/assets/voxygen/shaders/lod-terrain-frag.glsl index 4ae0f782b6..cdad89b649 100644 --- a/assets/voxygen/shaders/lod-terrain-frag.glsl +++ b/assets/voxygen/shaders/lod-terrain-frag.glsl @@ -2,7 +2,6 @@ #include #include -#include #include in vec3 f_pos; @@ -19,7 +18,7 @@ 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(srgb_to_linear(f_col), light, diffuse_light, ambient_light); + vec3 surf_color = illuminate(f_col, light, diffuse_light, ambient_light); float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x); @@ -27,7 +26,7 @@ void main() { vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, cam_pos.xyz, f_pos, 1.0, true, clouds); vec3 color = mix(mix(surf_color, fog_color, fog_level), clouds.rgb, clouds.a); - float mist_factor = max(1 - (f_pos.z + (texture(t_noise, f_pos.xy * 0.0005 + time_of_day.x * 0.001).x - 0.5) * 128.0) / 400.0, 0.0); + float mist_factor = max(1 - (f_pos.z + (texture(t_noise, f_pos.xy * 0.0005 + time_of_day.x * 0.0003).x - 0.5) * 128.0) / 400.0, 0.0); //float mist_factor = f_norm.z * 2.0; color = mix(color, vec3(1.0) * diffuse_light, clamp(mist_factor * 0.00005 * distance(f_pos.xy, focus_pos.xy), 0, 0.3)); diff --git a/voxygen/src/hud/settings_window.rs b/voxygen/src/hud/settings_window.rs index d59dc38dc0..aa8f9869e0 100644 --- a/voxygen/src/hud/settings_window.rs +++ b/voxygen/src/hud/settings_window.rs @@ -1612,7 +1612,8 @@ impl<'a> Widget for SettingsWindow<'a> { .set(state.ids.lod_detail_text, ui); if let Some(new_val) = ImageSlider::discrete( - ((self.global_state.settings.graphics.lod_detail as f32 / 100.0).log(5.0) * 10.0).round() as i32, + ((self.global_state.settings.graphics.lod_detail as f32 / 100.0).log(5.0) * 10.0) + .round() as i32, 0, 20, self.imgs.slider_indicator, @@ -1625,7 +1626,9 @@ impl<'a> Widget for SettingsWindow<'a> { .pad_track((5.0, 5.0)) .set(state.ids.lod_detail_slider, ui) { - events.push(Event::AdjustLodDetail((5.0f32.powf(new_val as f32 / 10.0) * 100.0) as u32)); + events.push(Event::AdjustLodDetail( + (5.0f32.powf(new_val as f32 / 10.0) * 100.0) as u32, + )); } Text::new(&format!(