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

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

building.vert50 lines · 2.5 KiBRaw
1#version 450
2
3// Building: extruded 3D walls and roof caps (WS-A, OSM Simple 3D Buildings).
4//
5// Unlike the position-only fill vertex, a building vertex carries a height, a face normal
6// and its own colour — the three things flat 2D layers never need. See `tess::roof` for the
7// layout the CPU packs.
8//
9// The incoming height is **tile-normalised** (a fraction of the tile's ground width, the
10// same unit x/y are) so the mesh is zoom-independent like every other tile mesh. `line.x`
11// carries the tile's world-px span for this frame; multiplying the two recovers the real
12// world-px height the WS0 perspective matrix expects in its `z` input. At pitch 0 that matrix
13// ignores z for x/y, so a building drawn from directly overhead collapses to its footprint.
14
15layout(location = 0) in vec3 inPosition; // tile-local (u, v, height); u,v in 0..1
16layout(location = 1) in vec3 inNormal; // face normal in the same tile-local space
17layout(location = 2) in vec4 inColor; // per-vertex ARGB, R8G8B8A8_UNORM -> 0..1
18
19layout(push_constant) uniform Push {
20 // Tile-local (u, v, height, 1) to clip space — the WS0 matrix. `height` is world-px.
21 mat4 tileToClip;
22 vec4 color;
23 // x: the tile's world-px span this frame, the tile-norm-height -> world-px scale (WS-A).
24 // y, z, w: unused by the building path.
25 vec4 line;
26 vec4 misc;
27 vec4 morph;
28} push;
29
30layout(location = 0) out vec3 vNormal;
31layout(location = 1) out vec4 vColor;
32
33void main() {
34 float worldHeight = inPosition.z * push.line.x;
35 // The height column's w term (tileToClip[2][3]) is 0 on the ortho fast-path (pitch 0) and
36 // -cos(pitch) once tilted. That distinguishes the two, and they need different z inputs.
37 if (abs(push.tileToClip[2][3]) < 1e-6) {
38 // Pitch 0: the ortho matrix copies z straight to clip depth and never lets height touch
39 // x/y, so the building already reads as its flat footprint. Feed a depth that falls as the
40 // tile-normalised height rises, so where roofs overlap (a tower part inside its podium's
41 // outline) the taller one wins LESS. Kept in [0, 0.99], in front of the terrain's 0.9995.
42 gl_Position = push.tileToClip * vec4(inPosition.xy, 0.99 - clamp(inPosition.z, 0.0, 0.99), 1.0);
43 } else {
44 // Tilted: the full perspective maps a real world-px height to both the projected rise and a
45 // depth inside the near/far range, so buildings extrude and occlude correctly.
46 gl_Position = push.tileToClip * vec4(inPosition.xy, worldHeight, 1.0);
47 }
48 vNormal = inNormal;
49 vColor = inColor;
50}