stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / terrain.fs
index fbfa276767f2f7a4f9e6de2dfe9584e27376edf9..98895a0d1ddd1c00df5eb3fb403d3911b9767940 100644 (file)
@@ -3,36 +3,52 @@ 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 = floor(aNorm*4.0+modnorm) * 0.25;
-
+   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 = 1.0-(dot( lightdir, qnorm )*0.5+0.5);
+   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));
    
-   qnorm = floor(aNorm*8.0)*0.125;
-   vec3 viewdelta = normalize( uCamera - aCo );
+   // Specular lighting
+   vec3 halfview = normalize( uCamera - aCo );
    vec3 specdir = reflect( -lightdir, qnorm );
-   float spec = pow(max(dot(viewdelta,specdir),0.0),10.0) * 0.2*rgarbage.r;
-   
-   diffuse = diffuse*(1.0-light1)+diffuse*shadow*light1;
-   FragColor = vec4(diffuse+spec, 1.0);
+   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));
 }