unity3d Texture2D的Unity 180旋转,或者两者都翻转

mnemlml8  于 2023-02-16  发布在  其他
关注(0)|答案(3)|浏览(386)

我需要一点帮助。我正在将一个png加载到Texture2D中,并且已经使用我找到的以下脚本在y轴上翻转了它。我现在需要在x轴上翻转它。我知道一个小的修改就可以做到这一点,但是我还没有得到我想要的结果。

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;
}
arknldoa

arknldoa1#

假设“PIX”是PNG,

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

这个可以把一个png旋转180度

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

这将围绕垂直轴镜像PNG

for(int row=0;row<256;++row)
            System.Array.Reverse(pix, row*256, 256);
kyvafyod

kyvafyod2#

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;
}

要调用它:
FlipTexture(camTexture, true);//倒置
FlipTexture(camTexture, false);//侧向

d5vmydt9

d5vmydt93#

这将上下翻转纹理:

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();

相关问题