- Home /
Calling "Generate Alpha From Grayscale" from scripting??
Hi, I am attempting to create 2D procedurally generated clouds and I have everything working except for one slight problem.. I need to procedurally imported script to have the property "Generate Alpha from Grayscale" enabled and I haven't been able to figure out how to do this via scripting.. Any help on this would be greatly Appreciated! Thanks!
Did you ever find a solution to this? I'm stuck on this one myself and if I loop the R values into A it still doesn't work.
Answer by syclamoth · Mar 07, 2012 at 03:02 AM
Use an editor script that iterates over the files that you want to modify, checking grayscaleToAlpha on each. Of course, you can't do this at runtime- because by that point the asset has already been imported.
I really need to do it at runtime with any user selected image ...
Answer by Berenger · Mar 07, 2012 at 03:04 AM
Try to look here, here or the OP's question on forum.
I suppose it's not to hard to generate it yourself though. Once you have the texture, go through each pixel and generate alpha with an average of the 3 values, or like jessy suggest it, a dot product.
I found those examples when I started trying to solve it, but I've tried several attempts and the new texture just never has the same import settings as the old one. If I have an image on a cube generating alpha from grayscale and has it's transparencies, I can upload a new texture to the cube from my GUI button, and I can assign a shader to it that handles transparencies, but the new texture doesn't generate alpha from grayscale like the one it replaces and doesn't have that option when I click on it in the editor while it's running and loaded...
You can't 'import' textures at runtime without a special script. Don't expect to be able to use Unity's asset importer at runtime.
Have you tried (pseudo code) ?
pixels = texture.GetPixels() for all p in pixels p.a = ComputeAFromRGB(p.rgb) texture.SetPixels(pixels) texture.Apply()