File tree Expand file tree Collapse file tree 2 files changed +12
-5
lines changed
src/engine/renderer/glsl_source Expand file tree Collapse file tree 2 files changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ IN(smooth) vec2 var_TexCoords;
3636IN(smooth ) vec4 var_Color;
3737
3838#if defined(USE_DEPTH_FADE)
39- IN(smooth ) vec2 var_FadeDepth;
39+ IN(smooth ) float var_FadeDepth;
4040uniform sampler2D u_DepthMap;
4141#endif
4242
@@ -65,7 +65,14 @@ void main()
6565
6666#if defined(USE_DEPTH_FADE)
6767 float depth = texture2D (u_DepthMap, gl_FragCoord .xy / r_FBufSize).x;
68- float fadeDepth = 0.5 * var_FadeDepth.x / var_FadeDepth.y + 0.5 ;
68+
69+ // convert z from normalized device coordinates [-1, 1]
70+ // to window coordinates [0, 1]
71+ float fadeDepth = 0.5 * var_FadeDepth + 0.5 ;
72+
73+ // HACK: the (distance from triangle to object behind it) / (shader's depthFade distance) ratio
74+ // is calculated by using (nonlinear) depth values instead of the correct world units, so the
75+ // fade curve will be different depending on the distance to the viewer and znear/zfar
6976 color.a *= smoothstep (gl_FragCoord .z, fadeDepth, depth);
7077#endif
7178
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ uniform mat4 u_ModelViewProjectionMatrix;
4747
4848#if defined(USE_DEPTH_FADE)
4949 uniform float u_DepthScale;
50- OUT(smooth ) vec2 var_FadeDepth;
50+ OUT(smooth ) float var_FadeDepth;
5151#endif
5252
5353OUT(smooth ) vec2 var_TexCoords;
@@ -91,8 +91,8 @@ void main() {
9191
9292 #if defined(USE_DEPTH_FADE)
9393 // compute z of end of fading effect
94- vec4 fadeDepth = u_ModelViewProjectionMatrix * (position - u_DepthScale * vec4 ( LB.normal, 0.0 ) );
95- var_FadeDepth = fadeDepth.zw ;
94+ vec4 fadeDepth = u_ModelViewProjectionMatrix * ( position - u_DepthScale * vec4 (LB.normal, 0.0 ) );
95+ var_FadeDepth = fadeDepth.z / fadeDepth.w ;
9696 #endif
9797
9898 SHADER_PROFILER_SET
You can’t perform that action at this time.
0 commit comments