diff --git a/src/engine/renderer/glsl_source/generic_fp.glsl b/src/engine/renderer/glsl_source/generic_fp.glsl index e9f04ef1df..1a3fef459a 100644 --- a/src/engine/renderer/glsl_source/generic_fp.glsl +++ b/src/engine/renderer/glsl_source/generic_fp.glsl @@ -35,7 +35,7 @@ IN(smooth) vec2 var_TexCoords; IN(smooth) vec4 var_Color; #if defined(USE_DEPTH_FADE) || defined(USE_VERTEX_SPRITE) -IN(smooth) vec2 var_FadeDepth; +IN(smooth) float var_FadeDepth; uniform sampler2D u_DepthMap; #endif @@ -55,7 +55,14 @@ void main() #if defined(USE_DEPTH_FADE) || defined(USE_VERTEX_SPRITE) float depth = texture2D(u_DepthMap, gl_FragCoord.xy / r_FBufSize).x; - float fadeDepth = 0.5 * var_FadeDepth.x / var_FadeDepth.y + 0.5; + + // convert z from normalized device coordinates [-1, 1] + // to window coordinates [0, 1] + float fadeDepth = 0.5 * var_FadeDepth + 0.5; + + // HACK: the (distance from triangle to object behind it) / (shader's depthFade distance) ratio + // is calculated by using (nonlinear) depth values instead of the correct world units, so the + // fade curve will be different depending on the distance to the viewer and znear/zfar color.a *= smoothstep(gl_FragCoord.z, fadeDepth, depth); #endif diff --git a/src/engine/renderer/glsl_source/generic_vp.glsl b/src/engine/renderer/glsl_source/generic_vp.glsl index 3765151031..b2af2e99d5 100644 --- a/src/engine/renderer/glsl_source/generic_vp.glsl +++ b/src/engine/renderer/glsl_source/generic_vp.glsl @@ -42,10 +42,10 @@ uniform mat4 u_ModelMatrix; uniform mat4 u_ModelViewProjectionMatrix; #if defined(USE_VERTEX_SPRITE) -OUT(smooth) vec2 var_FadeDepth; +OUT(smooth) float var_FadeDepth; #elif defined(USE_DEPTH_FADE) uniform float u_DepthScale; -OUT(smooth) vec2 var_FadeDepth; +OUT(smooth) float var_FadeDepth; #endif OUT(smooth) vec2 var_TexCoords; @@ -101,10 +101,10 @@ void main() #if defined(USE_DEPTH_FADE) // compute z of end of fading effect vec4 fadeDepth = u_ModelViewProjectionMatrix * (position - u_DepthScale * vec4(LB.normal, 0.0)); - var_FadeDepth = fadeDepth.zw; + var_FadeDepth = fadeDepth.z / fadeDepth.w; #elif defined(USE_VERTEX_SPRITE) vec4 fadeDepth = u_ModelViewProjectionMatrix * (position - depthScale * vec4(LB.normal, 0.0)); - var_FadeDepth = fadeDepth.zw; + var_FadeDepth = fadeDepth.z / fadeDepth.w; #endif var_Color = color;