From d40712937d150037faa99c6bb977cc97b4e8dfef Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Fri, 12 May 2023 16:06:54 -0700 Subject: [PATCH] Fog should be calculated in the user's world coordinates Fixes #6798 --- shaders/src/fog.fs | 2 +- shaders/src/main.fs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/shaders/src/fog.fs b/shaders/src/fog.fs index fa2d81a460..4c30ecf0cb 100644 --- a/shaders/src/fog.fs +++ b/shaders/src/fog.fs @@ -62,7 +62,7 @@ vec4 fog(vec4 color, highp vec3 view) { // Add sun colored fog when looking towards the sun vec3 sunColor = frameUniforms.lightColorIntensity.rgb * frameUniforms.lightColorIntensity.w; - float sunAmount = max(dot(-shading_view, frameUniforms.lightDirection), 0.0); // between 0 and 1 + float sunAmount = max(dot(normalize(view), frameUniforms.lightDirection), 0.0); // between 0 and 1 float sunInscattering = pow(sunAmount, frameUniforms.fogInscatteringSize); fogColor += sunColor * (sunInscattering * (1.0 - sunTransmittance)); diff --git a/shaders/src/main.fs b/shaders/src/main.fs index 3a516495f3..dc38ed6f18 100644 --- a/shaders/src/main.fs +++ b/shaders/src/main.fs @@ -44,6 +44,13 @@ void main() { #if defined(VARIANT_HAS_FOG) highp vec3 view = getWorldPosition() - getWorldCameraPosition(); + + // fog should be calculated in the "user's world coordinates" so that it's not + // affected by the IBL rotation. We're transofrming a vector, so we should use the + // cofactor (or inverse-transpose), but since we know this matrix is a rigid transform, + // we can use it as is. + view = mulMat3x3Float3(frameUniforms.userWorldFromWorldMatrix, view); + fragColor = fog(fragColor, view); #endif