- Home /
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.
well aware of the photoshop flip, was thinking it will save the art $$anonymous$$m some time and the memory all the extra textures in the projects folder.
was hoping there was another solution similar to scale, -21
Answer by silentreaver · Aug 14, 2020 at 03:07 PM
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;
}
Thanks for that @silentreaver Here's a $$anonymous$$or modification which doesn't create a new Texture and using the faster pixel32 functions.
public static void FlipTexture(ref Texture2D texture)
{
int textureWidth = texture.width;
int textureHeight = texture.height;
Color32[] pixels = texture.GetPixels32();
for (int y = 0; y < textureHeight; y++)
{
int yo = y * textureWidth;
for (int il = yo, ir = yo + textureWidth - 1; il < ir; il++, ir--)
{
Color32 col = pixels[il];
pixels[il] = pixels[ir];
pixels[ir] = col;
}
}
texture.SetPixels32(pixels);
texture.Apply();
}
Answer by The-Arc-Games · Sep 18, 2012 at 01:37 PM
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!
huh? you flip a texture by setting the texture scale as -1
open unity, click on an object, and try it ?
Since performance wise this is a heavy procedure (must be performed for every vertex in the mesh
no, it's trivial. the UV matrix is constantly being multiplied by the scale as mater of course. you just change the scale to a new value !!
You missed the part where the OP does not want to flip the image, and thus the necessity of doing it through the UV (of course you only change it once per mesh, but it's still heavy).
However you're absolutely right: in regards of duplicating the material, flipping the material's scale is an excellent way to do it.
This however requires you to reassign the material. If you need to flip the image, you need to manipulate it still.
ok, unfortunately I do not understand what is being said here. Hopefully he'll find a solution. Cheers!!!
You can try to flip from C# by calling this function:
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;
}
Answer by swapnil_unity613 · Apr 08 at 08:29 PM
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613081202im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
fliping texture through script issue 0 Answers
Assigning UV Map to model at runtime 0 Answers
How to get an object to turn around/flip at a certain point 2 Answers
Issue with normals when importing a .dae 0 Answers
Trouble with rotating/flipping textures 0 Answers