Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 iPedro · Oct 10, 2018 at 10:30 PM · texture2dcombinemergesetpixel

Combine 2 textures of different sizes at specific location

Hi All,

I've seen several posts on combining textures and have some code that works although it does not work when I try to place a texture at a specific location on the background texture. What I'm trying to achieve is shown in the following image where the green rectangle (layer1) is centered on top of the black rectangle (layer0) and results in the image shown. What I get when I run this code is the green rectangle positioned against the left wall of the black rectangle. Code is below and any help would be very much appreciated.

 public Texture2D MergeTextures(Texture2D background, Texture2D layer1)
     {
         int startX = 0; 
         int startY = 0;
 
         Texture2D newTex = new Texture2D(background.width, background.height, background.format, false);
         for (int x = 0; x < background.width; x++)
         {
             for (int y = 0; y < background.height; y++)
             {
                 if (x >= startX && y >= startY && x < layer1.width && y < layer1.height)
                 {
                     Color bgColor = background.GetPixel(x, y);
                     Color wmColor = layer1.GetPixel(x - startX, y - startY);
 
                     Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);
 
                     newTex.SetPixel(x, y, final_color);
                 }
                 else
                     newTex.SetPixel(x, y, background.GetPixel(x, y));
             }
         }
 
         newTex.Apply();
         return newTex;
     }



alt text

finaltexture.png (8.1 kB)
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
0
Best Answer

Answer by toddisarockstar · Oct 10, 2018 at 11:46 PM

the math for centering would be: subtract the layer size from the background and use half the result. do this for both width and height to get your start spot. also you forgot to add the start variable back to the right side of your less than statements. i made some untested adjustments but this should work for you

 public Texture2D MergeTextures(Texture2D background, Texture2D layer1)
      {
          int startX = 0; 
          int startY = 0;
          
          startX=background.width-layer1.width;
          startX=(int)(startX*.5f);
          startY=background.height-layer1.height;
          startY=(int)(startY*.5f);
 
 
          Texture2D newTex = new Texture2D(background.width, background.height, background.format, false);
          for (int x = 0; x < background.width; x++)
          {
              for (int y = 0; y < background.height; y++)
              {
                  if (x >= startX && y >= startY && x < layer1.width+startX && y < layer1.height+startY)
                  {
                      Color bgColor = background.GetPixel(x, y);
                      Color wmColor = layer1.GetPixel(x - startX, y - startY);
  
                      Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);
  
                      newTex.SetPixel(x, y, final_color);
                  }
                  else
                      newTex.SetPixel(x, y, background.GetPixel(x, y));
              }
          }
  
          newTex.Apply();
          return newTex;
      }
Comment
Add comment · Show 1 · 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 iPedro · Oct 11, 2018 at 12:27 AM 0
Share

Thank you @toddisarockstar ! It looks like you truly are a Rock Star. Really appreciate your help.

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

96 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to merge two textures of different size? 1 Answer

Texture2D update every frame - The right method 4 Answers

Fastest way to update Texture2D 0 Answers

Translate a pixel using SetPixel on a Texture2D? 1 Answer

Rotate Texture2d 1 Answer


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