Stabilize Vulkan rendering state for minimap, foliage, and water

This commit is contained in:
Kelsi 2026-02-22 09:34:27 -08:00
parent 8efc1548dc
commit bd0305f6dd
10 changed files with 834 additions and 117 deletions

View file

@ -38,7 +38,26 @@ layout(location = 0) out vec4 outColor;
void main() {
vec4 texColor = hasTexture != 0 ? texture(uTexture, TexCoord) : vec4(1.0);
if (alphaTest != 0 && texColor.a < 0.5) discard;
float alphaCutoff = 0.5;
if (alphaTest == 2) {
// Vegetation cutout: lower threshold to preserve leaf coverage at grazing angles.
alphaCutoff = 0.33;
} else if (alphaTest != 0) {
alphaCutoff = 0.35;
}
if (alphaTest == 2) {
float alpha = texColor.a;
float softBand = 0.12;
if (alpha < (alphaCutoff - softBand)) discard;
if (alpha < alphaCutoff) {
vec2 p = floor(gl_FragCoord.xy);
float n = fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
float keep = clamp((alpha - (alphaCutoff - softBand)) / softBand, 0.0, 1.0);
if (n > keep) discard;
}
} else if (alphaTest != 0 && texColor.a < alphaCutoff) {
discard;
}
if (colorKeyBlack != 0) {
float lum = dot(texColor.rgb, vec3(0.299, 0.587, 0.114));
if (lum < colorKeyThreshold) discard;
@ -46,10 +65,12 @@ void main() {
if (blendMode == 1 && texColor.a < 0.004) discard;
vec3 norm = normalize(Normal);
if (!gl_FrontFacing) norm = -norm;
bool foliageTwoSided = (alphaTest == 2);
if (!foliageTwoSided && !gl_FrontFacing) norm = -norm;
vec3 ldir = normalize(-lightDir.xyz);
float diff = max(dot(norm, ldir), 0.0);
float nDotL = dot(norm, ldir);
float diff = foliageTwoSided ? abs(nDotL) : max(nDotL, 0.0);
vec3 result;
if (unlit != 0) {
@ -64,10 +85,11 @@ void main() {
vec4 lsPos = lightSpaceMatrix * vec4(FragPos, 1.0);
vec3 proj = lsPos.xyz / lsPos.w * 0.5 + 0.5;
if (proj.z <= 1.0) {
float bias = max(0.005 * (1.0 - dot(norm, ldir)), 0.001);
float bias = max(0.005 * (1.0 - abs(dot(norm, ldir))), 0.001);
shadow = texture(uShadowMap, vec3(proj.xy, proj.z - bias));
}
shadow = mix(1.0, shadow, shadowParams.y);
if (foliageTwoSided) shadow = max(shadow, 0.45);
}
result = ambientColor.rgb * texColor.rgb
@ -82,5 +104,16 @@ void main() {
float fogFactor = clamp((fogParams.y - dist) / (fogParams.y - fogParams.x), 0.0, 1.0);
result = mix(fogColor.rgb, result, fogFactor);
outColor = vec4(result, texColor.a * fadeAlpha);
float outAlpha = texColor.a * fadeAlpha;
// Cutout materials should not remain partially transparent after discard,
// otherwise foliage cards look view-dependent.
if (alphaTest != 0 || colorKeyBlack != 0) {
outAlpha = fadeAlpha;
}
// Foliage cutout should stay opaque after alpha discard to avoid
// view-angle translucency artifacts.
if (alphaTest == 2) {
outAlpha = 1.0 * fadeAlpha;
}
outColor = vec4(result, outAlpha);
}