stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / terrain.fs
1 out vec4 FragColor;
2
3 uniform sampler2D uTexGarbage;
4 uniform sampler2D uTexGradients;
5 uniform vec3 uCamera;
6 uniform vec4 uPlane;
7
8 in vec4 aColour;
9 in vec2 aUv;
10 in vec3 aNorm;
11 in vec3 aCo;
12
13 float water_depth( vec3 pos, vec3 dir, vec4 plane )
14 {
15 float d = dot( plane.xyz, dir );
16 float t = dot((plane.xyz*plane.w - pos),plane.xyz) / d;
17 return t*0.05;
18 }
19
20 void main()
21 {
22 vec4 wgarbage = texture( uTexGarbage, aCo.xz * 0.015 );
23
24 // Creating normal patches
25 vec3 modnorm = (wgarbage.rgb-0.4) * 1.4;
26 vec3 qnorm = normalize(floor(aNorm*4.0+modnorm) * 0.25);
27 vec2 dir = normalize(qnorm.xz);
28 vec2 uvdiffuse = aCo.xz * 0.02;
29 uvdiffuse = mat2(dir.y, dir.x, -dir.x, dir.y) * uvdiffuse;
30
31 // Patch local noise
32 vec4 rgarbage = texture( uTexGarbage, uvdiffuse );
33
34 // Colour blending
35 float amtgrass = step(qnorm.y,0.6);
36 float amtsand = min(max((aCo.y + 90.0) * -0.08,0.0)*qnorm.y,1.0);
37 vec2 uvgradients = vec2( rgarbage.a, -amtgrass*0.125 ) + aUv;
38 vec3 diffuse = texture( uTexGradients, uvgradients ).rgb;
39 diffuse = mix( diffuse, vec3(1.0,0.9,0.7), amtsand );
40
41 // Lighting
42 vec3 lightdir = vec3(0.95,0.0,-0.3);
43 vec3 shadow = pow(vec3(0.014,0.034,0.084),vec3(1.0/3.2));
44 float light1 = dot( lightdir, mix(qnorm,aNorm,amtsand) )*0.5+0.5;
45 diffuse = diffuse * (light1*vec3(1.0,0.96,0.9)*1.2 + shadow*(1.0-light1));
46
47 // Specular lighting
48 vec3 halfview = normalize( uCamera - aCo );
49 vec3 specdir = reflect( -lightdir, qnorm );
50 float spec = pow(max(dot(halfview,specdir),0.0),10.0) * 0.2*rgarbage.r;
51 diffuse += spec * vec3(1.0,0.8,0.8);
52
53 FragColor = vec4(diffuse, water_depth(aCo,halfview,uPlane));
54 }