opengl_vertex.glsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. varying vec3 vNormal;
  8. varying vec3 vPosition;
  9. varying vec3 worldPosition;
  10. varying lowp vec4 varColor;
  11. #ifdef GL_ES
  12. varying mediump vec2 varTexCoord;
  13. #else
  14. centroid varying vec2 varTexCoord;
  15. #endif
  16. #ifdef ENABLE_DYNAMIC_SHADOWS
  17. // shadow uniforms
  18. uniform vec3 v_LightDirection;
  19. uniform float f_textureresolution;
  20. uniform mat4 m_ShadowViewProj;
  21. uniform float f_shadowfar;
  22. uniform float f_shadow_strength;
  23. uniform float f_timeofday;
  24. uniform vec4 CameraPos;
  25. varying float cosLight;
  26. varying float adj_shadow_strength;
  27. varying float f_normal_length;
  28. varying vec3 shadow_position;
  29. varying float perspective_factor;
  30. #endif
  31. varying vec3 eyeVec;
  32. varying float nightRatio;
  33. // Color of the light emitted by the light sources.
  34. const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
  35. varying float vIDiff;
  36. const float e = 2.718281828459;
  37. const float BS = 10.0;
  38. uniform float xyPerspectiveBias0;
  39. uniform float xyPerspectiveBias1;
  40. uniform float zPerspectiveBias;
  41. #ifdef ENABLE_DYNAMIC_SHADOWS
  42. vec4 getRelativePosition(in vec4 position)
  43. {
  44. vec2 l = position.xy - CameraPos.xy;
  45. vec2 s = l / abs(l);
  46. s = (1.0 - s * CameraPos.xy);
  47. l /= s;
  48. return vec4(l, s);
  49. }
  50. float getPerspectiveFactor(in vec4 relativePosition)
  51. {
  52. float pDistance = length(relativePosition.xy);
  53. float pFactor = pDistance * xyPerspectiveBias0 + xyPerspectiveBias1;
  54. return pFactor;
  55. }
  56. vec4 applyPerspectiveDistortion(in vec4 position)
  57. {
  58. vec4 l = getRelativePosition(position);
  59. float pFactor = getPerspectiveFactor(l);
  60. l.xy /= pFactor;
  61. position.xy = l.xy * l.zw + CameraPos.xy;
  62. position.z *= zPerspectiveBias;
  63. return position;
  64. }
  65. // custom smoothstep implementation because it's not defined in glsl1.2
  66. // https://docs.gl/sl4/smoothstep
  67. float mtsmoothstep(in float edge0, in float edge1, in float x)
  68. {
  69. float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  70. return t * t * (3.0 - 2.0 * t);
  71. }
  72. #endif
  73. float directional_ambient(vec3 normal)
  74. {
  75. vec3 v = normal * normal;
  76. if (normal.y < 0.0)
  77. return dot(v, vec3(0.670820, 0.447213, 0.836660));
  78. return dot(v, vec3(0.670820, 1.000000, 0.836660));
  79. }
  80. void main(void)
  81. {
  82. varTexCoord = (mTexture * inTexCoord0).st;
  83. gl_Position = mWorldViewProj * inVertexPosition;
  84. vPosition = gl_Position.xyz;
  85. vNormal = (mWorld * vec4(inVertexNormal, 0.0)).xyz;
  86. worldPosition = (mWorld * inVertexPosition).xyz;
  87. eyeVec = -(mWorldView * inVertexPosition).xyz;
  88. #if (MATERIAL_TYPE == TILE_MATERIAL_PLAIN) || (MATERIAL_TYPE == TILE_MATERIAL_PLAIN_ALPHA)
  89. vIDiff = 1.0;
  90. #else
  91. // This is intentional comparison with zero without any margin.
  92. // If normal is not equal to zero exactly, then we assume it's a valid, just not normalized vector
  93. vIDiff = length(inVertexNormal) == 0.0
  94. ? 1.0
  95. : directional_ambient(normalize(inVertexNormal));
  96. #endif
  97. #ifdef GL_ES
  98. vec4 color = inVertexColor.bgra;
  99. #else
  100. vec4 color = inVertexColor;
  101. #endif
  102. color *= emissiveColor;
  103. // The alpha gives the ratio of sunlight in the incoming light.
  104. nightRatio = 1.0 - color.a;
  105. color.rgb = color.rgb * (color.a * dayLight.rgb +
  106. nightRatio * artificialLight.rgb) * 2.0;
  107. color.a = 1.0;
  108. // Emphase blue a bit in darker places
  109. // See C++ implementation in mapblock_mesh.cpp final_color_blend()
  110. float brightness = (color.r + color.g + color.b) / 3.0;
  111. color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
  112. 0.07 * brightness);
  113. varColor = clamp(color, 0.0, 1.0);
  114. #ifdef ENABLE_DYNAMIC_SHADOWS
  115. if (f_shadow_strength > 0.0) {
  116. vec3 nNormal = normalize(vNormal);
  117. f_normal_length = length(vNormal);
  118. /* normalOffsetScale is in world coordinates (1/10th of a meter)
  119. z_bias is in light space coordinates */
  120. float normalOffsetScale, z_bias;
  121. float pFactor = getPerspectiveFactor(getRelativePosition(m_ShadowViewProj * mWorld * inVertexPosition));
  122. if (f_normal_length > 0.0) {
  123. nNormal = normalize(vNormal);
  124. cosLight = dot(nNormal, -v_LightDirection);
  125. float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
  126. normalOffsetScale = 0.1 * pFactor * pFactor * sinLight * min(f_shadowfar, 500.0) /
  127. xyPerspectiveBias1 / f_textureresolution;
  128. z_bias = 1e3 * sinLight / cosLight * (0.5 + f_textureresolution / 1024.0);
  129. }
  130. else {
  131. nNormal = vec3(0.0);
  132. cosLight = clamp(dot(v_LightDirection, normalize(vec3(v_LightDirection.x, 0.0, v_LightDirection.z))), 1e-2, 1.0);
  133. float sinLight = pow(1 - pow(cosLight, 2.0), 0.5);
  134. normalOffsetScale = 0.0;
  135. z_bias = 3.6e3 * sinLight / cosLight;
  136. }
  137. z_bias *= pFactor * pFactor / f_textureresolution / f_shadowfar;
  138. shadow_position = applyPerspectiveDistortion(m_ShadowViewProj * mWorld * (inVertexPosition + vec4(normalOffsetScale * nNormal, 0.0))).xyz;
  139. shadow_position.z -= z_bias;
  140. perspective_factor = pFactor;
  141. if (f_timeofday < 0.2) {
  142. adj_shadow_strength = f_shadow_strength * 0.5 *
  143. (1.0 - mtsmoothstep(0.18, 0.2, f_timeofday));
  144. } else if (f_timeofday >= 0.8) {
  145. adj_shadow_strength = f_shadow_strength * 0.5 *
  146. mtsmoothstep(0.8, 0.83, f_timeofday);
  147. } else {
  148. adj_shadow_strength = f_shadow_strength *
  149. mtsmoothstep(0.20, 0.25, f_timeofday) *
  150. (1.0 - mtsmoothstep(0.7, 0.8, f_timeofday));
  151. }
  152. }
  153. #endif
  154. }