#version 300 es
// Hash adapted from David Hoskins, Hash without Sine. See THIRD_PARTY_NOTICES.md.
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-liquids-lib-07-kelvin-ship-wake-behind-a-moving-point
// An aerial tracking view of a pilot launch and its deep-water Kelvin wake.
// uResolution: framebuffer dimensions in pixels. Optional uTime: seconds (default 0).
precision highp float;
uniform vec2 uResolution;
uniform float uTime;
out vec4 fragColor;
const float PI=3.14159265;
vec2 frame() {
    return (2.*gl_FragCoord.xy-uResolution)/min(uResolution.x,uResolution.y);
}

float hash(vec2 p) {
    vec3 q=fract(p.xyx*vec3(.1031,.1030,.0973));
    q+=dot(q,q.yzx+33.33);
    return fract((q.x+q.y)*q.z);
}
float noise2(vec2 p) {
    vec2 i=floor(p),f=fract(p);
    f=f*f*(3.-2.*f);
    return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
               mix(hash(i+vec2(0,1)),hash(i+1.),f.x),f.y);
}
float terrain(vec2 p) {
    mat2 m=mat2(1.62,1.19,-1.19,1.62);
    return .54*noise2(p)+.27*noise2(m*p+7.1)+.13*noise2(m*m*p+19.)+
           .06*noise2(m*m*m*p-8.3);
}

float sea(vec2 q,float t) {
    vec2 world=q+vec2(0.,.42*t);
    vec2 windCoord=world*vec2(6.,15.)+vec2(-.48*t,.66*t);
    windCoord+=1.8*vec2(terrain(world*3.),terrain(world*3.+9.));
    float wind=.009*terrain(windCoord)+.0035*noise2(windCoord*2.13+13.);
    float aft=max(-q.y,.01),ratio=abs(q.x)/aft;
    float root=sqrt(max(0.,1.-8.*ratio*ratio));
    float k1=2.*ratio/(1.+root), k2=min(4.,(1.+root)/max(.006,4.*ratio));
    float pa=33.*sqrt(1.+k1*k1)*(aft-abs(q.x)*k1);
    float modulation=terrain(7.*world+vec2(.12*t,0.));
    float pb=33.*sqrt(1.+k2*k2)*(aft-abs(q.x)*k2)+.48*(modulation-.5);
    float pixel=2./min(uResolution.x,uResolution.y);
    float filterB=exp(-.12*pow(33.*pixel*(1.+k2*k2),2.));
    float fan=smoothstep(.04,.24,aft)*(1.-smoothstep(.32,.38,ratio))/(1.+.40*aft);
    return wind+fan*(.012*sin(pa)+.008*sin(pb)*(.61+.66*modulation)*smoothstep(.08,.31,ratio)*filterB);
}
float rectangle(vec2 p,vec2 halfSize,float corner) {
    vec2 d=abs(p)-halfSize+corner;
    return length(max(d,0.))+min(max(d.x,d.y),0.)-corner;
}
vec3 vessel(vec2 q,vec3 ocean,float aa) {
    vec2 p=q/vec2(.078,.222);
    float beam=mix(.78,1.,smoothstep(-1.,-.48,p.y))*
               sqrt(max(0.,1.-pow(clamp((p.y+.06)/1.06,0.,1.),1.5)));
    float hull=max(abs(p.x)-beam,max(-p.y-.88,p.y-1.));
    float edge=aa/.078;
    vec3 deck=mix(vec3(.85,.80,.64),vec3(.49,.37,.22),.15+.15*noise2(q*170.));
    deck=mix(deck,vec3(.083,.18,.19),1.-smoothstep(.035,.09,-hull));
    deck=mix(deck,vec3(.92,.91,.80),1.-smoothstep(.015,.055,abs(hull+.13)));
    float cabin=rectangle(p-vec2(0.,-.03),vec2(.65,.40),.09);
    deck=mix(deck,vec3(.14,.23,.26),1.-smoothstep(-edge*.4,edge*.4,cabin));
    float roof=rectangle(p-vec2(0.,-.11),vec2(.53,.25),.06);
    deck=mix(deck,vec3(.86,.45,.19),1.-smoothstep(-edge*.4,edge*.4,roof));
    float stripe=abs(p.x+.15)-.06;
    deck=mix(deck,vec3(.94,.81,.51),.55*(1.-smoothstep(-edge,edge,stripe))*(1.-smoothstep(-edge,edge,roof)));
    float hatch=length((p-vec2(0.,.53))/vec2(.25,.093));
    deck=mix(deck,vec3(.21,.29,.28),1.-smoothstep(.8,1.,hatch));
    float engine=rectangle(p-vec2(0.,-.84),vec2(.22,.12),.08);
    deck=mix(deck,vec3(.13,.17,.18),1.-smoothstep(-edge,edge,engine));
    return mix(ocean,deck,1.-smoothstep(-edge,edge,hull));
}
void main() {
    vec2 uv=frame(); float t=uTime+3.18;
    vec2 q=mat2(.88,.475,-.475,.88)*(uv-vec2(.27,.56+.055*sin(.71*t)));
    float aa=2./min(uResolution.x,uResolution.y);
    vec2 e=vec2(max(.002,aa*.63),0.);
    vec2 g=vec2(sea(q+e,t)-sea(q-e,t),sea(q+e.yx,t)-sea(q-e.yx,t))/(2.*e.x);
    vec3 n=normalize(vec3(-g,1.)),refl=reflect(normalize(vec3(-.08,.20,-.98)),n);
    vec2 moving=q+vec2(0.,.42*t);
    float shoal=terrain(moving*vec2(1.8,2.6));
    vec3 col=mix(vec3(.046,.19,.21),vec3(.17,.42,.39),shoal);
    float sky=smoothstep(-.72,.83,refl.y+.48*refl.x);
    col+=vec3(.11,.18,.20)*sky;
    col+=vec3(.43,.46,.40)*pow(max(dot(refl,normalize(vec3(-.49,.53,.69))),0.),26.);
    float aft=max(0.,-q.y-.15);
    float churnWidth=.025+.078*sqrt(aft);
    float swirl=q.x+.034*sqrt(aft)*sin(12.*aft-2.8*t);
    float stirred=exp(-swirl*swirl/(churnWidth*churnWidth))*exp(-1.3*aft)*smoothstep(.0,.065,aft);
    vec2 f=vec2(q.x*48.,aft*24.-4.*t);
    float turbulent=terrain(f+2.2*vec2(noise2(f*.28),noise2(f*.24+3.)));
    float bow=exp(-abs(abs(q.x)-(.055+.26*max(0.,.10-q.y)))/(.008+.006*aft))
             *smoothstep(-.70,-.13,q.y)*(1.-smoothstep(.04,.15,q.y));
    float foam=stirred*smoothstep(.25,.66,turbulent)+bow*(.30+.62*turbulent);
    col=mix(col,vec3(.83,.91,.84),clamp(foam*1.30,0.,.97));
    fragColor=vec4(vessel(q,col,aa),1.);
}
