xnarenderingtexturesrender-to-texture

XNA - Render to a texture's alpha channel


I have a texture that I want to modify it's alpha channel in runtime.
Is there a way to draw on a texture's alpha channel ?
Or maybe replace the channel with that of another texture ?

Thanks,
SW.


Solution

  • Ok, based on your comment, what you should do is use a pixel shader. Your source image doesn't even need an alpha channel - let the pixel shader apply an alpha.

    In fact you should probably calculate the values for the alpha channel (ie: run your fluid solver) on the GPU as well.

    Your shader might look something like this:

    float4 main(float2 uv : TEXCOORD) : COLOR
    {
        float4 c = tex2D(textureSampler, uv);
        c.A = /* calculate alpha value here */;
        return c;
    }
    

    A good place to start would be the XNA Sprite Effects sample.

    There's even an effect similar to what you are doing:

    (source: msdn.com)

    The effect in the sample reads from a second texture to get values for the calculation of the alpha channel of the first texture when it is drawn.