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
1
Question by deets · Feb 16, 2015 at 01:36 PM · texturealphachannel

Change alpha channel of a texture in code

Is there a possibility to change a textures alpha channel with code?

Texture2D colorImg; Texture2D greyscaleImg;

colorImg.a = greyscaleImg.a;

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 CHPedersen · Feb 16, 2015 at 02:07 PM

A texture doesn't have one value for alpha that spans the entire texture, instead, the alpha channel is per pixel.

To copy the alpha channel from one texture to another, you have to pass through the texture in its entirety, pixel by pixel, and copy each pixel's alpha channel over. Here's a function that demonstrates that approach:

 private void TransferAlpha(Texture2D A, Texture2D B)
 {
     Color pixA, pixB;

     for (int i = 0; i < A.width; i++)
     {
         for (int j = 0; j < A.height; j++)
         {
             // Read out pixel value at that location in both textures
             pixA = A.GetPixel(i,j);
             pixB = B.GetPixel(i,j);

             // Copy the alpha channel from B's pixel and assign it to A's pixel
             pixA.a = pixB.a;

             // Set the changed pixel back to A in that location
             A.SetPixel(i, j, pixA);
         }
     }

     // Apply the results
     A.Apply();
 }

Notice that this is kind of slow and that there are better ways, such as reading out the whole pixel array and iterating over that, then using SetPixels to set the whole texture at a time. But this demonstrates the idea, at least.

Comment
Add comment · Show 2 · 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 deets · Feb 16, 2015 at 02:14 PM 0
Share

Thank you :)

avatar image CHPedersen · Feb 16, 2015 at 02:19 PM 1
Share

You're welcome. :) But please do modify that code to make it more robust. There are plenty of ways it can be slow or break. For example, it naively assumes A and B have the same size.

avatar image
0

Answer by leventalpsal · Jul 10, 2017 at 08:08 AM

This is my checklist, my road to salvation :) ( successful changing the alpha transparency of a texture per pixel )

  1. Standard shader render mode should be transparent or any other shader with transparency used

  2. Materials texture alpha is different from textures per pixel alpha. Materials texture alpha should be 1 so that when texture per pixel alpha is more than 0 it can be visible

  3. Texture must be read / write enabled

  4. Texture format should be one of the formats that support alpha like RGBA 32 bit

  5. Instantiating of the texture at runtime is needed in order to avoid modifying the original texture

  6. Instantiating should be done once at the start.

  7. The instantiated texture should be set as the materials texture.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

alpha mapping on 3D objects 1 Answer

Texture Format missing? 0 Answers

use Alpha from image in Fragment shader 0 Answers

Movie GUI Texture Alpha? 1 Answer

How to move alpha mask along the floor plane as the user (camera) moves 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