- Home /
What is the best way I could recolor all the pixels of a certain country on a texture of a political map(for achieving a map mode system such as in a grand strategy game)?
I'm using a texture of a political map where all the countries are painted with a unique color. I can assign data and know what the pointer is clicking/hovering over. So an example of an overlay would be:
Germany is color coded gray and has a high GDP. The shader draws dark blue where all the gray colored pixels are in the texture to show a high average GDP in the area of Germany. Moldova is color coded green and has a low GDP. The shader draws light blue where all the green colored pixels are in the texture to show a low average GDP in the area of Moldova. The darkness of the blue is determined by a dynamic GDP value from the data of a country. It would be very preferable if the texture of the map remains in 1 piece due to the normal map(hills and depressions) An answer to this would be to have a conditional in the shader per color of the color coded countries, but it would be extremely slow. Another answer would be to somehow flood fill all the countries and recolor them in order to create a new texture, but it would also be slow.
Looking to achieve such an effect as demonstrated in this video about Crusader Kings 2 map modes https://www.youtube.com/watch?v=hllUdi73h70
Answer by Pangamini · Apr 15, 2019 at 12:45 PM
The problem with color coding (Eg. using the exact numeric value to mark a country) means you cannot use any kind of filtering (like bilinear) or mipmaps. The interpoloation would generate invalid IDs. But IF you are OK with that (which I doubt), you could use an array of colors instead of conditionals (pick the color from the texture, calculate the array index, pick the color from the array). But you still have to implement the filtering in the shader (or have no filtering), which may be slow too.
I think the best option here would be to have separate single-channel texture for every country (also this doesn't have to mean that each country texture covers the whole world, you'd just map it to the correct position). Your reason for not splitting the map is "due to the normal map". Can you elaborate on that?
Also, have you considered using a geometry (mesh) ins$$anonymous$$d of the texture to represent the country shape?
What I mean by "normal map" is https://eu4.paradoxwikis.com/$$anonymous$$ap_modding#Normal_map Figured it would be the best to use large world maps representing various things like in the aforementioned link for greater flexibility. I'm trying to make a grand-strategy game, so why not try to emulate how the pros are doing it.
I understand what a normal map is, but I don't see why is it relevant in your question?
It is relevant to my question because I would like to find a way to do the aforementioned map modes, without sacrificing the flexibility of the system being able to dynamically piece together the political map, the normal map, rainfall map... and others from images.
I could of course do it more manually with single channel textures, but I'm just trying to find out if anyone has an idea about the less manual way.
Hopefully you have your own texture maps and you do not use those copyrighted images ^^
Answer by Bunny83 · Apr 15, 2019 at 04:05 PM
Actually recoloring your map isn't that expensive. You can use my Floodfill extension, however since you want to fill in several regions at once a slightly modified version would be better so you don't have to use GetPixels / SetPixels for each area you want to fill. You usually don't have to update this texture that often, only when you change the settings
It is possible to do this completely with shaders, however this requires that your color codes are an actual encoding of some sort of country index. There was a similar question some years ago about creating a selectable map. To do the overlay you just have to encode the desired tint color in a lookup texture where each pixel in that texture corresponds to one country. So in the shader you can use the color encoded index of a pixel of your map to lookup the desired tint color in the lookup texture. This would be done in the fragment shader for each pixel. All you have to do when you want to change the tinting, is to replace / edit the lookup texture. The texture could be a 1 pixel high and x pixel wide texture where x is the number of countries / destinct regions you have. However it's usually better to create a square texture and just encode the indices row by row. With a 32x32 texture you can encode 1024 distinct areas. So a 256x256 is enough for 64k.
Since we don't know how you actually have color coded your countries / area it's up to you to implement this for your data.
Your answer
Follow this Question
Related Questions
Apply Shader to overlay GUI 0 Answers
How to curve country meshes based upon a flat projection, into a sphere? 2 Answers
Two shaders for one model 3 Answers
Google API in unity? 0 Answers