3 uniform vec3 eyePosition;
4 uniform float animationTimer;
5 uniform vec4 emissiveColor;
6 uniform vec3 cameraOffset;
10 varying vec3 vPosition;
11 varying vec3 worldPosition;
12 varying lowp vec4 varColor;
14 varying mediump vec2 varTexCoord;
16 centroid varying vec2 varTexCoord;
19 #ifdef ENABLE_DYNAMIC_SHADOWS
21 uniform vec3 v_LightDirection;
22 uniform float f_textureresolution;
23 uniform mat4 m_ShadowViewProj;
24 uniform float f_shadowfar;
25 uniform float f_shadow_strength;
26 uniform float f_timeofday;
27 uniform vec4 CameraPos;
29 varying float cosLight;
30 varying float adj_shadow_strength;
31 varying float f_normal_length;
32 varying vec3 shadow_position;
33 varying float perspective_factor;
37 varying float nightRatio;
38 // Color of the light emitted by the light sources.
39 const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
41 const float e = 2.718281828459;
42 const float BS = 10.0;
43 uniform float xyPerspectiveBias0;
44 uniform float xyPerspectiveBias1;
45 uniform float zPerspectiveBias;
47 #ifdef ENABLE_DYNAMIC_SHADOWS
49 vec4 getRelativePosition(in vec4 position)
51 vec2 l = position.xy - CameraPos.xy;
53 s = (1.0 - s * CameraPos.xy);
58 float getPerspectiveFactor(in vec4 relativePosition)
60 float pDistance = length(relativePosition.xy);
61 float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
65 vec4 applyPerspectiveDistortion(in vec4 position)
67 vec4 l = getRelativePosition(position);
68 float pFactor = getPerspectiveFactor(l);
70 position.xy = l.xy * l.zw + CameraPos.xy;
71 position.z *= zPerspectiveBias;
75 // custom smoothstep implementation because it's not defined in glsl1.2
76 // https://docs.gl/sl4/smoothstep
77 float mtsmoothstep(in float edge0, in float edge1, in float x)
79 float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
80 return t * t * (3.0 - 2.0 * t);
85 float directional_ambient(vec3 normal)
87 vec3 v = normal * normal;
90 return dot(v, vec3(0.670820, 0.447213, 0.836660));
92 return dot(v, vec3(0.670820, 1.000000, 0.836660));
97 varTexCoord = (mTexture * inTexCoord0).st;
98 gl_Position = mWorldViewProj * inVertexPosition;
100 vPosition = gl_Position.xyz;
101 vNormal = (mWorld * vec4(inVertexNormal, 0.0)).xyz;
102 worldPosition = (mWorld * inVertexPosition).xyz;
103 eyeVec = -(mWorldView * inVertexPosition).xyz;
105 #if (MATERIAL_TYPE == TILE_MATERIAL_PLAIN) || (MATERIAL_TYPE == TILE_MATERIAL_PLAIN_ALPHA)
108 // This is intentional comparison with zero without any margin.
109 // If normal is not equal to zero exactly, then we assume it's a valid, just not normalized vector
110 vIDiff = length(inVertexNormal) == 0.0
112 : directional_ambient(normalize(inVertexNormal));
116 vec4 color = inVertexColor.bgra;
118 vec4 color = inVertexColor;
121 color *= emissiveColor;
123 // The alpha gives the ratio of sunlight in the incoming light.
124 nightRatio = 1.0 - color.a;
125 color.rgb = color.rgb * (color.a * dayLight.rgb +
126 nightRatio * artificialLight.rgb) * 2.0;
129 // Emphase blue a bit in darker places
130 // See C++ implementation in mapblock_mesh.cpp final_color_blend()
131 float brightness = (color.r + color.g + color.b) / 3.0;
132 color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
135 varColor = clamp(color, 0.0, 1.0);
138 #ifdef ENABLE_DYNAMIC_SHADOWS
139 if (f_shadow_strength > 0.0) {
140 vec3 nNormal = normalize(vNormal);
141 f_normal_length = length(vNormal);
143 /* normalOffsetScale is in world coordinates (1/10th of a meter)
144 z_bias is in light space coordinates */
145 float normalOffsetScale, z_bias;
146 float pFactor = getPerspectiveFactor(getRelativePosition(m_ShadowViewProj * mWorld * inVertexPosition));
147 if (f_normal_length > 0.0) {
148 nNormal = normalize(vNormal);
149 cosLight = dot(nNormal, -v_LightDirection);
150 float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
151 normalOffsetScale = 0.1 * pFactor * pFactor * sinLight * min(f_shadowfar, 500.0) /
152 xyPerspectiveBias1 / f_textureresolution;
153 z_bias = 1e3 * sinLight / cosLight * (0.5 + f_textureresolution / 1024.0);
157 cosLight = clamp(dot(v_LightDirection, normalize(vec3(v_LightDirection.x, 0.0, v_LightDirection.z))), 1e-2, 1.0);
158 float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
159 normalOffsetScale = 0.0;
160 z_bias = 3.6e3 * sinLight / cosLight;
162 z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
164 shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (inVertexPosition + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
165 shadow_position.z -= z_bias;
166 perspective_factor = pFactor;
168 if (f_timeofday < 0.2) {
169 adj_shadow_strength = f_shadow_strength * 0.5 *
170 (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday));
171 } else if (f_timeofday >= 0.8) {
172 adj_shadow_strength = f_shadow_strength * 0.5 *
173 mtsmoothstep(0.8, 0.83, f_timeofday);
175 adj_shadow_strength = f_shadow_strength *
176 mtsmoothstep(0.20, 0.25, f_timeofday) *
177 (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));