flipping texture

been searching the forums and threads, can’t find much detail on this.

possible to flip/mirror a texture on a gameobject, without using scale -1. it causes some problems.

Sadly no solution is going to be really efficient without going into threading, which you may or may not be able to do, as to flip the texture you have to move all the pixels in the image around to truly “flip” the image. I’ve attempted to create the most efficient method for flipping a texture horizontally. This avoids performing multiple get or set pixels, and processes the pixels 2 at a time. This should work for your project:

 public static Texture2D FlipTexture(this Texture2D original)
    {
        int textureWidth = original.width;
        int textureHeight = original.height;
    
        Color[] colorArray = original.GetPixels();
                   
        for (int j = 0; j < textureHeight; j++)
        {
            int rowStart = 0;
            int rowEnd = textureWidth - 1;
    
            while (rowStart < rowEnd)
            {
                Color hold = colorArray[(j * textureWidth) + (rowStart)];
                colorArray[(j * textureWidth) + (rowStart)] = colorArray[(j * textureWidth) + (rowEnd)];
                colorArray[(j * textureWidth) + (rowEnd)] = hold;
                rowStart++;
                rowEnd--;
            }
        }
                  
        Texture2D finalFlippedTexture = new Texture2D(original.width, original.height);
        finalFlippedTexture.SetPixels(colorArray);
        finalFlippedTexture.Apply();
    
        return finalFlippedTexture;
    }

If you want to rotate the image 180 degrees or flip both ways then just reverse the pixels array:

public static void FlipTexture(ref Texture2D texture)
{
    Color[] pixels = texture.GetPixels();
    Array.Reverse(pixels);
    texture.SetPixels(pixels);
}

Hi @theUndeadEmo,

If you can explain the problems you are having with switching scales. That would be really helpful for anyone else looking to flip their render texture using scale.


Coming back to your issue, if you don’t want to scale your render texture object, then you can use a compute shader to flip the render texture in almost realtime, and apply it back to the render texture you are showing.


Check this blog out, which explains creating a compute shader for flipping a texture: Christian Mills - How to Flip an Image With a Compute Shader

Technically speaking, you can’t flip a texture. You can flip an image, but a texture you can only ‘map differently’.
This means that, since the texturing is done through UV coordinates, if you don’t alter the image, you need to alter the UV coordinates.

Since performance wise this is a heavy procedure (must be performed for every vertex in the mesh), you’re probably better off with a custom editor that takes a texture, and duplicates it reversed using getpixel and setpixel!