good day!
I want to reflect a pixel array. I cant seem to find a formula for this anywhere. i tried using Reverse() but that just rotates the array 180.
Thanks!
good day!
I want to reflect a pixel array. I cant seem to find a formula for this anywhere. i tried using Reverse() but that just rotates the array 180.
Thanks!
GetPixels returns a single dimension array, to flip it you will have to reverse HEIGHT sections of WIDTH pixels each within the array.
i think i get what you are saying. any references for learning this? or maybe how to create a 2d array?
so maybe for a 128 x 128 it is something like get an array of item 0, 129, …etc? then reverse each one of those? wowzers
GetPixels returns a 1D array, so for 128x128 you would have 16,384 entries in the array. Reverse the first 128 using System.Array.Reverse, then the next 128, etc. Just use a loop, it’s simple.
–Eric
so help from a friend i got this and it works in case anyone else ever needs it.(or me in case i need to search it)
Color[] texColors;
int row = -1;
int width = 128;
void Flip()
{
Texture2D tex = new Texture2D (128, 128, TextureFormat.RGB24, false);
tex.ReadPixels (new Rect(0, 0, 128, 128), 0, 0);
yield return new WaitForEndOfFrame();
tex.Apply ();
texColors = tex.GetPixels();
//Create 2d array
Color [,] bigPicture = new Color[128, 128];
row = -1;
//Assign 1d array to 2d array
for( int i = 0; i < 128 * 128; ++i )
{
if ( i % width == 0 ) ++row;
bigPicture[row, i % 128] = texColors[i];
}
Color [,] flippedX = new Color[128, 128];
//flip 2d array
for( int i = 0; i < 128; ++i )
{
for( int j = 0; j < 128; ++j)
{
flippedX[128 - 1 - i, j] = bigPicture[i, j];
}
}
row = -1;
//convert back to 1d array
for( int i = 0; i < 128 * 128; ++i )
{
if ( i % 128 == 0 ) ++row;
texColors[i] = flippedX[row, i % 128];
}
tempFull.SetPixels(texColors, false);
tempFull.Apply();
}
You can just flip it in-place using the technique I mentioned; not very efficient to copy to a 2D array and back.
–Eric
well, its for an editor script. but it does seem like over kill. forgive me but i am somewhat new to this sort of heavy lifting. do you have an example?
System.Array.Reverse (texColors, 0, 128);
That would reverse the first row. Use a loop to go through all the rows, where the first integer in Reverse is the index of the array to start and the second is the number of items to reverse. Also, use GetPixels32 and an array of Color32 instead, since it’s faster and uses 4X less RAM compared to Color.
–Eric
how do you determine the axis?
You can only reverse the X axis by using Array.Reverse. You’d have to write your own routine for reversing the Y axis since the array entries in that case are not contiguous.
–Eric
public enum ReflectionAxis { X, Y }
public static void Reflect(this Texture2D texture, ReflectionAxis reflectionAxis)
{
Color32[] originalPixels = texture.GetPixels32();
if (reflectionAxis == ReflectionAxis.X)
originalPixels = originalPixels.Reverse().ToArray();
var reflectedPixels = Enumerable.Range(0, texture.height).SelectMany(
row => originalPixels.Skip(row * texture.width).Take(texture.width).Reverse()
);
texture.SetPixels32( reflectedPixels.ToArray() );
texture.Apply();
}
Or maybe
public static void FlipPixels(this Texture2D texture, bool flipX, bool flipY) {
Color32[] originalPixels = texture.GetPixels32();
var flippedPixels =
Enumerable.Range(0, texture.width * texture.height)
.Select(index => {
int x = index % texture.width;
int y = index / texture.width;
if (flipX)
x = texture.width - 1 - x;
if (flipY)
y = texture.height - 1 - y;
return originalPixels[y * texture.width + x];
}
);
texture.SetPixels32( flippedPixels.ToArray() );
texture.Apply();
}
What’s faster?
I don’t suppose simply using texture scale of -1 will be a sufficient solution?
Is there a good reason for the inverted X AND Y of GetPixels and SetPixels, compared to XNA etc?
You could maybe consider the Y coords “inverted”, but not X. In any case, (0, 0) is lower-left for UV mapping and (1, 1) is upper-right, so it makes that textures would be organized the same way.
–Eric
to save anyone some typing, to vertically mirror:
Color[] pix = photo.GetPixels(startAcross,0, 256,256);
if (needsToVerticallyMirror)
{
kRows .. number of rows (256 in example)
kCols .. number of cols (256 in example)
for(int row=0;row<kRows;++row)
System.Array.Reverse(pix, row*kCols, kCols);
}
I typically just push it all into a 2D array and then mess with it as I choose.
And to hell with caring about the GC