aboutsummaryrefslogtreecommitdiff
path: root/client/shaders/object_shader/opengl_vertex.glsl
diff options
context:
space:
mode:
Diffstat (limited to 'client/shaders/object_shader/opengl_vertex.glsl')
-rw-r--r--client/shaders/object_shader/opengl_vertex.glsl124
1 files changed, 101 insertions, 23 deletions
diff --git a/client/shaders/object_shader/opengl_vertex.glsl b/client/shaders/object_shader/opengl_vertex.glsl
index f135ab9dc..dc9c70cdf 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,54 @@ 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;
+ varying float perspective_factor;
#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 +98,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 +113,69 @@ 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;
+ perspective_factor = pFactor;
+
+ 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
}