Skip to content

Commit 982e03f

Browse files
authored
Improvements to colored shadows (#11516)
1 parent 21113ad commit 982e03f

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

client/shaders/nodes_shader/opengl_fragment.glsl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,11 @@ void main(void)
489489
if (distance_rate > 1e-7) {
490490

491491
#ifdef COLORED_SHADOWS
492-
vec4 visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
492+
vec4 visibility;
493+
if (cosLight > 0.0)
494+
visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
495+
else
496+
visibility = vec4(1.0, 0.0, 0.0, 0.0);
493497
shadow_int = visibility.r;
494498
shadow_color = visibility.gba;
495499
#else
@@ -507,7 +511,8 @@ void main(void)
507511

508512
shadow_int = 1.0 - (shadow_int * f_adj_shadow_strength);
509513

510-
col.rgb = mix(shadow_color,col.rgb,shadow_int)*shadow_int;
514+
// apply shadow (+color) as a factor to the material color
515+
col.rgb = col.rgb * (1.0 - (1.0 - shadow_color) * (1.0 - pow(shadow_int, 2.0)));
511516
// col.r = 0.5 * clamp(getPenumbraRadius(ShadowMapSampler, posLightSpace.xy, posLightSpace.z, 1.0) / SOFTSHADOWRADIUS, 0.0, 1.0) + 0.5 * col.r;
512517
#endif
513518

client/shaders/object_shader/opengl_fragment.glsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,11 @@ void main(void)
351351
vec3 posLightSpace = getLightSpacePosition();
352352

353353
#ifdef COLORED_SHADOWS
354-
vec4 visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
354+
vec4 visibility;
355+
if (cosLight > 0.0)
356+
visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
357+
else
358+
visibility = vec4(1.0, 0.0, 0.0, 0.0);
355359
shadow_int = visibility.r;
356360
shadow_color = visibility.gba;
357361
#else

client/shaders/shadow_shaders/pass1_trans_fragment.glsl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ uniform sampler2D ColorMapSampler;
22
varying vec4 tPos;
33

44
#ifdef COLORED_SHADOWS
5+
varying vec3 varColor;
6+
57
// c_precision of 128 fits within 7 base-10 digits
68
const float c_precision = 128.0;
79
const float c_precisionp1 = c_precision + 1.0;
@@ -30,7 +32,10 @@ void main()
3032

3133
//col.rgb = col.a == 1.0 ? vec3(1.0) : col.rgb;
3234
#ifdef COLORED_SHADOWS
33-
float packedColor = packColor(mix(col.rgb, black, col.a));
35+
col.rgb *= varColor.rgb;
36+
// alpha 0.0 results in all-white, 0.5 means full color, 1.0 means all black
37+
// resulting color is used as a factor in the final shader
38+
float packedColor = packColor(mix(mix(vec3(1.0), col.rgb, 2.0 * clamp(col.a, 0.0, 0.5)), black, 2.0 * clamp(col.a - 0.5, 0.0, 0.5)));
3439
gl_FragColor = vec4(depth, packedColor, 0.0,1.0);
3540
#else
3641
gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);

client/shaders/shadow_shaders/pass1_trans_vertex.glsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
uniform mat4 LightMVP; // world matrix
22
varying vec4 tPos;
3+
#ifdef COLORED_SHADOWS
4+
varying vec3 varColor;
5+
#endif
36

47
const float bias0 = 0.9;
58
const float zPersFactor = 0.5;
@@ -23,4 +26,8 @@ void main()
2326

2427
gl_Position = vec4(tPos.xyz, 1.0);
2528
gl_TexCoord[0].st = gl_MultiTexCoord0.st;
29+
30+
#ifdef COLORED_SHADOWS
31+
varColor = gl_Color.rgb;
32+
#endif
2633
}

src/client/clientmap.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,10 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
461461
layer.Texture = shadow->get_texture();
462462
layer.TextureWrapU = video::E_TEXTURE_CLAMP::ETC_CLAMP_TO_EDGE;
463463
layer.TextureWrapV = video::E_TEXTURE_CLAMP::ETC_CLAMP_TO_EDGE;
464-
layer.TrilinearFilter = true;
464+
// Do not enable filter on shadow texture to avoid visual artifacts
465+
// with colored shadows.
466+
// Filtering is done in shader code anyway
467+
layer.TrilinearFilter = false;
465468
}
466469
driver->setMaterial(material);
467470
++material_swaps;

0 commit comments

Comments
 (0)