X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=shaders%2Fwater.fs;h=938c6d1e04d04493226bdaa895d8bdf8a3990322;hb=3d8eaabcfd3b87fb52127eaa1241673e8d197bea;hp=da92d134e8e3bda2c3098b32a88bf3c154190f6e;hpb=afa80c76d03f5e983092e9d7be33a9102a7ab25e;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/shaders/water.fs b/shaders/water.fs index da92d13..938c6d1 100644 --- a/shaders/water.fs +++ b/shaders/water.fs @@ -2,33 +2,77 @@ out vec4 FragColor; uniform sampler2D uTexMain; uniform sampler2D uTexDudv; -uniform sampler2D uTexDepth; +uniform sampler2D uTexBack; + uniform vec2 uInvRes; uniform float uTime; +uniform vec3 uCamera; +uniform float uSurfaceY; + +uniform vec3 uShoreColour; +uniform vec3 uOceanColour; -in vec4 aUv; +in vec4 aColour; +in vec2 aUv; +in vec3 aNorm; in vec3 aCo; +in vec3 aWorldCo; + +#include "common_world.glsl" + +vec4 water_surf( vec3 halfview, vec3 vnorm, float depthvalue, + vec4 beneath, vec4 above ) +{ + vec3 surface_tint = mix(uShoreColour, uOceanColour, depthvalue); + + float ffresnel = pow(1.0-dot( vnorm, halfview ),5.0); + + vec3 lightdir = vec3(0.95,0.0,-0.3); + vec3 specdir = reflect( -lightdir, vnorm ); + float spec = pow(max(dot(halfview,specdir),0.0),20.0)*0.3; + + // Depth + float depthblend = pow( beneath.a,0.8 ); + + // Composite + vec3 vsurface = mix(surface_tint, above.rgb, ffresnel ); + //vsurface += spec; + + return vec4( vsurface,depthblend ); +} void main() { + // Create texture coords vec2 ssuv = gl_FragCoord.xy*uInvRes; - vec4 dudva = texture( uTexDudv, aUv.xy + vec2(uTime*0.04f,uTime*0.03f) ); - vec4 dudvb = texture( uTexDudv, aUv.xy - vec2(uTime*0.1,uTime*0.054) ); + + // Surface colour composite + float depthvalue = clamp( -world_depth_sample( aCo )*(1.0/25.0), 0.0, 1.0 ); - vec2 distortamt = (dudva.rg-0.5) * (dudvb.ba-0.5) * 2.0; + vec2 world_coord = aCo.xz * 0.008; + vec4 time_offsets = vec4( uTime ) * vec4( 0.008, 0.006, 0.003, 0.03 ); + vec4 dudva = texture( uTexDudv, world_coord + time_offsets.xy )-0.5; + vec4 dudvb = texture( uTexDudv, world_coord *7.0 - time_offsets.zw )-0.5; - vec4 reflected = texture( uTexMain, ssuv+distortamt ); - float depthvalue = texture( uTexDepth, aUv.zw ).r; - float opacity = smoothstep( 0.0, 0.1, depthvalue ); + vec3 surfnorm = dudva.rgb + dudvb.rgb; + surfnorm = normalize(vec3(0.0,1.0,0.0) + dudva.xyz*0.4 + dudvb.xyz*0.1); + + // Foam + float fband = fract( aCo.z*0.02+uTime*0.1+depthvalue*10.0 ); + fband = step( fband+dudva.a*0.8, 0.3 ) * max((1.0-depthvalue*4.0),0.0); - vec3 colour_shore = vec3( 0.96, 0.98, 0.9 ); - vec3 colour_ocean = vec3( 0.61, 0.84, 0.9 ); - vec3 surface_tint = mix( colour_shore, colour_ocean, depthvalue ); + // Lighting + vec3 halfview = -normalize( aCo-uCamera ); - vec3 comp_surf = mix(pow(surface_tint,vec3(6.0))*0.6,reflected.rgb, 0.7); - - float band = fract( aCo.z*0.1+uTime*0.1-depthvalue*10.0 ); - band = step( band + dudvb.g*0.8, 0.5 ) * max((1.0-depthvalue*4.0),0.0); + // Sample textures + vec4 above = texture( uTexMain, ssuv+ surfnorm.xz*0.2 ); + vec4 beneath = texture( uTexBack, ssuv ); + + // Fog + float fdist = pow(length( aCo.xz-uCamera.xz ) * 0.00047, 2.6); - FragColor = vec4(comp_surf + band*0.2,opacity+band*0.2); + // Composite + vec4 vsurface = water_surf( halfview, surfnorm, depthvalue, beneath, above ); + vsurface.a -= fdist; + FragColor = mix( vsurface, vec4(1.0,1.0,1.0,0.5), fband ); }