• 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 KrisCadle · Mar 07, 2012 at 05:51 AM · alpha

How can I 'Generate Alpha from Grayscale' at runtime from script

Hi, I'm having trouble figuring out how to get this to work. I want to be able import a new texture and have it generate alpha from grayscale just like it would if it had the box checked in the import settings, except this would be an image uploaded to the program by the user and not included in the build... sooo, i can't use the import settings... I know there must be a simple way to do this...

Here's the code I've got so far:

 var shader : Shader;
 private var message = "";
 private var alpha = 1.0;
 private var pathChar = "/"[0];
 var pixels : Color[];
 var pixel : Color;
 var url = "";
 

function LoadTex (pathToFile : String) {

         var fileIndex = pathToFile.LastIndexOf(pathChar);
         message = "You selected file: " + pathToFile.Substring(fileIndex+1, pathToFile.Length-fileIndex-1);
         
         url = ("file:" + pathToFile);
         
         renderer.material = new Material (shader);
         var texture = Texture2D;
         var www : WWW = new WWW (url);
         yield www;
         renderer.material.mainTexture = www.texture; 
         www.LoadImageIntoTexture(texture);

         var pixels : Color[] = texture.GetPixels();
                             
                           for (var i : int = 0; i<pixels.Length; ++i) { 
                 
                 pixel = pixels[i]; 
                 pixel.a = (pixel.r + pixel.g + pixel.b) / 3.0f; 
                 texture.SetPixels(pixels);
                 }
                 
         texture.Apply();
 }
Comment
Add comment · Show 7
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 KrisCadle · Mar 07, 2012 at 05:06 PM 0
Share

anyone got any ideas?

avatar image Berenger · Mar 07, 2012 at 05:21 PM 1
Share

Well, I told you on the other question to change the alpha of the texture with GetPixels / SetPixels, but you didn't seem too thrilled about it.

avatar image FishBone · Mar 07, 2012 at 05:38 PM 1
Share

I guess it's the same answer as the above comment, but i'll just post it anyway. It's in c#, but shouldn't take much work changing it to js

Texture2D texture; www.LoadImageIntoTexture(texture); Color[] pixels = texture.GetPixels();

for (int i=0; i

texture.SetPixels(pixels);

avatar image KrisCadle · Mar 07, 2012 at 06:06 PM 0
Share

I had been trying to use GetPixels / SetPixels in all kinds of ways but it still wasn't working... close, but this helps alot, Thanks very much for the guidance ;)

avatar image KrisCadle · Mar 09, 2012 at 07:12 PM 0
Share

Yeah, I've tried your suggestions with no luck. Are you sure that using GetPixels this way would have the same affect as the importer box 'Generate Alpha From Grayscale'? I'm having a hard time getting this to actually creat alpha from the grayscale image I'm using, much less get it to apply alpha to a user selected image file.

So what am I doing wrong? Are there any other ways of getting this to work?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dave_sf_42 · Jul 10, 2020 at 09:39 AM

This is the code I'm using to generate a specular-alpha channel from a classical non-alpha specular map.

  var pixels = KsTexture.GetPixels();
  for (var i=0 ; i < pixels.Length; i++) {
     var pixel = pixels[i];
     float grey = (float)(((double)pixel.r + (double)pixel.g + (double)pixel.b) / 3.0);
     pixels[i].a = grey;
  }
  KsTexture.SetPixels(pixels);
  KsTexture.Apply(updateMipmaps:true);

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.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Problem with alpha texture. Overlapping Faces 2 Answers

Can you get how much light is on the surface in a shader? 0 Answers

Optimize code changing transparency of large number of Objects 0 Answers

Does Unity premultiply alpha in imported PNGs? 0 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