Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 teomaragakis · Apr 24, 2014 at 02:48 PM · texture2dtransparencyalpha

How do I blend a transparent texture with an opaque one?

I have two RGBA32 textures from PNG files, one is opaque, the other one has opaque pixels, completely transparent pixels, and shadow pixels (black with 50% alpha. In the inspector, I can see the alpha of my transparent texture properly, I see black, white and grey.

Let's assume for argument's sake that both textures have the same resolution, say 32px * 64px. I am using:

 color2 = texture2.getpixels(0,0,32,64);
 texture1.setpixels(0,0,32,64,color2);

but when I render texture1 it shows texture2 only with white where the transparency (so the layer below) is supposed to be. I actually want to layer the two textures one on top of the other and use the result as one texture (including darkened parts by the "shadow" pixels.

If I set "Alpha is transparency" in the import settings, it appears even worse. The texture clamps on the pixels that should be completely transparent and the half-transparent ones appear black.

What am I doing wrong?

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

2 Replies

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

Answer by socialspiel · Apr 24, 2014 at 03:54 PM

Have you checked the value of color2.a?

Is texture1 the right format (ARGB32) and Read/Write enabled?

Comment
Add comment · Show 6 · 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 teomaragakis · Apr 24, 2014 at 07:48 PM 0
Share

It's RGBA32. Does it make a difference? Read / write is enabled, I can render it as I said. Didn't check the alpha programmatically but it wouldn't appear in the inspector correctly in alpha mode if it weren't correct.

avatar image teomaragakis · Apr 25, 2014 at 07:48 AM 0
Share

@socialspiel but is this the correct approach? Does set pixels "add" pixel alpha values or it just replaces?

avatar image socialspiel · Apr 25, 2014 at 08:52 AM 0
Share

setpixel only works on certain texture formats (rgba32 is one of them)

it completly replaces the the pixel. if you want to blend the alpha of both pixels, you have to get the pixels of texture1 too and multiply/blend the alpha values.

depending on your texture size you might want to use Graphics.Blit (https://docs.unity3d.com/Documentation/ScriptReference/Graphics.Blit.html) which is much faster and write a shader or experiment with the materials used for it

avatar image teomaragakis · Apr 26, 2014 at 08:16 AM 1
Share

@socialspiel almost there! I wrote this up:

 public Texture2D addTextures(Texture2D texture1, Texture2D texture2) {
     Color[] color1 = texture1.GetPixels();
     Color[] color2 = texture2.GetPixels();
 
     for (int i = 0; i < color1.Length; i++) {
         if (color2[i].a == 1) {
             color1[i] = color2[i];
         } else if (color2[i].a != 0) {
             color1[i].a = color2[i].a;
         }
 
     }
     texture1.SetPixels (color1);
     return texture1;
 }

Only issue is now the alpha (shadows). Tried with color1[i].a *= color2[i].a as well, nothing. Any ideas?

avatar image NinRac · Sep 24, 2014 at 06:51 PM 0
Share

@socialspiel @teomaragakis

You are soooo close with that coding sample. You're going to want to do

Color.Lerp(color1[i], color2[i], color2[i].a);

ins$$anonymous$$d of that if statement. That will cover your transparency, shadows, opaque, etc. Thanks to you both though for helping me fix out my auto-applied watermark issue that I figured out because of your work. That Color.Lerp is the last step of the puzzle that you needed (and if you want to take it a step further, just multiply that last argument by a value between 0-1 to deter$$anonymous$$e how transparent you want it to be)

avatar image EricJ13 NinRac · Aug 10, 2017 at 07:01 PM 0
Share

Thanks for that! I was doing something similar but was adding the colors. Worked well except for alphas.

avatar image
0

Answer by rujomarco · Oct 10, 2017 at 10:20 PM

thanks, this helped me a lot!

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

23 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

Related Questions

Using two fullscreen Graphics.DrawTexture images, overlapping? 1 Answer

Strumpy Shader Editor: Transparency Issues 0 Answers

Alpha not working in GUITex 0 Answers

Transparent / Vertex Lit Ignore Sides 1 Answer

Changing the alpha channel of a sprite using the new Unity sprite system 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