Terrain and splines

Is there a way to use unity spline and terrain to make smooth path and then place a road onto terrain using splines again? :slightly_smiling_face:

I m only able to use spline mesh then scale it down so before placing points so i can make a road,but road is thin and small bumps in terrain are causing issues

There’s no easy built-in way to do this (now that we can talk a bit about the fact that the current Terrain system is going to be replaced, it may be more obvious why not).

That said, it’s not too hard to do using existing tools. I think the approach I’d take would be something like this, which would do most of the work on the GPU. The shaders here should all be doable with shadergraphs.

  1. Captured a top-down view using an orthographic camera that matched the size of the terrain, using the terrain height setting to set the camera’s depth bounds. Set the resolution of the rendertexture to the resolution of the terrain mesh.
  2. Render the road meshes into a rendertexture on that camera using a shader that captured the depth into one channel and an on/off mask into another. Note that if you’re camera is looking down you’ll need to reverse the depth values, “0” will be higher than “1” because the camera looks down!
  3. (optional, but probably a good idea) I’d do a post-pass with a second shader to add some extra blur on the heights and the mask to provide a soft falloff. A simple gaussian blur a few pixels wide would probably be fine.
  4. Read the original heightmap from the terrain using the .heightMapTexture on your terrainData
  5. Make a new rendertexture, and use a shader to blend the your heigh-and-mask texture with the heights you got in step (4) to make a new combined height. You may run into the height encoding issue in this thread which is a simple one line fix.
  6. Now you terrain should match your splines, but the “grain” of the terrain may be too coarse to match the actual rendered height of your splines. You can try two different fixes: either offset the blended height by some small amount when you do the blend in (5) so that the terrain is a little bit below the splines or you can just use the mask you created in step 3 to make terrain holes using the SetHoles() method on the terrain.

The main drawback to the way I’ve sketched it out here is that this is a destructive operation, if you decide to resculpt the terrain or move the roads you’ll need to do it again.

1 Like