- Home /
After placing a texture2D's pixel alpha value into an int[] array, later change that value without knowing xy coordinates?
I am trying to figure out something I have had problems with in the past, but unfortunately I cannot find the answer I had found before...
Anyway, I take a Texture2D with some transparent pixels, and create an int array (which I init by doing int[someTexture2D.GetPixels32().Length] & then assign the int value to match the alpha value) and with that array I have every pixels alpha value, which is either completely transparent or completely solid. This seems to work no problem for me and I can tell which are solid, which are not...
Now the problem is that later, I need to take specific pixels, and change them to transparent or solid, and then have the values in this array match. I just need to find the same individual value in the int array to change, based on a Vector 2 I need to change.
So I would like to utilize some poor developer testing art to help describe my problems:
Here you can see that the top left of the purple sprite (0, 64 in unity, 0, 0 in photoshop) and surrounding it is transparent, which I take this sprites texture2d, and assign all the alpha values to the int array (which again is either transparent or solid) and later I am planning on removing certain pixels somewhere from the sprite, so how can I then take the 1 dimensional int array (same length as there are pixels) and find that one exact pixel?... lets say 30, 34 - which is near the center. I can't just take x y because, well that would be wrong! Whats the math magic that is slipping my mind? Is there something as simple as counting how many times its gone over the width/height of the images pixels, maybe like x y / width, take remainder as x, or y...? I have a feeling this is one of those questions I will refer to later... many many times...
ADDITIONAL INFO:
-to assign alpha to the ints, I try to iterate through x, and then y within that iteration, and take x, y values a, assign to the int... hope that makes sense to you. Problem is I don't know which int[number] to assign, based on an x, y value!
wish I knew a better way to store the alpha value without referring to the texture2d itself (for speed reasons), and thought an int array would be faster, if I could just compare the int array with the vector2's of the texture2d...
come up with incredibly complex ways to ask simple questions.
found this page about a similar issue, does this comment apply to my problem:
SniperED007 said... Looks confusing, but its pretty simple:
Say you have an array of 5 wide and 3 down, this would be:
When creating the arry [width * height] = [5*3] = 15.
So now we want to compare the very first pixel which is [x=0,y=0]
This would be [x+y*width] = [0+0*5] = 0
The next pixel would be [x=1, y=0] This would then be [x+y*width] = [1+0*width] = 1 (remember BODMAS, so the Multiplication happens before the addition)
Hope that makes more sense? Basically you getting rid of the Y co-ordinate and making one array with the total number of pixels and then just finding the position in that array by using x + (y * width) July 5, 2008 11:51 AM
Answer by robertbu · Aug 26, 2014 at 05:38 AM
The 2D array starts in the lower left corner and walks up the image row by row ending in the upper right corner. So given and x,y coordinate, you can find the index into the 1D array by:
var index = x + y * textureWidth;
Going the other way, given an index into your 1D array you an do:
y = index / width;
x = index % width;
This is essentially what the quote you pulled says.
I was just testing that out now, thanks for confir$$anonymous$$g that link @robertbu!
Your answer
Follow this Question
Related Questions
How to get/pass (Vector2) arrays in a shader? 1 Answer
Issues with Pixel Distortion 0 Answers
Use world space for EdgeCollider2D in Script ? 0 Answers