Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by lmbarns · Apr 12, 2013 at 06:07 AM · arraypixelgetpixelsbyte

Coordinate of matching pixel on texture

So I'm using GetPixels on a Texture2D to get a Color[] array of pixels.

I loop through the pixels in the array, checking against a color I want to search for, and setting it to transparent (0,0,0,0), then return it.

 Texture2D RemoveColor(Color c, Texture2D imgs){        
     Color[] pixels = imgs.GetPixels(0, 0, imgs.width, imgs.height, 0);
             for(int p = 0; p < pixels.Length; p++){ 
                    if(pixels[p].r == c.r && pixels[p].g == c.g){ 
                 Debug.Log (p);
                 pixels[p] = new Color(0,0,0,0);                    
                     }
             }             
         imgs.SetPixels(0, 0, imgs.width, imgs.height, pixels, 0);
         imgs.Apply ();
         return imgs;
         }    


Now when I debug 'p' I get numbers like 123,222, so pixels[123222] will be transparent and is the one I want to monitor, but is there any way to figure out what the x,y position would be on the screen if the texture filled the screen, based off knowing which pixel index in a byte array you want to find, and how many there are total in the array???

I was thinking about adding the matching pixels to another array the same size as the original texture's array rather than setting them transparent, and somehow utilize them from there but it all comes down to the same problem of translating it into x,y coordinates.

Comment
Add comment · Show 1
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 lmbarns · Apr 12, 2013 at 06:44 PM 0
Share

One other thing maybe you'll come back here, but I'm using the same script on 2 computers and one computer has Unity 3.4.2f and the other has Unity 4. For some reason they work differently when looping through the pixels in the byte array.

On unity 3.4.2f, if(pixels[p]== c){ //this fires correctly }

On unity 4 the same code does not fire...I've tried numerous combinations and the only way I can get it to fire is to compare individual colors like (pixels[p].r == c.r && pixels[p].b == c.b){ //this fires but finds some pixels I don't want }

2 Replies

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

Answer by Eric5h5 · Apr 12, 2013 at 06:29 AM

Just divide the index number by the width to get the y, and the remainder is the x. By the way, it's better to use GetPixels32 and SetPixels32 (faster, 4X less RAM usage).

Comment
Add comment · Show 4 · 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 Fattie · Apr 12, 2013 at 07:31 AM 0
Share

awesome "32" tip, I had no idea. that's worth $400! ;-)

avatar image lmbarns · Apr 12, 2013 at 06:10 PM 0
Share

For some reason when I convert every color[] to Color32[] and GetPixels32() and SetPixels32() it was setting all the pixels to (0,0,0,255). By remainder do you mean the difference between the total num of pixels and the index? So if the index is 100 and there are 300 total pixels, and the texture width is 1000, the y would be 10, (100/1000), and the x would be 5, (300-100)/1000?

avatar image Eric5h5 · Apr 12, 2013 at 09:42 PM 0
Share

Color32 works as expected when I've used it. By remainder I mean what's left over when you divide the index by the width. You can't have a texture width of 1000 if there are only 300 pixels...if you have a 10x20 image and the index is 47, then 47/10 = 4 (remember this is integer division), and the remainder is 7.

avatar image jister · Jun 02, 2014 at 04:25 PM 0
Share

then what about for your first row? you'll get division by zero... since 0/10 = 0 , the remainder 0 % 0/10 will give an error...

avatar image
1

Answer by Anisoropos · Jul 05, 2021 at 03:17 PM

 Color[] pixels = texture.GetPixels();
 
 for (int i = 0; i < pixels.Length; i++)
 {
     int x = i % texture.width;
     int y = i / texture.width;
 
     Vector2 coords01 = new Vector2(
         (float)x / (texture.width - 1),
         (float)y / (texture.height - 1));

     // Debug
     /*
     Color cA = texture.GetPixel(x, y);
     Color cB = pixels[i];
 
     if (cA != cB)
     {
         Debug.LogError("i: " + i + " x: " + x + " y: " + y);
         break;
     }
     */
 }
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

14 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

Related Questions

I can't see a reason for this Array.Reverse() error 3 Answers

convert string to byte array 3 Answers

C# convert Bytearray to System.Draw.Image 2 Answers

Custom Datatypes for optimization? 3 Answers

Use SetPixels, but dont set unvisible Pixels 1 Answer


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