- Home /
Is it possible to fill transparency with white in a texture in code ?
I have some textures containing some transparency parts (a donut, for example, which would show a transparent center). What I want to do is fill the middle of the donut (or anything else) with a plain white, in code (I don't want to have a double of all my assets that need this tweak in one part of my game).
Is there a way to do this ? Or do I really have to have 2 of each of my assets ?
Thanks for the help !
Answer by Owen-Reynolds · Jun 04, 2014 at 08:46 PM
Yes, this is simple (assuming you know loops and color formats.) Texture2D
should have all the info you need (getPixels, setPixels... ) to read the old texture, and create/write/save the new. Reading alpha is no different than reading RGB.
Probably use: Color c2=Color.Lerp(c1, Color.White, c1.alpha); c2.a=1;
to get the white to fade in with the alpha.
Or, if you just happen to have alpha==white in the original image, simply use a non-transparent shader (with the original image.) It will show all the RGB, ignoring the alpha.
But this will make all of my alpha become white right ? I want only the middle of said donut to be white ! This will make the whole "square" of my texture white, which is something I don't want. Am I missing something else here ?
No, it will make the transparent parts to white, and leave the nontransparent pixels as they were. Isn't that what you wanted?
Well, sure, if the inside and edges are transparent, this would change all. A cheap "center only" is to hand-do math to say "if within a radius of the center." Can use (x-halfPixelWd)/pixelWd
to get a -0.5 to 0.5 for how far a pixel on x is from the center. Can probably also look up how to make a circle texture, and s$$anonymous$$l that.
Umm, I'd say rather something like that:
Color c2 = Color.Lerp(Color.White, c1, c1.alpha/255f);
c2.a=255;
then you get an image as if you put png onto totally white background layer in Photoshop or similar soft.
No need to divide by 255. Colors in Unity are scaled into range 0-1. Color.white is (1,1,1,1), ca.alpha is 1 (for a solid image), and so on.
Answer by Nerevar · Jun 04, 2014 at 04:06 PM
Hello,
I think you would need to write an editor script for that.
using a custom TextureImporter
Then you may be able to access variables you need
if you can set AlphaFromGrayscale and AlpaIsTransparency booleans (which I did no test) problem would be solved.
regards
Thing is, I need to do this on the fly (in code), not change the texture or import settings !
Yes well you can do it with Texture importer on an editor script
Answer by Jeff-Kesselman · Jun 04, 2014 at 09:10 PM
Another way to do it is to create an all white RenderTexture of the same size and then use http://docs.unity3d.com/ScriptReference/Graphics.Blit.html to copy your original ontop of it.
I only want to fill the middle part of my texture, not the entire thing !
Your answer
Follow this Question
Related Questions
Filling array with gameObjects 2 Answers
What is the best way to evenly distribute objects to fill a curved space? 1 Answer
How do I soften the edges of a transparent Plane texture 1 Answer
Mobile shader with transparent rendering mode 1 Answer
How to fill circle in Eric5h5's TextureDrawCircle function 2 Answers