- Home /
Painting on a texture's alpha channel
Hi,
I am trying to do like a scratching effect on a texture so that when the user drags his finger on the screen, the texture below the first one should appear (but only in places where the upper texture is "scratched"). I suppose I should somehow paint the upper textures alpha channel so that it is invisible in the places where use drags (using another texture as a brush).. this code below works but of course it's not good because this happens every frame if the user is dragging so soon Unity will run out of memory (every frame the new texture is instantiated). Well actually my question is.. how to do this right so that it works on mobile devices. Thanks for any help!
i edited the code.. sorry I pasted the wrong one. In this code I am using the new instantiated image..
Texture2D DrawOnImage(Texture2D baseImage, Texture2D brushImage, int cx, int cy)
{
Texture2D newImage = (Texture2D)Instantiate(baseImage);
for (int y = 0; y < brushImage.height; ++y)
{
for (int x = 0; x < brushImage.width; ++x)
{
Color compositeColor = brushImage.GetPixel(x, y);
Color baseColor = baseImage.GetPixel(cx + x, cy + y);
Color newColor = new Color(1, 1, 1, compositeColor.a);
newImage.SetPixel (cx+x, cy+y, newColor);
}
}
return newImage;
}
Answer by DaveA · Apr 03, 2012 at 11:54 PM
You don't need to instantiate a new texture each time, you aren't even using it.
Your answer
Follow this Question
Related Questions
Painting issue, drawing is not consistent, how to fix? 1 Answer
How can I paint a picture and then compare it to another? 1 Answer
Is there a way to import png images with TextureType = GUI automatically? 1 Answer
scale textures using scrollbars that show/enable when texture is clicked on 0 Answers
How to fix it? 0 Answers