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

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

block.frag131 lines · 5.5 KiBRaw
1#version 450
2layout(location=0) in vec2 fragUv; // per-block tile counts (0..w, 0..h)
3layout(location=1) in vec3 fragColor;
4layout(location=2) in float fragAo;
5layout(location=3) in float fragDist;
6layout(location=4) in vec3 fragWorldPos;
7layout(location=5) in vec3 fragNormal;
8layout(location=6) in float fragTile;
9layout(location=7) in vec4 fragLightPos;
10layout(location=8) in float fragLight; // packed skylight + blocklight*16
11
12layout(binding=0) uniform Ubo {
13 mat4 viewProj;
14 vec3 sunDir;
15 float time;
16 vec3 fogColor;
17 float fogDensity;
18 vec4 playerPos;
19 float dayFactor;
20 mat4 invViewProj;
21 vec3 sunColor;
22 float cloudShadow;
23 vec3 ambientColor;
24 float _pad2;
25 mat4 lightViewProj;
26};
27
28layout(binding=1) uniform sampler2D atlasTex;
29layout(binding=2) uniform sampler2D shadowMap;
30
31layout(location=0) out vec4 outColor;
32
33// Atlas is 16 cols x 16 rows.
34const vec2 TILE_SPAN = vec2(1.0/16.0, 1.0/16.0);
35const float ATLAS_COLS = 16.0;
36
37// --- Cloud coverage (matches sky.frag) for cloud shadows on the ground. ---
38const float CLOUD_MID = 165.0;
39const float COVERAGE = 0.47;
40float hash(vec3 p){ p=fract(p*0.3183099+0.1); p*=17.0; return fract(p.x*p.y*p.z*(p.x+p.y+p.z)); }
41float vnoise(vec3 x){
42 vec3 i=floor(x); vec3 f=fract(x); f=f*f*(3.0-2.0*f);
43 return mix(mix(mix(hash(i+vec3(0,0,0)),hash(i+vec3(1,0,0)),f.x),
44 mix(hash(i+vec3(0,1,0)),hash(i+vec3(1,1,0)),f.x),f.y),
45 mix(mix(hash(i+vec3(0,0,1)),hash(i+vec3(1,0,1)),f.x),
46 mix(hash(i+vec3(0,1,1)),hash(i+vec3(1,1,1)),f.x),f.y),f.z);
47}
48float fbm2(vec3 p){ return 0.6*vnoise(p) + 0.4*vnoise(p*2.03+1.7); }
49float cloudShadowFactor(vec3 wp, vec3 s){
50 if (s.y <= 0.05) return 1.0;
51 float t = (CLOUD_MID - wp.y) / s.y;
52 if (t <= 0.0) return 1.0;
53 vec3 cp = wp + s * t;
54 vec3 q = cp*0.0045 + vec3(time*0.06, time*0.04, time*0.045); // match sky.frag cloud animation
55 q += 0.7*(fbm2(q*0.55) - 0.5); // approx domain warp (match sky)
56 float d = smoothstep(1.0-COVERAGE, 1.0-COVERAGE*0.25, fbm2(q));
57 return 1.0 - clamp(d * cloudShadow, 0.0, 0.8);
58}
59
60// Sun shadow map: 1 = lit, 0 = fully shadowed. 3x3 PCF with slope-scaled bias.
61const float SHADOW_TEXEL = 1.0 / 2048.0;
62float sunShadow(vec4 lightPos, float ndotl) {
63 vec3 lp = lightPos.xyz / lightPos.w; // Vulkan clip: xy in [-1,1], z in [0,1]
64 vec2 uv = lp.xy * 0.5 + 0.5;
65 if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || lp.z > 1.0 || lp.z < 0.0) return 1.0;
66 float bias = max(0.0016 * (1.0 - ndotl), 0.0004);
67 float cur = lp.z - bias;
68 float sum = 0.0;
69 for (int y = -1; y <= 1; y++) {
70 for (int x = -1; x <= 1; x++) {
71 float d = texture(shadowMap, uv + vec2(x, y) * SHADOW_TEXEL).r;
72 sum += cur <= d ? 1.0 : 0.0;
73 }
74 }
75 return sum / 9.0;
76}
77
78void main() {
79 // Wrap the per-block uv into this face's atlas tile so greedy-merged faces tile (no stretch).
80 vec2 fuv = fract(fragUv);
81 float tile = fragTile;
82 vec4 texColor;
83 vec3 albedo;
84 if (tile > 999.0) {
85 // Grass side: dirt base + the tinted grass_block_side_overlay fringe, composited in one quad
86 // (perfectly aligned with the top face, no z-fight offset).
87 vec2 dirtUV = vec2(mod(1.0, ATLAS_COLS) * TILE_SPAN.x, floor(1.0 / ATLAS_COLS + 0.001) * TILE_SPAN.y) + fuv * TILE_SPAN;
88 vec2 ovUV = vec2(mod(16.0, ATLAS_COLS) * TILE_SPAN.x, floor(16.0 / ATLAS_COLS + 0.001) * TILE_SPAN.y) + fuv * TILE_SPAN;
89 vec3 dirt = texture(atlasTex, dirtUV).rgb;
90 vec4 ov = texture(atlasTex, ovUV);
91 albedo = mix(dirt, ov.rgb * fragColor, ov.a);
92 texColor = vec4(albedo, 1.0);
93 } else {
94 vec2 origin = vec2(mod(tile, ATLAS_COLS) * TILE_SPAN.x, floor(tile / ATLAS_COLS + 0.001) * TILE_SPAN.y);
95 texColor = texture(atlasTex, origin + fuv * TILE_SPAN);
96 if (texColor.a < 0.05) discard;
97 albedo = texColor.rgb * fragColor;
98 }
99
100 vec3 n = normalize(fragNormal); // true per-face normal (stable regardless of camera)
101 vec3 s = normalize(sunDir);
102
103 float NdotL = max(dot(n, s), 0.0);
104 // Sun visibility = cloud shadow * shadow-map (terrain self/occluder shadows).
105 float shadow = cloudShadowFactor(fragWorldPos, s) * sunShadow(fragLightPos, NdotL);
106 // Hemisphere ambient: sky above is brighter than the ground bounce below.
107 float hemi = 0.5 + 0.5 * n.y;
108 vec3 ambient = ambientColor * mix(0.55, 1.0, hemi);
109 float aoFactor = mix(0.5, 1.0, fragAo); // per-corner ambient occlusion
110
111 // Dynamic lighting: sky exposure gates sun+ambient (so caves/overhangs go dark), block light is
112 // additive emissive (torches/glowstone glow warm). Tiny floor keeps unlit areas barely visible.
113 float sky = mod(fragLight, 16.0) / 15.0;
114 float blockL = floor(fragLight / 16.0) / 15.0;
115 vec3 blockLightCol = vec3(1.0, 0.82, 0.5) * (blockL * blockL);
116 vec3 lit = albedo * ((sunColor * NdotL * shadow + ambient) * sky + blockLightCol + vec3(0.02)) * aoFactor;
117 // Night Vision (_pad2): lift dark areas to a visible floor.
118 if (_pad2 > 0.5) { lit = max(lit, albedo * 0.62 * aoFactor); }
119
120 float fog = 1.0 - exp(-fragDist * fogDensity);
121 fog = clamp(fog, 0.0, 0.85);
122 vec3 col = mix(lit, fogColor, fog);
123
124 // Underwater: tint deep blue and darken with distance.
125 if (playerPos.w > 0.5) {
126 float uw = clamp(1.0 - exp(-fragDist * 0.05), 0.0, 0.85);
127 col = mix(col, vec3(0.05, 0.20, 0.32), uw) * 0.75;
128 }
129
130 outColor = vec4(col, texColor.a); // linear HDR; tonemap happens in the composite pass
131}