water.glslv 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var PRECISION highp
  6. #var USE_TBN_SHADING 0
  7. #var DEBUG_WIREFRAME 0
  8. #var NUM_NORMALMAPS 0
  9. #var FOAM 0
  10. #var SHORE_PARAMS 0
  11. #var MAX_SHORE_DIST 100.0
  12. #var SHORE_MAP_SIZE_X 1.0
  13. #var SHORE_MAP_SIZE_Y 1.0
  14. #var SHORE_MAP_CENTER_X 0.0
  15. #var SHORE_MAP_CENTER_Y 0.0
  16. #var GENERATED_MESH 0
  17. #var DST_NOISE_SCALE_0 0.05
  18. #var DST_NOISE_SCALE_1 0.03
  19. #var DST_NOISE_FREQ_0 1.3
  20. #var DST_NOISE_FREQ_1 1.0
  21. #var DIR_MIN_SHR_FAC 0.4
  22. #var DIR_FREQ 0.5
  23. #var DIR_NOISE_SCALE 0.05
  24. #var DIR_NOISE_FREQ 0.07
  25. #var DIR_MIN_NOISE_FAC 0.5
  26. #var DST_MIN_FAC 0.2
  27. #var WAVES_HOR_FAC 5.0
  28. #var DISABLE_FOG 0
  29. #var WAVES_HEIGHT 1.0
  30. #var WAVES_LENGTH 10.0
  31. #var WATER_LEVEL 0.0
  32. #var SHORE_SMOOTHING 0
  33. #var DYNAMIC 0
  34. #var REFLECTION_TYPE REFL_NONE
  35. #var REFRACTIVE 0
  36. #var STATIC_BATCH 0
  37. #var WATER_EFFECTS 0
  38. /*==============================================================================
  39. INCLUDES
  40. ==============================================================================*/
  41. #include <std.glsl>
  42. #include <math.glslv>
  43. #include <to_world.glslv>
  44. #include <procedural.glslf>
  45. /*==============================================================================
  46. SHADER INTERFACE
  47. ==============================================================================*/
  48. GLSL_IN vec3 a_position;
  49. #if !GENERATED_MESH && (!DYNAMIC || NUM_NORMALMAPS > 0)
  50. GLSL_IN vec4 a_tbn_quat;
  51. #endif
  52. #if !GENERATED_MESH && (NUM_NORMALMAPS > 0 || FOAM)
  53. GLSL_IN vec2 a_texcoord;
  54. #endif
  55. #if DEBUG_WIREFRAME
  56. GLSL_IN float a_polyindex;
  57. #endif
  58. //------------------------------------------------------------------------------
  59. GLSL_OUT vec3 v_eye_dir;
  60. GLSL_OUT vec3 v_pos_world;
  61. GLSL_OUT vec3 v_normal;
  62. #if (NUM_NORMALMAPS > 0 || FOAM) && !GENERATED_MESH
  63. GLSL_OUT vec2 v_texcoord;
  64. #endif
  65. #if NUM_NORMALMAPS > 0
  66. GLSL_OUT vec3 v_tangent;
  67. GLSL_OUT vec3 v_binormal;
  68. # endif
  69. #if (NUM_NORMALMAPS > 0 || FOAM) && GENERATED_MESH && DYNAMIC
  70. GLSL_OUT vec3 v_calm_pos_world;
  71. #endif
  72. #if SHORE_PARAMS
  73. GLSL_OUT vec3 v_shore_params;
  74. #endif
  75. #if SHORE_SMOOTHING || REFLECTION_TYPE == REFL_PLANE || REFRACTIVE
  76. GLSL_OUT vec3 v_tex_pos_clip;
  77. #endif
  78. #if SHORE_SMOOTHING || REFLECTION_TYPE == REFL_PLANE || REFRACTIVE || !DISABLE_FOG
  79. GLSL_OUT float v_view_depth;
  80. #endif
  81. #if DEBUG_WIREFRAME
  82. GLSL_OUT vec3 v_barycentric;
  83. #endif
  84. #if USE_TBN_SHADING
  85. GLSL_OUT vec3 v_shade_tang;
  86. #endif
  87. /*==============================================================================
  88. UNIFORMS
  89. ==============================================================================*/
  90. #if STATIC_BATCH
  91. // NOTE: mat3(0.0, 0.0, 0.0, --- trans
  92. // 1.0, --- scale
  93. // 0.0, 0.0, 0.0, 1.0, --- quat
  94. // 0.0);
  95. const mat3 u_model_tsr = mat3(0.0, 0.0, 0.0,
  96. 1.0,
  97. 0.0, 0.0, 0.0, 1.0,
  98. 0.0);
  99. #else
  100. uniform mat3 u_model_tsr;
  101. #endif
  102. uniform mat3 u_view_tsr;
  103. uniform mat4 u_proj_matrix;
  104. uniform vec3 u_camera_eye;
  105. #if DYNAMIC
  106. uniform PRECISION float u_time;
  107. uniform vec3 u_wind;
  108. #endif
  109. #if SHORE_SMOOTHING || REFLECTION_TYPE == REFL_PLANE || REFRACTIVE || !DISABLE_FOG
  110. uniform PRECISION float u_view_max_depth;
  111. #endif
  112. #if SHORE_PARAMS
  113. uniform sampler2D u_shore_dist_map;
  114. #endif
  115. #if SHORE_PARAMS
  116. vec3 extract_shore_params(in vec2 pos) {
  117. // shore coordinates from world position
  118. vec2 shore_coords = 0.5 +
  119. vec2((pos.x - SHORE_MAP_CENTER_X) / SHORE_MAP_SIZE_X,
  120. (pos.y - SHORE_MAP_CENTER_Y) / SHORE_MAP_SIZE_Y);
  121. // unpack shore parameters from texture
  122. vec4 shore_params = GLSL_TEXTURE(u_shore_dist_map, shore_coords);
  123. const vec2 bit_shift = vec2( 1.0/255.0, 1.0);
  124. float shore_dist = dot(shore_params.ba, bit_shift);
  125. vec2 dir_to_shore = normalize(shore_params.rg * 2.0 - 1.0);
  126. return vec3(dir_to_shore, shore_dist);
  127. }
  128. #endif
  129. #if DYNAMIC
  130. #define SMALL_WAVES_FAC 0.2
  131. void offset(inout vec3 pos, in float time, in vec3 shore_params) {
  132. // waves far from the shore
  133. float dist_waves =
  134. snoise(DST_NOISE_SCALE_0 * (pos.xy + DST_NOISE_FREQ_0 * time))
  135. * snoise(DST_NOISE_SCALE_1 * (pos.yx - DST_NOISE_FREQ_1 * time));
  136. # if SHORE_PARAMS
  137. float shore_waves_length = WAVES_LENGTH / MAX_SHORE_DIST / M_PI;
  138. float shore_dist = shore_params.b;
  139. float dist_fact = sqrt(shore_dist);
  140. // waves moving towards the shore
  141. float shore_dir_waves = max(shore_dist, DIR_MIN_SHR_FAC)
  142. * sin(dist_fact / shore_waves_length + DIR_FREQ * time);
  143. float dir_noise =
  144. max(snoise(DIR_NOISE_SCALE * (pos.xy + DIR_NOISE_FREQ * time)),
  145. DIR_MIN_NOISE_FAC);
  146. shore_dir_waves *= dir_noise;
  147. // mix two types of waves
  148. float waves_height = WAVES_HEIGHT * mix(shore_dir_waves, dist_waves,
  149. max(dist_fact, DST_MIN_FAC));
  150. // move high vertices towards the shore
  151. vec2 dir_to_shore = shore_params.rg;
  152. float wave_factor = WAVES_HOR_FAC * shore_dir_waves
  153. * max(MAX_SHORE_DIST / 35.0 * (0.05 - shore_dist), 0.0);
  154. // horizontal offset for wave inclination
  155. vec2 hor_offset = wave_factor * dir_to_shore;
  156. # else
  157. float waves_height = WAVES_HEIGHT * dist_waves;
  158. # endif // SHORE_PARAMS
  159. #if GENERATED_MESH
  160. // high resolution geometric noise waves
  161. vec2 coords21 = 1.0 * (pos.xy - 0.3 * time);
  162. vec2 coords22 = 0.7 * (pos.yx + 0.07 * time);
  163. float small_waves =
  164. cellular2x2(coords21).x + cellular2x2(coords22).x - 1.0;
  165. # if SHORE_PARAMS
  166. pos.xy += hor_offset;
  167. small_waves *= shore_dist;
  168. # endif // SHORE_PARAMS
  169. waves_height += SMALL_WAVES_FAC * small_waves;
  170. #endif // GENERATED_MESH
  171. pos.z += waves_height;
  172. }
  173. #endif // DYNAMIC
  174. /*==============================================================================
  175. MAIN
  176. ==============================================================================*/
  177. void main(void) {
  178. #if DEBUG_WIREFRAME
  179. if (a_polyindex == 0.0)
  180. v_barycentric = vec3(1.0, 0.0, 0.0);
  181. else if (a_polyindex == 1.0)
  182. v_barycentric = vec3(0.0, 0.0, 1.0);
  183. else // ~0.5 because of normalizing an unsigned byte value
  184. v_barycentric = vec3(0.0, 1.0, 0.0);
  185. #endif
  186. #if GENERATED_MESH
  187. vec3 position = a_position;
  188. float casc_step = abs(position.z);
  189. vec2 step_xy = u_camera_eye.xy - mod(u_camera_eye.xy, casc_step);
  190. # if WATER_EFFECTS
  191. position.z = WATER_LEVEL;
  192. # else
  193. position.z = 0.0;
  194. # endif
  195. position.xy += step_xy;// + vec2(15.0, -15.0);
  196. vec3 world_pos = position;
  197. #else
  198. # if NUM_NORMALMAPS > 0 || FOAM
  199. v_texcoord = a_texcoord;
  200. # endif
  201. vertex world = to_world(a_position, vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0),
  202. vec3(0.0), u_model_tsr);
  203. vec3 world_pos = world.position;
  204. #endif
  205. #if SHORE_PARAMS
  206. v_shore_params = extract_shore_params(world_pos.xy);
  207. #endif
  208. #if DYNAMIC
  209. float wind_str = length(u_wind);
  210. if (wind_str == 0.0) // use default value if there is no wind
  211. wind_str = 1.0;
  212. float w_time = u_time;
  213. w_time *= wind_str;
  214. # if GENERATED_MESH
  215. float vertex_delta = casc_step;
  216. // generate two neighbour vertices
  217. vec3 neighbour1 = world_pos + vec3(vertex_delta, 0.0, 0.0);
  218. vec3 neighbour2 = world_pos + vec3(0.0, vertex_delta, 0.0);
  219. # if NUM_NORMALMAPS > 0 || FOAM
  220. v_calm_pos_world = world_pos;
  221. # endif
  222. # else
  223. vec3 neighbour1 = world_pos + vec3(0.05, 0.0, 0.0);
  224. vec3 neighbour2 = world_pos + vec3(0.0, 0.05, 0.0);
  225. # endif // GENERATED_MESH
  226. # if SHORE_PARAMS
  227. vec3 shore_params_n1 = extract_shore_params(neighbour1.xy);
  228. vec3 shore_params_n2 = extract_shore_params(neighbour2.xy);
  229. offset(neighbour1, w_time, shore_params_n1);
  230. offset(neighbour2, w_time, shore_params_n2);
  231. offset(world_pos, w_time, v_shore_params);
  232. # else
  233. offset(neighbour1, w_time, vec3(0.0));
  234. offset(neighbour2, w_time, vec3(0.0));
  235. offset(world_pos, w_time, vec3(0.0));
  236. # endif
  237. # if GENERATED_MESH && WATER_EFFECTS
  238. // Last cascad needs to be flat and a bit lower than others
  239. if (a_position.z < 0.0) {
  240. world_pos.z = WATER_LEVEL - 1.0;
  241. neighbour1.z = world_pos.z;
  242. neighbour2.z = world_pos.z;
  243. }
  244. # endif // GENERATED_MESH
  245. // calculate all surface vectors based on 3 positions
  246. vec3 bitangent = normalize(neighbour1 - world_pos);
  247. vec3 tangent = normalize(neighbour2 - world_pos);
  248. v_normal = normalize(cross(bitangent, tangent));
  249. // NOTE: protect mesh from extreme normal values
  250. float up_dot_norm = dot(v_normal, UP_VECTOR);
  251. float factor = clamp(0.8 - up_dot_norm, 0.0, 1.0);
  252. v_normal = mix(v_normal, UP_VECTOR, factor);
  253. #elif !GENERATED_MESH
  254. v_normal = qrot(a_tbn_quat, vec3(0.0, 1.0, 0.0));
  255. #else
  256. v_normal = vec3(0.0, 0.0, 1.0);
  257. #endif // DYNAMIC
  258. #if NUM_NORMALMAPS > 0
  259. # if !DYNAMIC
  260. # if !GENERATED_MESH
  261. vec3 tangent = qrot(a_tbn_quat, vec3(1.0, 0.0, 0.0));
  262. # elif !DYNAMIC
  263. vec3 tangent = vec3(1.0, 0.0, 0.0);
  264. # endif
  265. # endif
  266. v_tangent = tangent;
  267. v_binormal = cross(v_normal, v_tangent);
  268. #endif // NUM_NORMALMAPS > 0
  269. v_pos_world = world_pos;
  270. v_eye_dir = u_camera_eye - world_pos;
  271. vec4 pos_view = vec4(tsr9_transform(u_view_tsr, world_pos), 1.0);
  272. vec4 pos_clip = u_proj_matrix * pos_view;
  273. #if SHORE_SMOOTHING || REFLECTION_TYPE == REFL_PLANE || REFRACTIVE
  274. float xc = pos_clip.x;
  275. float yc = pos_clip.y;
  276. float wc = pos_clip.w;
  277. v_tex_pos_clip.x = (xc + wc) / 2.0;
  278. v_tex_pos_clip.y = (yc + wc) / 2.0;
  279. v_tex_pos_clip.z = wc;
  280. #endif
  281. #if SHORE_SMOOTHING || REFLECTION_TYPE == REFL_PLANE || REFRACTIVE || !DISABLE_FOG
  282. v_view_depth = -pos_view.z / u_view_max_depth;
  283. #endif
  284. gl_Position = pos_clip;
  285. }