veloren/assets/voxygen/shaders/include/lod.glsl

102 lines
2.7 KiB
Plaintext
Raw Normal View History

2019-11-21 20:57:46 +00:00
#include <random.glsl>
2020-02-21 13:48:40 +00:00
uniform sampler2D t_map;
vec2 pos_to_uv(vec2 pos) {
2020-02-24 15:14:06 +00:00
vec2 uv_pos = (pos + 16) / 32768.0;
2020-02-21 13:48:40 +00:00
return vec2(uv_pos.x, 1.0 - uv_pos.y);
}
2020-04-03 05:56:11 +00:00
// 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);
}
2019-11-21 20:57:46 +00:00
float alt_at(vec2 pos) {
2020-02-21 16:15:13 +00:00
return texture(t_map, pos_to_uv(pos)).a * (1300.0) + 140.0;
2020-02-24 17:49:53 +00:00
//+ (texture(t_noise, pos * 0.002).x - 0.5) * 64.0;
2020-02-21 13:48:40 +00:00
2019-11-21 20:57:46 +00:00
return 0.0
+ pow(texture(t_noise, pos * 0.00005).x * 1.4, 3.0) * 1000.0
+ texture(t_noise, pos * 0.001).x * 100.0
+ texture(t_noise, pos * 0.003).x * 30.0;
}
2020-02-24 12:38:53 +00:00
vec2 splay(vec2 pos) {
return pos * pow(length(pos) * 0.5, 3.0);
2019-11-21 20:57:46 +00:00
}
vec3 lod_norm(vec2 pos) {
2020-02-24 12:38:53 +00:00
const float SAMPLE_W = 32;
2020-02-23 16:53:17 +00:00
float altx0 = alt_at(pos + vec2(-1, 0) * SAMPLE_W);
float altx1 = alt_at(pos + vec2(1, 0) * SAMPLE_W);
float alty0 = alt_at(pos + vec2(0, -1) * SAMPLE_W);
float alty1 = alt_at(pos + vec2(0, 1) * SAMPLE_W);
float slope = abs(altx1 - altx0) + abs(alty0 - alty1);
2019-11-21 20:57:46 +00:00
return normalize(vec3(
2020-02-23 16:53:17 +00:00
(altx0 - altx1) / SAMPLE_W,
(alty0 - alty1) / SAMPLE_W,
SAMPLE_W / (slope + 0.00001) // Avoid NaN
2019-11-21 20:57:46 +00:00
));
}
vec3 lod_pos(vec2 v_pos, vec2 focus_pos) {
2020-02-24 12:38:53 +00:00
vec2 hpos = focus_pos.xy + splay(v_pos) * 1000000.0;
// Remove spiking by "pushing" vertices towards local optima
vec2 nhpos = hpos;
2020-02-24 12:54:55 +00:00
for (int i = 0; i < 3; i ++) {
nhpos -= lod_norm(hpos).xy * 15.0;
}
2020-02-24 12:38:53 +00:00
hpos = hpos + normalize(nhpos - hpos + 0.001) * min(length(nhpos - hpos), 32);
return vec3(hpos, alt_at(hpos));
}
2019-11-21 20:57:46 +00:00
vec3 lod_col(vec2 pos) {
2020-02-24 12:38:53 +00:00
//return vec3(0, 0.5, 0);
2020-04-03 05:56:11 +00:00
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;
2019-11-21 20:57:46 +00:00
}