Advertisement
matrefeytontias

Black hole distortion effect

Apr 18th, 2015
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2. #ifdef GL_ES
  3. precision mediump float;
  4. #endif
  5.  
  6. varying vec2 vTexCoord;
  7. uniform sampler2D uImage0;
  8. uniform float blackHoleX;
  9. uniform float blackHoleY;
  10. uniform float apply;
  11.  
  12. void main(void)
  13. {
  14.     vec2 uv = vTexCoord;
  15.     // Calculate line equation between point and black hole
  16.     vec3 dirP = vec3(uv.y - blackHoleY, -(uv.x - blackHoleX), 0);
  17.     dirP.z = -dirP.x * blackHoleX - dirP.y * blackHoleY;
  18.    
  19.     // Calculate closest point belonging to both any edge of the screen and said line
  20.     vec2 inter = vec2(0.);
  21.     vec2 p1 = vec2(0.);
  22.     vec2 p2 = vec2(0.);
  23.    
  24.     if(dirP.x == 0.0)
  25.         inter = vec2(uv.x > 0.5 ? 1.0 : 0.0, blackHoleY);
  26.     else if(dirP.y == 0.0)
  27.         inter = vec2(blackHoleX, uv.y > 0.5 ? 1.0 : 0.0);
  28.     else
  29.     {
  30.         if(uv.x > blackHoleX)
  31.         {
  32.             if(uv.y > blackHoleY)
  33.             {
  34.                 p1 = vec2(1.0, (-dirP.z - dirP.x) / dirP.y);
  35.                 p2 = vec2((-dirP.z - dirP.y) / dirP.x, 1.0);
  36.             }
  37.             else
  38.             {
  39.                 p1 = vec2(1.0, (-dirP.z - dirP.x) / dirP.y);
  40.                 p2 = vec2(-dirP.z / dirP.x, 0.);
  41.             }
  42.         }
  43.         else
  44.         {
  45.             if(uv.y > blackHoleY)
  46.             {
  47.                 p1 = vec2(0., -dirP.z / dirP.y);
  48.                 p2 = vec2((-dirP.z - dirP.y) / dirP.x, 1.0);
  49.             }
  50.             else
  51.             {
  52.                 p1 = vec2(0., -dirP.z / dirP.y);
  53.                 p2 = vec2(-dirP.z / dirP.x, 0.);
  54.             }
  55.         }
  56.         inter = distance(uv, p1) < distance(uv, p2) ? p1 : p2;
  57.     }
  58.    
  59.     float d = distance(uv, inter) * distance(inter, vec2(0.5, 0.5)) / distance(inter, vec2(blackHoleX, blackHoleY));
  60.    
  61.     gl_FragColor = texture2D(uImage0, d * normalize(vec2(blackHoleX, blackHoleY) - inter) + inter);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement