2
0

opengl_vertex.glsl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. uniform mat4 mWorld;
  2. // Color of the light emitted by the sun.
  3. uniform vec3 dayLight;
  4. // The cameraOffset is the current center of the visible world.
  5. uniform highp vec3 cameraOffset;
  6. uniform float animationTimer;
  7. varying vec3 vNormal;
  8. varying vec3 vPosition;
  9. // World position in the visible world (i.e. relative to the cameraOffset.)
  10. // This can be used for many shader effects without loss of precision.
  11. // If the absolute position is required it can be calculated with
  12. // cameraOffset + worldPosition (for large coordinates the limits of float
  13. // precision must be considered).
  14. varying vec3 worldPosition;
  15. varying lowp vec4 varColor;
  16. // The centroid keyword ensures that after interpolation the texture coordinates
  17. // lie within the same bounds when MSAA is en- and disabled.
  18. // This fixes the stripes problem with nearest-neighbor textures and MSAA.
  19. #ifdef GL_ES
  20. varying mediump vec2 varTexCoord;
  21. #else
  22. centroid varying vec2 varTexCoord;
  23. #endif
  24. #ifdef ENABLE_DYNAMIC_SHADOWS
  25. // shadow uniforms
  26. uniform vec3 v_LightDirection;
  27. uniform float f_textureresolution;
  28. uniform mat4 m_ShadowViewProj;
  29. uniform float f_shadowfar;
  30. uniform float f_shadow_strength;
  31. uniform float f_timeofday;
  32. uniform vec4 CameraPos;
  33. varying float cosLight;
  34. varying float normalOffsetScale;
  35. varying float adj_shadow_strength;
  36. varying float f_normal_length;
  37. varying vec3 shadow_position;
  38. varying float perspective_factor;
  39. #endif
  40. varying float area_enable_parallax;
  41. varying highp vec3 eyeVec;
  42. varying float nightRatio;
  43. // Color of the light emitted by the light sources.
  44. const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
  45. const float e = 2.718281828459;
  46. const float BS = 10.0;
  47. uniform float xyPerspectiveBias0;
  48. uniform float xyPerspectiveBias1;
  49. uniform float zPerspectiveBias;
  50. #ifdef ENABLE_DYNAMIC_SHADOWS
  51. vec4 getRelativePosition(in vec4 position)
  52. {
  53. vec2 l = position.xy - CameraPos.xy;
  54. vec2 s = l / abs(l);
  55. s = (1.0 - s * CameraPos.xy);
  56. l /= s;
  57. return vec4(l, s);
  58. }
  59. float getPerspectiveFactor(in vec4 relativePosition)
  60. {
  61. float pDistance = length(relativePosition.xy);
  62. float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
  63. return pFactor;
  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. // custom smoothstep implementation because it's not defined in glsl1.2
  75. // https://docs.gl/sl4/smoothstep
  76. float mtsmoothstep(in float edge0, in float edge1, in float x)
  77. {
  78. float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  79. return t * t * (3.0 - 2.0 * t);
  80. }
  81. #endif
  82. float smoothCurve(float x)
  83. {
  84. return x * x * (3.0 - 2.0 * x);
  85. }
  86. float triangleWave(float x)
  87. {
  88. return abs(fract(x + 0.5) * 2.0 - 1.0);
  89. }
  90. float smoothTriangleWave(float x)
  91. {
  92. return smoothCurve(triangleWave(x)) * 2.0 - 1.0;
  93. }
  94. #if MATERIAL_WAVING_LIQUID && ENABLE_WAVING_WATER
  95. //
  96. // Simple, fast noise function.
  97. // See: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
  98. //
  99. vec4 perm(vec4 x)
  100. {
  101. return mod(((x * 34.0) + 1.0) * x, 289.0);
  102. }
  103. float snoise(vec3 p)
  104. {
  105. vec3 a = floor(p);
  106. vec3 d = p - a;
  107. d = d * d * (3.0 - 2.0 * d);
  108. vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
  109. vec4 k1 = perm(b.xyxy);
  110. vec4 k2 = perm(k1.xyxy + b.zzww);
  111. vec4 c = k2 + a.zzzz;
  112. vec4 k3 = perm(c);
  113. vec4 k4 = perm(c + 1.0);
  114. vec4 o1 = fract(k3 * (1.0 / 41.0));
  115. vec4 o2 = fract(k4 * (1.0 / 41.0));
  116. vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
  117. vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
  118. return o4.y * d.y + o4.x * (1.0 - d.y);
  119. }
  120. #endif
  121. void main(void)
  122. {
  123. varTexCoord = inTexCoord0.st;
  124. float disp_x;
  125. float disp_z;
  126. // OpenGL < 4.3 does not support continued preprocessor lines
  127. #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
  128. vec4 pos2 = mWorld * inVertexPosition;
  129. float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002;
  130. disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) +
  131. smoothTriangleWave(animationTimer * 11.0 + tOffset)) * 0.4;
  132. disp_z = (smoothTriangleWave(animationTimer * 31.0 + tOffset) +
  133. smoothTriangleWave(animationTimer * 29.0 + tOffset) +
  134. smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5;
  135. #endif
  136. vec4 pos = inVertexPosition;
  137. #if MATERIAL_WAVING_LIQUID && ENABLE_WAVING_WATER
  138. // Generate waves with Perlin-type noise.
  139. // The constants are calibrated such that they roughly
  140. // correspond to the old sine waves.
  141. vec3 wavePos = (mWorld * pos).xyz + cameraOffset;
  142. // The waves are slightly compressed along the z-axis to get
  143. // wave-fronts along the x-axis.
  144. wavePos.x /= WATER_WAVE_LENGTH * 3.0;
  145. wavePos.z /= WATER_WAVE_LENGTH * 2.0;
  146. wavePos.z += animationTimer * WATER_WAVE_SPEED * 10.0;
  147. pos.y += (snoise(wavePos) - 1.0) * WATER_WAVE_HEIGHT * 5.0;
  148. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
  149. pos.x += disp_x;
  150. pos.y += disp_z * 0.1;
  151. pos.z += disp_z;
  152. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
  153. if (varTexCoord.y < 0.05) {
  154. pos.x += disp_x;
  155. pos.z += disp_z;
  156. }
  157. #endif
  158. worldPosition = (mWorld * pos).xyz;
  159. gl_Position = mWorldViewProj * pos;
  160. vPosition = gl_Position.xyz;
  161. eyeVec = -(mWorldView * pos).xyz;
  162. #ifdef SECONDSTAGE
  163. normalPass = normalize((inVertexNormal+1)/2);
  164. #endif
  165. vNormal = inVertexNormal;
  166. // Calculate color.
  167. vec4 color = inVertexColor;
  168. // Red, green and blue components are pre-multiplied with
  169. // the brightness, so now we have to multiply these
  170. // colors with the color of the incoming light.
  171. // The pre-baked colors are halved to prevent overflow.
  172. // The alpha gives the ratio of sunlight in the incoming light.
  173. nightRatio = 1.0 - color.a;
  174. color.rgb = color.rgb * (color.a * dayLight.rgb +
  175. nightRatio * artificialLight.rgb) * 2.0;
  176. color.a = 1.0;
  177. // Emphase blue a bit in darker places
  178. // See C++ implementation in mapblock_mesh.cpp final_color_blend()
  179. float brightness = (color.r + color.g + color.b) / 3.0;
  180. color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
  181. 0.07 * brightness);
  182. varColor = clamp(color, 0.0, 1.0);
  183. #ifdef ENABLE_DYNAMIC_SHADOWS
  184. if (f_shadow_strength > 0.0) {
  185. #if MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
  186. // The shadow shaders don't apply waving when creating the shadow-map.
  187. // We are using the not waved inVertexPosition to avoid ugly self-shadowing.
  188. vec4 shadow_pos = inVertexPosition;
  189. #else
  190. vec4 shadow_pos = pos;
  191. #endif
  192. vec3 nNormal;
  193. f_normal_length = length(vNormal);
  194. /* normalOffsetScale is in world coordinates (1/10th of a meter)
  195. z_bias is in light space coordinates */
  196. float normalOffsetScale, z_bias;
  197. float pFactor = getPerspectiveFactor(getRelativePosition(m_ShadowViewProj * mWorld * shadow_pos));
  198. if (f_normal_length > 0.0) {
  199. nNormal = normalize(vNormal);
  200. cosLight = max(1e-5, dot(nNormal, -v_LightDirection));
  201. float sinLight = pow(1.0 - pow(cosLight, 2.0), 0.5);
  202. normalOffsetScale = 2.0 * pFactor * pFactor * sinLight * min(f_shadowfar, 500.0) /
  203. xyPerspectiveBias1 / f_textureresolution;
  204. z_bias = 1.0 * sinLight / cosLight;
  205. }
  206. else {
  207. nNormal = vec3(0.0);
  208. cosLight = clamp(dot(v_LightDirection, normalize(vec3(v_LightDirection.x, 0.0, v_LightDirection.z))), 1e-2, 1.0);
  209. float sinLight = pow(1.0 - pow(cosLight, 2.0), 0.5);
  210. normalOffsetScale = 0.0;
  211. z_bias = 3.6e3 * sinLight / cosLight;
  212. }
  213. z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
  214. shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (shadow_pos + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
  215. #if !defined(ENABLE_TRANSLUCENT_FOLIAGE) || MATERIAL_TYPE != TILE_MATERIAL_WAVING_LEAVES
  216. shadow_position.z -= z_bias;
  217. #endif
  218. perspective_factor = pFactor;
  219. if (f_timeofday < 0.2) {
  220. adj_shadow_strength = f_shadow_strength * 0.5 *
  221. (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday));
  222. } else if (f_timeofday >= 0.8) {
  223. adj_shadow_strength = f_shadow_strength * 0.5 *
  224. mtsmoothstep(0.8, 0.83, f_timeofday);
  225. } else {
  226. adj_shadow_strength = f_shadow_strength *
  227. mtsmoothstep(0.20, 0.25, f_timeofday) *
  228. (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
  229. }
  230. }
  231. #endif
  232. }