Skip to content

Commit

Permalink
GLSL cleanup: make var_FadeDepth a scalar
Browse files Browse the repository at this point in the history
Divide out the w coordinate in the vertex shader instead of the fragment
shader.

Also add comment about hacky depth fraction calc.
  • Loading branch information
slipher committed Oct 3, 2024
1 parent 2a1a0e2 commit 1e22fe7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/engine/renderer/glsl_source/generic_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/engine/renderer/glsl_source/generic_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1e22fe7

Please sign in to comment.