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

Modern-Apps.git / library / map / src / main / rust / shaders / terrain.vert

terrain.vert68 lines · 3.6 KiBRaw
1#version 450
2
3// Terrain: the DEM-displaced ground grid (WS-G, 3D terrain relief).
4//
5// Like the building vertex, a terrain vertex carries a height and a normal — but no per-vertex
6// colour: the whole ground is one style colour (the `earth` layer's), pushed per draw, and the
7// relief is conveyed by shading that colour against the normal. See `tess::terrain` for the layout.
8//
9// The incoming height is **tile-normalised** (a fraction of the tile's ground width, the same unit
10// x/y are) so the grid is zoom-independent like every other tile mesh. `line.x` carries the tile's
11// world-px span for this frame; multiplying the two recovers the real world-px height the WS0
12// perspective matrix expects in its `z` input. At pitch 0 that matrix ignores z for x/y, so the
13// grid reads as the flat tile footprint.
14//
15// Lighting runs per fragment: the normal goes out on `vNormal` and the raw tile-local position
16// on `vTilePos`, so terrain.frag can shade the Lambert term and the Blinn-Phong specular against
17// the tile-local eye `misc.xyz` pushes per draw — no per-vertex scalar, so ridgelines stay sharp
18// however the grid is tessellated.
19
20layout(location = 0) in vec3 inPosition; // tile-local (u, v, height); u,v in 0..1
21layout(location = 1) in vec3 inNormal; // surface normal in the same tile-local space
22
23layout(push_constant) uniform Push {
24 // Tile-local (u, v, height, 1) to clip space — the WS0 matrix. `height` is world-px.
25 mat4 tileToClip;
26 vec4 color;
27 // x: the tile's world-px span this frame, the tile-norm-height -> world-px scale (WS-G).
28 // y, z, w: unused by the terrain path.
29 vec4 line;
30 vec4 misc;
31 vec4 morph;
32} push;
33
34// Placed just short of the far plane so, at pitch 0, the ground sits behind every building (which
35// write depths in [0, 0.99]) rather than in front of them — see the pitch-0 branch below.
36const float ORTHO_GROUND_DEPTH = 0.9995;
37
38layout(location = 0) out vec3 vNormal;
39layout(location = 1) out vec3 vTilePos;
40
41void main() {
42 float worldHeight = inPosition.z * push.line.x;
43 // The height column's w term (tileToClip[2][3]) is 0 on the ortho fast-path (pitch 0) and
44 // -cos(pitch) once tilted — the same test the building shader uses to tell the two apart.
45 bool ortho = abs(push.tileToClip[2][3]) < 1e-6;
46
47 // The normal in tile-local space, forwarded to the fragment stage for per-fragment Lambert
48 // (mirroring building.frag). Shared by both branches so relief shades at pitch 0 too —
49 // previously the ortho branch forced vShade = 1.0 and hills were invisible top-down.
50 vNormal = inNormal;
51 // The raw tile-local position, forwarded alongside the normal so the fragment stage can
52 // build the view vector against the pushed tile-local eye for the specular term.
53 vTilePos = inPosition;
54
55 if (ortho) {
56 // Pitch 0: the ortho matrix never lets height touch x/y, so the grid already reads as the
57 // flat tile footprint. Feed a fixed near-far depth instead of the terrain height so the
58 // ground never wins the depth test against the buildings drawn over it (they write small
59 // depths near the front); the flat 2D layers are depth-off and paint over it regardless.
60 gl_Position = push.tileToClip * vec4(inPosition.xy, 0.0, 1.0);
61 gl_Position.z = ORTHO_GROUND_DEPTH * gl_Position.w;
62 } else {
63 // Tilted: the full perspective maps a real world-px height to both the projected rise and a
64 // depth inside the near/far range, so hills rise, occlude what is behind them, and let
65 // buildings on the far side of a ridge be hidden by it.
66 gl_Position = push.tileToClip * vec4(inPosition.xy, worldHeight, 1.0);
67 }
68}