- Home /
I want to change the alpha of individual pixels on a texture based on a particle effect
So I have a texture, to be able to visualise the problem a bit more let's say it is a solid vertical blue rectangle.
What i want to do is have it start off invisible (0 alpha), then i'd like it to start becoming visible from the top and crawl downwards, imagine nannites crawling down it and eating a layer of dirt off the top as they go so it becomes visible where they are... that kind of thing.
My first thought was to try and use a particle effect, setting the alpha of any pixel that has a particle over it to 1.
So i guess what i'm really asking is, is it possible to edit alpha levels of a texture pixel by pixel. And can you tell it to detect if there is a particle there and react to that?
Many thanks
After doing a fair bit of research into different methods i discovered how to use a clipping mask by writing a fairly basic shader. I can use that to do a simplified version if it comes to it but i would really like to have more of a random effect.
In order to do that i'm intending to use get pixel and set pixel in order to change alpha levels based on those of pixels around that pixel in question. However documentation on get and set pixel is 1) only in US rather than c# and 2) not commented/explained very well.
Could any one help me understand how to use them? Just to clarify:
I want to check the alpha of a pixel adjacent to current pixel, and then set current pixel's alpha dependent on that.
If possible, avoid using get/set pixel. It's fine if you're using it once or twice, but it's very, very expensive, so using it continuously like you're planning could be extremely slow. You can probably achieve this entire effect within a shader, though it would be a little more complex than what you have at the moment.
yeah i was a little concerned about that, i'm intending to try and make it cheaper by only checking the "next" row of pixels as it were ins$$anonymous$$d of all pixels on each pass, and using getpixels to check all adjacent pixels at once rather than doing each individually... do you think this would be enough?
$$anonymous$$y problem is that I have absolutely no idea where to start with shaders, I haven't looked at them before now. And the last one i tried looked pretty much like nonsense, I didn't really understand what it was doing.
It depends. If you're ai$$anonymous$$g for mobile, then no. Low-end PCs, maybe not, but you're probably fine if you're ai$$anonymous$$g for a decent PC. Shaders aren't too tricky to learn, though. I managed to learn most of the basics of vertex and surface shaders in about a week, it just takes a bit of trial and error, and internet tutorials. Generally, when working with pixel data as a texture, shaders are the way to go.
I am experimenting a little at the moment trying to use get and set pixels to accomplish what is needed. But for some reason the following code seems to be doing absolutely nothing, the material is still completely visible:
// Use this for initialization
void Start () {
//store texture being used
texture = (Texture2D)renderer.material.mainTexture;
//store color data for all pixels in texture
Color [] textureColors = texture.GetPixels(0,0,texture.width,texture.height);
//set all pixel's alpha channel to 0
for(int index = 0; index< textureColors.Length; index++)
{
textureColors[index].a = 0;
}
//set textures pixels using new colors
texture.SetPixels(0,0,texture.width,texture.height,textureColors);
//update renderer's material to edited texture
renderer.material.mainTexture = texture;
}
can any one help me with this? Why is this not setting the alpha to 0? thanks guys.
ps. Thanks hoeloe i may well look into shaders then.
Your answer
Follow this Question
Related Questions
Switch default diffuse light to vertex-light 0 Answers
Unity changes pixel colors 2 Answers
Point Lights and fullforwardshadows 0 Answers
Can I change tile colours depending on proximity to the player? 0 Answers