out vec4 FragColor; uniform sampler2D uTexGarbage; uniform sampler2D uTexGradients; 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.05; } void main() { vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 ); // Creating normal patches vec3 modnorm = (wgarbage.rgb-0.4) * 1.4; vec3 qnorm = normalize(floor(aNorm*4.0+modnorm) * 0.25); vec2 dir = normalize(qnorm.xz); vec2 uvdiffuse = aCo.xz * 0.02; uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse; // Patch local noise vec4 rgarbage = texture( uTexGarbage, uvdiffuse ); // Colour blending float amtgrass = step(qnorm.y,0.6); float amtsand = min(max((aCo.y + 90.0) * -0.08,0.0)*qnorm.y,1.0); vec2 uvgradients = vec2( rgarbage.a, -amtgrass*0.125 ) + aUv; vec3 diffuse = texture( uTexGradients, uvgradients ).rgb; diffuse = mix( diffuse, vec3(1.0,0.9,0.7), amtsand ); // Lighting vec3 lightdir = vec3(0.95,0.0,-0.3); vec3 shadow = pow(vec3(0.014,0.034,0.084),vec3(1.0/3.2)); float light1 = dot( lightdir, mix(qnorm,aNorm,amtsand) )*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.2*rgarbage.r; diffuse += spec * vec3(1.0,0.8,0.8); FragColor = vec4(diffuse, water_depth(aCo,halfview,uPlane)); }