opengl_vertex.glsl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. uniform mat4 mWorldViewProj;
  2. uniform mat4 mWorld;
  3. // Color of the light emitted by the sun.
  4. uniform vec3 dayLight;
  5. uniform vec3 eyePosition;
  6. // The cameraOffset is the current center of the visible world.
  7. uniform vec3 cameraOffset;
  8. uniform float animationTimer;
  9. varying vec3 vPosition;
  10. // World position in the visible world (i.e. relative to the cameraOffset.)
  11. // This can be used for many shader effects without loss of precision.
  12. // If the absolute position is required it can be calculated with
  13. // cameraOffset + worldPosition (for large coordinates the limits of float
  14. // precision must be considered).
  15. varying vec3 worldPosition;
  16. varying vec3 eyeVec;
  17. varying vec3 lightVec;
  18. varying vec3 tsEyeVec;
  19. varying vec3 tsLightVec;
  20. varying float area_enable_parallax;
  21. // Color of the light emitted by the light sources.
  22. const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
  23. const float e = 2.718281828459;
  24. const float BS = 10.0;
  25. float smoothCurve(float x)
  26. {
  27. return x * x * (3.0 - 2.0 * x);
  28. }
  29. float triangleWave(float x)
  30. {
  31. return abs(fract(x + 0.5) * 2.0 - 1.0);
  32. }
  33. float smoothTriangleWave(float x)
  34. {
  35. return smoothCurve(triangleWave(x)) * 2.0 - 1.0;
  36. }
  37. // OpenGL < 4.3 does not support continued preprocessor lines
  38. #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
  39. //
  40. // Simple, fast noise function.
  41. // See: https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83
  42. //
  43. vec4 perm(vec4 x)
  44. {
  45. return mod(((x * 34.0) + 1.0) * x, 289.0);
  46. }
  47. float snoise(vec3 p)
  48. {
  49. vec3 a = floor(p);
  50. vec3 d = p - a;
  51. d = d * d * (3.0 - 2.0 * d);
  52. vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
  53. vec4 k1 = perm(b.xyxy);
  54. vec4 k2 = perm(k1.xyxy + b.zzww);
  55. vec4 c = k2 + a.zzzz;
  56. vec4 k3 = perm(c);
  57. vec4 k4 = perm(c + 1.0);
  58. vec4 o1 = fract(k3 * (1.0 / 41.0));
  59. vec4 o2 = fract(k4 * (1.0 / 41.0));
  60. vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
  61. vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);
  62. return o4.y * d.y + o4.x * (1.0 - d.y);
  63. }
  64. #endif
  65. void main(void)
  66. {
  67. gl_TexCoord[0] = gl_MultiTexCoord0;
  68. //TODO: make offset depending on view angle and parallax uv displacement
  69. //thats for textures that doesnt align vertically, like dirt with grass
  70. //gl_TexCoord[0].y += 0.008;
  71. //Allow parallax/relief mapping only for certain kind of nodes
  72. //Variable is also used to control area of the effect
  73. #if (DRAW_TYPE == NDT_NORMAL || DRAW_TYPE == NDT_LIQUID || DRAW_TYPE == NDT_FLOWINGLIQUID)
  74. area_enable_parallax = 1.0;
  75. #else
  76. area_enable_parallax = 0.0;
  77. #endif
  78. float disp_x;
  79. float disp_z;
  80. // OpenGL < 4.3 does not support continued preprocessor lines
  81. #if (MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES) || (MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS)
  82. vec4 pos2 = mWorld * gl_Vertex;
  83. float tOffset = (pos2.x + pos2.y) * 0.001 + pos2.z * 0.002;
  84. disp_x = (smoothTriangleWave(animationTimer * 23.0 + tOffset) +
  85. smoothTriangleWave(animationTimer * 11.0 + tOffset)) * 0.4;
  86. disp_z = (smoothTriangleWave(animationTimer * 31.0 + tOffset) +
  87. smoothTriangleWave(animationTimer * 29.0 + tOffset) +
  88. smoothTriangleWave(animationTimer * 13.0 + tOffset)) * 0.5;
  89. #endif
  90. worldPosition = (mWorld * gl_Vertex).xyz;
  91. // OpenGL < 4.3 does not support continued preprocessor lines
  92. #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
  93. // Generate waves with Perlin-type noise.
  94. // The constants are calibrated such that they roughly
  95. // correspond to the old sine waves.
  96. vec4 pos = gl_Vertex;
  97. vec3 wavePos = worldPosition + cameraOffset;
  98. // The waves are slightly compressed along the z-axis to get
  99. // wave-fronts along the x-axis.
  100. wavePos.x /= WATER_WAVE_LENGTH * 3;
  101. wavePos.z /= WATER_WAVE_LENGTH * 2;
  102. wavePos.z += animationTimer * WATER_WAVE_SPEED * 10;
  103. pos.y += (snoise(wavePos) - 1) * WATER_WAVE_HEIGHT * 5;
  104. gl_Position = mWorldViewProj * pos;
  105. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES && ENABLE_WAVING_LEAVES
  106. vec4 pos = gl_Vertex;
  107. pos.x += disp_x;
  108. pos.y += disp_z * 0.1;
  109. pos.z += disp_z;
  110. gl_Position = mWorldViewProj * pos;
  111. #elif MATERIAL_TYPE == TILE_MATERIAL_WAVING_PLANTS && ENABLE_WAVING_PLANTS
  112. vec4 pos = gl_Vertex;
  113. if (gl_TexCoord[0].y < 0.05) {
  114. pos.x += disp_x;
  115. pos.z += disp_z;
  116. }
  117. gl_Position = mWorldViewProj * pos;
  118. #else
  119. gl_Position = mWorldViewProj * gl_Vertex;
  120. #endif
  121. vPosition = gl_Position.xyz;
  122. // Don't generate heightmaps when too far from the eye
  123. float dist = distance (vec3(0.0, 0.0, 0.0), vPosition);
  124. if (dist > 150.0) {
  125. area_enable_parallax = 0.0;
  126. }
  127. vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);
  128. vec3 normal, tangent, binormal;
  129. normal = normalize(gl_NormalMatrix * gl_Normal);
  130. tangent = normalize(gl_NormalMatrix * gl_MultiTexCoord1.xyz);
  131. binormal = normalize(gl_NormalMatrix * gl_MultiTexCoord2.xyz);
  132. vec3 v;
  133. lightVec = sunPosition - worldPosition;
  134. v.x = dot(lightVec, tangent);
  135. v.y = dot(lightVec, binormal);
  136. v.z = dot(lightVec, normal);
  137. tsLightVec = normalize (v);
  138. eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz;
  139. v.x = dot(eyeVec, tangent);
  140. v.y = dot(eyeVec, binormal);
  141. v.z = dot(eyeVec, normal);
  142. tsEyeVec = normalize (v);
  143. // Calculate color.
  144. // Red, green and blue components are pre-multiplied with
  145. // the brightness, so now we have to multiply these
  146. // colors with the color of the incoming light.
  147. // The pre-baked colors are halved to prevent overflow.
  148. vec4 color;
  149. // The alpha gives the ratio of sunlight in the incoming light.
  150. float nightRatio = 1 - gl_Color.a;
  151. color.rgb = gl_Color.rgb * (gl_Color.a * dayLight.rgb +
  152. nightRatio * artificialLight.rgb) * 2;
  153. color.a = 1;
  154. // Emphase blue a bit in darker places
  155. // See C++ implementation in mapblock_mesh.cpp final_color_blend()
  156. float brightness = (color.r + color.g + color.b) / 3;
  157. color.b += max(0.0, 0.021 - abs(0.2 * brightness - 0.021) +
  158. 0.07 * brightness);
  159. gl_FrontColor = gl_BackColor = clamp(color, 0.0, 1.0);
  160. }