Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / shaders / terrain.frag
terrain.frag52 lines · 2.6 KiBRaw
| 1 | #version 450 |
| 2 | |
| 3 | // Terrain fragment: the ground colour, slope-shaded per fragment (WS-G, 3D terrain relief). |
| 4 | // |
| 5 | // The base colour is the `earth` style layer's, pushed per draw (linear RGBA, palette- and |
| 6 | // opacity-resolved by the renderer). The vertex stage forwards the surface normal and the |
| 7 | // tile-local position; lighting runs here against the same fixed north-west light as |
| 8 | // building.frag, so ridgelines shade sharply instead of interpolating a per-vertex scalar — |
| 9 | // and relief reads at pitch 0 too, where the old path forced vShade = 1.0. |
| 10 | // |
| 11 | // Over the Lambert term sits a Blinn-Phong specular: the half-vector between the view direction |
| 12 | // (fragment to the pushed tile-local eye in `misc.xyz`) and the light, raised to a shininess of |
| 13 | // 48 and scaled by 0.20. Tilted views gain a soft sun glint off facing slopes; at pitch 0 the |
| 14 | // specular is skipped outright (the ortho fast-path test, same as the vertex stage's), so the |
| 15 | // overhead map is byte-identical to the Lambert-only output. The total is clamped at 1.0 so the |
| 16 | // highlight tints rather than blowing out to white. Blending is the pipeline's job, so the |
| 17 | // colour passes through with its own alpha. |
| 18 | |
| 19 | layout(location = 0) in vec3 vNormal; |
| 20 | layout(location = 1) in vec3 vTilePos; |
| 21 | |
| 22 | layout(push_constant) uniform Push { |
| 23 | mat4 tileToClip; |
| 24 | vec4 color; // the earth layer's colour, linear RGBA |
| 25 | vec4 line; |
| 26 | // xyz: the tile-local eye position (u, v in 0..1, height in tile-norm units), pushed per |
| 27 | // terrain draw; w: the per-frame clock, unread on this path. |
| 28 | vec4 misc; |
| 29 | vec4 morph; |
| 30 | } push; |
| 31 | |
| 32 | layout(location = 0) out vec4 outColor; |
| 33 | |
| 34 | void main() { |
| 35 | // A fixed light from above and the north-west, in tile-local space (x east, y south, z up), |
| 36 | // so slopes facing it read brighter and the far sides of hills sit a shade darker. |
| 37 | vec3 n = normalize(vNormal); |
| 38 | vec3 lightDir = normalize(vec3(-0.4, -0.4, 1.0)); |
| 39 | float diffuse = max(dot(n, lightDir), 0.0); |
| 40 | // Generous ambient so a shadowed slope stays legible rather than going black. |
| 41 | float light = 0.55 + 0.45 * diffuse; |
| 42 | // Tilted only: the ortho fast-path test (the height column's w term is 0 there). At pitch |
| 43 | // 0 the eye sits far above the tile and the highlight would be a flat lift, so skip it and |
| 44 | // keep the overhead map exactly the Lambert output. |
| 45 | if (abs(push.tileToClip[2][3]) >= 1e-6) { |
| 46 | vec3 viewDir = normalize(push.misc.xyz - vTilePos); |
| 47 | vec3 h = normalize(viewDir + lightDir); |
| 48 | float spec = pow(max(dot(n, h), 0.0), 48.0) * 0.20; |
| 49 | light = min(light + spec, 1.0); |
| 50 | } |
| 51 | outColor = vec4(push.color.rgb * light, push.color.a); |
| 52 | } |