x


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;
}
more ▼

asked Apr 03 '12 at 11:26 PM

dbravo gravatar image

dbravo
1 1 1 2

There is a generic form of Instantiate. Check the docs.

Apr 03 '12 at 11:53 PM Jessy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You don't need to instantiate a new texture each time, you aren't even using it.

more ▼

answered Apr 03 '12 at 11:54 PM

DaveA gravatar image

DaveA
26.4k 151 171 256

(comments are locked)
10|3000 characters needed characters left

Dave I corrected my code

more ▼

answered Apr 05 '12 at 12:38 PM

dbravo gravatar image

dbravo
1 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x354
x58
x7

asked: Apr 03 '12 at 11:26 PM

Seen: 616 times

Last Updated: Apr 05 '12 at 12:38 PM