better water
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / terrain.fs
index 04751e9ec45ae40c4e3f1c76573d22f5f5ba514d..a6d24006fbfdf9a224a5fff0f6c4891e2d6a7ea7 100644 (file)
@@ -1,6 +1,54 @@
-#include "common.glsl"
+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.04;
+}
 
 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 - 10.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 = vec3(0.27,0.25,0.34);
+   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));
 }