- Home /
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.
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 }
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).
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?
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.
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...
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;
}
*/
}
Your answer
Follow this Question
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