- Home /
im trying to pull colour values from an image and it seems to be wrong
width = texture.width;
height = texture.height;
List<string> textDocument = new List<string>();
//texture.Resize(texture.width / 2, texture.height / 2);
for (int i = 0; i < texture.width; i++)
{
for (int y = 0; y < texture.height; y++)
{
//Position width by height
switch ((texture.GetPixel(i, y).r * 255) + "" + (texture.GetPixel(i, y).g * 255) + "" + (texture.GetPixel(i, y).b * 255))
{
case "83146":
case "86143":
case "86144":
case "85145":
case "828127":
case "77989":
case "82147":
case "81148":
case "81147":
case "84145":
case "816136":
case "811140":
case "80149":
case "821132":
case "833123":
case "833124":
case "747112":
case "78187":
case "814138":
case "822132":
case "82146":
case "737120":
case "837121":
case "818134":
case "813139":
case "739119":
case "810141":
case "820133":
case "845114":
case "77294":
case "826129":
case "832124":
case "736120":
case "739118":
case "831124":
case "846114":
//Water
markedTerritory.Add(new Vector3(i, y, 0));
Instantiate(water, new Vector3(i, y, 0), transform.rotation, grassParent.transform);
break;
case "79775":
case "79676":
case "712356":
case "713745":
case "714656":
case "711462":
case "710271":
case "710668":
case "79477":
case "713150":
case "712852":
case "710569":
case "713249":
case "713050":
case "713348":
case "713447":
case "713349":
case "713547":
case "79577":
case "713845":
case "713546":
case "710966":
case "710371":
//Grass
Instantiate(grassObject, new Vector3(i, y, 0), transform.rotation, grassParent.transform);
break;
default:
//Other colours
//Color chosenColor = new Color(texture.GetPixel(i, y).r, texture.GetPixel(i, y).g, texture.GetPixel(i, y).b, 1.0f);
//Debug.Log(chosenColor.ToString());
textDocument.Add(texture.GetPixel(i, y).r * 255 + " " + texture.GetPixel(i, y).g * 255 + " " + texture.GetPixel(i, y).b * 255);
//Instantiate(grassObject, new Vector3(i, y, 0), transform.rotation, grassParent.transform);
break;
}
//Check colours
//Debug.Log(texture.GetPixel(i, y).r * 255 + " " + texture.GetPixel(i, y).g * 255 + " " + texture.GetPixel(i, y).b * 255 + " At location: " + i + y);
}
}
This is the code avaliable for this as you can see im trying to pull many colour values from an image the image is located here https://cdn.discordapp.com/attachments/483090636002820106/585776440860082186/map1.31.png the colours that it outputs are here https://cdn.discordapp.com/attachments/483090636002820106/586498821287182337/Levels.txt
Answer by Bunny83 · Jun 08, 2019 at 12:39 AM
There are several things more than strange in your setup. First of all your image is not a power of two texture which can cause all sorts of issues because your image would be resampled to the next power of two unless you specify a certain non power of two texture mode during import. It's generally recommended to use power of two textures. If you really need a non power of two texture, make sure you import it correctly.
Next thing is your image actually has only two solid colors
7 132 49 // green
8 3 146 // blue
None of those is in your strange color list. Where does this list of colors come from? Are those the values that you read using your strange code? If that's the case you most likely suffer from a resampled texture.
Next is the way you translate your color values to a string results in ambiguities since you just concatenate decimal values. For example "818134" could be produced by the colors "8 18 134" or "81 81 34" or "8 181 34"
Using GetPixel in a loop has several disadvantages. First of all it's slow and already recommended in the documentation that it should not be used in a loop. It's way faster to use GetPixels and even faster to use GetPixels32 and iterate over the returned array. Since you also convert each color component into a byte range, using GetPixels32 would make the most sense since it already returns the colors as Color32 values which represent each component as one byte (range 0-255).
So it seems you actually have only two color values. It would be way faster and optimized when you compare the actual Color / Color32 values in an if statement rather then converting each one into a string.
So when you fixed your texture import you should be able to use:
Color32 blue = new Color32(8, 3, 146);
Color32 green = new Color32(7, 132, 49);
Color32[] colors = texture.GetPixels32();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int i = x + y * width;
Color c = colors[i];
if (c == blue)
{
// water
}
else if (c == green)
{
// grass
}
}
}
Your answer
Follow this Question
Related Questions
Graphic issue in voxel map 0 Answers
Duplicated objects, different colors 1 Answer
Changing Mesh Vertex Colors in editor 0 Answers
Get Average Colour From Texture? 1 Answer
How do I make a colour editor UI? 1 Answer