Skip to content

Commit 67d3a0f

Browse files
committed
GLSL cleanup: make var_FadeDepth a scalar
Divide out the w coordinate in the vertex shader instead of the fragment shader. Also add comment about hacky depth fraction calc.
1 parent c2afb67 commit 67d3a0f

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/engine/renderer/glsl_source/generic_fp.glsl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ IN(smooth) vec2 var_TexCoords;
3636
IN(smooth) vec4 var_Color;
3737

3838
#if defined(USE_DEPTH_FADE)
39-
IN(smooth) vec2 var_FadeDepth;
39+
IN(smooth) float var_FadeDepth;
4040
uniform 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

src/engine/renderer/glsl_source/generic_vp.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff 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

5353
OUT(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

0 commit comments

Comments
 (0)