Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by theUndeadEmo · Sep 18, 2012 at 09:34 AM · textureflip

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image theUndeadEmo · Sep 18, 2012 at 10:10 AM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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;
     }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pjbaron · Apr 19, 2021 at 02:19 AM 1
Share

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();
         }
avatar image silentreaver pjbaron · Apr 19, 2021 at 01:51 PM 0
Share

Ace! Thank you

avatar image
0

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Sep 18, 2012 at 02:52 PM 1
Share

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 !!

avatar image The-Arc-Games · Sep 18, 2012 at 03:55 PM -1
Share

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.

avatar image Fattie · Sep 18, 2012 at 06:37 PM 0
Share

ok, unfortunately I do not understand what is being said here. Hopefully he'll find a solution. Cheers!!!

avatar image chanfort · Nov 02, 2014 at 08:18 PM 1
Share

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;
     }
 
avatar image
0

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




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges