Advertisement
Guest User

Untitled

a guest
Jan 11th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. varying vec2 v_vTexcoord;
  2.  
  3. uniform vec2 texel_size;
  4. vec2 velocity_factor = texel_size * 50.0;
  5.  
  6. float dt = 0.15, k = 0.2;
  7. float scale = 1.0 / 2.0;
  8. float s = 0.2 / 0.15;
  9. float v = 0.1;
  10.  
  11. void main() {
  12.     // Get and convert data.
  13.     vec4 center = texture2D(gm_BaseTexture, v_vTexcoord); center.xy -= vec2(128.0 / 255.0); center.xy *= 10.0; center.z *= 3.0;
  14.     vec3 right = texture2D(gm_BaseTexture, mod(v_vTexcoord + vec2(texel_size.x, 0.0), 1.0)).xyz; right.xy -= vec2(128.0 / 255.0); right.xy *= 10.0;
  15.     vec3 left = texture2D(gm_BaseTexture, mod(v_vTexcoord - vec2(texel_size.x, 0.0), 1.0)).xyz; left.xy -= vec2(128.0 / 255.0); left.xy *= 10.0;
  16.     vec3 top = texture2D(gm_BaseTexture, mod(v_vTexcoord - vec2(0.0, texel_size.y), 1.0)).xyz; top.xy -= vec2(128.0 / 255.0); top.xy *= 10.0;
  17.     vec3 bottom = texture2D(gm_BaseTexture, mod(v_vTexcoord + vec2(0.0, texel_size.y), 1.0)).xyz; bottom.xy -= vec2(128.0 / 255.0); bottom.xy *= 10.0;
  18.    
  19.     // Uses numerical approximation to obtain the partial derivatives over the current fragment.
  20.     vec3 du_dx = (right - left) * scale, du_dy = (top - bottom) * scale;
  21.     float u_div = du_dx.x + du_dy.y;
  22.     vec2 Ddx = vec2(du_dx.z, du_dy.z);
  23.    
  24.     center.z -= dt * dot(vec3(Ddx, u_div), center.xyz);
  25.     center.z = clamp(center.z, 0.5, 3.0);
  26.    
  27.     vec2 PdX = s * Ddx;
  28.     vec2 laplacian = vec2(right.x + left.x + top.x + bottom.x, right.y + left.y + top.y + bottom.y) - 4.0 * center.xy;
  29.     vec2 viscosity_force = v * laplacian;
  30.    
  31.     vec2 Was = v_vTexcoord - dt * center.xy * texel_size;
  32.     center.xy = texture2D(gm_BaseTexture, mod(Was, 1.0)).xy;
  33.    
  34.     center.xy += dt * (viscosity_force - PdX + vec2(0.0));
  35.    
  36.     // Convert back to texture.
  37.     center.z /= 3.0;
  38.     right.xy /= 10.0; right.xy += vec2(128.0 / 255.0);
  39.     left.xy /= 10.0; left.xy += vec2(128.0 / 255.0);
  40.     top.xy /= 10.0; top.xy += vec2(128.0 / 255.0);
  41.     bottom.xy /= 10.0; bottom.xy += vec2(128.0 / 255.0);
  42.     gl_FragColor = center;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement