aboutsummaryrefslogtreecommitdiff
path: root/client/shaders/nodes_shader/opengl_fragment.glsl
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2022-05-17 22:12:00 +0200
committerElias Fleckenstein <eliasfleckenstein@web.de>2022-05-17 22:12:00 +0200
commit21df26984da91143c15587f5a03c98d68c3adc4e (patch)
treeaaa707a628ad331f67890023dffe1b4f60dd01d3 /client/shaders/nodes_shader/opengl_fragment.glsl
parentb09fc5de5cdb021f43ad32b7e3f50dc75c0bc622 (diff)
parenteabf05758e3ba5f6f4bb1b8d1d1f02179b84e410 (diff)
downloaddragonfireclient-21df26984da91143c15587f5a03c98d68c3adc4e.tar.xz
Merge branch 'master' of https://github.com/minetest/minetest
Diffstat (limited to 'client/shaders/nodes_shader/opengl_fragment.glsl')
-rw-r--r--client/shaders/nodes_shader/opengl_fragment.glsl111
1 files changed, 59 insertions, 52 deletions
diff --git a/client/shaders/nodes_shader/opengl_fragment.glsl b/client/shaders/nodes_shader/opengl_fragment.glsl
index 87ef9af7d..8110f6fd3 100644
--- a/client/shaders/nodes_shader/opengl_fragment.glsl
+++ b/client/shaders/nodes_shader/opengl_fragment.glsl
@@ -1,5 +1,6 @@
uniform sampler2D baseTexture;
+uniform vec3 dayLight;
uniform vec4 skyBgColor;
uniform float fogDistance;
uniform vec3 eyePosition;
@@ -15,10 +16,15 @@ uniform float animationTimer;
uniform float f_textureresolution;
uniform mat4 m_ShadowViewProj;
uniform float f_shadowfar;
- varying float normalOffsetScale;
+ uniform float f_shadow_strength;
+ uniform vec4 CameraPos;
+ uniform float xyPerspectiveBias0;
+ uniform float xyPerspectiveBias1;
+
varying float adj_shadow_strength;
varying float cosLight;
varying float f_normal_length;
+ varying vec3 shadow_position;
#endif
@@ -42,23 +48,7 @@ varying float nightRatio;
const float fogStart = FOG_START;
const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
-
-
#ifdef ENABLE_DYNAMIC_SHADOWS
-const float bias0 = 0.9;
-const float zPersFactor = 0.5;
-const float bias1 = 1.0 - bias0 + 1e-6;
-
-vec4 getPerspectiveFactor(in vec4 shadowPosition)
-{
-
- float pDistance = length(shadowPosition.xy);
- float pFactor = pDistance * bias0 + bias1;
-
- shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPersFactor);
-
- return shadowPosition;
-}
// assuming near is always 1.0
float getLinearDepth()
@@ -68,16 +58,7 @@ float getLinearDepth()
vec3 getLightSpacePosition()
{
- vec4 pLightSpace;
- // some drawtypes have zero normals, so we need to handle it :(
- #if DRAW_TYPE == NDT_PLANTLIKE
- pLightSpace = m_ShadowViewProj * vec4(worldPosition, 1.0);
- #else
- float offsetScale = (0.0057 * getLinearDepth() + normalOffsetScale);
- pLightSpace = m_ShadowViewProj * vec4(worldPosition + offsetScale * normalize(vNormal), 1.0);
- #endif
- pLightSpace = getPerspectiveFactor(pLightSpace);
- return pLightSpace.xyz * 0.5 + 0.5;
+ return shadow_position * 0.5 + 0.5;
}
// custom smoothstep implementation because it's not defined in glsl1.2
// https://docs.gl/sl4/smoothstep
@@ -170,13 +151,13 @@ float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDis
float getBaseLength(vec2 smTexCoord)
{
- float l = length(2.0 * smTexCoord.xy - 1.0); // length in texture coords
- return bias1 / (1.0 / l - bias0); // return to undistorted coords
+ float l = length(2.0 * smTexCoord.xy - 1.0 - CameraPos.xy); // length in texture coords
+ return xyPerspectiveBias1 / (1.0 / l - xyPerspectiveBias0); // return to undistorted coords
}
float getDeltaPerspectiveFactor(float l)
{
- return 0.1 / (bias0 * l + bias1); // original distortion factor, divided by 10
+ return 0.04 * pow(512.0 / f_textureresolution, 0.4) / (xyPerspectiveBias0 * l + xyPerspectiveBias1); // original distortion factor, divided by 10
}
float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance, float multiplier)
@@ -185,6 +166,9 @@ float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDist
float perspectiveFactor;
// Return fast if sharp shadows are requested
+ if (PCFBOUND == 0.0)
+ return 0.0;
+
if (SOFTSHADOWRADIUS <= 1.0) {
perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
return max(2 * length(smTexCoord.xy) * 2048 / f_textureresolution / pow(perspectiveFactor, 3), SOFTSHADOWRADIUS);
@@ -479,36 +463,59 @@ void main(void)
vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
#ifdef ENABLE_DYNAMIC_SHADOWS
- float shadow_int = 0.0;
- vec3 shadow_color = vec3(0.0, 0.0, 0.0);
- vec3 posLightSpace = getLightSpacePosition();
+ if (f_shadow_strength > 0.0) {
+ float shadow_int = 0.0;
+ vec3 shadow_color = vec3(0.0, 0.0, 0.0);
+ vec3 posLightSpace = getLightSpacePosition();
- float distance_rate = (1 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 20.0));
- float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z ),0.0);
+ float distance_rate = (1.0 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 10.0));
+ if (max(abs(posLightSpace.x - 0.5), abs(posLightSpace.y - 0.5)) > 0.5)
+ distance_rate = 0.0;
+ float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0);
+
+ if (distance_rate > 1e-7) {
- if (distance_rate > 1e-7) {
-
#ifdef COLORED_SHADOWS
- vec4 visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
- shadow_int = visibility.r;
- shadow_color = visibility.gba;
+ vec4 visibility;
+ if (cosLight > 0.0 || f_normal_length < 1e-3)
+ visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
+ else
+ visibility = vec4(1.0, 0.0, 0.0, 0.0);
+ shadow_int = visibility.r;
+ shadow_color = visibility.gba;
#else
- shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
+ if (cosLight > 0.0 || f_normal_length < 1e-3)
+ shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
+ else
+ shadow_int = 1.0;
#endif
- shadow_int *= distance_rate;
- shadow_int *= 1.0 - nightRatio;
+ shadow_int *= distance_rate;
+ shadow_int = clamp(shadow_int, 0.0, 1.0);
+ }
- }
+ // turns out that nightRatio falls off much faster than
+ // actual brightness of artificial light in relation to natual light.
+ // Power ratio was measured on torches in MTG (brightness = 14).
+ float adjusted_night_ratio = pow(max(0.0, nightRatio), 0.6);
+
+ // Apply self-shadowing when light falls at a narrow angle to the surface
+ // Cosine of the cut-off angle.
+ const float self_shadow_cutoff_cosine = 0.035;
+ if (f_normal_length != 0 && cosLight < self_shadow_cutoff_cosine) {
+ shadow_int = max(shadow_int, 1 - clamp(cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
+ shadow_color = mix(vec3(0.0), shadow_color, min(cosLight, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
+ }
- if (f_normal_length != 0 && cosLight < 0.035) {
- shadow_int = max(shadow_int, min(clamp(1.0-nightRatio, 0.0, 1.0), 1 - clamp(cosLight, 0.0, 0.035)/0.035));
- }
+ shadow_int *= f_adj_shadow_strength;
- shadow_int = 1.0 - (shadow_int * f_adj_shadow_strength);
-
- col.rgb = mix(shadow_color,col.rgb,shadow_int)*shadow_int;
- // col.r = 0.5 * clamp(getPenumbraRadius(ShadowMapSampler, posLightSpace.xy, posLightSpace.z, 1.0) / SOFTSHADOWRADIUS, 0.0, 1.0) + 0.5 * col.r;
+ // calculate fragment color from components:
+ col.rgb =
+ adjusted_night_ratio * col.rgb + // artificial light
+ (1.0 - adjusted_night_ratio) * ( // natural light
+ col.rgb * (1.0 - shadow_int * (1.0 - shadow_color)) + // filtered texture color
+ dayLight * shadow_color * shadow_int); // reflected filtered sunlight/moonlight
+ }
#endif
#if ENABLE_TONE_MAPPING
@@ -528,6 +535,6 @@ void main(void)
- fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
col = mix(skyBgColor, col, clarity);
col = vec4(col.rgb, base.a);
-
+
gl_FragColor = col;
}