• 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
2
Question by Tyler 10 · Jul 15, 2011 at 06:58 PM · texture2duvcopypaste

Copy Paste multiple Texture2d's Into New Texture2d

Hey guys, thanks for looking at my question.

I want to take say a shirt from one texture 2d and the skin of another, and copy them onto the same texture at runtime. I see ReadPixels()/SetPixels() in the script reference but I think these will overwrite all the pixels.

Does anyone have experience with merging texture2d's?

I need something like ActionScript 3 has - copyPixels(blendMode etc. . . ). Any ideas? Thanks again for reading!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Tyler 10 · Jul 20, 2011 at 05:09 PM

If anyone finds this question while searching I figured it out. I wrote this function to combine textures. I am using this to copy different shirt textures and tattoo textures onto one skin texture. You basically just get the pixels from each map and then set your new map to the combination of the two using the second maps alpha as the lerp % between pixel a and pixel b. The maps need to be read/write enabled and be RGBA32 and the same height and width. You could easily write something generate the mips and all that as well if one was so inclined. Thats all I got gentlemen.

 public static Texture2D CombineTextures(Texture2D aBaseTexture, Texture2D aToCopyTexture)
 {
     int aWidth = aBaseTexture.width;
     int aHeight = aBaseTexture.height;

     Texture2D aReturnTexture = new Texture2D(aWidth, aHeight, TextureFormat.RGBA32, false);
             
     Color[] aBaseTexturePixels = aBaseTexture.GetPixels();
     Color[] aCopyTexturePixels = aToCopyTexture.GetPixels();
     Color[] aColorList = new Color[aBaseTexturePixels.Length];

     int aPixelLength = aBaseTexturePixels.Length;
     
     for(int p = 0; p < aPixelLength; p++)
     {
         aColorList[p] = Color.Lerp(aBaseTexturePixels[p], aCopyTexturePixels[p], aCopyTexturePixels[p].a);
     }
     
     aReturnTexture.SetPixels(aColorList);
     aReturnTexture.Apply(false);
     
     return aReturnTexture;
 }
Comment
Add comment · Show 3 · 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 soulburner · Apr 25, 2012 at 08:06 PM 0
Share

Thanks a lot for sharing!

I wonder, how fast is it...

avatar image Garth-Smith · Apr 11, 2013 at 08:28 PM 0
Share

Thanks for posting the answer. Almost two years later and it's still helpful to see such a good example of how to work with Texture2Ds.

avatar image TutiBueno2 · Oct 01, 2014 at 05:51 AM 0
Share

That was really helpful! Thanks!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I keep a texture different on copied objects? 1 Answer

Why cant I copy anything I have in my Project Tab to the Hierarchy Tab or Scene? 1 Answer

login window with user name 1 Answer

Clipboard pasting twice into InputField on Mac build 3 Answers

Stopping Copy/paste into Textfield. 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges