Advertisement
matrefeytontias

Actual black hole distortion effect

Apr 27th, 2016
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Default vars
  2. varying vec2 vTexCoord;
  3. uniform sampler2D uImage0;
  4. uniform vec2 uResolution;
  5. uniform float uTime;
  6.  
  7. // Black hole vars
  8. uniform float enable;
  9. uniform float appdis; // display static if the black hole just appeared or is about to disappear
  10. // Center
  11. uniform float blackHoleX;
  12. uniform float blackHoleY;
  13. // Radii
  14. // singularity < horizon
  15. uniform float horizon;
  16. uniform float singularity;
  17.  
  18. /* General principle :
  19. We need a way to make the image look like it falls into the black hole as the pixels are closer.
  20. A solution is to increase the distance of the pixel to the image's plane as it gets closer to the black hole,
  21. so that pixels right on the horizon are not distorted, and so that pixels right on the singularity are all mapped to the image's pixel
  22. that is under the center of the black hole.
  23. To do that, we take the vector between the pixel and the center of the black hole and we remap its length to catch another of the image's pixels
  24. that is closer to the black hole's center's location.
  25.  
  26. Algorithm :
  27.  
  28. for each pixel in screen
  29. (
  30.     dist = distance(pixel, black hole center)
  31.     if dist > horizon
  32.         gl_FragColor = pixel.color
  33.     else if dist > singularity
  34.         (new distance) = f(dist)
  35.         (new pixel) = (pixel - (black hole center)) * (new distance) + (black hole center)
  36.         gl_FragColor = (new pixel).color
  37.     else
  38.         gl_FragColor = black
  39. )
  40.  
  41. f is the scaling function ; it transforms a linear range into an open asymptotic range
  42. f is caracterized by f(horizon) = 1,        lim       f(x) = +∞
  43.                                      x -> singularity
  44.  
  45. Here, f : ]singularity, horizon] -> [ 1, +∞ [
  46.           x |-> ____________1____________
  47.                 3.5 ____________________
  48.                    /   x - singularity
  49.                   / ---------------------
  50.                 \/  horizon - singularity
  51. */
  52.  
  53. float f(float d)
  54. {
  55.     return 1. / pow((d - singularity) / (horizon - singularity), 1. / 3.5);
  56. }
  57.  
  58. float rand()
  59. {
  60.     return fract( sin((vTexCoord.x * vTexCoord.y + uTime) * 136974) * 4123645 ); // Random number generators : keyboard mashing 101
  61. }
  62.  
  63. void main()
  64. {
  65.     vec2 frustrum = vec2(uResolution.x / uResolution.y, uResolution.y / uResolution.y);
  66.     vec2 uv = vTexCoord - vec2(0.5, 0.5);
  67.     uv *= frustrum;
  68.     vec4 c = vec4(0);
  69.    
  70.     if(appdis > 0.5)
  71.         c = vec4(rand());
  72.     else if(enable > 0.5)
  73.     {
  74.         vec2 attractionPoint = vec2(blackHoleX, blackHoleY) - vec2(0.5, 0.5);
  75.         //attractionPoint *= frustrum;
  76.         float d = distance(uv, attractionPoint);
  77.        
  78.         if(d < singularity)
  79.             c = vec4(vec3(0.), 1.);
  80.         else
  81.         {
  82.             if(d < horizon)
  83.                 uv = f(d) * (uv - attractionPoint) + attractionPoint;
  84.             c = texture2D(uImage0, uv / frustrum + vec2(0.5, 0.5)) + vec4(cos(uTime * 3) * 0.5 + 0.5, 0, 0, 0);
  85.         }
  86.     }
  87.     else
  88.         c = texture2D(uImage0, vTexCoord);
  89.     gl_FragColor = c;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement