XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Pixel Shader Question, converting purple to transparent

Last post 26/07/2009 21:24 by Eckish. 2 replies.
  • 26/07/2009 20:28

    Pixel Shader Question, converting purple to transparent

    So I only want to apply a distortion pixel shader to a specific image that is over a background image.

    So i have a background image, an image that I want to be affected by the shader over the background image, and the normal map that the shader is using.

    The problem is that when I pass through the image (a .png with some transparency in places) and the normal map, the parts that do not have any color information show up as a default purple color which blocks the background image drawn behind it.

    If I pass through the background image and the image I want affected in the pixel shader, it distorts part of the background image that I do not want affected.

    How is it possible to identify this purple color and translate the value of that into something transparent?

    I've tried passing through the background image in a register like so

    sampler2D inputBackground : register(s3);

    and then identifying and replacing the purple

    if(color.r<0.3)
    {
    color = tex2D(inputBackground, texCoord);
    }

    however this still leaves a slightly purple outline around what I'm trying to distort as you can see here

    http://img20.imageshack.us/img20/7959/shown.png

    any better solutions?
  • 26/07/2009 21:10 In reply to

    Re: Pixel Shader Question, converting purple to transparent

    Answer
    Reply Quote
    Make sure just after you set the rendertarget to call GraphicsDevice.Clear(Color.TransparentBlack);
    Regards,
    Louis Ingenthron
    Fortis Venaliter
    Lead Developer of FV ProductionsFV Productions
  • 26/07/2009 21:24 In reply to

    Re: Pixel Shader Question, converting purple to transparent

    You shouldn't have to do anything in your pixel shader to get alpha blending to work if everything is setup correctly.  If you have alpha blending turned on, the correct blending function and the image is imported properly, then you should be able to just draw the pixels as they are and not worry about it.  Because any pixel with an alpha of 0 should get blended out, regardless of the color set on it. 

    Now, purple is the default clear color for XNA, so, I suspect there is an issue with the order you are doing things.  My guess is that your start with a clear render, then draw one pass that processes your effect.  And your effect either chooses to draw a pixel from the background or a pixel from the distorted image.  The problem is when it chooses to draw a pixel from the distorted image and it has an alpha value less than 1, then it will get blended with what is currently on the render image... which is the default purple.  If you go this route, then the proper way would be always draw both a background and a distorted pixel.  However, you would have to do the alpha blending yourself.  The other issue is that unless your distorted image is always a full screen draw, you will only get a background for the rectangular area where you apply your effect.

    What you probably want to do is to draw your entire background to the current render first (and verify it shows up on its own before moving to the next step).  And then perform a draw pass applying your distorted effect with alpha blending turned on.  In your shader, you wouldn't have to worry about what is in the background at all.  You just have to make sure that whatever your effect is doing is not skewing your alpha values in any way that will make the draw fail.
Page 1 of 1 (3 items) Previous Next