]> git.lizzy.rs Git - dragonfireclient.git/blob - client/shaders/object_shader/opengl_vertex.glsl
Improve shadow filters (#12195)
[dragonfireclient.git] / client / shaders / object_shader / opengl_vertex.glsl
1 uniform mat4 mWorld;
2 uniform vec3 dayLight;
3 uniform vec3 eyePosition;
4 uniform float animationTimer;
5 uniform vec4 emissiveColor;
6 uniform vec3 cameraOffset;
7
8
9 varying vec3 vNormal;
10 varying vec3 vPosition;
11 varying vec3 worldPosition;
12 varying lowp vec4 varColor;
13 #ifdef GL_ES
14 varying mediump vec2 varTexCoord;
15 #else
16 centroid varying vec2 varTexCoord;
17 #endif
18
19 #ifdef ENABLE_DYNAMIC_SHADOWS
20         // shadow uniforms
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;
28
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;
34 #endif
35
36 varying vec3 eyeVec;
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);
40 varying float vIDiff;
41 const float e = 2.718281828459;
42 const float BS = 10.0;
43 uniform float xyPerspectiveBias0;
44 uniform float xyPerspectiveBias1;
45 uniform float zPerspectiveBias;
46
47 #ifdef ENABLE_DYNAMIC_SHADOWS
48
49 vec4 getRelativePosition(in vec4 position)
50 {
51         vec2 l = position.xy - CameraPos.xy;
52         vec2 s = l / abs(l);
53         s = (1.0 - s * CameraPos.xy);
54         l /= s;
55         return vec4(l, s);
56 }
57
58 float getPerspectiveFactor(in vec4 relativePosition)
59 {
60         float pDistance = length(relativePosition.xy);
61         float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
62         return pFactor;
63 }
64
65 vec4 applyPerspectiveDistortion(in vec4 position)
66 {
67         vec4 l = getRelativePosition(position);
68         float pFactor = getPerspectiveFactor(l);
69         l.xy /= pFactor;
70         position.xy = l.xy * l.zw + CameraPos.xy;
71         position.z *= zPerspectiveBias;
72         return position;
73 }
74
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)
78 {
79         float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
80         return t * t * (3.0 - 2.0 * t);
81 }
82 #endif
83
84
85 float directional_ambient(vec3 normal)
86 {
87         vec3 v = normal * normal;
88
89         if (normal.y < 0.0)
90                 return dot(v, vec3(0.670820, 0.447213, 0.836660));
91
92         return dot(v, vec3(0.670820, 1.000000, 0.836660));
93 }
94
95 void main(void)
96 {
97         varTexCoord = (mTexture * inTexCoord0).st;
98         gl_Position = mWorldViewProj * inVertexPosition;
99
100         vPosition = gl_Position.xyz;
101         vNormal = (mWorld * vec4(inVertexNormal, 0.0)).xyz;
102         worldPosition = (mWorld * inVertexPosition).xyz;
103         eyeVec = -(mWorldView * inVertexPosition).xyz;
104
105 #if (MATERIAL_TYPE == TILE_MATERIAL_PLAIN) || (MATERIAL_TYPE == TILE_MATERIAL_PLAIN_ALPHA)
106         vIDiff = 1.0;
107 #else
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
111                 ? 1.0
112                 : directional_ambient(normalize(inVertexNormal));
113 #endif
114
115 #ifdef GL_ES
116         vec4 color = inVertexColor.bgra;
117 #else
118         vec4 color = inVertexColor;
119 #endif
120
121         color *= emissiveColor;
122
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;
127         color.a = 1.0;
128
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) +
133                 0.07 * brightness);
134
135         varColor = clamp(color, 0.0, 1.0);
136
137
138 #ifdef ENABLE_DYNAMIC_SHADOWS
139         if (f_shadow_strength > 0.0) {
140                 vec3 nNormal = normalize(vNormal);
141                 f_normal_length = length(vNormal);
142
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);
154                 }
155                 else {
156                         nNormal = vec3(0.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;
161                 }
162                 z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
163
164                 shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (inVertexPosition + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
165                 shadow_position.z -= z_bias;
166                 perspective_factor = pFactor;
167
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);
174                 } else {
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));
178                 }
179         }
180 #endif
181 }