Here’s what I’m trying to do. I am trying to figure out how to create selectable territories in my game world with irregular borders. What I mean by this is I want to allow the user to click on the terrain and that will make whichever territory the point is in active.
My plan was to use an overhead ortho screenshot of my terrain as a base map. Then, in Photoshop, I created a new layer and drew borders to demark the different territories. Then I filled each territory in with its own distinct color. Next, I brought that colormap into Unity. Here’s how I was planning on making this work.
The player would click on a point on the terrain. The real terrain corrdinates (x,z) would then be converted into bilinear coordinates (ie. clicking dead center on the terain would return the bilinear coords of (.5,.5)). Using those coords, I would check it against the colormap. For example, I would use GetPixel(.5*,.5*) to return the Color on the colormap that corresponds with the point on the terrain that was clicked. In theory, I should be able to then say Color x is associated with Territory y, thus making Territory y the selected territory.
Now, this is where it isn’t working. In order to associate each territory with its corresponding color on the color map, this is what I tried:
var temp = territoryColorMap.GetPixels();
var prevColor : Color = temp[0];
var count : int = 1;
territories.Add(new Territory(count,temp[0]));
for(var p : Color in temp)
{
if(p != Color.black p != prevColor)
{
territories.Add(new Territory(count+1,p));
prevColor = p;
Debug.Log(p);
}
}
It doesn’t work as expected. As you can see, I was printing out each distinct color that it found. Even though, in Photoshop, the colormap colors are uniform in their RGBA values for each territory, GetPixels appears to be picking up varying values within single territories. For some reason, importing into Unity is resulting in the pixel color values changing. Granted, the pixel values are almost identical, but the fact that there are variances is causing my colormap to reflect 7000+ territories when I only have 9 distinct colors in the texture.
Now I have two questions. First, what do I need to do to make my imported texture have the same uniform color values as are reflected in Photoshop?
Second, the reason I went into the indepth explanation of my technique for creating selectable territories is that I am hoping someone will say, “You idiot!! Why go through all that trouble? This is an easy way to do it!!!”