first commit
This commit is contained in:
66
addons/zylann.hterrain/shaders/include/heightmap.gdshaderinc
Executable file
66
addons/zylann.hterrain/shaders/include/heightmap.gdshaderinc
Executable file
@@ -0,0 +1,66 @@
|
||||
|
||||
// Use functions from this file everywhere a heightmap is used,
|
||||
// so it is easy to track and change the format
|
||||
|
||||
float sample_heightmap(sampler2D spl, vec2 pos) {
|
||||
// RF
|
||||
return texture(spl, pos).r;
|
||||
}
|
||||
|
||||
vec4 encode_height_to_viewport(float h) {
|
||||
//return vec4(encode_height_to_rgb8_unorm(h), 1.0);
|
||||
|
||||
// Encode regular floats into an assumed RGBA8 output color.
|
||||
// This is used because Godot 4.0 doesn't support RF viewports,
|
||||
// and the irony is, even if float viewports get supported, it's likely it will end up RGBAF,
|
||||
// which is wasting bandwidth because we are only interested in R...
|
||||
uint u = floatBitsToUint(h);
|
||||
return vec4(
|
||||
float((u >> 0u) & 255u),
|
||||
float((u >> 8u) & 255u),
|
||||
float((u >> 16u) & 255u),
|
||||
float((u >> 24u) & 255u)
|
||||
) / vec4(255.0);
|
||||
}
|
||||
|
||||
float decode_height_from_viewport(vec4 c) {
|
||||
uint u = uint(c.r * 255.0)
|
||||
| (uint(c.g * 255.0) << 8u)
|
||||
| (uint(c.b * 255.0) << 16u)
|
||||
| (uint(c.a * 255.0) << 24u);
|
||||
return uintBitsToFloat(u);
|
||||
}
|
||||
|
||||
float sample_height_from_viewport(sampler2D screen, vec2 uv) {
|
||||
ivec2 ts = textureSize(screen, 0);
|
||||
vec2 norm_to_px = vec2(ts);
|
||||
|
||||
// Convert to pixels and apply a small offset so we interpolate from pixel centers
|
||||
vec2 uv_px_f = uv * norm_to_px - vec2(0.5);
|
||||
|
||||
ivec2 uv_px = ivec2(uv_px_f);
|
||||
|
||||
// Get interpolation pixel positions
|
||||
ivec2 p00 = uv_px;
|
||||
ivec2 p10 = uv_px + ivec2(1, 0);
|
||||
ivec2 p01 = uv_px + ivec2(0, 1);
|
||||
ivec2 p11 = uv_px + ivec2(1, 1);
|
||||
|
||||
// Get pixels
|
||||
vec4 c00 = texelFetch(screen, p00, 0);
|
||||
vec4 c10 = texelFetch(screen, p10, 0);
|
||||
vec4 c01 = texelFetch(screen, p01, 0);
|
||||
vec4 c11 = texelFetch(screen, p11, 0);
|
||||
|
||||
// Decode heights
|
||||
float h00 = decode_height_from_viewport(c00);
|
||||
float h10 = decode_height_from_viewport(c10);
|
||||
float h01 = decode_height_from_viewport(c01);
|
||||
float h11 = decode_height_from_viewport(c11);
|
||||
|
||||
// Linear filter
|
||||
vec2 f = fract(uv_px_f);
|
||||
float h = mix(mix(h00, h10, f.x), mix(h01, h11, f.x), f.y);
|
||||
|
||||
return h;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cejki3yhcmmd8
|
||||
57
addons/zylann.hterrain/shaders/include/heightmap_rgb8_encoding.gdshaderinc
Executable file
57
addons/zylann.hterrain/shaders/include/heightmap_rgb8_encoding.gdshaderinc
Executable file
@@ -0,0 +1,57 @@
|
||||
|
||||
const float V2_UNIT_STEPS = 1024.0;
|
||||
const float V2_MIN = -8192.0;
|
||||
const float V2_MAX = 8191.0;
|
||||
const float V2_DF = 255.0 / V2_UNIT_STEPS;
|
||||
|
||||
float decode_height_from_rgb8_unorm_2(vec3 c) {
|
||||
return (c.r * 0.25 + c.g * 64.0 + c.b * 16384.0) * (4.0 * V2_DF) + V2_MIN;
|
||||
}
|
||||
|
||||
vec3 encode_height_to_rgb8_unorm_2(float h) {
|
||||
// TODO Check if this has float precision issues
|
||||
// TODO Modulo operator might be a performance/compatibility issue
|
||||
h -= V2_MIN;
|
||||
int i = int(h * V2_UNIT_STEPS);
|
||||
int r = i % 256;
|
||||
int g = (i / 256) % 256;
|
||||
int b = i / 65536;
|
||||
return vec3(float(r), float(g), float(b)) / 255.0;
|
||||
}
|
||||
|
||||
float decode_height_from_rgb8_unorm(vec3 c) {
|
||||
return decode_height_from_rgb8_unorm_2(c);
|
||||
}
|
||||
|
||||
vec3 encode_height_to_rgb8_unorm(float h) {
|
||||
return encode_height_to_rgb8_unorm_2(h);
|
||||
}
|
||||
|
||||
// TODO Remove for now?
|
||||
// Bilinear filtering appears to work well enough without doing this.
|
||||
// There are some artifacts, but we could easily live with them,
|
||||
// and I suspect they could be easy to patch somehow in the encoding/decoding.
|
||||
//
|
||||
// In case bilinear filtering is required.
|
||||
// This is slower than if we had a native float format.
|
||||
// Unfortunately, Godot 4 removed support for 2D HDR viewports. They were used
|
||||
// to edit this format natively. Using compute shaders would force users to
|
||||
// have Vulkan. So we had to downgrade performance a bit using a technique from the GLES2 era...
|
||||
float sample_height_bilinear_rgb8_unorm(sampler2D heightmap, vec2 uv) {
|
||||
vec2 ts = vec2(textureSize(heightmap, 0));
|
||||
vec2 p00f = uv * ts;
|
||||
ivec2 p00 = ivec2(p00f);
|
||||
|
||||
vec3 s00 = texelFetch(heightmap, p00, 0).rgb;
|
||||
vec3 s10 = texelFetch(heightmap, p00 + ivec2(1, 0), 0).rgb;
|
||||
vec3 s01 = texelFetch(heightmap, p00 + ivec2(0, 1), 0).rgb;
|
||||
vec3 s11 = texelFetch(heightmap, p00 + ivec2(1, 1), 0).rgb;
|
||||
|
||||
float h00 = decode_height_from_rgb8_unorm(s00);
|
||||
float h10 = decode_height_from_rgb8_unorm(s10);
|
||||
float h01 = decode_height_from_rgb8_unorm(s01);
|
||||
float h11 = decode_height_from_rgb8_unorm(s11);
|
||||
|
||||
vec2 f = p00f - vec2(p00);
|
||||
return mix(mix(h00, h10, f.x), mix(h01, h11, f.x), f.y);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://ctuxl7ko61ko0
|
||||
Reference in New Issue
Block a user