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 RealMTG · Mar 05, 2017 at 10:23 AM · texture2dalphatransparentpixels

Check if every pixel of a texture is transparent

Hello

I am currently trying to check if a texture is fully transparent. Basically, check if there's absolutely nothing on the texture. I am generating these textures at runtime and it can happen that there's nothing on the texture because of the lack of renderers on an object, and if there's nothing there, I want to take care of it in some other way. But I don't have any idea how I could check every pixel of a texture to see if it's 100% transparent.

Any help with this would be greatly appreciated!

Thanks

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

Answer by AurimasBlazulionis · Mar 05, 2017 at 10:58 AM

just do a for loop:

 bool IsTransparent(Texture2D tex) {
     for (int x = 0; x < tex.width; x++)
         for (int y = 0; y < tex.height; y++)
             if (tex.GetPixel(x, y).a != 0)
                 return false;
     return true;
 }

If you also need to check if all color values are 0, then just change the if statement to if (tex.GetPixel(x, y).Equals(new Color(0,0,0,0))). Sure, you could define the color 0 0 0 0 as a variable and use it so no stack/heap allocation occurs.

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 RealMTG · Mar 05, 2017 at 11:03 AM 0
Share

Thank you for your help! It worked perfectly! I honestly didn't have a clue about GetPixel. I must have missed it both while testing in Unity and when checking the docs. :S

avatar image austinborden · Apr 20, 2018 at 07:27 PM 1
Share

Ins$$anonymous$$d of calling GetPixel in a nested for loop, call GetPixels or GetPixels32 before the loop and reference that array of pixels for better performance.

avatar image Bunny83 austinborden · Apr 20, 2018 at 08:06 PM 1
Share

It does not only increase the performance dramatically but also simplifies the iteration since you just have one big array you have to deal with. I'm even wondering since he's generating the texture at runtime he should already have such an array when generating the texture.

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

68 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

Related Questions

Shader alpha value consistent with all transparent objects 0 Answers

Transparent terrain shader not working 2 Answers

Stencil buffer and transparency 0 Answers

Change Alpha on individual Texture2Ds 1 Answer

Slowly fades from opaque to alpha 2 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