Advertisement
Hiemas

liquid shader

Jun 10th, 2025
18
0
3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. /* Shader for the liquid surface. Renders behind the liquid. */
  2.  
  3. shader_type spatial;
  4.  
  5. render_mode cull_disabled, shadows_disabled;
  6.  
  7. uniform vec2 coeff; /* Coefficients of the linear function for the surface. */
  8.  
  9. uniform float fill_amount : hint_range(0.0f, 1.0f);
  10. uniform float glass_thickness = 0.01f;
  11.  
  12. uniform float height = 0.32f;
  13. uniform float width = 0.1f;
  14.  
  15. uniform sampler2D waves_noise;
  16. uniform float wave_intensity = 0.05f;
  17.  
  18. uniform vec4 liquid_color : source_color;
  19. uniform vec4 glow_color : source_color;
  20.  
  21. uniform float color_modifier = 1.1f;
  22.  
  23. uniform float FOV : hint_range(20, 120) = 75;
  24. uniform bool foreground = false;
  25.  
  26. varying vec3 pos;
  27. varying vec3 normal;
  28. varying float liquid_line;
  29.  
  30. float lerp(float a, float b, float t) {
  31. return b * t + a * (1.0f - t);
  32. }
  33.  
  34. void vertex() {
  35. if (foreground) {
  36. float scale = 1.0 / tan(FOV * 0.5 * PI / -180.0);
  37.  
  38. PROJECTION_MATRIX[0][0] = scale / (-VIEWPORT_SIZE.x / VIEWPORT_SIZE.y);
  39. PROJECTION_MATRIX[1][1] = scale;
  40. }
  41.  
  42. // Move vertices inwards to simulate glass thickness.
  43. VERTEX -= glass_thickness * NORMAL;
  44.  
  45. // Position inside container rotated to world.
  46. pos = mat3(MODEL_MATRIX)*VERTEX;
  47.  
  48. // Lerp between height and width depending on world orientation of z axis.
  49. float d = dot(vec3(MODEL_MATRIX[0][1], MODEL_MATRIX[1][1], MODEL_MATRIX[2][1]), vec3(0.0f, 1.0f, 0.0f));
  50. float m = lerp(width, height, abs(d));
  51.  
  52. // wave noise not working
  53. liquid_line = (fill_amount - 0.5f) * m
  54. + wave_intensity * length(coeff) * (texture(waves_noise, 2.0*pos.xz + 0.5*TIME * vec2(1.0, 1.0)).r - 0.5f
  55. + texture(waves_noise, 2.0*pos.xz - 0.5*TIME * vec2(1.0, 1.0)).g - 0.5f)
  56. + dot(pos.xz, coeff);
  57. }
  58.  
  59. void fragment() {
  60. // Set normals depending on incline.
  61. NORMAL = vec3(coeff.x, 1.0f, coeff.y);
  62.  
  63. // Discard all vertices above the liquid line.
  64. if (pos.y > liquid_line) {
  65. discard;
  66. } else {
  67. ALBEDO = (liquid_color.rgb * color_modifier);
  68. }
  69.  
  70. if (foreground) {
  71. DEPTH = 1.0 - (1.0 - FRAGCOORD.z) * 0.7;
  72. } else {
  73. DEPTH = FRAGCOORD.z;
  74. }
  75.  
  76. if (FRONT_FACING) {
  77. ALBEDO = vec3(1.0, 1.0, 1.0);
  78. //discard;
  79. }
  80.  
  81. ALPHA = 0.9;
  82. }
  83.  
  84. void light() {
  85. float d = dot(-VIEW, LIGHT);
  86. float dd = d*d;
  87. if (d > 0.0f) DIFFUSE_LIGHT = dd * glow_color.rgb;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement