Skip to content

GLSL cleanup: make var_FadeDepth a scalar #1338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
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)
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not linearize it then? I assume the different fading based on near/far plane etc. is not intended.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed like more effort than it's worth since I would have to plumb through the near/far values (which can apparently be different for every view) with a uniform. There's an r_zNear GLSL define, but it seems to have a tenuous (at best) connection with the value which is actually used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, zFar is supposed to cover the whole map in whatever direction, while zNear can change for portals. depthtile1_vp does this, albeit with r_zNear, but if it's discreet enough then it's fine as is I guess.

// 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
4 changes: 2 additions & 2 deletions src/engine/renderer/glsl_source/generic_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ uniform mat4 u_ModelViewProjectionMatrix;

#if 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 @@ -96,7 +96,7 @@ 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;
#endif

var_Color = color;
Expand Down