// Name: New Leaked 3I/Atlas NASA Footage
// Author: msm01
// URL: https://www.shadertoy.com/view/3ftcRr
#version 330 core
precision highp float;

out vec4 FragColor;
in vec2 vUV;
uniform vec2 iResolution;
uniform float iTime;

// A small improv/fanart from yesterday.

#define s(a,b,c) smoothstep(a,b,c)
#define PI     3.14159
#define NBCaps 3.

mat2 r2d( float a ){ float c = cos(a), s = sin(a); return mat2( c, s, -s, c ); }
float metaDiamond(vec2 p, vec2 pixel, float r){ vec2 d = abs(p-pixel); return r / (d.x + d.y); }

// Dave Hoskins's hash ! Noone can hash hashes like he hashes !
float hash12(vec2 p)
{
      vec3 p3  = fract(vec3(p.xyx) * .1031);
      p3 += dot(p3, p3.yzx + 33.33);
      return fract((p3.x + p3.y) * p3.z);
}
// Smoothed 1D-noise. Just like Zoltraak : stupid simple. Powerful.
float fbm(in vec2 v_p)
{
      float pvpx = v_p.x;
      vec2 V1 = vec2(floor(pvpx      ));
      vec2 V2 = vec2(floor(pvpx + 1.0));
      return mix(hash12(V1),hash12(V2),smoothstep(0.0,1.0,fract(pvpx)));
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
     // Get Base Coordinates
     vec2 p = vec2( (1.0/iResolution.y)*(fragCoord.x - iResolution.x/2.0),fragCoord.y / iResolution.y - 0.5);

     // reversed, for accurate Martian perspective... :)
     p.x=-p.x;

     // Zoom out
     p*=150.0;

     // Init the Accumulator
     vec4 col = vec4(0.05,0.05,0.15,1.0);

     // Make up other boxes and save base in them.
     vec2 save1 = p;
     vec2 save2 = p;

     // Faint Nebula Background

     // Tilt the camera
     p*= r2d(-0.05);
     // Space Background Gradient
     col = mix(col,vec4(0.2,0.3,0.5,1.0),smoothstep(75.0,0.0,abs(p.y - 5.0*fbm(vec2(0.01*(p.x - 33.333*iTime))) + 3.5)));
     // Untilt the camera
     p*= r2d( 0.05);

     // BG Starfield

     // Rotate
     p*= r2d(-0.05);
     // Zoom In
     p*= 0.35;
     // Scroll Left
     p+= vec2(-5.0*iTime,0.0);

     // Hack the coords...
     vec2 b = fract(5.0*p);
     p = floor(5.0*p);
     // Draw the stars
     if( fbm(vec2(p.x*p.y)) > 0.996)col += clamp(1.0-pow(3.0*length(b+vec2(-0.5)),0.5),0.0,1.0);

     // Reload because the coords are all f.. up now !
     p = save1;
     // Another Box...
     vec2 save3;

     // We're going to draw max 4 capsules max.
     // Yes we could draw more but Earth must survive, man. Have mercy !
     float Nb_Capsules = clamp(NBCaps,0.0,4.0);
     for( float i = 0.0;i<Nb_Capsules; i++ )
     {
          // Reloooooaaaaad !
          p = save1;
          // Tilt as much as we tilted the stars...
          p*= r2d(-0.05);
          // Zoom out a bit
          p*=2.5;
          // Then zoom in a bit closer every loop.
          p*=1.0-0.25*i;
          // Compute Random Coordinates For Each Capsule Position
          // Move To That Position.
          p += vec2(150.0*fbm(vec2(0.15*iTime + i*54.321)) - 75.0,  // X varies randomly
                     50.0*sin(     0.25*iTime + i*54.321)  - 25.0); // Y varies periodically
          // Save Position
          save3 = p;

          // Mega Zoom
          p*=0.04;
          // (Ox)-axis Symetry for jets
          p.y = abs(p.y);

          if( p.x > 0.0 )
          {
              // Green Jet
              col += vec4(0.0,1.0,0.5,1.0)*smoothstep(0.2,0.0,abs(p.y - 0.05*fbm(vec2(1.5*p.x - 40.0*iTime)))-0.05)*smoothstep(29.0,0.0,abs(p.x));
              // White Jet
              col += vec4(1.0,1.0,1.0,1.0)*smoothstep(0.1,0.0,abs(p.y - 0.05*fbm(vec2(1.5*p.x - 40.0*iTime)))-0.05)*smoothstep(29.0,0.0,abs(p.x));
          };

          // Reload !
          p = save3;
          // (Ox)-axis Symetry for the flames
          p.y = abs(p.y);
          // Fine-tuning Flames position
          p+= vec2(-10.0,0.0);
          // Fine-tuning Flames Shape
          p *= vec2(0.75,1.0);

         // Green Flames
         col += 0.8*vec4(0.0,1.0,0.5,1.0)*s(20.0,0.0,length(p)-25.0+7.0*sin(0.30*length(p)*atan(p.y,p.x) + 55.0*iTime));
         // White flames
         col += 0.8*vec4(1.0,1.0,1.0,1.0)*s(20.0,0.0,length(p)-20.0+7.0*sin(0.30*length(p)*atan(p.y,p.x) + 55.0*iTime));

         p = save3;

         // Fat Aura
         col = mix(col,vec4(1.0),0.5*s(10.0,0.0,length(p + vec2(5.0,0.0))-20.0)*abs(sin(50.0*iTime)));
         // Less-Fat Aura
         col = mix(col,vec4(1.0),0.5*s(20.0,0.0,length(p + vec2(5.0,0.0))-20.0));
         // Frieren : "Aura ? Shader yourself !"

         // The Pod

         // White Disk
         col = mix(col,vec4(1.0),s(0.01,0.0,length(p)-20.0));

         if( length(p) - 20.0 < 0.0 ) // Basic Masking
         {
             // 2D Shading : bluish large shadow
             col = mix(col,vec4(0.65,0.68,0.68 + 0.1*(3.0-i),1.0),s(0.5,0.0,length(p - vec2(2.0,0.0))-17.0));
             // 2D Shading : dark small shadow
             // If Outside Porthole Zone
             if(s(0.0,1.0,length(vec2(3.0,2.0)*p + vec2(33.5,0.0))-23.0)>0.0)
             col = mix(col,vec4(0.45,0.55,0.55 + 0.1*(3.0-i),1.0),0.75*s(0.5,0.0,length(p - vec2(2.0,0.0)+ 0.5*fbm(vec2(4.5*atan(p.y,p.x))))-9.0));

             // Small 2D Indentation Details On The Spheres Using A Procedural Texture
             // NOTE: Original used texture(iChannel0, ...) which is not supported
             // Texture detail removed - not essential for the effect
             // vec4 colorCapsule = vec4(hash12(0.0003*p*dot(p,p) + 0.789*i));
             // if(colorCapsule.x>0.75)if(s(0.0,1.0,length(vec2(3.0,2.0)*p + vec2(33.5,0.0))-23.0)>0.0)col *= vec4(0.25,0.25,0.25,1.0);

             // Bigger Dark Line All Around The Pod
             col = mix(col,vec4(0.0),s(0.2,0.0,abs(length(p)-19.9)-0.20));
             // Draw The Porthole :
             col = mix(col,vec4(0.5,0.2,0.3,1.0)                               // Base Color
                           -s(5.0,0.0,length(p + vec2(-6.0,15.0))-20.0)        // Main Shadow
                           -s(0.25,0.0,abs(length(p + vec2(0.0,3.0))-15.0)-0.4)// Vertical Shadow
                           -s(0.0,1.5,p.y-8.5)                                 // top Shadow
                           +0.25*vec4(1.0,0.5,0.0,1.0)*s(10.0,0.0,abs(p.y))    // Fake Glass Gradient
                           ,
                           s(0.5,0.0,length(vec2(3.0,2.0)*p + vec2(35.0,0.0))-19.9));

             // Porthole Black Rings
             // Internal
             col = mix(col,vec4(0.0,0.0,0.0,1.0),s(1.0,0.0,abs(length(vec2(3.0,2.0)*p + vec2(35.0,0.0))-19.9)-0.1));
             // External
             col = mix(col,vec4(0.0,0.0,0.0,1.0),s(1.0,0.0,abs(length(vec2(3.0,2.0)*p + vec2(33.5,0.0))-23.0)-0.1));

             // Pod Tennis-Ball Door Line...
             if(p.y>0.0)col = mix(col,vec4(0.0,0.0,0.0,1.0),s(1.0,0.0,abs(length(vec2(3.0,2.0)*p + vec2( 29.0,0.0))-30.0)-0.1));
             if(p.y<0.0)col = mix(col,vec4(0.0,0.0,0.0,1.0),s(1.0,0.0,abs(length(vec2(3.0,2.0)*p + vec2(-31.0,0.0))-30.0)-0.1));
         };
     };
   // WAKE UP SHEEPLE !
   fragColor = clamp(col,0.0,1.0);
}

void main() {
    vec2 fragCoordPixels = vUV * iResolution;
    vec4 outColor;
    mainImage(outColor, fragCoordPixels);
    FragColor = outColor;
}
