Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by magzh · Oct 26, 2019 at 02:38 PM · texture2dgetpixel

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image magzh · Oct 26, 2019 at 03:19 PM 0
Share

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.

avatar image magzh · Oct 26, 2019 at 03:41 PM 0
Share

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.

avatar image Bunny83 magzh · Oct 26, 2019 at 04:56 PM 0
Share

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.

avatar image magzh Bunny83 · Oct 26, 2019 at 05:54 PM 0
Share

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));
                 }
Show more comments
avatar image
0

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
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity crashes trying to get pixels and using Debug.Log 0 Answers

GetPixel in c# 1 Answer

Get set of Pixels from PolygonCollider2d Position 0 Answers

Steps in Texture2D after writing values to an array 1 Answer

getPixel co-ordinates not screen co-ordinates based on mouse position 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges