pub const GROUND_VISIBILITY_WGSL: &str = r"
#ifdef TERRAIN
fn ground_depth_opacity(ndc: vec2<f32>, w: f32, depth_factor: f32) -> f32 {
if (any(abs(ndc) > vec2<f32>(1.0))) {
return 1.0;
}
let size = vec2<i32>(textureDimensions(ground_depth));
let pos = (vec2<f32>(ndc.x, -ndc.y) * 0.5 + 0.5) * vec2<f32>(size);
let texel = clamp(vec2<i32>(floor(pos)), vec2<i32>(0), size - 1);
let bytes = vec4<u32>(round(textureLoad(ground_depth, texel, 0) * 255.0));
let ground = bitcast<f32>(bytes.x | (bytes.y << 8u) | (bytes.z << 16u) | (bytes.w << 24u));
if (!(ground > 0.0)) {
return 1.0;
}
let d = depth_factor * (1.0 / w - 1.0 / ground) + 0.0001;
return 1.0 - clamp(-500.0 * d, 0.0, 1.0);
}
fn ground_visibility(clip: vec4<f32>, view_height: f32) -> f32 {
if (clip.w <= 0.0) {
return 1.0;
}
let ndc = clip.xy / clip.w;
let depth_factor = view_height / 25.0;
let visibility = ground_depth_opacity(ndc, clip.w, depth_factor);
if (visibility > 0.95) {
return 1.0;
}
return (visibility + ground_depth_opacity(ndc + vec2<f32>(0.0, 0.01), clip.w, depth_factor)) / 2.0;
}
#endif
";
pub const GROUND_VISIBILITY_MSL: &str = r"
#if defined(TERRAIN)
static float ground_depth_opacity(float2 ndc, float w, float depth_factor, texture2d<float, access::read> depth) {
if (any(abs(ndc) > float2(1.0))) {
return 1.0;
}
const int2 size = int2(int(depth.get_width()), int(depth.get_height()));
const float2 pos = (float2(ndc.x, -ndc.y) * 0.5 + 0.5) * float2(size);
const int2 texel = clamp(int2(floor(pos)), int2(0), size - 1);
const uint4 bytes = uint4(round(depth.read(uint2(texel)) * 255.0));
const float ground = as_type<float>(bytes.x | (bytes.y << 8) | (bytes.z << 16) | (bytes.w << 24));
if (!(ground > 0.0)) {
return 1.0;
}
const float d = depth_factor * (1.0 / w - 1.0 / ground) + 0.0001;
return 1.0 - clamp(-500.0 * d, 0.0, 1.0);
}
static float ground_visibility(float4 clip, float view_height, texture2d<float, access::read> depth) {
if (clip.w <= 0.0) {
return 1.0;
}
const float2 ndc = clip.xy / clip.w;
const float depth_factor = view_height / 25.0;
const float visibility = ground_depth_opacity(ndc, clip.w, depth_factor, depth);
if (visibility > 0.95) {
return 1.0;
}
return (visibility + ground_depth_opacity(ndc + float2(0.0, 0.01), clip.w, depth_factor, depth)) / 2.0;
}
#endif
";
pub const GROUND_VISIBILITY_GLSL: &str = r"
#ifdef TERRAIN
highp float ground_depth_opacity(highp vec2 ndc, highp float w, highp float depth_factor) {
if (any(greaterThan(abs(ndc), vec2(1.0)))) {
return 1.0;
}
ivec2 size = textureSize(u_ground_depth, 0);
#ifdef MLN_GL
highp vec2 pos = (ndc * 0.5 + 0.5) * vec2(size);
#else
highp vec2 pos = (vec2(ndc.x, -ndc.y) * 0.5 + 0.5) * vec2(size);
#endif
ivec2 texel = clamp(ivec2(floor(pos)), ivec2(0), size - 1);
highp uvec4 bytes = uvec4(round(texelFetch(u_ground_depth, texel, 0) * 255.0));
highp float ground = uintBitsToFloat(bytes.x | (bytes.y << 8u) | (bytes.z << 16u) | (bytes.w << 24u));
if (!(ground > 0.0)) {
return 1.0;
}
highp float d = depth_factor * (1.0 / w - 1.0 / ground) + 0.0001;
return 1.0 - clamp(-500.0 * d, 0.0, 1.0);
}
highp float ground_visibility(highp vec4 clip, highp float view_height) {
if (clip.w <= 0.0) {
return 1.0;
}
highp vec2 ndc = clip.xy / clip.w;
highp float depth_factor = view_height / 25.0;
highp float visibility = ground_depth_opacity(ndc, clip.w, depth_factor);
if (visibility > 0.95) {
return 1.0;
}
return (visibility + ground_depth_opacity(ndc + vec2(0.0, 0.01), clip.w, depth_factor)) / 2.0;
}
#endif
";