SetPixels Not Transparent?

I am trying to use SetPixel with a brush image that has a round brush shape with a transparent background. The code is below. When I run this, the brush shape displays but with a white background. The output values for the first pixel are listed below, which should be transparent(?) since a=0.0, but it appears white when rendered. Or am I misunderstanding something?

function testAlpha(){
    var tex=Instantiate(renderer.material.mainTexture);
    var pixels=brushTexture.GetPixels(0,0,brushTexture.width, brushTexture.height,0);
    var outputString="";
    for (var y : int = 0; y < brushTexture.height; ++y) {
        for (var x : int = 0; x < brushTexture.width; ++x) {
            var brushColor=brushTexture.GetPixel(x,y);
            if(x==0){outputString+="brush color: "+brushColor+",";}
            if(x==0){outputString+="old text color: "+tex.GetPixel(x, y)+",";}
            tex.SetPixel (x, y, brushColor);
            if(x==0){outputString+="new tex color: "+tex.GetPixel(x, y)+"|";}
        }
    }
    Debug.Log(outputString);
    renderer.material.mainTexture=tex;
    tex.Apply();
}

brush color: RGBA(1.000, 1.000, 1.000, 0.000),old text color: RGBA(0.227, 0.459, 0.773, 1.000),new tex color: RGBA(1.000, 1.000, 1.000, 0.000)

3 Answers

3

Here's the code I came up with that does this:

function compositeAlphaImage(baseImage, compositeImage, cx, cy){
    // composite image with alpha channel onto base image
    // cx and cy define the location of composite image relative to base image
    var newImage=Instantiate(baseImage);
    for (var y : int = 0; y < compositeImage.height; ++y) {
        for (var x : int = 0; x < compositeImage.width; ++x) {
           var compositeColor=compositeImage.GetPixel(x,y);
           var baseColor=baseImage.GetPixel(cx+x, cy+y);
           var alpha=compositeColor.a;
           var newRed=(1-alpha)*baseColor.r+alpha*compositeColor.r;
           var newGreen=(1-alpha)*baseColor.g+alpha*compositeColor.g;
           var newBlue=(1-alpha)*baseColor.b+alpha*compositeColor.b;
           var newAlpha=1.0;
           var newColor=Color(newRed,newGreen,newBlue,newAlpha);
           newImage.SetPixel (cx+x, cx+y, newColor);
        }
    }
    return newImage;
}

I think there is a mistake here: newImage.SetPixel (cx+x, cx+y, newColor); it has to be newImage.SetPixel (cx+x, cy+y, newColor);

Are you using a transparent shader to render with? Otherwise it'll disregard your texture's alpha.

I've tried both. Non-transparent shader results in a red circle with a white background (my brush) on a blue texture (my canvas). A transparent shader results in a red circle with transparency around the circle in the brush image area which shows transparency work. However, I'm trying to get a red circle on a blue background where the transparency applies when setting the pixel but not on the final texture.

Aah, I see, then Kourosh's answer is probably what you want (blending between the colours), unless you just render an opaque blue plane behind it

Check this answer:

http://answers.unity3d.com/questions/43888/painting-stencil-on-a-surface