Unity 180 rotation for a Texture2D, or maybe flip both
Asked Answered
M

3

5

I could use just a little help. I am loading a png into a Texture2D, and have managed to flip it over the y axis using the following script I found. I need to flip it over the x axis now. I know a small modification should do it, but I have not managed to get the results I want.

    Texture2D FlipTexture(Texture2D original){
    Texture2D flipped = new Texture2D(original.width,original.height);

    int xN = original.width;
    int yN = original.height;

    for(int i=0;i<xN;i++){
        for(int j=0;j<yN;j++){
            flipped.SetPixel(xN-i-1, j, original.GetPixel(i,j));
        }
    }

    flipped.Apply();

    return flipped;
}
Metalloid answered 11/3, 2016 at 21:56 Comment(3)
you mean mirror to the x-axis?Winfredwinfrey
The image is upside down and I want it right side up. Yes mirror is another way to say it.Metalloid
All I did was swap the xN-i-1 and j then swap i,j and it works.Winfredwinfrey
V
9

say "pix" is a png,

Texture2D photo;
Color[] pix = photo.GetPixels(startAcross,0, 256,256);
// (256 is just an example size)

this ENTIRELY ROTATES a png 180 degrees

System.Array.Reverse(pix, 0, pix.Length);

this mirrors a PNG just around the upright axis

        for(int row=0;row<256;++row)
            System.Array.Reverse(pix, row*256, 256);
Ventilation answered 11/3, 2016 at 22:12 Comment(3)
Thank you for the response. I want to flip it from upside down to right side up now. Shouldn't I be able to make a small change to the indexes of my current code to do that?Metalloid
I do not want to turn it. It is the orientation I want, but the image is upside down.Metalloid
Deleting my current code, and reversing the array ended up solving my problem. A 180 degree rotation was the equivalent of an x and y flip.Metalloid
W
4
Texture2D FlipTexture(Texture2D original, bool upSideDown = true)
{

    Texture2D flipped = new Texture2D(original.width, original.height);

    int xN = original.width;
    int yN = original.height;


    for (int i = 0; i < xN; i++)
    {
        for (int j = 0; j < yN; j++)
        {
            if (upSideDown)
            {
                flipped.SetPixel(j, xN - i - 1, original.GetPixel(j, i));
            }
            else
            {
                flipped.SetPixel(xN - i - 1, j, original.GetPixel(i, j));
            }
        }
    }
    flipped.Apply();

    return flipped;
}

To call it:

FlipTexture(camTexture, true); //Upside down

FlipTexture(camTexture, false); //Sideways

Winfredwinfrey answered 11/3, 2016 at 22:17 Comment(5)
That is what I did. The result was a rotationMetalloid
Mine is not. It flips it. I tested it with an image and I know the difference between rotation and mirroring. Just copy this code and call it. Post a picture of before and after in your answer and I will show you that is is NOT rotationWinfredwinfrey
So maybe this changes thing. I am doing this twice. Once over the x and once over the y. One seems to be undoing the other however.Metalloid
Do it once like you should then you get an object mirrored in the y-axis. upside down. Also I did it twice. Doing it twice will only undo it and make the image look like the original image. You are probably modifying the image somewhere else.Winfredwinfrey
Update. If you just want it to mirror upside down, use FlipTexture(camTexture, true);. If you want it to mirror sideways use FlipTexture(camTexture, false);. The reason you are seeing rotation is because you are doing FlipTexture(camTexture, true); followed by FlipTexture(camTexture, false); . So it flips the image up side down then flips it again to sideways. Just use FlipTexture(camTexture, true); for upside down and call FlipTexture(camTexture, true); again to go back to the original image.Winfredwinfrey
A
2

This flips the texture upside down:

int width = texture.width;
int height = texture.height;
Texture2D snap = new Texture2D(width, height);
Color[] pixels = texture.GetPixels();
Color[] pixelsFlipped = new Color[pixels.Length];

for (int i = 0; i < height; i++)
{
    Array.Copy(pixels, i*width, pixelsFlipped, (height-i-1) * width , width);
}

snap.SetPixels(pixelsFlipped);
snap.Apply();
Armure answered 7/6, 2019 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.