Files
blight/blight-assets/src/main/resources/Shaders/BlightFog.frag

38 lines
1.2 KiB
GLSL

#import "Common/ShaderLib/GLSLCompat.glsllib"
uniform sampler2D m_Texture;
uniform sampler2D m_DepthTexture;
uniform vec4 m_FogColor;
uniform float m_FogDensity;
uniform float m_FogDistance;
// Kamera-Frustum (near, far) — wird jeden Frame aus der echten Kamera gesetzt
uniform vec2 m_FrustumNearFar;
in vec2 texCoord;
void main() {
vec4 sceneColor = texture2D(m_Texture, texCoord);
float rawDepth = texture2D(m_DepthTexture, texCoord).r;
// Sky-Pixel (Depth-Buffer = 1.0) nicht nebeln
if (rawDepth >= 1.0) {
gl_FragColor = sceneColor;
return;
}
float near = m_FrustumNearFar.x;
float far = m_FrustumNearFar.y;
// Depth-Buffer [0,1] → echte Weltdistanz (Meter)
// Herleitung: z_view = near*far / (far - rawDepth*(far-near))
float linearDist = (near * far) / (far - rawDepth * (far - near));
// Normiert auf FogDistance; d=1 entspricht genau der eingestellten Nebel-Distanz
float d = linearDist / m_FogDistance;
// Exponential-squared fog (gleiche Formel wie JME FogFilter, korrekte Tiefe)
float fogFactor = clamp(exp2(-m_FogDensity * m_FogDensity * d * d * 1.4426950408), 0.0, 1.0);
gl_FragColor = mix(m_FogColor, sceneColor, fogFactor);
}