Drawing

Vector lines, polygons and points

A map is mostly vector data: roads and rivers are lines, lakes and parks are polygons, shops are points. Their shape comes from the data, but their look comes from a style: a road is 7 pixels wide at any zoom. This page shows how to draw them on the GPU: lines spread from their center line in the vertex shader, joins and caps, smooth edges from the distance to them, polygons with holes, points, the order they are drawn in, how they lie on terrain, and what culling must do for widths measured in pixels. The last part shows how RASM does it.

In short

  • A line's vertices keep its center line, and an offset in half widths. The vertex shader multiplies the offset by the style's width, so a style changes without a rebuild.
  • Where two segments meet, a join fills the gap (miter, bevel or round); the ends get a cap (butt, square or round).
  • Each fragment knows its distance to the edge. One pixel's change of it (fwidth) gives a smooth edge, at any width and angle.
  • A line is never drawn under one pixel: a thinner one keeps one pixel and fades by its coverage, so it neither vanishes nor shimmers.
  • A width is in pixels or in metres, spread across the screen or in the ground's plane.
  • Polygons with holes are cut into triangles by ear clipping. Points are quads with a round edge.
  • Layers are drawn in a set order, over the ground by a small depth bias, or over everything.
  • On terrain, features are cut on the terrain's grid, so each piece lies flat on one terrain triangle.
  • Culling must pad a feature's bounds by its width in pixels at its distance, or wide lines vanish at the screen's edge.

1The problem

A GPU draws triangles. A road is a list of points, and its width is a number of pixels on screen: as the camera zooms out, the road keeps its 7 pixels while the world shrinks. So the triangles of a line depend on the camera. Rebuilding them on the CPU every frame is slow; building them once in world units makes them too thin far away and too wide near.

Triangles in world units

near: wide far: under a pixel, gaps

The width follows the perspective: a far road is thinner than a pixel and breaks up.

A center line and offsets

offset y = −1 … +1, times half the width

The data is built once; the vertex shader spreads it by the width every frame.

2A line as a center line and offsets

Each segment of the line becomes a quad of four vertices. All four sit on the center line, two at each end. Each also has an offset (x along the segment, y across it), in half widths: −1 on one side, +1 on the other. The vertex shader puts the vertex at anchor + (x · along + y · across) · half width.

AB (0, +1)(0, +1) (0, −1)(0, −1) half width a fragment's |y| is its distance from the center line, in half widths: 1 is the edge
The CPU builds this once: anchors and offsets. The width lives in the style, read by the shader, so changing a road's width costs nothing.

3Joins and caps

Two quads that meet at an angle leave a wedge open on the outer side of the turn. A join fills it. The ends of a line get a cap.

Miter

The outer edges meet in a point. A sharp turn makes a long spike, so past a limit it becomes a bevel.

Bevel

The two outer corners joined by one triangle.

Round

A fan of triangles around the joint; its edge is round to the pixel.

Drag the middle point and change the join. Make the turn sharp with a miter and watch it fall back to a bevel.

4Smooth edges, and never thinner than a pixel

Each vertex hands its offset to the fragments, interpolated. A fragment's distance to the edge is |y| on the body, the length of (x, y) in a round join or cap, the larger of |x| and |y| in a square cap. The geometry reaches one pixel past the edge, and the fragment covers what is inside:

coverage = saturate((1 − distance) / fwidth(distance) + 0.5)

distance
in half widths: 1 at the edge
fwidth
how much the distance changes over one pixel: the smooth band is one pixel wide at any width, distance and angle

A line of 0.3 pixels, drawn as it is

Only the pixels whose centers it crosses are hit: dashes that crawl and shimmer.

One pixel, faded to 0.3

Every pixel along it gets a little: a faint, steady line. MSAA and TAA then have nothing to fight.

5Pixels or metres; the screen or the ground

Pixels

A road of a map style: the same width at any zoom. The world width is the pixel width times the pixel's size at that depth.

Metres

A river 40 m wide, as it is in the world. Far away it reaches the one-pixel minimum and fades.

a band on the screen

Across the screen

The offset is turned to the screen: the line never foreshortens. For 3D paths, routes, markers.

In the ground's plane

The band lies on the ground: a road draped on hills. Seen edge-on it gets thinner, down to the minimum.

6Polygons with holes

A lake is an outline with islands inside, each a ring of points. Ear clipping cuts it into triangles: it joins each hole to the outline by a bridge, so the shape is one ring, then cuts off "ears", triangles made of three neighbour points with no other point inside, until nothing is left. A polygon's own edges are triangle edges, so MSAA or TAA smooths them.

outline and an island a bridge makes one ring ears: triangles around the hole
Ear clipping takes O(n²) at worst; with a spatial index of the points, as earcut has, it is fast on real map data with thousands of points.

7Points and markers

A point is a quad around its anchor, its four corners at offsets (±1, ±1). The fragment's distance is the length of its offset, so the disc is round and smooth; a ring along its edge is the same distance test at 1 − ring width. An icon is the same quad with texture coordinates from an atlas.

8Draw order and depth

Map layers overlap on purpose: a road over a park over the ground. All lie in the same plane, so the depth test cannot order them. They are blended with their smooth edges, in a fixed order of layers, each one a little nearer than the ground by a depth bias. An overlay (a route, a selection) is drawn at the near plane, over everything.

A marker at its anchor's depth

lower half under the ground

A quad that faces the screen stands up from the ground: below its anchor the ground is nearer and hides it.

The ground's depth under each corner

corners at the ground's depth

Each corner takes the depth of the ground under its pixel, through the ground plane at the anchor, when that is nearer.

9Draping on terrain

A road on hilly terrain must follow the drawn ground exactly, or it sinks into a hill or floats over a valley. The terrain is drawn as two triangles per cell, each flat. So cut every segment where it crosses a grid line or a cell's diagonal, and lift each new point by the terrain's own rule: then each piece lies flat on one terrain triangle. Polygons are cut the same way.

cut at x = k, z = k and x + z = k cut: on the ground everywhere; uncut (dashed): through the hills
The heights come from the same samples and the same rule the terrain shader uses, so the line and the ground agree to the bit at each cut, and a tiny depth bias is enough.

That holds for the line's middle. A road also has a width, and the shader spreads it in the plane of the triangle under the middle. Where the road is wider than a cell, its sides reach over the next triangles, and where the ground bends up beside the road (a valley's floor, the inside of a turn), it rises above that plane and covers the road's edges in wedges. So a road whose width is known in metres is draped whole: build its outline on the flat grid, at its full width, with its joins and caps, then cut those triangles on the grid like a polygon and lift every corner. Each corner keeps where it is across the road (its offset, for the smooth edge and the outline) and along it (for the dashes).

middle draped: edges covered the band's plane is under the ground at its sides whole width draped cut where it crosses the grid, each piece lifted
The width is built into the mesh, so it cannot change with the zoom. A width in pixels keeps the draped middle and is drawn nearer by its half width in the world, a few pixels' worth; so is a marker.

The smooth edge still reaches a pixel past the road. The shader moves each corner out by that pixel in the plane of its own triangle, and draws the road nearer by as much as it moved, so the pixel does not sink into a slope.

10Culling a width in pixels

A line's bounding sphere is built from its center line. A line 40 pixels wide whose center line is just off the screen still covers 20 pixels of it. A pixel is larger far away, so the pad grows with depth: pad the sphere by the widest offset in pixels times the size of a pixel at the sphere's far side, for each view. The occlusion test then uses the padded sphere too.

the screen center line's sphere: outside padded by half the width: kept
Without the pad, the line pops in only when its center line enters the screen. The pad is per view: an orthographic map view has the same pixel size everywhere, a perspective one a larger one far away.

11Motion for TAA

TAA moves each pixel's history by the motion of what is in it. A smooth edge is part line, part ground: the line writes its anchor's motion scaled by its coverage, and the blend keeps the rest of the motion behind it. So a line over the sky, the ground or a moving object reprojects without a smear.

12Pitfalls

Thin lines break into crawling dashes
They are thinner than a pixel. Keep one pixel and fade by the coverage.
Wide lines vanish at the screen's edge
Culling uses the center line's bounds. Pad by the width in pixels at the far side, per view.
Markers and roads are cut in half on a tilted map
A quad facing the screen sinks into the ground below its anchor. Give each corner the depth of the ground under it.
A see-through line is darker at its joints
Its quads and joins overlap on the inner side of a turn and blend twice. Draw see-through lines opaque, or as one layer with a lower alpha.
Roads sink into hills or float over valleys
The line is not cut on the terrain's grid, or the terrain is drawn from other heights than the ones used to drape.
A wide road on hills is shredded at its edges
Only its middle is draped; its width is flat and the ground beside it rises above it. Drape the whole width: cut the road's outline on the grid, like a polygon.
Layers flicker against each other
Same depth, no order. Draw layers in a fixed order, blended, with a depth bias against the ground only.
Mesh simplification ruins the lines
LODs and meshlets are for meshes whose vertices stay where they are. Build vector meshes without them.

13In RASM

A layer of vector features is an ordinary entity: a mesh built by the vector builders (include/rasm/vector.h), drawn with a MaterialType::VECTOR material whose VectorMaterial is the style. So it goes through the GPU culling, the Transparent pass's sort, TAA and MSAA like any blended mesh, and is a TileStreamer tile's content like any other. Labels are vector features too: a fourth kind of vertex, the glyph, with its own page, Text and labels.

Pixel sizes in an export In a tiled export's pipelines (RASM_EXPORT_VIEW) sizes given in pixels are multiplied by SceneData::pixelScale (getPixelScale), its ExportDesc::pixelScale, which also divides the LOD's pixels per unit; in every other view it is 1 and the shaders do no work for it. Sizes in metres do not change.
any thread the renderer's thread shaders
any thread addVectorLinesaddVectorPolygonsaddVectorPoints VectorGeometry: anchors,offsets, kinds buildVectorMesha MeshBuild renderer createMesh(build)only uploads entity at the origin,VECTOR material material row:VectorData shaders cull.slang: pad, no shadow,overlay never occluded sort.slang: drawOrder,then far to near getVectorVertex shadeVector
The builders touch no renderer, so a map's tiles are built on its own threads end to end; the main thread only uploads (P30's fix: buildMesh and createMesh(MeshBuild&&), for any mesh).
// From examples/25_vector_map/main.cpp: a road layer, then a tile's roads built on a server thread.
rasm::MaterialHandle roads = makeLayer(engine, {.color = glm::vec4(1.0f, 0.75f, 0.25f, 1.0f), .width = 7.0f, .outlineColor = glm::vec4(0.55f, 0.32f, 0.08f, 1.0f), .outlineWidth = 1.5f, .drawOrder = -10});
// ... on a server thread:
rasm::VectorGeometry geometry;
rasm::addVectorLines(geometry, origin, points.data(), sizes.data(), static_cast<uint32_t>(sizes.size()), {.join = rasm::LineJoin::ROUND, .cap = rasm::LineCap::ROUND});
data.layers[ROADS] = rasm::buildVectorMesh(std::move(geometry));
// ... on the main thread, when the tile arrives:
const rasm::MeshHandle mesh = engine.createMesh(std::move(data.layers[ROADS]));
const rasm::EntityHandle entity = engine.createEntity("layer", mesh, roads, rasm::Transform{origin});

Where RASM follows this page, and where it differs

TopicThis pageRASM
The vertexanchor and offsetVertex: pos the anchor, normal the ground's up, tangent the direction the offset is measured from (w: the feature id's bits, exact for any 32-bit id), uv[0] the offset, uv[1] the distance along the line and, at a join, its turn, or an icon's coordinates, uv[2] the kind and the distance metric (shaders/lib/vector.slang).
Joins, capsmiter, bevel, round; butt, square, roundLineShape: LineJoin, LineCap, miterLimit up to MAX_VECTOR_MITER (2 half widths). Each miter triangle is in its own segment's frame, so the distance across is exact on both sides; a bevel is measured round from the joint. A join's vertices carry the turn on the ground: across the screen the shader turns each offset to the screen's angle (getScreenJoinOffset), so the join meets the next segment under any camera.
Smooth edgesdistance and fwidthThe geometry reaches one pixel past the edge; getVectorCoverage. The minimum is one pixel, faded below (fade). In the ground's plane, a band seen edge-on under half a pixel widens to half a pixel and fades.
Stylewidth, units, planeVectorMaterial: color, width, widthUnit (PIXELS, METRES), facing (SCREEN, GROUND), dashLength, gapLength, outlineColor, outlineWidth, drawOrder, depth (TESTED, OVERLAY). Dashes in pixels follow the pixel's size at each vertex, so along a line that runs into the distance they are close, not exact.
Polygonsear clippingearcut (mapbox/earcut.hpp, vcpkg earcut-hpp, header-only), in the ground's plane through the origin, or across the tile when draped. No smooth edges of their own: MSAA or TAA.
Order and depthlayers in order, a bias, overlaysVectors are always in the blend run. The Transparent pass sorts by (VECTOR_ORDER_SCENE + drawOrder, then far to near); the scene's see-through draws sort at order 0. A tested feature takes the depth of a point moved toward the camera by 1/4096 of its distance (VECTOR_DEPTH_BIAS), so the bias holds for perspective, orthographic and finite-far cameras; a screen-facing corner takes the ground's point under it when that is nearer (getGroundPoint); a line or point whose width is in pixels is drawn nearer again by its half width in the world, since the ground beside its anchor may rise above the plane it spreads in; an overlay, and a label whose anchor the Label Depth pass found in view (Text and labels), is at the near plane and never occlusion culled. The order holds while the GPU blend sort runs: with frozen culling the blend run is drawn in list order.
Drapingcut on the gridVectorPlacement::drapeTile and drapeHeights: cut at x = k, z = k and x + z = k (the grid's split), lifted by getTerrainPosition, with the triangle's normal as the ground's up.
A road's whole widththe outline cut on the gridLineShape::drapeWidth (metres, 0 by default): a draped line is built that wide on the flat grid by the same joins and caps (pushLine), then each triangle is cut on the grid and each corner lifted (pushDrapedLine, on a lattice of 2-20 cells, so pieces that meet lift a point the same). What reaches past the tile is cut at its edge: give each tile the lines within half the width of it, and the neighbour draws the rest. Its vertices are VECTOR_KIND_BAND: pos on the ground, uv[0] the offset interpolated in its triangle, uv[1] the distance along and the built half width. The shader moves a corner only by its smooth edge's pixel (or up to a pixel's width when the band is thinner), in its own ground's plane, and draws it nearer by that pixel's move (not by the widening of a band seen edge-on); it draws the built width whatever the material's width, widthUnit and facing (the outline and the dashes keep the material's unit), so a new width takes a rebuild. Bug B35: a road wider than a cell, draped by its middle only, was covered at its edges where the ground bends up.
Cullingpad per viewgetWorldSphere adds getVectorPadding for a VECTOR material: 2 half widths and 3 pixels at the sphere's far side (in metres for a metre width), in the view's own pixel size, so the frustum test, the Hi-Z test, the blend sort and the culling debug view all see it. Shadow maps leave vectors out (VIEW_DEPTH_ONLY); vector draws are drawn whole, never by meshlets.
Data linesa color per pointaddVectorLines' colors or scalars, one per point: every vertex a point makes takes its value (VectorGeometry::colors, the mesh's vertex colors); lines without values beside them are white. With VectorMaterial::colorMode SCALAR the vertex carries the scalar, the line interpolates it, and the pixel maps it through colormap and range (getVectorColor in rasm.slang; Values through a colormap). A draped band takes no values.
Motionthe anchor's, by coveragetoVectorOutput writes the anchor's motion times alpha; the premultiplied blend keeps the rest of what is behind.
1.6 msthe longest frame of 25_vector_map after the first 30, 1500 frames, two views (TAA and 4× MSAA)Release, headless, RTX 3060
7layers in a tile, one draw each: parks, water, river, streets, roads, rail, placesmaterials shared by every tile
0meshes built on the main thread: the tiles' meshes are built on 3 server threadscreateMesh(MeshBuild&&) only uploads

Where it lives in code

WhatWhere
The buildersaddVectorLines, addVectorPolygons, addVectorPoints, buildVectorMesh, VectorGeometry, VectorPlacement, LineShape in include/rasm/vector.h; src/scene/vector.cpp
The styleVectorMaterial, VectorUnit, VectorFacing, VectorDepth in include/rasm/material.h; gfx::VectorData in MaterialData, and a data line's colormap and range in its gfx::PointData
Meshes off the main threadbuildMesh, MeshBuild, MeshBuildOptions (include/rasm/mesh.h), Renderer::createMesh(MeshBuild&&) in src/assets/mesh.cpp
Shadersshaders/lib/vector.slang (getVectorVertex, shadeVector, getVectorPadding, getBlendOrder); rasm.slang; frustumCulling.slang, cull.slang, sort.slang
The C API (1.5)rasm_build_mesh, rasm_create_mesh_from_build, rasm_vector_*, rasm_vector_material in src/c/cVector.cpp
Teststests/vectorTests.cpp (offsets, joins, caps, sides, holes, draping on the drawn ground, a road draped across its width, points); image tests vectors, vectors_msaa4, vectors_taa (with a line just off screen that culling's pad keeps), draped, draped_msaa4, draped_taa (a road draped across its width and one in pixels on hills)
Examplesexamples/25_vector_map (a city's vector tiles, --culling for the culling debug view); examples/24_map_tiles (roads and lakes draped on the terrain); examples/26_picking (a road draped across its width, whose edges it picks from above; --fly, --no-aa)
Keep these true
  • src/scene/vector.cpp and shaders/lib/vector.slang agree on the vertex: the kinds, the metrics, the across direction cross(along, up).
  • No offset reaches past MAX_VECTOR_MITER half widths: culling's pad counts on it.
  • Vector meshes have no LODs and no meshlets (buildVectorMesh).
  • Every pass that draws a VECTOR material spreads its vertices, the pick pass too (Picking): it compiles vertMain with RASM_PICK and writes the feature id. The feature id's bits are in tangent.w: copy them, never compute with them.
  • A vertex behind the camera takes the sizes of one at the near plane (getSafeW): a line that crosses the near plane keeps a bounded width.
Not done yet

See-through lines blend twice where their pieces overlap (D45). Dashes in pixels are close, not exact, on a line that runs into the distance. Decoding vector tile files (MVT) is the application's.

Further reading