aboutsummaryrefslogtreecommitdiff
path: root/client/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'client/shaders')
-rw-r--r--client/shaders/nodes_shader/opengl_fragment.glsl111
-rw-r--r--client/shaders/nodes_shader/opengl_vertex.glsl90
-rw-r--r--client/shaders/object_shader/opengl_fragment.glsl381
-rw-r--r--client/shaders/object_shader/opengl_vertex.glsl122
-rw-r--r--client/shaders/shadow_shaders/pass1_trans_fragment.glsl6
-rw-r--r--client/shaders/shadow_shaders/pass1_trans_vertex.glsl42
-rw-r--r--client/shaders/shadow_shaders/pass1_vertex.glsl37
7 files changed, 563 insertions, 226 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;
}
diff --git a/client/shaders/nodes_shader/opengl_vertex.glsl b/client/shaders/nodes_shader/opengl_vertex.glsl
index d316930b2..3ea0faa36 100644
--- a/client/shaders/nodes_shader/opengl_vertex.glsl
+++ b/client/shaders/nodes_shader/opengl_vertex.glsl
@@ -32,10 +32,13 @@ centroid varying vec2 varTexCoord;
uniform float f_shadowfar;
uniform float f_shadow_strength;
uniform float f_timeofday;
+ uniform vec4 CameraPos;
+
varying float cosLight;
varying float normalOffsetScale;
varying float adj_shadow_strength;
varying float f_normal_length;
+ varying vec3 shadow_position;
#endif
@@ -45,8 +48,38 @@ varying float nightRatio;
const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
const float e = 2.718281828459;
const float BS = 10.0;
+uniform float xyPerspectiveBias0;
+uniform float xyPerspectiveBias1;
+uniform float zPerspectiveBias;
#ifdef ENABLE_DYNAMIC_SHADOWS
+
+vec4 getRelativePosition(in vec4 position)
+{
+ vec2 l = position.xy - CameraPos.xy;
+ vec2 s = l / abs(l);
+ s = (1.0 - s * CameraPos.xy);
+ l /= s;
+ return vec4(l, s);
+}
+
+float getPerspectiveFactor(in vec4 relativePosition)
+{
+ float pDistance = length(relativePosition.xy);
+ float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
+ return pFactor;
+}
+
+vec4 applyPerspectiveDistortion(in vec4 position)
+{
+ vec4 l = getRelativePosition(position);
+ float pFactor = getPerspectiveFactor(l);
+ l.xy /= pFactor;
+ position.xy = l.xy * l.zw + CameraPos.xy;
+ position.z *= zPerspectiveBias;
+ return position;
+}
+
// custom smoothstep implementation because it's not defined in glsl1.2
// https://docs.gl/sl4/smoothstep
float mtsmoothstep(in float edge0, in float edge1, in float x)
@@ -193,24 +226,45 @@ void main(void)
varColor = clamp(color, 0.0, 1.0);
#ifdef ENABLE_DYNAMIC_SHADOWS
- vec3 nNormal = normalize(vNormal);
- cosLight = dot(nNormal, -v_LightDirection);
- float texelSize = 767.0 / f_textureresolution;
- float slopeScale = clamp(1.0 - abs(cosLight), 0.0, 1.0);
- normalOffsetScale = texelSize * slopeScale;
-
- if (f_timeofday < 0.2) {
- adj_shadow_strength = f_shadow_strength * 0.5 *
- (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday));
- } else if (f_timeofday >= 0.8) {
- adj_shadow_strength = f_shadow_strength * 0.5 *
- mtsmoothstep(0.8, 0.83, f_timeofday);
- } else {
- adj_shadow_strength = f_shadow_strength *
- mtsmoothstep(0.20, 0.25, f_timeofday) *
- (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
+ if (f_shadow_strength > 0.0) {
+ vec3 nNormal;
+ f_normal_length = length(vNormal);
+
+ /* normalOffsetScale is in world coordinates (1/10th of a meter)
+ z_bias is in light space coordinates */
+ float normalOffsetScale, z_bias;
+ float pFactor = getPerspectiveFactor(getRelativePosition(m_ShadowViewProj * mWorld * inVertexPosition));
+ if (f_normal_length > 0.0) {
+ nNormal = normalize(vNormal);
+ cosLight = dot(nNormal, -v_LightDirection);
+ float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
+ normalOffsetScale = 2.0 * pFactor * pFactor * sinLight * min(f_shadowfar, 500.0) /
+ xyPerspectiveBias1 / f_textureresolution;
+ z_bias = 1.0 * sinLight / cosLight;
+ }
+ else {
+ nNormal = vec3(0.0);
+ cosLight = clamp(dot(v_LightDirection, normalize(vec3(v_LightDirection.x, 0.0, v_LightDirection.z))), 1e-2, 1.0);
+ float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
+ normalOffsetScale = 0.0;
+ z_bias = 3.6e3 * sinLight / cosLight;
+ }
+ z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
+
+ shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (inVertexPosition + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
+ shadow_position.z -= z_bias;
+
+ if (f_timeofday < 0.2) {
+ adj_shadow_strength = f_shadow_strength * 0.5 *
+ (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday));
+ } else if (f_timeofday >= 0.8) {
+ adj_shadow_strength = f_shadow_strength * 0.5 *
+ mtsmoothstep(0.8, 0.83, f_timeofday);
+ } else {
+ adj_shadow_strength = f_shadow_strength *
+ mtsmoothstep(0.20, 0.25, f_timeofday) *
+ (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
+ }
}
- f_normal_length = length(vNormal);
#endif
-
}
diff --git a/client/shaders/object_shader/opengl_fragment.glsl b/client/shaders/object_shader/opengl_fragment.glsl
index 9a0b90f15..7baf5826f 100644
--- a/client/shaders/object_shader/opengl_fragment.glsl
+++ b/client/shaders/object_shader/opengl_fragment.glsl
@@ -1,28 +1,14 @@
uniform sampler2D baseTexture;
uniform vec4 emissiveColor;
+uniform vec3 dayLight;
uniform vec4 skyBgColor;
uniform float fogDistance;
uniform vec3 eyePosition;
-varying vec3 vNormal;
-varying vec3 vPosition;
-varying vec3 worldPosition;
-varying lowp vec4 varColor;
-#ifdef GL_ES
-varying mediump vec2 varTexCoord;
-#else
-centroid varying vec2 varTexCoord;
-#endif
-
-varying vec3 eyeVec;
-varying float vIDiff;
-
-const float e = 2.718281828459;
-const float BS = 10.0;
-const float fogStart = FOG_START;
-const float fogShadingParameter = 1.0 / (1.0 - fogStart);
-
+// The cameraOffset is the current center of the visible world.
+uniform vec3 cameraOffset;
+uniform float animationTimer;
#ifdef ENABLE_DYNAMIC_SHADOWS
// shadow texture
uniform sampler2D ShadowMapSampler;
@@ -31,57 +17,41 @@ const float fogShadingParameter = 1.0 / (1.0 - fogStart);
uniform float f_textureresolution;
uniform mat4 m_ShadowViewProj;
uniform float f_shadowfar;
- uniform float f_timeofday;
- 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
-#if ENABLE_TONE_MAPPING
-/* Hable's UC2 Tone mapping parameters
- A = 0.22;
- B = 0.30;
- C = 0.10;
- D = 0.20;
- E = 0.01;
- F = 0.30;
- W = 11.2;
- equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
-*/
-
-vec3 uncharted2Tonemap(vec3 x)
-{
- return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
-}
-vec4 applyToneMapping(vec4 color)
-{
- color = vec4(pow(color.rgb, vec3(2.2)), color.a);
- const float gamma = 1.6;
- const float exposureBias = 5.5;
- color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
- // Precalculated white_scale from
- //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
- vec3 whiteScale = vec3(1.036015346);
- color.rgb *= whiteScale;
- return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
-}
+varying vec3 vNormal;
+varying vec3 vPosition;
+// World position in the visible world (i.e. relative to the cameraOffset.)
+// This can be used for many shader effects without loss of precision.
+// If the absolute position is required it can be calculated with
+// cameraOffset + worldPosition (for large coordinates the limits of float
+// precision must be considered).
+varying vec3 worldPosition;
+varying lowp vec4 varColor;
+#ifdef GL_ES
+varying mediump vec2 varTexCoord;
+#else
+centroid varying vec2 varTexCoord;
#endif
+varying vec3 eyeVec;
+varying float nightRatio;
-#ifdef ENABLE_DYNAMIC_SHADOWS
-const float bias0 = 0.9;
-const float zPersFactor = 0.5;
-const float bias1 = 1.0 - bias0;
+varying float vIDiff;
-vec4 getPerspectiveFactor(in vec4 shadowPosition)
-{
- float pDistance = length(shadowPosition.xy);
- float pFactor = pDistance * bias0 + bias1;
- shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPersFactor);
+const float fogStart = FOG_START;
+const float fogShadingParameter = 1.0 / (1.0 - fogStart);
- return shadowPosition;
-}
+#ifdef ENABLE_DYNAMIC_SHADOWS
// assuming near is always 1.0
float getLinearDepth()
@@ -91,11 +61,14 @@ float getLinearDepth()
vec3 getLightSpacePosition()
{
- vec4 pLightSpace;
- float normalBias = 0.0005 * getLinearDepth() * cosLight + normalOffsetScale;
- pLightSpace = m_ShadowViewProj * vec4(worldPosition + normalBias * normalize(vNormal), 1.0);
- 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
+float mtsmoothstep(in float edge0, in float edge1, in float x)
+{
+ float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
+ return t * t * (3.0 - 2.0 * t);
}
#ifdef COLORED_SHADOWS
@@ -124,10 +97,10 @@ vec4 getHardShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDist
{
vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy).rgba;
- float visibility = step(0.0, (realDistance-2e-5) - texDepth.r);
+ float visibility = step(0.0, realDistance - texDepth.r);
vec4 result = vec4(visibility, vec3(0.0,0.0,0.0));//unpackColor(texDepth.g));
if (visibility < 0.1) {
- visibility = step(0.0, (realDistance-2e-5) - texDepth.r);
+ visibility = step(0.0, realDistance - texDepth.b);
result = vec4(visibility, unpackColor(texDepth.a));
}
return result;
@@ -138,13 +111,13 @@ vec4 getHardShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDist
float getHardShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
{
float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
- float visibility = step(0.0, (realDistance-2e-5) - texDepth);
-
+ float visibility = step(0.0, realDistance - texDepth);
return visibility;
}
#endif
+
#if SHADOW_FILTER == 2
#define PCFBOUND 3.5
#define PCFSAMPLES 64.0
@@ -163,6 +136,76 @@ float getHardShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance
#define PCFSAMPLES 1.0
#endif
#endif
+#ifdef COLORED_SHADOWS
+float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
+{
+ vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy);
+ float depth = max(realDistance - texDepth.r, realDistance - texDepth.b);
+ return depth;
+}
+#else
+float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
+{
+ float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
+ float depth = realDistance - texDepth;
+ return depth;
+}
+#endif
+
+float getBaseLength(vec2 smTexCoord)
+{
+ 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.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)
+{
+ float baseLength = getBaseLength(smTexCoord);
+ 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);
+ }
+
+ vec2 clampedpos;
+ float texture_size = 1.0 / (2048 /*f_textureresolution*/ * 0.5);
+ float y, x;
+ float depth = 0.0;
+ float pointDepth;
+ float maxRadius = SOFTSHADOWRADIUS * 5.0 * multiplier;
+
+ float bound = clamp(PCFBOUND * (1 - baseLength), 0.0, PCFBOUND);
+ int n = 0;
+
+ for (y = -bound; y <= bound; y += 1.0)
+ for (x = -bound; x <= bound; x += 1.0) {
+ clampedpos = vec2(x,y);
+ perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * maxRadius);
+ clampedpos = clampedpos * texture_size * perspectiveFactor * maxRadius * perspectiveFactor + smTexCoord.xy;
+
+ pointDepth = getHardShadowDepth(shadowsampler, clampedpos.xy, realDistance);
+ if (pointDepth > -0.01) {
+ depth += pointDepth;
+ n += 1;
+ }
+ }
+
+ depth = depth / n;
+ depth = pow(clamp(depth, 0.0, 1000.0), 1.6) / 0.001;
+
+ perspectiveFactor = getDeltaPerspectiveFactor(baseLength);
+ return max(length(smTexCoord.xy) * 2 * 2048 / f_textureresolution / pow(perspectiveFactor, 3), depth * maxRadius);
+}
#ifdef POISSON_FILTER
const vec2[64] poissonDisk = vec2[64](
@@ -238,17 +281,28 @@ vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance
{
vec2 clampedpos;
vec4 visibility = vec4(0.0);
+ float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
+ if (radius < 0.1) {
+ // we are in the middle of even brightness, no need for filtering
+ return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
+ }
+
+ float baseLength = getBaseLength(smTexCoord);
+ float perspectiveFactor;
float texture_size = 1.0 / (f_textureresolution * 0.5);
- int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-PCFSAMPLES)));
- int end_offset = int(PCFSAMPLES) + init_offset;
+ int samples = int(clamp(PCFSAMPLES * (1 - baseLength) * (1 - baseLength), PCFSAMPLES / 4, PCFSAMPLES));
+ int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
+ int end_offset = int(samples) + init_offset;
for (int x = init_offset; x < end_offset; x++) {
- clampedpos = poissonDisk[x] * texture_size * SOFTSHADOWRADIUS + smTexCoord.xy;
+ clampedpos = poissonDisk[x];
+ perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius);
+ clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor + smTexCoord.xy;
visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
}
- return visibility / PCFSAMPLES;
+ return visibility / samples;
}
#else
@@ -257,17 +311,28 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
{
vec2 clampedpos;
float visibility = 0.0;
+ float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.5); // scale to align with PCF
+ if (radius < 0.1) {
+ // we are in the middle of even brightness, no need for filtering
+ return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
+ }
+
+ float baseLength = getBaseLength(smTexCoord);
+ float perspectiveFactor;
float texture_size = 1.0 / (f_textureresolution * 0.5);
- int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-PCFSAMPLES)));
- int end_offset = int(PCFSAMPLES) + init_offset;
+ int samples = int(clamp(PCFSAMPLES * (1 - baseLength) * (1 - baseLength), PCFSAMPLES / 4, PCFSAMPLES));
+ int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
+ int end_offset = int(samples) + init_offset;
for (int x = init_offset; x < end_offset; x++) {
- clampedpos = poissonDisk[x] * texture_size * SOFTSHADOWRADIUS + smTexCoord.xy;
+ clampedpos = poissonDisk[x];
+ perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius);
+ clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor + smTexCoord.xy;
visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
}
- return visibility / PCFSAMPLES;
+ return visibility / samples;
}
#endif
@@ -281,19 +346,31 @@ vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance
{
vec2 clampedpos;
vec4 visibility = vec4(0.0);
- float sradius=0.0;
- if( PCFBOUND>0)
- sradius = SOFTSHADOWRADIUS / PCFBOUND;
+ float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
+ if (radius < 0.1) {
+ // we are in the middle of even brightness, no need for filtering
+ return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
+ }
+
+ float baseLength = getBaseLength(smTexCoord);
+ float perspectiveFactor;
+
float texture_size = 1.0 / (f_textureresolution * 0.5);
float y, x;
+ float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
+ int n = 0;
+
// basic PCF filter
- for (y = -PCFBOUND; y <= PCFBOUND; y += 1.0)
- for (x = -PCFBOUND; x <= PCFBOUND; x += 1.0) {
- clampedpos = vec2(x,y) * texture_size* sradius + smTexCoord.xy;
+ for (y = -bound; y <= bound; y += 1.0)
+ for (x = -bound; x <= bound; x += 1.0) {
+ clampedpos = vec2(x,y); // screen offset
+ perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius / bound);
+ clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor / bound + smTexCoord.xy; // both dx,dy and radius are adjusted
visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
+ n += 1;
}
- return visibility / PCFSAMPLES;
+ return visibility / n;
}
#else
@@ -301,20 +378,31 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
{
vec2 clampedpos;
float visibility = 0.0;
- float sradius=0.0;
- if( PCFBOUND>0)
- sradius = SOFTSHADOWRADIUS / PCFBOUND;
-
+ float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance, 1.0);
+ if (radius < 0.1) {
+ // we are in the middle of even brightness, no need for filtering
+ return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
+ }
+
+ float baseLength = getBaseLength(smTexCoord);
+ float perspectiveFactor;
+
float texture_size = 1.0 / (f_textureresolution * 0.5);
float y, x;
+ float bound = clamp(PCFBOUND * (1 - baseLength), PCFBOUND / 2, PCFBOUND);
+ int n = 0;
+
// basic PCF filter
- for (y = -PCFBOUND; y <= PCFBOUND; y += 1.0)
- for (x = -PCFBOUND; x <= PCFBOUND; x += 1.0) {
- clampedpos = vec2(x,y) * texture_size * sradius + smTexCoord.xy;
+ for (y = -bound; y <= bound; y += 1.0)
+ for (x = -bound; x <= bound; x += 1.0) {
+ clampedpos = vec2(x,y); // screen offset
+ perspectiveFactor = getDeltaPerspectiveFactor(baseLength + length(clampedpos) * texture_size * radius / bound);
+ clampedpos = clampedpos * texture_size * perspectiveFactor * radius * perspectiveFactor / bound + smTexCoord.xy; // both dx,dy and radius are adjusted
visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
+ n += 1;
}
- return visibility / PCFSAMPLES;
+ return visibility / n;
}
#endif
@@ -322,12 +410,46 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
#endif
#endif
+#if ENABLE_TONE_MAPPING
+
+/* Hable's UC2 Tone mapping parameters
+ A = 0.22;
+ B = 0.30;
+ C = 0.10;
+ D = 0.20;
+ E = 0.01;
+ F = 0.30;
+ W = 11.2;
+ equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
+*/
+
+vec3 uncharted2Tonemap(vec3 x)
+{
+ return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
+}
+
+vec4 applyToneMapping(vec4 color)
+{
+ color = vec4(pow(color.rgb, vec3(2.2)), color.a);
+ const float gamma = 1.6;
+ const float exposureBias = 5.5;
+ color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
+ // Precalculated white_scale from
+ //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
+ vec3 whiteScale = vec3(1.036015346);
+ color.rgb *= whiteScale;
+ return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
+}
+#endif
+
+
+
void main(void)
{
vec3 color;
vec2 uv = varTexCoord.st;
- vec4 base = texture2D(baseTexture, uv).rgba;
+ vec4 base = texture2D(baseTexture, uv).rgba;
// If alpha is zero, we can just discard the pixel. This fixes transparency
// on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
// and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
@@ -341,34 +463,66 @@ void main(void)
#endif
color = base.rgb;
- vec4 col = vec4(color.rgb, base.a);
- col.rgb *= varColor.rgb;
- col.rgb *= emissiveColor.rgb * vIDiff;
+ vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
+ col.rgb *= vIDiff;
#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.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) {
#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)
+ if (cosLight > 0.0)
+ shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
+ else
+ shadow_int = 1.0;
#endif
-
- if (f_normal_length != 0 && cosLight <= 0.001) {
- shadow_int = clamp(shadow_int + 0.5 * abs(cosLight), 0.0, 1.0);
+ 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.14;
+ 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);
+ }
+
+ shadow_int *= f_adj_shadow_strength;
+
+ // 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
}
-
- shadow_int = 1.0 - (shadow_int * adj_shadow_strength);
-
- col.rgb = mix(shadow_color, col.rgb, shadow_int) * shadow_int;
#endif
-
-
#if ENABLE_TONE_MAPPING
col = applyToneMapping(col);
#endif
@@ -385,6 +539,7 @@ void main(void)
float clarity = clamp(fogShadingParameter
- fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
col = mix(skyBgColor, col, clarity);
-
- gl_FragColor = vec4(col.rgb, base.a);
+ col = vec4(col.rgb, base.a);
+
+ gl_FragColor = col;
}
diff --git a/client/shaders/object_shader/opengl_vertex.glsl b/client/shaders/object_shader/opengl_vertex.glsl
index f135ab9dc..6dc25f854 100644
--- a/client/shaders/object_shader/opengl_vertex.glsl
+++ b/client/shaders/object_shader/opengl_vertex.glsl
@@ -1,7 +1,10 @@
uniform mat4 mWorld;
-
+uniform vec3 dayLight;
uniform vec3 eyePosition;
uniform float animationTimer;
+uniform vec4 emissiveColor;
+uniform vec3 cameraOffset;
+
varying vec3 vNormal;
varying vec3 vPosition;
@@ -21,19 +24,53 @@ centroid varying vec2 varTexCoord;
uniform float f_shadowfar;
uniform float f_shadow_strength;
uniform float f_timeofday;
+ uniform vec4 CameraPos;
+
varying float cosLight;
- varying float normalOffsetScale;
varying float adj_shadow_strength;
varying float f_normal_length;
+ varying vec3 shadow_position;
#endif
varying vec3 eyeVec;
+varying float nightRatio;
+// Color of the light emitted by the light sources.
+const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
varying float vIDiff;
-
const float e = 2.718281828459;
const float BS = 10.0;
+uniform float xyPerspectiveBias0;
+uniform float xyPerspectiveBias1;
+uniform float zPerspectiveBias;
#ifdef ENABLE_DYNAMIC_SHADOWS
+
+vec4 getRelativePosition(in vec4 position)
+{
+ vec2 l = position.xy - CameraPos.xy;
+ vec2 s = l / abs(l);
+ s = (1.0 - s * CameraPos.xy);
+ l /= s;
+ return vec4(l, s);
+}
+
+float getPerspectiveFactor(in vec4 relativePosition)
+{
+ float pDistance = length(relativePosition.xy);
+ float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
+ return pFactor;
+}
+
+vec4 applyPerspectiveDistortion(in vec4 position)
+{
+ vec4 l = getRelativePosition(position);
+ float pFactor = getPerspectiveFactor(l);
+ l.xy /= pFactor;
+ position.xy = l.xy * l.zw + CameraPos.xy;
+ position.z *= zPerspectiveBias;
+ return position;
+}
+
// custom smoothstep implementation because it's not defined in glsl1.2
// https://docs.gl/sl4/smoothstep
float mtsmoothstep(in float edge0, in float edge1, in float x)
@@ -60,7 +97,7 @@ void main(void)
gl_Position = mWorldViewProj * inVertexPosition;
vPosition = gl_Position.xyz;
- vNormal = inVertexNormal;
+ vNormal = (mWorld * vec4(inVertexNormal, 0.0)).xyz;
worldPosition = (mWorld * inVertexPosition).xyz;
eyeVec = -(mWorldView * inVertexPosition).xyz;
@@ -75,29 +112,68 @@ void main(void)
#endif
#ifdef GL_ES
- varColor = inVertexColor.bgra;
+ vec4 color = inVertexColor.bgra;
#else
- varColor = inVertexColor;
+ vec4 color = inVertexColor;
#endif
-#ifdef ENABLE_DYNAMIC_SHADOWS
+ color *= emissiveColor;
- cosLight = max(0.0, dot(vNormal, -v_LightDirection));
- float texelSize = 0.51;
- float slopeScale = clamp(1.0 - cosLight, 0.0, 1.0);
- normalOffsetScale = texelSize * slopeScale;
- if (f_timeofday < 0.2) {
- adj_shadow_strength = f_shadow_strength * 0.5 *
- (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday));
- } else if (f_timeofday >= 0.8) {
- adj_shadow_strength = f_shadow_strength * 0.5 *
- mtsmoothstep(0.8, 0.83, f_timeofday);
- } else {
- adj_shadow_strength = f_shadow_strength *
- mtsmoothstep(0.20, 0.25, f_timeofday) *
- (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
- }
- f_normal_length = length(vNormal);
+ // The alpha gives the ratio of sunlight in the incoming light.
+ nightRatio = 1.0 - color.a;
+ color.rgb = color.rgb * (color.a * dayLight.rgb +
+ nightRatio * artificialLight.rgb) * 2.0;
+ color.a = 1.0;
+ // Emphase blue a bit in darker places
+ // See C++ implementation in mapblock_mesh.cpp final_color_blend()
+ float brightness = (color.r + color.g + color.b) / 3.0;
+ color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
+ 0.07 * brightness);
+
+ varColor = clamp(color, 0.0, 1.0);
+
+
+#ifdef ENABLE_DYNAMIC_SHADOWS
+ if (f_shadow_strength > 0.0) {
+ vec3 nNormal = normalize(vNormal);
+ f_normal_length = length(vNormal);
+
+ /* normalOffsetScale is in world coordinates (1/10th of a meter)
+ z_bias is in light space coordinates */
+ float normalOffsetScale, z_bias;
+ float pFactor = getPerspectiveFactor(getRelativePosition(m_ShadowViewProj * mWorld * inVertexPosition));
+ if (f_normal_length > 0.0) {
+ nNormal = normalize(vNormal);
+ cosLight = dot(nNormal, -v_LightDirection);
+ float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
+ normalOffsetScale = 0.1 * pFactor * pFactor * sinLight * min(f_shadowfar, 500.0) /
+ xyPerspectiveBias1 / f_textureresolution;
+ z_bias = 1e3 * sinLight / cosLight * (0.5 + f_textureresolution / 1024.0);
+ }
+ else {
+ nNormal = vec3(0.0);
+ cosLight = clamp(dot(v_LightDirection, normalize(vec3(v_LightDirection.x, 0.0, v_LightDirection.z))), 1e-2, 1.0);
+ float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
+ normalOffsetScale = 0.0;
+ z_bias = 3.6e3 * sinLight / cosLight;
+ }
+ z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
+
+ shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (inVertexPosition + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
+ shadow_position.z -= z_bias;
+
+ if (f_timeofday < 0.2) {
+ adj_shadow_strength = f_shadow_strength * 0.5 *
+ (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday));
+ } else if (f_timeofday >= 0.8) {
+ adj_shadow_strength = f_shadow_strength * 0.5 *
+ mtsmoothstep(0.8, 0.83, f_timeofday);
+ } else {
+ adj_shadow_strength = f_shadow_strength *
+ mtsmoothstep(0.20, 0.25, f_timeofday) *
+ (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
+ }
+ }
#endif
}
diff --git a/client/shaders/shadow_shaders/pass1_trans_fragment.glsl b/client/shaders/shadow_shaders/pass1_trans_fragment.glsl
index 9f9e5be8c..b267c2214 100644
--- a/client/shaders/shadow_shaders/pass1_trans_fragment.glsl
+++ b/client/shaders/shadow_shaders/pass1_trans_fragment.glsl
@@ -2,6 +2,8 @@ uniform sampler2D ColorMapSampler;
varying vec4 tPos;
#ifdef COLORED_SHADOWS
+varying vec3 varColor;
+
// c_precision of 128 fits within 7 base-10 digits
const float c_precision = 128.0;
const float c_precisionp1 = c_precision + 1.0;
@@ -30,7 +32,9 @@ void main()
//col.rgb = col.a == 1.0 ? vec3(1.0) : col.rgb;
#ifdef COLORED_SHADOWS
- float packedColor = packColor(mix(col.rgb, black, col.a));
+ col.rgb *= varColor.rgb;
+ // premultiply color alpha (see-through side)
+ float packedColor = packColor(col.rgb * (1.0 - col.a));
gl_FragColor = vec4(depth, packedColor, 0.0,1.0);
#else
gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
diff --git a/client/shaders/shadow_shaders/pass1_trans_vertex.glsl b/client/shaders/shadow_shaders/pass1_trans_vertex.glsl
index ca59f2796..244d2562a 100644
--- a/client/shaders/shadow_shaders/pass1_trans_vertex.glsl
+++ b/client/shaders/shadow_shaders/pass1_trans_vertex.glsl
@@ -1,26 +1,50 @@
uniform mat4 LightMVP; // world matrix
+uniform vec4 CameraPos;
varying vec4 tPos;
+#ifdef COLORED_SHADOWS
+varying vec3 varColor;
+#endif
-const float bias0 = 0.9;
-const float zPersFactor = 0.5;
-const float bias1 = 1.0 - bias0 + 1e-6;
+uniform float xyPerspectiveBias0;
+uniform float xyPerspectiveBias1;
+uniform float zPerspectiveBias;
-vec4 getPerspectiveFactor(in vec4 shadowPosition)
+vec4 getRelativePosition(in vec4 position)
{
- float pDistance = length(shadowPosition.xy);
- float pFactor = pDistance * bias0 + bias1;
- shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPersFactor);
+ vec2 l = position.xy - CameraPos.xy;
+ vec2 s = l / abs(l);
+ s = (1.0 - s * CameraPos.xy);
+ l /= s;
+ return vec4(l, s);
+}
- return shadowPosition;
+float getPerspectiveFactor(in vec4 relativePosition)
+{
+ float pDistance = length(relativePosition.xy);
+ float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
+ return pFactor;
}
+vec4 applyPerspectiveDistortion(in vec4 position)
+{
+ vec4 l = getRelativePosition(position);
+ float pFactor = getPerspectiveFactor(l);
+ l.xy /= pFactor;
+ position.xy = l.xy * l.zw + CameraPos.xy;
+ position.z *= zPerspectiveBias;
+ return position;
+}
void main()
{
vec4 pos = LightMVP * gl_Vertex;
- tPos = getPerspectiveFactor(LightMVP * gl_Vertex);
+ tPos = applyPerspectiveDistortion(LightMVP * gl_Vertex);
gl_Position = vec4(tPos.xyz, 1.0);
gl_TexCoord[0].st = gl_MultiTexCoord0.st;
+
+#ifdef COLORED_SHADOWS
+ varColor = gl_Color.rgb;
+#endif
}
diff --git a/client/shaders/shadow_shaders/pass1_vertex.glsl b/client/shaders/shadow_shaders/pass1_vertex.glsl
index a6d8b3db8..1dceb93c6 100644
--- a/client/shaders/shadow_shaders/pass1_vertex.glsl
+++ b/client/shaders/shadow_shaders/pass1_vertex.glsl
@@ -1,26 +1,43 @@
uniform mat4 LightMVP; // world matrix
+uniform vec4 CameraPos; // camera position
varying vec4 tPos;
-const float bias0 = 0.9;
-const float zPersFactor = 0.5;
-const float bias1 = 1.0 - bias0 + 1e-6;
+uniform float xyPerspectiveBias0;
+uniform float xyPerspectiveBias1;
+uniform float zPerspectiveBias;
-vec4 getPerspectiveFactor(in vec4 shadowPosition)
+vec4 getRelativePosition(in vec4 position)
{
- float pDistance = length(shadowPosition.xy);
- float pFactor = pDistance * bias0 + bias1;
- shadowPosition.xyz *= vec3(vec2(1.0 / pFactor), zPersFactor);
+ vec2 l = position.xy - CameraPos.xy;
+ vec2 s = l / abs(l);
+ s = (1.0 - s * CameraPos.xy);
+ l /= s;
+ return vec4(l, s);
+}
- return shadowPosition;
+float getPerspectiveFactor(in vec4 relativePosition)
+{
+ float pDistance = length(relativePosition.xy);
+ float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
+ return pFactor;
}
+vec4 applyPerspectiveDistortion(in vec4 position)
+{
+ vec4 l = getRelativePosition(position);
+ float pFactor = getPerspectiveFactor(l);
+ l.xy /= pFactor;
+ position.xy = l.xy * l.zw + CameraPos.xy;
+ position.z *= zPerspectiveBias;
+ return position;
+}
void main()
{
vec4 pos = LightMVP * gl_Vertex;
- tPos = getPerspectiveFactor(pos);
+ tPos = applyPerspectiveDistortion(pos);
gl_Position = vec4(tPos.xyz, 1.0);
- gl_TexCoord[0].st = gl_MultiTexCoord0.st;
+ gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}