2
0

opengl_vertex.glsl 5.1 KB

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