2
0

opengl_vertex.glsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. uniform mat4 mWorld;
  2. // Color of the light emitted by the sun.
  3. uniform vec3 dayLight;
  4. uniform vec3 eyePosition;
  5. // The cameraOffset is the current center of the visible world.
  6. uniform vec3 cameraOffset;
  7. uniform float animationTimer;
  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-neighbour textures and MSAA.
  19. #ifdef GL_ES
  20. varying mediump vec2 varTexCoord;
  21. #else
  22. centroid varying vec2 varTexCoord;
  23. #endif
  24. varying vec3 eyeVec;
  25. // Color of the light emitted by the light sources.
  26. const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
  27. const float e = 2.718281828459;
  28. const float BS = 10.0;
  29. float smoothCurve(float x)
  30. {
  31. return x * x * (3.0 - 2.0 * x);
  32. }
  33. float triangleWave(float x)
  34. {
  35. return abs(fract(x + 0.5) * 2.0 - 1.0);
  36. }
  37. float smoothTriangleWave(float x)
  38. {
  39. return smoothCurve(triangleWave(x)) * 2.0 - 1.0;
  40. }
  41. // OpenGL < 4.3 does not support continued preprocessor lines
  42. #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_OPAQUE || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_BASIC) && ENABLE_WAVING_WATER
  43. //
  44. // Simple, fast noise function.
  45. // See: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
  46. //
  47. vec4 perm(vec4 x)
  48. {
  49. return mod(((x * 34.0) + 1.0) * x, 289.0);
  50. }
  51. float snoise(vec3 p)
  52. {
  53. vec3 a = floor(p);
  54. vec3 d = p - a;
  55. d = d * d * (3.0 - 2.0 * d);
  56. vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
  57. vec4 k1 = perm(b.xyxy);
  58. vec4 k2 = perm(k1.xyxy + b.zzww);
  59. vec4 c = k2 + a.zzzz;
  60. vec4 k3 = perm(c);
  61. vec4 k4 = perm(c + 1.0);
  62. vec4 o1 = fract(k3 * (1.0 / 41.0));
  63. vec4 o2 = fract(k4 * (1.0 / 41.0));
  64. vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
  65. vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
  66. return o4.y * d.y + o4.x * (1.0 - d.y);
  67. }
  68. #endif
  69. void main(void)
  70. {
  71. varTexCoord = inTexCoord0.st;
  72. float disp_x;
  73. float disp_z;
  74. // OpenGL < 4.3 does not support continued preprocessor lines
  75. #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
  76. vec4 pos2 = mWorld * inVertexPosition;
  77. float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002;
  78. disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) +
  79. smoothTriangleWave(animationTimer * 11.0 + tOffset)) * 0.4;
  80. disp_z = (smoothTriangleWave(animationTimer * 31.0 + tOffset) +
  81. smoothTriangleWave(animationTimer * 29.0 + tOffset) +
  82. smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5;
  83. #endif
  84. worldPosition = (mWorld * inVertexPosition).xyz;
  85. // OpenGL < 4.3 does not support continued preprocessor lines
  86. #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_OPAQUE || MATERIAL_TYPE == TILE_MATERIAL_WAVING_LIQUID_BASIC) && ENABLE_WAVING_WATER
  87. // Generate waves with Perlin-type noise.
  88. // The constants are calibrated such that they roughly
  89. // correspond to the old sine waves.
  90. vec4 pos = inVertexPosition;
  91. vec3 wavePos = worldPosition + cameraOffset;
  92. // The waves are slightly compressed along the z-axis to get
  93. // wave-fronts along the x-axis.
  94. wavePos.x /= WATER_WAVE_LENGTH * 3.0;
  95. wavePos.z /= WATER_WAVE_LENGTH * 2.0;
  96. wavePos.z += animationTimer * WATER_WAVE_SPEED * 10.0;
  97. pos.y += (snoise(wavePos) - 1.0) * WATER_WAVE_HEIGHT * 5.0;
  98. gl_Position = mWorldViewProj * pos;
  99. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
  100. vec4 pos = inVertexPosition;
  101. pos.x += disp_x;
  102. pos.y += disp_z * 0.1;
  103. pos.z += disp_z;
  104. gl_Position = mWorldViewProj * pos;
  105. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
  106. vec4 pos = inVertexPosition;
  107. if (varTexCoord.y < 0.05) {
  108. pos.x += disp_x;
  109. pos.z += disp_z;
  110. }
  111. gl_Position = mWorldViewProj * pos;
  112. #else
  113. gl_Position = mWorldViewProj * inVertexPosition;
  114. #endif
  115. vPosition = gl_Position.xyz;
  116. eyeVec = -(mWorldView * inVertexPosition).xyz;
  117. // Calculate color.
  118. // Red, green and blue components are pre-multiplied with
  119. // the brightness, so now we have to multiply these
  120. // colors with the color of the incoming light.
  121. // The pre-baked colors are halved to prevent overflow.
  122. vec4 color;
  123. // The alpha gives the ratio of sunlight in the incoming light.
  124. float nightRatio = 1.0 - inVertexColor.a;
  125. color.rgb = inVertexColor.rgb * (inVertexColor.a * dayLight.rgb +
  126. nightRatio * artificialLight.rgb) * 2.0;
  127. color.a = 1.0;
  128. // Emphase blue a bit in darker places
  129. // See C++ implementation in mapblock_mesh.cpp final_color_blend()
  130. float brightness = (color.r + color.g + color.b) / 3.0;
  131. color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
  132. 0.07 * brightness);
  133. varColor = clamp(color, 0.0, 1.0);
  134. }