Highlightable Tiles on Unity Terrain

Unity v2021.3
URP

I have ground created via Unity terrain and a grid system for it. I want to highlight tiles just like in the below image with the red squares.
I originally thought that I could use a square object that lies on top of the ground as a highlight but that does not work well with sloping ground.
Is there a way I can do something like the red tile highlighting in the image but it will wrap around the contours of terrain?

A few different ways to approach this - the naive approach which may be absolutely fine for your needs is to use Decals and ‘project’ an image onto the ground. Here’s the docs for URP and here’s the docs for the standard renderer. On extreme slopes it’ll really stretch in a visibly unpleasant way, so that’s the downside.

A slightly more involved way is to process the terrain and calculate and generate mesh data for each tile. If you have access to the height map data of the terrain, and the mesh you’re generating is essentially a flat plane that you’re deforming, then it’s not too complicated to procedurally generate that mesh data with the right vertex positions. Here’s some of the basics of procedural mesh generation to get you started. With that mesh data you’re just populating a mesh filter and have a mesh renderer + material render it, like any other 3d object in your scene.

Thank you for your reply.
The decal way is very intuitive but I have further questions about the 2nd way with the renderers.
I will preface my question by saying that I do not know much about the mesh/material renderer.
If I already have 3 variants of terrain (cobblestone, grass, road) and 3 variants of the tile highlight effect I want (red, orange, green).
Does that mean I would have to create 3x3=9 combinations of materials (red tinted cobblestone, red tinted grass, etc.) to apply to the mesh?
I understand with decals I can just plop a highlight effect onto the mesh but I have no idea what I would be doing with this 2nd method.

Regarding the second approach, no, it wouldn’t be 9 combinations you’re concerned about, the mesh renderer would ideally have a transparent material and shader applied that would apply the desired coloured highlight to the terrain beneath. So, just like the decals do. Like when you have a layer in Photoshop with the effect set to ‘Overlay’, it would wash the cobblestone/grass/road colours with red/orange/green.

Perhaps give the decal method a try first, perhaps that’s all that you need. I think the two ways that Unity gives you to do decals (projectors in SRP and decals in URP) give you what I’ve just described for free.

1 Like