Territorial map in 3D space

I making a new unity interface for my 4X game in space and I am looking for a solution to draw a territorial map.

Since my game old interface was written with javascript and using html canvas, I calculate the border polygon points of each territory and then just call to functions that allow the painting of the polygon border and fill with a color.

I’ve been thinking on using line renderer to paint the territory border, but I cannt use it to fill the polygon with a color. The most accurate solution seems to be creating a mesh with the border points, use some function that creates the triangles for that mesh and then use any material/shader to paint the territory.

However, there is another different approach. I’ve been thinking on creating just circular zones from each solar system and paint them (this would also need a mesh). The problem is how to paint the border where two different territories overlap.

Any suggestions to implementthis would be appreciated.

I spent about 3 years researching this particular problem and in the end had to get the help of two other programmers in order to implement it in Unity.

This should save you a lot of trouble but just be warned, it is not straightforward and part of that is because Unity didn’t like the maths that we were throwing at it. For my game, I wanted to do a ring and stop them from overlapping while going over terrain but you could just as easily do a sphere or any other shape with some tweaking but that would rely on you properly understanding what’s being implemented here.

As you can see, I went all over the place in more ways than one with my questions :smile: most of the methods I found on this particular border effect problem were extremely outdated and no longer really worked for modern version of unity but this should.

1 Like

There is yet another approach. You could use a voronoi map to assign each “star system” its territory. But you should distinguish your 2 issues/problems/tasks here. 1) how to define the “territory” of a star and 2) how to display it.

Just pick the closest star.
My suggestion is, to use a “large” texture on a quad laid about your “galaxy” map. Then just calculate the position of each pixel, find out which star is closest (this can be done once at initialization since it does not change), look up it’s color/owner and set the pixel to it. So there is no need for a custom mesh. You should use a distance threshhold so stars at the border don’t fill up the texture up until it’s border. That is an easy and relatively simple solution which solves many “issues”. It just depends on the size of your map and the texture resolution (it should not look pixelated).

Anyway, it’s advisable to use one mesh for all stars. Not each star it’s own territory mesh.

1 Like

Hi Lethn,

I had already read your posts. Very interesting and also you did a good job telling others how you were solving your problem. In your case, you are using 3D and your border has a height and it is composed by triangles, it is a 2D problem in my case and I think there must be an easier solution even though I use meshes.

1 Like

Great advices exiguous:

I’m going to research about the voronoi map. The idea of using one single mesh for the whole map is very good. I will show you my advances.

Thank you very much.

Just to be clear. It could be as easy as having a quad (a custom one, Unity’s built in quad has many vertices) and a texture on it. The “reference” star could be stored in a byte array (if you have no more than 255 stars, else short or int). So you calculate once the closest star, set it’s “index” in this array (which has the same resolution as your texture) and when you want to change the color of a star you iterate the array, look for star’s index and when you find it you set the color in an an accompanying color array. This is then used to set your texture (you should not manipulate texture pixels directly for performance reasons).
This is the simplest method I can think of. You can always go more complex if you need it.

As for voronoi there are some libraries out there to use. But these handle only the “which region belongs to which star” part. Then you still have the issue of how to display it. But creating triangles from the center to 2 adjacent corners should work for mesh generation. But I’m not sure if the corner points are returned in the correct order or if there needs to be done some decision logic (and keep triangle winding order in mind).

Good luck. I would apreciate when you post a screenshot.

If you have further issues/questions, feel free to ask.

1 Like

It’s also worth pointing out if we’re looking purely at 2D there are more options that I found like fog of war and 2D metaballs for blending in territories the way you want it so they might also be worth looking up. In fact I’m trying to remember but I think there might be some even simpler mask effects you can use for what you want in 2D.

Cutout masks! That’s what I was thinking of.

1 Like

I’m using 3D, I meant I dont need to use the Y axis, but still I need to use 3D tools.

Thank you for pointing that tools out .

Since my map has no limits and it is proceduraly generated, I cannt use one single texture, but thinking on your solution, I could just use different textures that cover one sector of a X size. I would generate as many sectores and textures as needed. However, it is possible that I have bad connections betwen textures, especially If I try to paint a border line or something like that.

Even if you’re using ‘3D models’ etc. you could potentially still get away with a 2D overlay I would think as long as your camera angle isn’t changing and so on, just something to consider so you don’t make life too complicated for yourself. Games developers use cheats like this all the time, even the big companies with billions, as long as you get the right effect and prevent gamers from no-clipping etc. out of the boundary no one will notice.

Thats right. And that would have been an important information in your initial post ;).

That would have been my suggestion too.

Will the player be able to see all sectors at once (I doubt it when there are “unlimited”). If he just can see one sector create this and the surrounding ones. When camera shifts reuse the ones which became invisble.

Not necessarily. I would suggest to calculate the position in space for a pixel, then search the star positions for the 2 closest ones. Then you can compare their distances. A pixel very close to the star will have its color. When the pixel is close enough to 2 stars (it’s border) you draw it differently.
I just wonder how you “store” the stars when there are infinite ones. So you also need a method to create the stars of a sector on demand.

As you can see the exact requirements and conditions determine which solutions are suitable and which not. So it is necessary to know them to provide suggestions.

Anyway. If you don’t know it look up the starmap of the game “Stellaris”. Albeit it produces some artifacts maybe thats the direction you want to head?

Also I’m not too deep in shaders (I have used a compute shader to draw hexagons at vertex positions and color them). But maybe there is an even simpler solution achievable with them. So I suggest you ask in the shader subforum (best with a screenshot of the effect you desire) if anyone knows how to achieve this. Also I would make a search as probably someone already has done something similar so you can get inspiration and hints and don’t need to start from scratch.

The camera can move like most strategy games. From the top, not fixed angle. In any case, I will look deeper into the information you provided, but in the first look I wasn’t able to find the use in this problem.

You help is very appreciated in any case.
Thank you very much

You are right. To be honest, I wasnt looking to someone who made my job, just some suggestions to point me in the right direction and I didnt want to write too much information.

Exactly, you can see everything in the beggining when the map is not very extense, but in the late game you are just able to see some regions.

Perhaps this is the solution for borders, I will have to paint those borders to see if they look ok.

All the information is stored in a database. Data is requested from time to time to “update” the political map, so I receive a json with a list of stars and star data (position, magnitude, name, etc…)

A political map like Stellaris is exactly what I am looking for, but I wasnt able to find enough information about it.

Dividing the space in sectors might be a solution, but perhaps I could make a mesh where each point is a vertex and every star is in the position of one of the vertex. As you said, I would calculate the distance from every vertex to surrounding stars and I would “color” the vertex using a shader. If this is possible I could create space ust adding new vertex to the giant mesh that behaves like terrain.

I also forgot to mention that I am using an hexagonal grid, so the vertex positions are not in a regular rectangle. This could be solved using a mesh because I could place each vertex in the center of each hexagon.


[EDIT] This last idea of using a mesh would have very low resolution (unless I create more triangles per hexagon) and also would not the solve the problem of paiting borders.

Since I already have a function that calculates the territory borders points, I have use those points to draw the territory borders using line renderer.

1 Like

I’ve been doing some research on the “Cutout masks”. I havent found the way to use it yet, but this might be the simplest solution. It seems that I would need to create a mesh with the territorial border shape that “masks” a simple plane where the map would be rendered. Every mask uses a shader that could do anything, i.e: “change color”.

Thanks again for your help.

[EDIT] Thinking about it… if I am able to make a mesh with the territory shape I can just shader it with a color and no need to use a mask :smile:.

This seems to be what I am looking for:
https://www.habrador.com/tutorials/math/10-triangulation/

1 Like

It’s all about experimentation, as you’re discovering, it’s just one hell of a rabbit hole you can go down. I’m a big fan of these kinds of territory effects as well and I was surprised there was so little up to date information available on implementing them, Stellaris and Elite Dangerous’ power play map are pretty nice influence map implementations. Interestingly, I’ve yet to see any kind of modern day game engine implementation of the old settlers border maps.