• 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 thom · Jan 21, 2010 at 06:47 AM · texture2dalpha

Adding Colors with alpha channels

I have two color Arrays and I want to add them together. One of them is a solid (dest), and the other one (mask) has an alpha cutout. I want to stamp the alpha cutout onto the solid cleanly.

What I am doing is:

[Loop all of the pixels] if( mask[ptr].a != 0) dest[ptr] = mask[ptr];

then writing it to the texture.

It works great, however the edges end up a little dirty. Anyone know a way to add these together in a true alpha stamp as it would look in photoshop?

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
0
Best Answer

Answer by runevision · Jan 21, 2010 at 09:43 AM

If I understand you correctly, you want to apply a cutout texture, but with edges that are not 100% sharp. So you can use the alpha from the mask to control the strength with which it should be applied.

For each pixel you can apply the mask this way:

float maskAlpha = mask[ptr].a;

// Calculate the solid mask color (the mask color with full alpha) Color maskSolidColor = mask[ptr]; maskSolidColor.a = 1;

// Now apply the solid mask color with the strength // controlled by the mask alpha dest[ptr] = Color.Lerp(dest[ptr], maskSolidColor, maskAlpha);

This way should even work for dest pixels that are not necessarily solid.

Optional: If you want to be able to control how sharp the cutout's edges should be, you need variables to control the sharpness (contrast) and where the threshold should be (usually at 0.5):

float threshold = 0.5f;
float contrast = 1.0f;

Then you can calculate the maskAlpha this way instead of the way above:

// Calculate a mask alpha value with increased contrast
//float maskAlpha = mask[ptr].a;
float maskAlpha = Mathf.Clamp01((mask[ptr].a - threshold) * contrast + threshold);

Hope that helps!

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 thom · Jan 26, 2010 at 03:07 AM 0
Share

Wow! Great thank you! Looks WAY Better than it did!

avatar image
0
Best Answer

Answer by duck · Jan 21, 2010 at 07:16 AM

Try this. It just uses the minimum of the two alpha values:

dest[ptr].a = Mathf.Min(mask[ptr].a, dest[ptr].a);
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 duck ♦♦ · Jan 21, 2010 at 09:35 AM 0
Share

(oops, fixed typo in code)

avatar image runevision ♦♦ · Jan 21, 2010 at 09:50 AM 0
Share

This method applies a mask to the dest texture. I think the poster asked for a way to stamp the "mask" texture onto the dest texture, using the alpha channel of the "mask".

avatar image duck ♦♦ · Jan 21, 2010 at 10:42 AM 0
Share

Ah yes, I read the question as being how to punch a hole through one image, using the alpha of another image. On re-reading, I think you're correct.

avatar image thom · Jan 26, 2010 at 03:07 AM 0
Share

I guess my original question wasn't so clear. The dest array is solid, so it looks like the best way to do it is the answer below. Thanks for your help anyway--Sorry I wasn't so clear!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Determine if a Texture2D has alpha 3 Answers

How can I change texture alpha with another texture by script? 0 Answers

Change Alpha on individual Texture2Ds 1 Answer

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

Blending two 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