Skip to content

Commit 1e22fe7

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 2a1a0e2 commit 1e22fe7

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/engine/renderer/glsl_source/generic_fp.glsl

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ IN(smooth) vec2 var_TexCoords;
3535
IN(smooth) vec4 var_Color;
3636

3737
#if defined(USE_DEPTH_FADE) || defined(USE_VERTEX_SPRITE)
38-
IN(smooth) vec2 var_FadeDepth;
38+
IN(smooth) float var_FadeDepth;
3939
uniform sampler2D u_DepthMap;
4040
#endif
4141

@@ -55,7 +55,14 @@ void main()
5555

5656
#if defined(USE_DEPTH_FADE) || defined(USE_VERTEX_SPRITE)
5757
float depth = texture2D(u_DepthMap, gl_FragCoord.xy / r_FBufSize).x;
58-
float fadeDepth = 0.5 * var_FadeDepth.x / var_FadeDepth.y + 0.5;
58+
59+
// convert z from normalized device coordinates [-1, 1]
60+
// to window coordinates [0, 1]
61+
float fadeDepth = 0.5 * var_FadeDepth + 0.5;
62+
63+
// HACK: the (distance from triangle to object behind it) / (shader's depthFade distance) ratio
64+
// is calculated by using (nonlinear) depth values instead of the correct world units, so the
65+
// fade curve will be different depending on the distance to the viewer and znear/zfar
5966
color.a *= smoothstep(gl_FragCoord.z, fadeDepth, depth);
6067
#endif
6168

src/engine/renderer/glsl_source/generic_vp.glsl

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ uniform mat4 u_ModelMatrix;
4242
uniform mat4 u_ModelViewProjectionMatrix;
4343

4444
#if defined(USE_VERTEX_SPRITE)
45-
OUT(smooth) vec2 var_FadeDepth;
45+
OUT(smooth) float var_FadeDepth;
4646
#elif defined(USE_DEPTH_FADE)
4747
uniform float u_DepthScale;
48-
OUT(smooth) vec2 var_FadeDepth;
48+
OUT(smooth) float var_FadeDepth;
4949
#endif
5050

5151
OUT(smooth) vec2 var_TexCoords;
@@ -101,10 +101,10 @@ void main()
101101
#if defined(USE_DEPTH_FADE)
102102
// compute z of end of fading effect
103103
vec4 fadeDepth = u_ModelViewProjectionMatrix * (position - u_DepthScale * vec4(LB.normal, 0.0));
104-
var_FadeDepth = fadeDepth.zw;
104+
var_FadeDepth = fadeDepth.z / fadeDepth.w;
105105
#elif defined(USE_VERTEX_SPRITE)
106106
vec4 fadeDepth = u_ModelViewProjectionMatrix * (position - depthScale * vec4(LB.normal, 0.0));
107-
var_FadeDepth = fadeDepth.zw;
107+
var_FadeDepth = fadeDepth.z / fadeDepth.w;
108108
#endif
109109

110110
var_Color = color;

0 commit comments

Comments
 (0)