Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / games / voxels / src / main / rust / shaders / water.frag

water.frag74 lines · 2.9 KiBRaw
1#version 450
2layout(location=0) in vec3 fragWorldPos;
3layout(location=1) in float fragDist;
4layout(location=2) in vec3 fragNormal;
5
6layout(binding=0) uniform Ubo {
7 mat4 viewProj;
8 vec3 sunDir;
9 float time;
10 vec3 fogColor;
11 float fogDensity;
12 vec4 playerPos; // .w = underwater flag
13 float dayFactor;
14 mat4 invViewProj;
15 vec3 sunColor;
16 float cloudShadow;
17 vec3 ambientColor;
18 float _pad2;
19};
20
21layout(location=0) out vec4 outColor;
22
23void main() {
24 vec3 s = normalize(sunDir);
25 vec3 viewDir = normalize(playerPos.xyz - fragWorldPos);
26 vec3 wp = fragWorldPos;
27
28 // Animated surface normal from several NON-axis-aligned directional ripples. Summing waves that
29 // travel along diagonal directions avoids the plaid "square wave" look that axis-aligned
30 // sin(x)+sin(z) produces on the block grid.
31 vec2 pw = wp.xz;
32 vec3 n = normalize(fragNormal);
33 vec2 d1 = vec2(0.936, 0.351); // ~20 deg
34 vec2 d2 = vec2(-0.406, 0.914); // ~114 deg
35 vec2 d3 = vec2(0.274, -0.962); // ~-74 deg
36 float w1 = sin(dot(pw, d1) * 0.9 + time * 1.6);
37 float w2 = sin(dot(pw, d2) * 1.7 + time * 2.1);
38 float w3 = cos(dot(pw, d3) * 3.1 + time * 2.9);
39 n.x += 0.11 * w1 * d1.x + 0.07 * w2 * d2.x + 0.035 * w3 * d3.x;
40 n.z += 0.11 * w1 * d1.y + 0.07 * w2 * d2.y + 0.035 * w3 * d3.y;
41 n = normalize(n);
42
43 // Fresnel: near-transparent looking straight down, mirror-like at grazing angles.
44 float ndv = max(dot(n, viewDir), 0.0);
45 float fres = mix(0.04, 1.0, pow(1.0 - ndv, 4.0));
46
47 // Body colour: teal in the shallows, deep blue when looking straight down into it.
48 vec3 shallow = vec3(0.06, 0.30, 0.36);
49 vec3 deep = vec3(0.015, 0.08, 0.14);
50 vec3 body = mix(shallow, deep, ndv);
51 // Cheap sky reflection colour, brighter when the sun is up.
52 vec3 skyRefl = mix(vec3(0.30, 0.45, 0.60), vec3(0.55, 0.72, 0.95), clamp(s.y, 0.0, 1.0)) + sunColor * 0.10;
53 vec3 col = mix(body, skyRefl, fres);
54 // Darken with the day/night cycle so water doesn't glow at night while the terrain is dark.
55 // (Sun glint below uses sunColor, which is already ~0 at night, so it stays correct.)
56 col *= mix(0.06, 1.0, clamp(dayFactor, 0.0, 1.0));
57
58 // Sharp sun glint + a broad sheen.
59 vec3 hlf = normalize(s + viewDir);
60 float nh = max(dot(n, hlf), 0.0);
61 col += sunColor * (pow(nh, 400.0) * 1.6 + pow(nh, 40.0) * 0.12);
62
63 // More transparent looking down (see the floor), opaque/reflective at grazing angles.
64 float alpha = mix(0.55, 0.95, fres);
65
66 // Distance fog toward the horizon so the ocean fades into the sky instead of ending abruptly.
67 float fog = clamp(1.0 - exp(-fragDist * fogDensity), 0.0, 0.9);
68 col = mix(col, fogColor, fog);
69 alpha = mix(alpha, 1.0, fog);
70
71 if (playerPos.w > 0.5) { col = mix(col, vec3(0.05, 0.20, 0.32), 0.6); alpha = 0.85; }
72
73 outColor = vec4(col, alpha); // linear HDR; tonemap in composite
74}