top of page

How to Carve Roads and Rivers into a Terrain with Splines

11 minutes ago
5 min read

To carve roads into terrain with splines, you set every heightmap point near the spline to the road's height, fully on the road bed and less and less across its shoulders. The details are in how you measure "near", how the shoulders blend, and how you keep it fast.

This guide builds that tool from scratch in C# against Unity's TerrainData. The method itself does not depend on Unity, so the same steps work for any heightmap.

An orange spline drawn over hilly terrain, dipping under a ridge it would need to cut through
The spline before carving: it runs straight through the ridge

Step 1: Turn the knots into a polyline

People click knots onto the ground and expect the road to pass through every one. A Catmull-Rom spline does that. Sample it into a dense polyline, 16 points per segment is plenty, and work with straight segments from here on.

static Vector3 CatmullRom(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
    float t2 = t * t, t3 = t2 * t;
    return 0.5f * (2f * p1 + (p2 - p0) * t
                 + (2f * p0 - 5f * p1 + 4f * p2 - p3) * t2
                 + (3f * p1 - p0 - 3f * p2 + p3) * t3);
}

Step 2: Even out the road's grade

If each knot snaps to the ground under it, the road copies every bump. Average the polyline's heights with their neighbors for a few dozen passes, keeping both ends pinned. The road then climbs and descends at a steady rate, and the carve decides where to cut and where to fill.

for (int pass = 0; pass < passes; pass++)
for (int i = 1; i < line.Count - 1; i++)
{
    Vector3 p = line[i];
    p.y = (line[i - 1].y + p.y * 2f + line[i + 1].y) * 0.25f;
    line[i] = p;
}

Snap knots to the original ground, not the carved one. Otherwise each new carve starts from the last road.

Step 3: Decide the profile

Every heightmap point gets a weight from its distance to the center line: 1 on the road bed, a smooth falloff across the shoulder, 0 beyond it. The new height is lerp(ground, roadHeight, weight).

static float Weight(float distance, float halfWidth, float shoulder)
{
    if (distance <= halfWidth) return 1f;
    float t = Mathf.Clamp01((distance - halfWidth) / shoulder);
    return 1f - t * t * (3f - 2f * t); // smoothstep: no crease where the shoulder meets the ground
}

A linear falloff leaves a visible crease at both edges of the shoulder. Smoothstep is flat at both ends, so the cut blends in.

Cross-section across the road: the original ground slopes, the carved ground has a flat 8 m bed and curved shoulders rejoining the ground 18 m out
A real cross-section from the demo: flat bed, smoothstep shoulders

Step 4: Measure distance per segment, not per point

The obvious loop visits every heightmap point and tests it against every segment. On a 513 × 513 heightmap with an 81 point polyline, that took 7.4 seconds in our demo, because a diagonal road's bounding box covers almost the whole terrain.

Flip the loops. Each segment visits only the points within reach of itself (half width plus shoulder) and every point keeps its closest result:

Array.Fill(distance, float.MaxValue);
for (int i = 0; i < line.Count - 1; i++)
{
    // x0..x1, z0..z1: the cells inside this segment's box, widened by 'reach'
    for (int z = z0; z <= z1; z++)
    for (int x = x0; x <= x1; x++)
    {
        Vector2 p = CellPosition(x, z);
        float t = Mathf.Clamp01(Vector2.Dot(p - a, ab) / ab.sqrMagnitude);
        float d = Vector2.Distance(p, a + ab * t);
        int k = z * res + x;
        if (d < distance[k]) { distance[k] = d; roadY[k] = Mathf.Lerp(aY, bY, t); }
    }
}

Same heights, in 109 ms: about 68 times faster. The work now grows with the road's length, not the terrain's size. roadY is the height on the center line at the closest point, which keeps the road level from side to side.

Step 5: Carve from a copy

Blend each point toward its road height, reading from a saved copy of the untouched heightmap:

float w = Weight(distance[k], halfWidth, shoulder);
result[z, x] = Mathf.Lerp(source[z, x], roadHeightNormalized, w);

Always carve from source, never from the previous result. Then moving a knot and carving again replaces the road instead of stacking a second one on top. In Unity, heights are normalized to 0..1 of the terrain's height and indexed [z, x].

Paint the same distance field into a texture layer with a narrower bed, and the road gets its surface too.

The carved dirt road winding across the terrain
After carving and painting
Close view of the road cutting through a ridge, with smooth banks on both sides
Where the road meets the ridge, it cuts through with smooth banks

Rivers use the same code

A river is a road below the ground. Lower the center line by the water depth, widen the shoulder into banks, and make the height only go downhill: walk the polyline from the source and clamp each point to Mathf.Min(line[i].y, line[i - 1].y). Water never flows uphill, so neither should the bed.

Pitfalls

  • Road narrower than the heightmap. A 513 heightmap over 400 m is about 0.8 m per point. Keep the bed at least 4 points wide, or its edges stair step.

  • Self crossing splines. The closest segment wins, so a loop or a junction picks one road's height at random. Carve junctions as their own flat area.

  • Ends of the spline. Clamping t to 0..1 rounds the road's ends. Extend the first and last segment if you need a square end.

The ready made version: Vista

The tool above carves one road, on one terrain, once. A production version needs live preview, many splines, many terrains and a way to keep the road when the terrain changes.

Vista is a procedural terrain generator for Unity. You describe a terrain as a graph of nodes (noise, erosion, masks, texturing, trees and grass) and Vista generates it on the GPU onto Unity terrains, in the editor or at runtime. Splines plug into that graph.

Any spline tool. Draw the road with Unity Splines, Curvy, Dreamteck or Bezier Solution, then add the matching Vista spline evaluator to it. The evaluator gives the spline an Id and a width, and turns it into a ribbon mesh that Vista renders on the GPU: the same distance and falloff idea as above, without the loops. Add several evaluators to one spline for different jobs, such as a narrow one for the road and a wide one for clearing trees.

Nodes that read the spline by Id:

  • Ramp conforms the height to the spline with a falloff curve, takes an optional noise input to break up the shoulder edges, and outputs a ramp mask for texturing them.

  • Path paints a mask along the spline, for texturing the road.

  • Thin Out Along removes trees and grass on the road, or keeps only the ones on it.

  • Spline Extract exposes the raw data (mask, filled region, height, anchor and point positions) for anything else.

The Vista graph editor with a Ramp node between a mountain height input and the Height Output node
Vista's Ramp node in a terrain graph

Because the road is a node, it is part of every generation. Change anything upstream and the road is carved again into the new result. Splines are a Vista Pro feature.

FAQ

How wide should a road be compared to the heightmap resolution? At least 4 heightmap points across the flat bed. Narrower roads get jagged edges.

Why does my carved road look bumpy? The knots follow bumpy ground. Smooth the polyline's heights before carving.

Can I carve rivers with the same code? Yes. Lower the center line by the water depth and clamp its height so it only goes downhill.

How do I make spline carving fast? Loop over spline segments and only visit the heightmap points within reach of each one.

Try it

Copy the five steps into a script, point it at a terrain and drag some knots. For roads that regenerate with the terrain, see Vista.

Comments


  • Discord
  • Facebook
  • X
  • Youtube
  • Reddit
  • Pinterest

© 2015-2025, Pinwheel Studio. All Rights Reserved.

bottom of page