Skip to content

Commit 684a19f

Browse files
authored
Added distance fade and alpha dithering (#531)
* Added distance fade and alpha dithering * Added shader compilation time log
1 parent a36db7f commit 684a19f

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

Resources/Engine/Shaders/Common/Utils.ovfxh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ bool IsOrthographic(mat4 projectionMatrix)
1919
return projectionMatrix[3][3] > 0.5;
2020
}
2121

22+
float DistanceFadeAlpha(vec3 fragPos, vec3 viewPos, float fadeStart, float fadeLength)
23+
{
24+
const float distanceToFragment = length(fragPos - viewPos);
25+
const float alphaBasedOnDistance = 1.0 - (distanceToFragment - fadeStart) / fadeLength;
26+
return alphaBasedOnDistance;
27+
}
28+
29+
float Dithering(float alpha, vec2 fragCoord)
30+
{
31+
const mat4 kThresholdMatrix = mat4(
32+
1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
33+
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
34+
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
35+
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
36+
);
37+
38+
return alpha - kThresholdMatrix[int(fragCoord.x) % 4][int(fragCoord.y) % 4];
39+
}
40+
2241
// Expects a height map with values in the range [0, 1].
2342
// 1.0 means the height is at the maximum depth, 0.0 means the height is at the minimum depth.
2443
vec2 ApplyParallaxOcclusionMapping(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale, int minLayers, int maxLayers)

Resources/Engine/Shaders/Standard.ovfx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#feature SHADOW_PASS
22
#feature PARALLAX_MAPPING
33
#feature ALPHA_CLIPPING
4+
#feature ALPHA_DITHERING
45
#feature NORMAL_MAPPING
6+
#feature DISTANCE_FADE
57

68
#shader vertex
79
#version 450 core
@@ -90,13 +92,27 @@ uniform float u_AlphaClippingThreshold = 0.1;
9092
uniform float u_ShadowClippingThreshold = 0.5;
9193
#endif
9294

95+
#if defined(DISTANCE_FADE)
96+
uniform float u_DistanceFadeStart = 100.0;
97+
uniform float u_DistanceFadeLength = 10.0;
98+
#endif
99+
93100
uniform sampler2D _ShadowMap;
94101
uniform mat4 _LightSpaceMatrix;
95102

96103
out vec4 FRAGMENT_COLOR;
97104

98105
void main()
99106
{
107+
#if defined(DISTANCE_FADE)
108+
const float distanceAlpha = DistanceFadeAlpha(fs_in.FragPos, ubo_ViewPos, u_DistanceFadeStart, u_DistanceFadeLength);
109+
const float ditheredDistanceAlpha = Dithering(distanceAlpha, gl_FragCoord.xy);
110+
if (ditheredDistanceAlpha < 0)
111+
{
112+
discard;
113+
}
114+
#endif
115+
100116
vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);
101117

102118
#if defined(PARALLAX_MAPPING)
@@ -124,6 +140,14 @@ void main()
124140
}
125141
#endif
126142

143+
#if defined(ALPHA_DITHERING)
144+
const float ditheredAlpha = Dithering(diffuse.a, gl_FragCoord.xy);
145+
if (ditheredAlpha < 0)
146+
{
147+
discard;
148+
}
149+
#endif
150+
127151
if (diffuse.a > 0.0)
128152
{
129153
#if defined(NORMAL_MAPPING)

Resources/Engine/Shaders/StandardPBR.ovfx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#feature SHADOW_PASS
22
#feature PARALLAX_MAPPING
33
#feature ALPHA_CLIPPING
4+
#feature ALPHA_DITHERING
45
#feature NORMAL_MAPPING
6+
#feature DISTANCE_FADE
57

68
#shader vertex
79
#version 450 core
@@ -92,13 +94,27 @@ uniform float u_AlphaClippingThreshold = 0.1;
9294
uniform float u_ShadowClippingThreshold = 0.5;
9395
#endif
9496

97+
#if defined(DISTANCE_FADE)
98+
uniform float u_DistanceFadeStart = 100.0;
99+
uniform float u_DistanceFadeLength = 10.0;
100+
#endif
101+
95102
uniform sampler2D _ShadowMap;
96103
uniform mat4 _LightSpaceMatrix;
97104

98105
out vec4 FRAGMENT_COLOR;
99106

100107
void main()
101108
{
109+
#if defined(DISTANCE_FADE)
110+
const float distanceAlpha = DistanceFadeAlpha(fs_in.FragPos, ubo_ViewPos, u_DistanceFadeStart, u_DistanceFadeLength);
111+
const float ditheredDistanceAlpha = Dithering(distanceAlpha, gl_FragCoord.xy);
112+
if (ditheredDistanceAlpha < 0)
113+
{
114+
discard;
115+
}
116+
#endif
117+
102118
vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);
103119

104120
#if defined(PARALLAX_MAPPING)
@@ -126,6 +142,14 @@ void main()
126142
}
127143
#endif
128144

145+
#if defined(ALPHA_DITHERING)
146+
const float ditheredAlpha = Dithering(albedo.a, gl_FragCoord.xy);
147+
if (ditheredAlpha < 0)
148+
{
149+
discard;
150+
}
151+
#endif
152+
129153
if (albedo.a > 0.0)
130154
{
131155
#if defined(NORMAL_MAPPING)

Sources/Overload/OvRendering/src/OvRendering/Resources/Loaders/ShaderLoader.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ void main()
417417
*/
418418
ShaderAssembleResult AssembleShader(const ShaderParseResult& p_parseResult)
419419
{
420+
const auto startTime = std::chrono::high_resolution_clock::now();
421+
420422
const auto variantCount = (size_t{ 1UL } << p_parseResult.features.size());
421423

422424
uint32_t failures = 0;
@@ -465,6 +467,8 @@ void main()
465467
);
466468
}
467469

470+
const auto endTime = std::chrono::high_resolution_clock::now();
471+
468472
if (failures > 0)
469473
{
470474
if (__LOGGING_SETTINGS.summary)
@@ -479,9 +483,10 @@ void main()
479483
else if (__LOGGING_SETTINGS.summary)
480484
{
481485
OVLOG_INFO(std::format(
482-
"[Shader Assembling] {}: {} variant(s) assembled.",
486+
"[Shader Assembling] {}: {} variant(s) assembled in {} ms.",
483487
p_parseResult.inputInfo.name,
484-
variantCount
488+
variantCount,
489+
std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count()
485490
));
486491
}
487492

0 commit comments

Comments
 (0)