Use bicubic interpolation for terrain.

This commit is contained in:
Joshua Yanovski 2020-04-03 07:56:11 +02:00
parent f6fc9307a1
commit b0ac9f36f7
3 changed files with 52 additions and 6 deletions

View File

@ -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;
}

View File

@ -2,7 +2,6 @@
#include <globals.glsl>
#include <sky.glsl>
#include <srgb.glsl>
#include <lod.glsl>
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));

View File

@ -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!(