#version 300 es precision highp float; uniform float uTime; uniform float uMotion; uniform vec2 uResolution; uniform float uLayout; uniform vec2 uSurfaceSize; uniform sampler2D uSurface; uniform sampler2D uOpenWater; out vec4 fragColor; // One optical camera for the photographic coast, moving sea and pearl scene. // Photographs are upright, bottom-origin textures. The portrait photograph is // a central crop of the same 16:9 view, rather than a different camera. const float SEA_REFERENCE_ASPECT = 16.0 / 9.0; const float SEA_FOCAL_LENGTH = 2.0; const vec3 SEA_REFERENCE_EYE = vec3(0.0, 4.5, 43.0); float oceanEase(float a, float b, float t) { float x = clamp((t - a) / (b - a), 0.0, 1.0); return x * x * x * (x * (x * 6.0 - 15.0) + 10.0); } // Quintic Hermite segment: positions and velocities match at each endpoint, // with zero endpoint acceleration. Units for the two velocities are metres/s. float seaTrack(float t, float a, float b, float p0, float p1, float v0, float v1) { float duration = b - a; float x = clamp((t - a) / duration, 0.0, 1.0); float d = p1 - p0, m0 = v0 * duration, m1 = v1 * duration; float c3 = 10.0 * d - 6.0 * m0 - 4.0 * m1; float c4 = -15.0 * d + 8.0 * m0 + 7.0 * m1; float c5 = 6.0 * d - 3.0 * m0 - 3.0 * m1; return p0 + m0 * x + x * x * x * (c3 + x * (c4 + x * c5)); } float oceanImmersion(float t) { return oceanEase(14.0, 17.8, t); } float seaEyeHeight(float t) { if (t < 1.0) return 4.5; if (t < 11.0) return seaTrack(t, 1.0, 11.0, 4.5, 1.05, 0.0, -.25); if (t < 14.0) return seaTrack(t, 11.0, 14.0, 1.05, .30, -.25, -.16); if (t < 16.0) return seaTrack(t, 14.0, 16.0, .30, .02, -.16, -.12); if (t < 17.8) return seaTrack(t, 16.0, 17.8, .02, -.20, -.12, -.17); if (t < 21.0) return seaTrack(t, 17.8, 21.0, -.20, -3.12, -.17, 0.0); return -3.12; } void seaReferenceBasis(out vec3 fw, out vec3 right, out vec3 up) { // tan(pitch) = (2 * .662 - 1) / focal. This calibrates the camera to // the photographed horizon instead of moving the image independently. fw = normalize(vec3(0.0, -.162, -1.0)); right = vec3(1.0, 0.0, 0.0); up = cross(right, fw); } void seaCamera(float t, out vec3 ro, out vec3 fw) { float portrait = step(.5, uLayout); float portraitDistance = mix(1.0, 1.48, portrait); float arrivalZ = mix(16.0, 20.7, portrait); float arrivalVelocity = mix(-.55, -.14, portrait); float settledZ = 13.4 * portraitDistance; float z; if (t < 1.0) z = 43.0; else if (t < 11.0) z = seaTrack(t, 1.0, 11.0, 43.0, arrivalZ, 0.0, arrivalVelocity); else if (t < 20.0) z = seaTrack(t, 11.0, 20.0, arrivalZ, settledZ, arrivalVelocity, 0.0); else z = settledZ; // The deep-water approach and orbit retain the approved shell framing. float approach = smoothstep(20.0, 27.5, t); float orbit = smoothstep(22.0, 31.0, t); ro = vec3(1.85 * orbit * portraitDistance, seaEyeHeight(t) + portrait * .12 * approach, z - 8.35 * approach * portraitDistance); float pitch = mix(atan(.162), atan(.38), oceanEase(5.0, 14.0, t)); vec3 entryForward = vec3(0.0, -sin(pitch), -cos(pitch)); vec3 deepForward = normalize(vec3(0.0, -4.15, 0.0) - ro); fw = normalize(mix(entryForward, deepForward, oceanEase(17.8, 21.0, t))); } float seaPhotoWidthFraction() { float imageAspect = uSurfaceSize.x / max(uSurfaceSize.y, 1.0); return min(1.0, imageAspect / SEA_REFERENCE_ASPECT); } vec2 seaPhotoUV(vec2 screenUV) { float viewAspect = uResolution.x / max(uResolution.y, 1.0); float imageAspect = uSurfaceSize.x / max(uSurfaceSize.y, 1.0); vec2 crop = vec2(min(1.0, viewAspect / imageAspect), min(1.0, imageAspect / viewAspect)); return (screenUV - .5) * crop + .5; } void seaRay(vec2 screenUV, float t, out vec3 ro, out vec3 rd) { vec3 fw; seaCamera(t, ro, fw); vec3 right = normalize(cross(fw, vec3(0.0, 1.0, 0.0))); vec3 up = cross(right, fw); vec2 photoUV = seaPhotoUV(screenUV); vec2 sensor = 2.0 * (photoUV - .5); sensor.x *= seaPhotoWidthFraction() * SEA_REFERENCE_ASPECT; // Recompose only once fully submerged. Camera position continues forwards; // no portrait dolly backwards or abrupt change to the optical centre occurs. float framing = oceanEase(18.0, 21.0, t); float viewAspect = uResolution.x / max(uResolution.y, 1.0); vec2 deepSensor = (2.0 * screenUV - 1.0) * vec2(viewAspect, 1.0); if (uLayout < .5) deepSensor.x -= viewAspect * .32; else deepSensor.y += .28; sensor = mix(sensor, deepSensor, framing); rd = normalize(fw * SEA_FOCAL_LENGTH + right * sensor.x + up * sensor.y); } vec2 seaReferenceProject(vec3 direction) { vec3 fw, right, up; seaReferenceBasis(fw, right, up); float forward = max(dot(direction, fw), .00001); vec2 sensor = SEA_FOCAL_LENGTH * vec2(dot(direction, right), dot(direction, up)) / forward; vec2 photoUV = .5 + .5 * sensor / vec2(SEA_REFERENCE_ASPECT, 1.0); photoUV.x = .5 + (photoUV.x - .5) / seaPhotoWidthFraction(); return photoUV; } vec2 coastProject(vec3 worldPoint) { return seaReferenceProject(worldPoint - SEA_REFERENCE_EYE); } vec2 skyProject(vec3 worldDirection) { return seaReferenceProject(worldDirection); } // One world-space shoreline and water surface, seen by the shared camera. // The source photograph is projected onto the fixed coast. Moving the camera // changes perspective; neither the coast nor the sea is a screen-space plate. float seaShore(vec2 p){ return 26.0+.22*sin(p.x*.17)+.11*sin(p.x*.43); } float seaHeight(vec2 p,float motion){ float swell=.065*sin(dot(p,vec2(.12,.67))-motion*.64) +.040*sin(dot(p,vec2(-.38,.90))-motion*.87) +.022*sin(dot(p,vec2(.84,.29))+motion*.73) +.012*sin(dot(p,vec2(1.63,1.17))-motion*1.27); return swell*(1.-smoothstep(23.5,27.,p.y)); } vec3 seaNormal(vec2 p,float motion){ vec2 gradient=.065*vec2(.12,.67)*cos(dot(p,vec2(.12,.67))-motion*.64) +.040*vec2(-.38,.90)*cos(dot(p,vec2(-.38,.90))-motion*.87) +.022*vec2(.84,.29)*cos(dot(p,vec2(.84,.29))+motion*.73) +.012*vec2(1.63,1.17)*cos(dot(p,vec2(1.63,1.17))-motion*1.27); gradient*=1.-smoothstep(23.5,27.,p.y); return normalize(vec3(-gradient.x,1.,-gradient.y)); } // Height-field ray intersection. No switch to an unrelated flat surface. float seaHit(vec3 ro,vec3 rd,float motion){ const float bound=.15; float enter=0.,leave=650.; if(abs(rd.y)>.00001){ float a=(-bound-ro.y)/rd.y,b=(bound-ro.y)/rd.y; enter=max(0.,min(a,b));leave=min(650.,max(a,b)); }else if(abs(ro.y)>bound)return -1.; if(leave=0.?1.:-1.; float distance=enter,previous=enter; float slope=.065*abs(dot(rd.xz,vec2(.12,.67))) +.040*abs(dot(rd.xz,vec2(-.38,.90))) +.022*abs(dot(rd.xz,vec2(.84,.29))) +.012*abs(dot(rd.xz,vec2(1.63,1.17))); // Include the attenuation derivative only when this ray crosses the coast. float nearShore=step(23.5,max(ro.z+rd.z*enter,ro.z+rd.z*leave)); slope+=nearShore*.060*abs(rd.z); float speed=abs(rd.y)+slope; vec4 frequency=vec4(dot(rd.xz,vec2(.12,.67)),dot(rd.xz,vec2(-.38,.90)), dot(rd.xz,vec2(.84,.29)),dot(rd.xz,vec2(1.63,1.17))); float curvature=dot(vec4(.065,.040,.022,.012),frequency*frequency); for(int i=0;i<128;i++){ vec3 p=ro+rd*distance; float separation=(p.y-seaHeight(p.xz,motion))*side; // At distance, resolve to a fraction of a pixel instead of demanding // the same 1.5 mm accuracy as water next to the lens. float tolerance=clamp(.30*distance/(uResolution.y*SEA_FOCAL_LENGTH),.0015,.025); if(separation0.)low=mid;else high=mid; } distance=(low+high)*.5; } return distance; } previous=distance; float advance=separation/max(speed,.001); if(p.z<23.5&&rd.z<=0.){ vec3 normal=seaNormal(p.xz,motion); float derivative=(rd.y+dot(normal.xz,rd.xz)/normal.y)*side; // A lower Taylor bound using the exact directional curvature limit. // This remains conservative near a tangent, where a global first- // derivative bound otherwise takes many tiny steps. float denominator=sqrt(derivative*derivative+2.*curvature*separation)-derivative; float quadratic=2.*separation/max(denominator,.000001); advance=max(advance,quadratic); } distance+=max(.0015,advance*.92); if(distance>leave)return -1.; } // Conservative tracing exhausted its budget. Refine a real wave crossing // from the remaining slab, rather than substituting the mean sea plane. vec3 endPoint=ro+rd*leave; if((endPoint.y-seaHeight(endPoint.xz,motion))*side<0.){ float low=distance,high=leave; for(int j=0;j<14;j++){ float mid=(low+high)*.5; vec3 q=ro+rd*mid; if((q.y-seaHeight(q.xz,motion))*side>0.)low=mid;else high=mid; } return (low+high)*.5; } return -1.; } // Broad, filtered colour variation only. A photograph contains lighting, // not surface height, so it must never supply the water's normal map. vec3 seaBodyTint(vec2 world){ vec2 tile=fract(world*vec2(.24,.21)+vec2(.317,.193)); vec2 shifted=fract(tile+.5); vec2 weight=.5-.5*cos(tile*6.2831853); vec2 origin=vec2(.018,.025),size=vec2(.29,.35); vec3 a=textureLod(uOpenWater,origin+tile*size,1.5).rgb; vec3 b=textureLod(uOpenWater,origin+vec2(shifted.x,tile.y)*size,1.5).rgb; vec3 c=textureLod(uOpenWater,origin+vec2(tile.x,shifted.y)*size,1.5).rgb; vec3 d=textureLod(uOpenWater,origin+shifted*size,1.5).rgb; vec3 photographed=mix(mix(d,c,weight.x),mix(b,a,weight.x),weight.y); // Use filtered luminance for subtle water variation, without baking the // source photograph's warm highlights into the transmitted water colour. float variation=dot(photographed,vec3(.2126,.7152,.0722)); return vec3(.10,.30,.36)*(.82+.85*variation); } // Short, intersecting ripples. Low-frequency phase bending breaks up straight // crests; its analytic derivative keeps normals continuous without pixel noise. vec2 seaRipple(vec2 p,vec2 direction,float frequency,float amplitude, float phase,float footprint){ float resolved=1.-smoothstep(.45,1.7,footprint*frequency); vec2 across=vec2(-direction.y,direction.x); float bend=dot(p,across)*.71+phase*.19; float along=dot(p,direction)*.31-phase*.13; float wave=dot(p,direction)*frequency+phase+.62*sin(bend)+.27*sin(along); vec2 gradient=direction*(frequency+.27*.31*cos(along)) +across*(.62*.71*cos(bend)); return gradient*(amplitude*cos(wave)*resolved); } vec2 seaRippleGradient(vec2 p,float motion,float footprint){ return seaRipple(p,normalize(vec2(.8,.6)),3.2,.024,-motion*1.45,footprint) + seaRipple(p,normalize(vec2(-.55,.84)),6.1,.010,motion*1.8+.7,footprint) + seaRipple(p,normalize(vec2(.96,.28)),10.3,.004,-motion*2.15+2.4,footprint) + seaRipple(p,normalize(vec2(-.91,.41)),19.7,.0025,motion*2.7+1.3,footprint) + seaRipple(p,normalize(vec2(.31,.95)),31.3,.0014,-motion*3.4+3.1,footprint) + seaRipple(p,normalize(vec2(-.72,.69)),47.1,.0007,motion*4.1+.5,footprint); } // Projected photographs have a finite field of view. End their contribution // before the border, never stretch a clamped column/row across the water. float seaPhotoCoverage(vec2 uv,float top,float edge){ vec2 lower=smoothstep(vec2(.001),vec2(edge,.025),uv); vec2 upper=1.-smoothstep(vec2(1.-edge,top-.025),vec2(.999,top),uv); return lower.x*lower.y*upper.x*upper.y; } float seaPhotoConfidence(vec3 p,vec3 ro,vec3 sourceEye){ float viewDistance=max(length(p-ro),.001); float sourceDistance=max(length(p-sourceEye),.001); float scale=viewDistance/sourceDistance; float incidence=abs(ro.y-p.y)*sourceDistance /max(abs(sourceEye.y-p.y)*viewDistance,.001); // Oblique re-projection elongates photographed ripples. Close magnification // also runs out of real detail. In both cases use the continuous water field. float angle=smoothstep(.18,.72,incidence); float detail=smoothstep(.12,.48,min(scale,scale/max(incidence,.001))); return angle*detail; } vec3 seaEnvironment(vec3 direction){ float elevation=clamp(direction.y,0.,1.); float glow=exp(-direction.x*direction.x*6.) *exp(-pow((direction.y-.025)*3.8,2.)); vec3 horizon=mix(vec3(.40,.46,.51),vec3(.79,.58,.43),glow*.58); vec3 sky=mix(horizon,vec3(.29,.47,.63),sqrt(elevation)); return mix(vec3(.16,.32,.38),sky,smoothstep(-.16,.025,direction.y)); } vec3 seaSky(vec3 direction){ vec2 uv=skyProject(direction); // Rays beyond the finite tracing range still see the photographed // far sea. Clamping these to the sky horizon repeats a row into a stripe. vec3 colour=texture(uSurface,clamp(uv,vec2(.001,.60),vec2(.999))).rgb; colour=mix(seaEnvironment(direction),colour,smoothstep(.60,.635,uv.y)); float beyond=1.-exp(-max(uv.y-.96,0.)*1.6); return mix(colour,vec3(.30,.46,.66),beyond); } vec3 seaReflection(vec3 direction,float footprint){ vec2 uv=skyProject(direction); // A small reflection lobe represents unresolved capillary waves. // Keeping every cloud edge razor-sharp makes low waves look like metal. // Preserve the same angular softness when the optional 4K coast is used. float lod=4.5+min(1.5,log2(1.+footprint*8.))+log2(max(uSurfaceSize.y/941.,1.)); vec3 colour=textureLod(uSurface,clamp(uv,vec2(.001,.664),vec2(.999)),lod).rgb; float coverage=seaPhotoCoverage(uv,.995,.065)*smoothstep(.664,.70,uv.y); return mix(seaEnvironment(direction),colour,coverage); } vec3 seaAbove(vec3 ro,vec3 rd,float storyTime){ float motion=uMotion; float groundDistance=rd.y<-.00001?-ro.y/rd.y:-1.; vec3 ground=ro+rd*max(groundDistance,0.); if(groundDistance>0.&&ground.z>seaShore(ground.xz)){ vec2 uv=coastProject(ground); return texture(uSurface,clamp(uv,vec2(.001),vec2(.999))).rgb; } float distance=seaHit(ro,rd,motion); if(distance<0.)return seaSky(rd); vec3 p=ro+rd*distance; if(p.z>seaShore(p.xz)){ vec2 uv=coastProject(ground); return texture(uSurface,clamp(uv,vec2(.001),vec2(.999))).rgb; } vec3 geometricNormal=seaNormal(p.xz,motion); vec3 normal=geometricNormal; // Continuous, world-scale ripples over the geometric swells. Their // frequencies are filtered for the projected pixel footprint at grazing angles. float footprint=max(.001,2.*distance/(uResolution.y*SEA_FOCAL_LENGTH*max(.06,abs(rd.y)))); vec2 ripple=seaRippleGradient(p.xz,motion,footprint); normal=normalize(normal+vec3(-ripple.x,0.,-ripple.y)); vec3 reflected=reflect(rd,normal); // Unresolved small waves broaden the reflected lighting instead of // preserving high-contrast cloud shapes as long bright ribbons. vec3 reflection=mix(seaEnvironment(reflected),seaReflection(reflected,footprint),.35); // Average reflectance across the small-wave population; only the // reflection direction follows each ripple. Per-ripple Fresnel would turn // warm sky reflections into outlined, metallic contours. vec3 fresnelNormal=normalize(mix(geometricNormal,normal,.20)); float grazing=1.-max(dot(-rd,fresnelNormal),0.); float fresnel=.02+.98*pow(grazing,5.); vec3 body=seaBodyTint(p.xz); body*=1.+clamp(dot(normal.xz,vec2(-.65,-.76))*.70,-.14,.14); vec3 physical=mix(body,reflection,.10+.85*fresnel); // Retain the original photograph wherever its perspective remains credible. // Coverage and magnification are geometry-based, so the transition follows // the camera and does not introduce a timed change of scene. vec3 planePoint=vec3(p.x,0.,p.z); vec2 photoUV=coastProject(planePoint); vec3 coastColour=texture(uSurface,clamp(photoUV,vec2(.001),vec2(.999))).rgb; // The coast is also the exact opening view. Keep its valid border // pixels intact and feather only outside that field of view. float coastCoverage=smoothstep(-.005,0.,photoUV.x) *(1.-smoothstep(1.,1.005,photoUV.x)) *smoothstep(.001,.025,photoUV.y) *(1.-smoothstep(.640,.665,photoUV.y)); float coastWeight=coastCoverage *seaPhotoConfidence(planePoint,ro,SEA_REFERENCE_EYE); vec3 offshoreEye=vec3(0.,5.,35.); vec2 offshoreUV=skyProject(planePoint-offshoreEye); offshoreUV.x=.5+(offshoreUV.x-.5)*seaPhotoWidthFraction(); vec3 offshoreColour=texture(uOpenWater,clamp(offshoreUV,vec2(.001),vec2(.999,.657))).rgb; float offshore=1.-smoothstep(16.,25.,p.z); float offshoreWeight=offshore*.78*seaPhotoCoverage(offshoreUV,.657,.065) *seaPhotoConfidence(planePoint,ro,offshoreEye); float photoDistance=smoothstep(2.,14.,distance); vec3 water=mix(physical,coastColour,coastWeight*photoDistance); water=mix(water,offshoreColour,offshoreWeight*photoDistance); float close=1.-smoothstep(3.,19.,distance); vec3 sunDirection=normalize(vec3(0.,.022,-1.)); float sun=pow(max(dot(reflected,sunDirection),0.),180.); water+=vec3(1.,.69,.38)*sun*.10*close; // Short, dissolving foam on the physical shoreline only. float shoreDistance=seaShore(p.xz)-p.z; float foam=(1.-smoothstep(.15,.85,shoreDistance)) *smoothstep(.55,.93,sin(p.x*2.6+motion*.6)*.5+.5); water=mix(water,vec3(.76,.80,.80),foam*.24); // Fade geometric detail into the photographed far sea before the finite // tracing range ends, avoiding a visible horizontal range boundary. return mix(clamp(water,0.,1.),seaSky(rd),smoothstep(250.,600.,distance)); } // A compact underwater continuation lets this renderer keep travelling while // the detailed shell renderer is still preparing. It uses the same water, // camera and lens crossing, with no surface hold or second camera. vec3 seaFallbackFog(vec3 ro,vec3 rd,float distance){ float y=smoothstep(-.55,.42,rd.y); vec3 colour=mix(vec3(.002,.095,.105),vec3(.006,.32,.34),y); float sunCone=pow(max(dot(rd,normalize(vec3(.32,.62,-.72))),0.),5.); colour+=vec3(.15,.42,.36)*sunCone; float haze=.5+.5*sin(rd.x*3.5+rd.z*.8); colour=mix(colour,colour*vec3(.73,1.06,1.04),haze*.35); float shafts=0.; for(int i=0;i<4;i++){ vec3 p=ro+rd*(float(i)+.65)*2.2; vec2 surface=p.xz-p.y*vec2(.56,.14); float warp=.28*sin(surface.y*.72+uMotion*.035); float ray=pow(max(0.,sin(surface.x*1.17+surface.y*.47+warp+uMotion*.028)),14.); float broad=pow(max(0.,sin(surface.x*.43-surface.y*.21+.7)),6.); shafts+=(ray*.31+broad*.085)*(1.-float(i)*.12); } colour+=vec3(.40,.82,.70)*shafts*min(distance*.2,1.); vec2 screen=gl_FragCoord.xy/uResolution; vec2 fromSun=vec2((screen.x-.83)*(uResolution.x/uResolution.y),1.28-screen.y); float angle=atan(fromSun.x,fromSun.y); float drift=.055*sin(angle*4.+uMotion*.045); float fine=pow(max(0.,sin(angle*23.+1.6*sin(angle*8.)+drift)),6.); float soft=pow(max(0.,sin(angle*11.-.6+drift)),3.); float fan=(fine*.10+soft*.11)*smoothstep(-.18,1.15,screen.y); return colour+vec3(.40,.85,.74)*fan*min(distance*.21,1.); } // The compact path has a complete animated shell of its own. Exact quadric // intersections replace the detailed renderer's distance marching, so a slow // shader never turns the reveal into a jump to an already-open photograph. struct SeaShellHit { float distance; vec3 normal; vec3 localPoint; float material; float upper; }; vec2 seaEllipsoidRoots(vec3 ro,vec3 rd,vec3 radii){ vec3 o=ro/radii,d=rd/radii; float a=dot(d,d),b=dot(o,d),c=dot(o,o)-1.; float h=b*b-a*c; if(h<0.)return vec2(-1.); float root=sqrt(h); return vec2(-b-root,-b+root)/a; } vec3 seaValveRotate(vec3 p,float angle){ float c=cos(angle),s=sin(angle); return vec3(p.x,c*p.y-s*p.z,s*p.y+c*p.z); } void seaShellAccept(float t,vec3 normal,vec3 point,float material,float upper,float angle,inout SeaShellHit hit){ if(t<=.002||t>=hit.distance)return; hit.distance=t; hit.normal=seaValveRotate(normal,-angle); hit.localPoint=point; hit.material=material; hit.upper=upper; } void seaShellValve(vec3 ro,vec3 rd,float upper,float angle,inout SeaShellHit hit){ const vec3 hinge=vec3(0.,0.,-1.11); ro=seaValveRotate(ro-vec3(0.,-4.55,0.)-hinge,angle)+hinge; rd=seaValveRotate(rd,angle); vec3 outer=vec3(1.50,mix(.430,.880,upper),1.15); vec3 inner=mix(vec3(1.390,.260,1.040),vec3(1.405,.795,1.055),upper); float side=mix(-1.,1.,upper); vec2 outside=seaEllipsoidRoots(ro,rd,outer),inside=seaEllipsoidRoots(ro,rd,inner); for(int i=0;i<2;i++){ float t=outside[i];vec3 p=ro+rd*t; if(t>0.&&p.y*side>=0.&&dot(p/inner,p/inner)>=.9999) seaShellAccept(t,normalize(p/(outer*outer)),p,1.,upper,angle,hit); t=inside[i];p=ro+rd*t; if(t>0.&&p.y*side>=0.&&dot(p/outer,p/outer)<=1.0001) seaShellAccept(t,-normalize(p/(inner*inner)),p,2.,upper,angle,hit); } // The annulus at the cut plane closes the shell's substantial pale lip. if(abs(rd.y)>.00001){ float t=-ro.y/rd.y;vec3 p=ro+rd*t; if(t>0.&&dot(p/outer,p/outer)<=1.&&dot(p/inner,p/inner)>=1.) seaShellAccept(t,vec3(0.,-side,0.),p,2.,upper,angle,hit); } } SeaShellHit seaCompactShell(vec3 ro,vec3 rd,float maximum){ SeaShellHit hit=SeaShellHit(maximum,vec3(0.,1.,0.),vec3(0.),0.,0.); float opening=smoothstep(27.,32.,min(uTime,35.)); float angle=opening*.95993109; seaShellValve(ro,rd,0.,0.,hit); seaShellValve(ro,rd,1.,angle,hit); vec3 q=ro-vec3(0.,-4.396,.10); vec3 radii=vec3(.420,.414,.407); vec2 roots=seaEllipsoidRoots(q,rd,radii); float t=roots.x>.002?roots.x:roots.y; vec3 p=q+rd*t; seaShellAccept(t,normalize(p/(radii*radii)),p,3.,0.,0.,hit); q=ro-vec3(0.,-4.55,-1.08);radii=vec3(.22,.12,.12); roots=seaEllipsoidRoots(q,rd,radii);t=roots.x>.002?roots.x:roots.y;p=q+rd*t; seaShellAccept(t,normalize(p/(radii*radii)),p,1.,0.,0.,hit); return hit; } vec3 seaCompactEnvironment(vec3 direction){ float sky=clamp(direction.y*.5+.5,0.,1.); vec3 colour=mix(vec3(.19,.115,.10),vec3(.38,.65,.67),pow(sky,1.4)); colour+=vec3(.95,.80,.56)*pow(max(dot(direction,normalize(vec3(.48,.87,.12))),0.),12.); colour+=vec3(.23,.32,.40)*pow(max(dot(direction,normalize(vec3(-.6,.35,.5))),0.),4.); return colour; } float seaCompactHash(vec2 p){ p=fract(p*vec2(123.34,456.21));p+=dot(p,p+45.32);return fract(p.x*p.y); } float seaCompactNoise(vec2 p){ vec2 i=floor(p),f=fract(p);f=f*f*(3.-2.*f); return mix(mix(seaCompactHash(i),seaCompactHash(i+vec2(1,0)),f.x), mix(seaCompactHash(i+vec2(0,1)),seaCompactHash(i+vec2(1)),f.x),f.y); } vec3 seaCompactBRDF(vec3 n,vec3 v,vec3 l,vec3 base,float rough,vec3 f0){ vec3 h=normalize(v+l); float nl=max(dot(n,l),0.),nv=max(dot(n,v),.001),nh=max(dot(n,h),0.),vh=max(dot(v,h),0.); float a=rough*rough,a2=a*a; float d=a2/(3.14159265*pow(nh*nh*(a2-1.)+1.,2.)); float k=pow(rough+1.,2.)/8.; float g=nv/(nv*(1.-k)+k)*nl/(nl*(1.-k)+k); vec3 f=f0+(1.-f0)*pow(1.-vh,5.); return ((1.-f)*base/3.14159265+d*g*f/max(4.*nv*nl,.001))*nl; } vec3 seaCompactShellColour(SeaShellHit hit,vec3 worldPoint,vec3 rd){ vec3 n=hit.normal,v=-rd,q=hit.localPoint; vec3 light=normalize(vec3(.48,.87,.12)); vec2 fanPoint=vec2(q.x,q.z+1.11); float radial=length(fanPoint*vec2(.84,.74)); float fan=atan(fanPoint.x,fanPoint.y+.035); float mineral=seaCompactNoise(q.xz*7.3+q.yy*2.1); float grain=seaCompactNoise(q.xz*112.+q.yy*41.); float ribs=.5+.5*cos(fan*24.+1.1*sin(fan*4.)+.9*sin(radial*3.)); float strata=sin(radial*54.+sin(fan*7.)*1.6+mineral*2.8); float finer=sin(radial*167.+sin(fan*11.)*.8); vec3 tangent=seaValveRotate(vec3(0.,0.,1.),-hit.upper*smoothstep(27.,32.,uTime)*.95993109); tangent-=n*dot(tangent,n); vec3 across=vec3(1.,0.,0.)-n*n.x; vec3 base,film=vec3(1.),f0=vec3(.045); float rough=.4; if(hit.material<1.5){ float terrace=smoothstep(-.25,.65,strata)*(.55+.45*mineral); base=mix(vec3(.39,.20,.16),vec3(.74,.47,.29),mineral*.7+ribs*ribs*.3); base=mix(base,vec3(.92,.73,.51),terrace*.48); float patina=seaCompactNoise(q.xz*3.1+vec2(7.,1.)); base=mix(base,vec3(.63,.30,.37),smoothstep(.46,.78,patina)*.32); base*=.92+.14*grain;rough=.56+.14*mineral; float weather=(strata*.018+finer*.005)*(1.-abs(n.y)*.28); n=normalize(n+across*(weather*fanPoint.x)+tangent*(weather*fanPoint.y)); }else if(hit.material<2.5){ float layer=.5+.5*sin(radial*31.+mineral*1.7+sin(fan*4.)*.35); base=mix(vec3(.83,.39,.35),vec3(1.,.79,.52),.42+.32*mineral); base*=.82+.10*layer+.025*grain+.13*ribs*ribs; float bump=.0055*strata+.0025*finer; float ridge=sin(fan*24.+sin(fan*4.)*1.1+radial*.8)*.095*smoothstep(.12,.7,radial); vec2 side=vec2(fanPoint.y,-fanPoint.x)/max(length(fanPoint),.15); n=normalize(n+across*(bump*fanPoint.x+ridge*side.x)+tangent*(bump*fanPoint.y+ridge*side.y)); float incidence=max(dot(n,v),0.); float sheen=.5+.5*sin(incidence*6.5+radial*1.9+mineral*.8); film=mix(vec3(.43,.82,.92),vec3(1.,.49,.65),sheen); film=mix(film,vec3(.79,.61,.98),pow(.5+.5*sin(radial*5.2+incidence*9.),4.)*.55); base=mix(base,film,.31+.19*(1.-incidence)); rough=.22+.055*mineral;f0=vec3(.047); }else{ vec3 pearl=q/.42; float layers=.5+.5*sin(pearl.y*13.+sin(pearl.x*4.+pearl.z*2.)*.35); float orient=.5+.5*sin(max(dot(n,v),0.)*5.9+pearl.y*1.4); film=mix(vec3(.42,.88,.94),vec3(.98,.48,.73),orient); film=mix(film,vec3(.84,.68,.99),pow(.5+.5*sin(pearl.x*3.+pearl.y*4.),4.)*.55); base=mix(vec3(.94,.83,.68),film,.48+layers*.08); rough=.17+layers*.018;f0=vec3(.11); } // One exact analytic light ray supplies real pearl/valve occlusion, without // adding the detailed renderer's repeated distance-map shadow marcher. SeaShellHit blocker=seaCompactShell(worldPoint+hit.normal*.018,light,3.5); float shadow=blocker.material>.5?.32:1.; float ao=1.; if(hit.material>1.5&&hit.material<2.5&&hit.upper<.5){ vec2 bed=q.xz-vec2(0.,.1);ao=1.-.25*exp(-dot(bed,bed)*3.5); } vec3 reflection=reflect(rd,n); vec3 colour=seaCompactBRDF(n,v,light,base,rough,f0)*vec3(2.7,2.45,1.96)*.92*shadow; colour+=base*seaCompactEnvironment(n)*(hit.material>2.5?1.12:.98)*ao; colour+=seaCompactBRDF(n,v,normalize(vec3(.4,.12,1.)),base,max(rough,.26),f0)*vec3(1.65,.78,.59)*ao; float incidence=max(dot(n,v),0.); float fresnel=.045+.45*pow(1.-incidence,4.); if(hit.material>1.5){ colour+=seaCompactEnvironment(reflection)*fresnel*(hit.material>2.5?1.65:1.)*mix(vec3(1.),film,.62); float orientLight=pow(max(dot(reflection,normalize(vec3(.65,.35,-.35))),0.),14.); colour+=film*orientLight*(hit.material>2.5?.33:.28)*(.32+.68*pow(1.-incidence,2.))*ao; } if(hit.material>2.5){ colour+=vec3(.87,.94,.88)*pow(max(dot(reflection,normalize(vec3(-.7,.6,.4))),0.),30.)*.52; colour+=vec3(.30,.63,.68)*pow(max(dot(reflection,normalize(vec3(.55,.55,-.3))),0.),70.)*.24; colour+=(.5+.5*cos(6.283*(dot(n,vec3(.36,.28,.15))+vec3(0.,.33,.67))))*pow(1.-incidence,1.5)*.29; } return colour; } vec3 seaFallbackBelow(vec3 ro,vec3 rd){ // A single plane intersection keeps the fallback inexpensive. Broad mineral // variation and filtered caustics remain anchored to the moving world. float distance=rd.y<-.008?(-5.-ro.y)/rd.y:140.; distance=clamp(distance,0.,140.); SeaShellHit shell=seaCompactShell(ro,rd,distance); if(shell.material>.5){ distance=shell.distance; vec3 transmission=exp(-vec3(.096,.052,.050)*distance); return seaCompactShellColour(shell,ro+rd*distance,rd)*transmission+seaFallbackFog(ro,rd,distance)*(1.-transmission); } vec3 fog=seaFallbackFog(ro,rd,distance); if(distance>=140.)return fog; vec3 p=ro+rd*distance; float mineral=.5+.25*sin(p.x*.47+p.z*.29)+.25*sin(p.z*.61-p.x*.19); vec3 sand=mix(vec3(.50,.43,.31),vec3(.80,.73,.57),mineral); float ripples=.5+.5*sin(p.x*8.+.7*sin(p.z*.9)); float footprint=2.*distance/(uResolution.y*SEA_FOCAL_LENGTH*max(.06,abs(rd.y))); sand=mix(sand,vec3(.91,.84,.68),.19+ripples*.12*(1.-smoothstep(.12,.6,footprint))); vec2 phase=p.xz*1.35+vec2(sin(p.z*.65+uMotion*.12),cos(p.x*.81-uMotion*.09)); float web=sin(phase.x*2.1+sin(phase.y*1.7)) +sin(phase.y*2.3+sin(phase.x*1.4)+uMotion*.15); float caustic=exp(-abs(web)*12.)*(1.-smoothstep(.10,.75,footprint))*exp(-distance*.04); float shellShadow=1.-.65*exp(-dot(p.xz,p.xz)*.60); vec3 colour=sand*.91*(.60+caustic*.50)*shellShadow; vec3 transmission=exp(-vec3(.096,.052,.050)*distance); return colour*transmission+fog*(1.-transmission); } vec3 seaFallbackTone(vec3 colour){ colour=max(vec3(0.),colour); return clamp((colour*(2.51*colour+.03))/(colour*(2.43*colour+.59)+.14),0.,1.); } vec3 surfaceView(vec2 screenUV,float storyTime){ vec3 ro,rd; seaRay(screenUV,storyTime,ro,rd); if(ro.y>.55)return seaAbove(ro,rd,storyTime); // Match the full renderer's physical near-lens water line. Every pixel // crosses as the same wave reaches it, rather than on a timed screen wipe. vec3 lensPoint=ro+rd*.24; float depth=seaHeight(lensPoint.xz,uMotion)-lensPoint.y; float wet=smoothstep(-.009,.009,depth); float contact=1.-smoothstep(.08,.32,abs(depth)); vec3 normal=seaNormal(lensPoint.xz,uMotion); vec3 wetRay=normalize(rd+vec3(normal.x,0.,normal.z)*.025*contact); vec3 colour=vec3(0.); if(wet<1.)colour=seaAbove(lensPoint,rd,storyTime); if(wet>0.){ vec3 below=seaFallbackBelow(ro,wetRay); float vignette=1.-.10*pow(length((screenUV-.5)*vec2(1.,.8)),1.6); below=pow(seaFallbackTone(below*.95)*vignette,vec3(1./2.2)); colour=wet>=1.?below:mix(colour,below,wet); } float meniscus=exp(-abs(depth)*85.); colour*=1.-.06*meniscus; colour+=vec3(.035,.042,.035)*exp(-abs(depth)*210.); return clamp(colour,0.,1.); } void main(){fragColor=vec4(surfaceView(gl_FragCoord.xy/uResolution,uTime),1.);}