out vec4 FragColor; uniform sampler2D uTexGarbage; uniform sampler2D uTexGradients; uniform sampler2D uTexDepth; uniform vec4 uDepthBounds; uniform vec3 uCamera; uniform vec4 uPlane; in vec4 aColour; in vec2 aUv; in vec3 aNorm; in vec3 aCo; float water_depth( vec3 pos, vec3 dir, vec4 plane ) { float d = dot( plane.xyz, dir ); float t = dot((plane.xyz*plane.w - pos),plane.xyz) / d; return t*0.04; } float sample_height( vec3 pos ) { vec2 depth_coords = (pos.xz-uDepthBounds.xy)*uDepthBounds.zw; return texture( uTexDepth, depth_coords ).r; } float create_shadowing( vec3 vdir ) { return clamp( sample_height( aCo+vdir ) - (aCo.y+vdir.y), 0.1, 0.2 )-0.1; } void main() { vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.160 ); // Creating normal patches vec3 modnorm = (wgarbage.rgb-0.4) * 1.4; vec3 qnorm = normalize(floor(aNorm*4.0+modnorm)*0.25) + vec3(0.001,0.0,0.0); vec3 tangent0 = normalize(cross(qnorm,vec3(0.0,1.0,0.0))); vec3 tangent1 = cross(qnorm,tangent0); vec2 uvdiffuse = vec2( dot(tangent0,aCo), dot(tangent1,aCo) ) * 0.160; // Patch local noise vec4 rgarbage = texture( uTexGarbage, uvdiffuse ); // Colour blending float fblendclip = step(0.380,aColour.r + (rgarbage.r-0.5)*-1.740)*0.320; vec2 uvgradients = aUv + vec2( fblendclip, 0.0 ); vec3 diffuse = texture( uTexGradients, uvgradients ).rgb; diffuse -= rgarbage.a*0.04; // Lighting vec3 lightdir = normalize(vec3(0.5,0.5,-0.1)); vec3 shadow = vec3(0.27,0.25,0.34); float light1 = dot( lightdir, aNorm )*0.5+0.5; diffuse = diffuse * (light1*vec3(1.0,0.96,0.9)*1.2 + shadow*(1.0-light1)); // Specular lighting vec3 halfview = normalize( uCamera - aCo ); vec3 specdir = reflect( -lightdir, qnorm ); float spec = pow(max(dot(halfview,specdir),0.0),10.0) * 0.3*rgarbage.r; //diffuse += spec * vec3(1.0,0.8,0.8); float faccum = 0.0; vec3 offs = vec3(rgarbage.x, 0.0, rgarbage.z)*4.0; faccum += create_shadowing( vec3( 0.0, 0.5, 0.0 )*0.6); faccum += create_shadowing( vec3( 2.0, 0.3, 0.0 )*0.6); faccum += create_shadowing( vec3( 3.0, 1.0, 0.0 )*0.6); faccum += create_shadowing( vec3( 5.0, 1.0, 0.0 )*0.6); faccum += create_shadowing( vec3( 0.0, 0.5, 0.0 )*0.6*1.5+offs); faccum += create_shadowing( vec3( 2.0, 0.3, 0.0 )*0.6*1.5); faccum += create_shadowing( vec3( 3.0, 1.0, 0.0 )*0.6*1.5-offs); faccum += create_shadowing( vec3( 5.0, 1.0, 0.0 )*0.6*1.5); diffuse = mix( diffuse, vec3(0.15,0.1,0.2), min(faccum*1.0,1.0)); FragColor = vec4(diffuse, water_depth(aCo,halfview,uPlane)); }