- Home /
GetPixel optimisation
I have the following code in my program:
for (int y = 0; y < provinces.height; y++)
{
for(int x = 0; x < provinces.width; x++)
{
int R = (int)((255 * provinces.GetPixel(x, y).r));
int G = (int)((255 * provinces.GetPixel(x, y).g));
int B = (int)((255 * provinces.GetPixel(x, y).b));
// some light function with these RGB values here
}
}
Everything works well on small scale like 300x300 map, but when I try the intended map size, which is 5k by 2k, this function takes 17 minutes to process everything and I feel like there should be a better way to get RGB values without calling GetPixel 3 times.
So, is it possible to optimise the first 3 lines, where I get each pixel's colour?
Answer by Bunny83 · Oct 26, 2019 at 02:59 PM
Have you read the GetPixel documentation? It already states that if you read a large block of pixels you should use GetPixels / GetPixels32 instead. Since you seem to need the colors in the byte range anyways using GetPixels32 would make the most sense. In tight loops you also want to avoid using properties continuously. Get the values once and cache them:
int width = provinces.width;
int height = provinces.height;
Color32[] colors = provinces.GetPixels32();
for (int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
Color32 col = colors[x + y * width]
int R = col.r;
int G = col.g;
int B = col.b;
// some light function with these RGB values here
}
}
Note that as long as your "light function" does not depend on any other shared state you could run the loop even on a seperate thread. Of course if you do this you can not use most part of the Unity engine. So you can not touch gameobjects, materials, textures, etc. But If everything you do can be stored in simple types / structures you can actually off-load the work to background threads.
Thanks, this worked and reduced loading time by about 30% on a small scale example.
This part of the code is used only once during the program start, so I am already planning on how to multi-thread it, as some of the systems can be independently loaded into memory before game start.
I did read about GetPixels32, but I couldn't make it work correctly before.
On big scale, where the image is 5k by 2k, the load time decreased significantly and went from 17 $$anonymous$$utes to 7.5 $$anonymous$$utes. Thanks again.
7 $$anonymous$$utes? What on earth are you doing with those pixels? You either have a very complex algorithm, a really bad implementation or a really bad PC ^^. You probably can boost performance by inlining all of your code. Have a look at my example over here. It's just a simple bilinear interpolation routine. However after inlining the code it ran about 8 times faster. Though even the straight forward implementation only took 17 seconds for an image larger than yours.
I am creating a fully modable map for my strategy game. It takes multiple images and creates the world map based on them. This part of the code is for the provinces on the world map. Each province has an associated unique colour with it, so this code is taking each pixel and attaches it to the relevant province. As each province has non-uniform size and non-uniform shape, I have to check every pixel and then add them to their provinces. Here is the part of the code that was omitted:
if (oldRGB.Equals(new RGB(R, G, B)))
{
newAreas.Add(new ProvinceArea(x, y));
}
else
{
for (int z = 0; z < newAreas.Count; z++)
{
ProvinceList.First(item => item.rgb.Equals(oldRGB)).addToArea(newAreas[z]);
}
oldRGB = new RGB(R, G, B);
newAreas.Clear();
newAreas.Add(new ProvinceArea(x, y));
}
Answer by Hellium · Oct 26, 2019 at 02:51 PM
Color[] pixels = provinces.GetPixels();
int width = provinces.width;
int height = provinces.height;
for (int index = 0 ; index < pixels.Length ; ++index)
{
int x = index % width;
int y = index / width
int R = (int)((255 * pixels[index].r));
int G = (int)((255 * pixels[index].g));
int B = (int)((255 * pixels[index].b));
// some light function with these RGB values here
}
Your answer
Follow this Question
Related Questions
How can I check how many pixels on a texture are transparent without tanking performance? 4 Answers
GetPixel not working on iPhone 1 Answer
The non-trivial problem with image processing. 1 Answer
Dynamically created texture is off in BFE... 0 Answers
WebcamTexture.GetPixel() bug on Android? 0 Answers