//https://www.shadertoy.com/view/3lsXzf
#define DISPLAY_SCALE 1.0

/*****************************************************************************
   We're using 2D Triangle noise as our reference.


   Copied from here: https://www.shadertoy.com/view/XtGGRt

   Auroras by nimitz 2017 (twitter: @stormoid)
   License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
   Contact the author for other licensing options

******************************************************************************/

mat2 mm2(in float a){float c = cos(a), s = sin(a);return mat2(c,s,-s,c);}
mat2 m2 = mat2(0.95534, 0.29552, -0.29552, 0.95534);
float tri(in float x){return clamp(abs(fract(x)-.5),0.01,0.49);}
vec2 tri2(in vec2 p){return vec2(tri(p.x)+tri(p.y),tri(p.y+tri(p.x)));}

float triNoise2d(in vec2 p, float spd)
{
    float z=1.8;
    float z2=2.5;
	float rz = 0.;
    p *= mm2(p.x*0.06);
    vec2 bp = p;
	for (float i=0.; i<5.; i++ )
	{
        vec2 dg = tri2(bp*1.85)*.75;
        dg *= mm2(iTime*spd);
        p -= dg/z2;

        bp *= 1.3;
        z2 *= .45;
        z *= .42;
		p *= 1.21 + (rz-1.0)*.02;
        
        rz += tri(p.x+tri(p.y))*z;
        p*= -m2;
	}
    return clamp(1./pow(rz*29., 1.0),0.,.55);
}

/*****************************************************************************
   And this is my noise... I dub this: 'lightning noise'.
   The main two things to get right were the motion and the noisy fins!

   Covered under the MIT license:

   Copyright (c) 2019 TooMuchVoltage Software Inc.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.

   Hit me up!

   Twitter: twitter.com/toomuchvoltage
   Facebook: fb.com/toomuchvoltage
   YouTube: youtube.com/toomuchvoltage
   Mastodon: https://mastodon.gamedev.place/@toomuchvoltage
   Website: www.toomuchvoltage.com

******************************************************************************/

mat2 rotate(in float a)
{
    float c = cos(a), s = sin(a);
    return mat2(c,s,-s,c);
}

float lightningNoise (vec2 forPos)
{
    forPos *= 4.0;
    forPos.y *= 0.85;
    float wobbleAmount1 = sin(forPos.y) * 0.5 + sin(forPos.y * 2.0) * 0.25 + sin(forPos.y * 4.0) * 0.125 + sin(forPos.y * 8.0) * 0.0625;
    float wobbleAmount2 = sin(forPos.x) * 0.5 + sin(forPos.x * 2.0) * 0.25 + sin(forPos.x * 4.0) * 0.125 + sin(forPos.x * 8.0) * 0.0625;
    float horizontalStrike = 1.0 - abs(sin(forPos.x + wobbleAmount1 * 1.1));
    float verticalStrike = 1.0 - abs(cos(forPos.y + wobbleAmount2 * 1.1));
    return (horizontalStrike + verticalStrike) * 0.35;
}

float fBm (in vec2 uv)
{
    float amp = 0.5;
    float res = 0.0;
    for (int i = 0; i < 5; ++i) {
        res += amp * lightningNoise(uv);
        uv *= 2.0;
        amp *= 0.5;
    }
    return res;
}

float domainWarp (in vec2 uv)
{
    mat2 rotMat = rotate(0.1);
    float v1 = fBm(rotMat * uv + 0.02 * iTime);
    uv = rotMat * uv + 1.0;
    float v2 = fBm(uv - 0.02 * iTime);
    uv = rotMat * uv + 1.0;
    float v3 = fBm(uv + 0.02 * iTime);
    uv = rotMat * uv + 1.0;
    float v4 = fBm(uv - 0.02 * iTime);
    float finalRes = fBm (vec2 (fBm(vec2 (v1,v2)), fBm(vec2 (v3,v4))));
    return clamp (pow (1.0 - finalRes, 10.0) * lightningNoise (rotate (0.02 * iTime) * uv) * 10.0, 0.0, 0.55);
}

/*****************************************************************************
   And finally a modified version of Nimitz's Auroras: https://www.shadertoy.com/view/XtGGRt
   That uses our noise instead! :) ... just to see it in action really...

   Auroras by nimitz 2017 (twitter: @stormoid)
   License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
   Contact the author for other licensing options
******************************************************************************/

#define time iTime

float hash21(in vec2 n){ return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453); }
vec4 aurora(vec3 ro, vec3 rd)
{
    vec4 col = vec4(0);
    vec4 avgCol = vec4(0);
    
    for(float i=0.;i<50.;i++)
    {
        float of = 0.006*hash21(gl_FragCoord.xy)*smoothstep(0.,15., i);
        float pt = ((.8+pow(i,1.4)*.002)-ro.y)/(rd.y*2.+0.4);
        pt -= of;
    	vec3 bpos = ro + pt*rd;
        vec2 p = bpos.zx;
        float rzt = domainWarp(p);
        vec4 col2 = vec4(0,0,0, rzt);
        col2.rgb = (sin(1.-vec3(2.15,-.5, 1.2)+i*0.043)*0.5+0.5)*rzt;
        avgCol =  mix(avgCol, col2, .5);
        col += avgCol*exp2(-i*0.065 - 2.5)*smoothstep(0.,5., i);
        
    }
    
    col *= (clamp(rd.y*15.+.4,0.,1.));
    
    
    //return clamp(pow(col,vec4(1.3))*1.5,0.,1.);
    //return clamp(pow(col,vec4(1.7))*2.,0.,1.);
    //return clamp(pow(col,vec4(1.5))*2.5,0.,1.);
    //return clamp(pow(col,vec4(1.8))*1.5,0.,1.);
    
    //return smoothstep(0.,1.1,pow(col,vec4(1.))*1.5);
    return col*1.8;
    //return pow(col,vec4(1.))*2.
}


//-------------------Background and Stars--------------------

vec3 nmzHash33(vec3 q)
{
    uvec3 p = uvec3(ivec3(q));
    p = p*uvec3(374761393U, 1103515245U, 668265263U) + p.zxy + p.yzx;
    p = p.yzx*(p.zxy^(p >> 3U));
    return vec3(p^(p >> 16U))*(1.0/vec3(0xffffffffU));
}

vec3 stars(in vec3 p)
{
    vec3 c = vec3(0.);
    float res = iResolution.x*1.;
    
	for (float i=0.;i<4.;i++)
    {
        vec3 q = fract(p*(.15*res))-0.5;
        vec3 id = floor(p*(.15*res));
        vec2 rn = nmzHash33(id).xy;
        float c2 = 1.-smoothstep(0.,.6,length(q));
        c2 *= step(rn.x,.0005+i*i*0.001);
        c += c2*(mix(vec3(1.0,0.49,0.1),vec3(0.75,0.9,1.),rn.y)*0.1+0.9);
        p *= 1.3;
    }
    return c*c*.8;
}

vec3 bg(in vec3 rd)
{
    float sd = dot(normalize(vec3(-0.5, -0.6, 0.9)), rd)*0.5+0.5;
    sd = pow(sd, 5.);
    vec3 col = mix(vec3(0.05,0.1,0.2), vec3(0.1,0.05,0.2), sd);
    return col*.63;
}
//-----------------------------------------------------------


void aurorasWithLightningNoise( out vec4 fragColor, in vec2 fragCoord )
{
	vec2 q = fragCoord.xy / iResolution.xy;
    vec2 p = q - 0.5;
	p.x*=iResolution.x/iResolution.y;
    
    vec3 ro = vec3(0,0,-6.7);
    vec3 rd = normalize(vec3(p,1.3));
    vec2 mo = iMouse.xy / iResolution.xy-.5;
    mo = (mo==vec2(-.5))?mo=vec2(-0.1,0.1):mo;
	mo.x *= iResolution.x/iResolution.y;
    rd.yz *= mm2(mo.y);
    rd.xz *= mm2(mo.x + sin(time*0.05)*0.2);
    
    vec3 col = vec3(0.);
    vec3 brd = rd;
    float fade = smoothstep(0.,0.01,abs(brd.y))*0.1+0.9;
    
    col = bg(rd)*fade;
    
    if (rd.y > 0.){
        vec4 aur = smoothstep(0.,1.5,aurora(ro,rd))*fade;
        col += stars(rd);
        col = col*(1.-aur.a) + aur.rgb;
    }
    else //Reflections
    {
        rd.y = abs(rd.y);
        col = bg(rd)*fade*0.6;
        vec4 aur = smoothstep(0.0,2.5,aurora(ro,rd));
        col += stars(rd)*0.1;
        col = col*(1.-aur.a) + aur.rgb;
        vec3 pos = ro + ((0.5-ro.y)/rd.y)*rd;
        float nz2 = domainWarp(pos.xz*vec2(.5,.7));
        col += mix(vec3(0.2,0.25,0.5)*0.08,vec3(0.3,0.3,0.5)*0.7, nz2*0.4);
    }
    
	fragColor = vec4(col, 1.);
}

/**************************************************
    This part is just captain planet
***************************************************/

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = ((fragCoord/iResolution.xy) * 4.0 - 2.0) * DISPLAY_SCALE;
    float xOnScreen = fragCoord.x / iResolution.x;
    float yOnScreen = fragCoord.y / iResolution.y;
    
    if (xOnScreen < 0.33333 )
    {
        if ( yOnScreen < 0.5 )
		    fragColor = vec4(vec3 (triNoise2d (uv, 0.4)),1.0);
        else
		    fragColor = vec4(vec3 (domainWarp (uv)),1.0);
    }
    else
        aurorasWithLightningNoise (fragColor, fragCoord);
}