Procedural Generate Mesh from Color Map

I’m trying to create a Strategy game with an interactive map like Hearts of Iron, Europa Universalis and so on. From the Wiki of Europa Universalis I found out that they achieve this by working with different layers of maps/textures that they then read data from and populate the game with.

So at the moment, I’ve already created a Color map of my world (Included in the post), and I use RayCasts to identify what color that I hit with my cursor, and which province/part of the world that color corresponds to.

But now I want to add Graphics and Image effects. For example, I want to highlight the province in the game when I move the mouse over it, or perhaps draw interactive borders around the provinces in-game.

As I understand it (Please correct me if I’m wrong), to do these things I need to have Mesh’s that are in the shape of my provinces in game. It’s not enough with just a Map Texture. So, I need to generate a mesh from my color map, that I can then wrap with borders, add UI elements to or highlight when it’s selected.

Where do I start out with this? I’ve found some tutorials of generating meshes in simple shapes such as Squares or Rectangles. But countries and provinces on a world map are not shaped as squares. They got very complex shapes.

Can anyone point a few fingers where I should start? Is there some functions where I can track the color of a Texture, get coordinates of it and then generate a Mesh out of that? I’m completely lost and I’d love some pointers.

are these areas going to change size/shape during the game?

if not you’re probably going to have a far easier time using a modelling package to cut up a plane with a knife tool using the the image as a “background” to “trace” and using the resultant meshes in your game

blender is free and would achieve this…

1 Like

Well, the provinces themselves will not be able to change their size or shape, however a Country will be made up by multiple Provinces. So the borders of the country will expand if the country acquire a new Province (However the shape of the mesh/province will not update).

texture2d has a variety of functions to access the pixel data, “GetPixel” and the like
http://docs.unity3d.com/ScriptReference/Texture2D.html

you could “brute force” it and go with 1 vertex per pixel, but you might want to consider something like quadtrees (start with a low res and work out where the edges are with increasing levels of detail).

not sure if it’s applicable or not, but there is the “marching squares” algorithm… might be of interest (even if its just for the mesh manipulation bits)
https://unity3d.com/learn/tutorials/modules/advanced/scripting/procedural-cave-generation-pt2

You could also make every edge pixel a vertex and use the triangulate script from the wiki to make it into a mesh.

I also feel like some shader magic might help. It should be possible to pass in a particular colour to a shader, along with your colour map, and say “highlight where this colour appears”. Of course I have no clue when it comes to shaders, so I can’t offer you more advice.