Pixel Shader Bug in BasicPaletteEffect

Topics: User Forum
Feb 19, 2007 at 5:31 PM
Edited Feb 19, 2007 at 5:31 PM
I'm sorry to start a new thread on this, is there a more appropriate place to report small bugs?

Anyway, there's a bug on your BasicPaletteEffect pixel shader that ignores the textures' alpha channel.

In BasicPaletteEffect.cs, PixelShaderCode property, the output.color.w should be set to the texture's alpha when you have a texture and no light:

if (LightingEnable == false && TextureEnabled)
    output.color.xyz = tex2D(TextureSampler,input.texcoord).xyz * saturate(EmissiveColor + DiffuseColor);
    output.color.w = tex2D(TextureSampler,input.texcoord).w;

also, when there's no texture nor light, the alpha value should be the same as the diffuse alpha:

// Same as above, except no texture
else if (LightingEnable == false)
    output.color.xyz = saturate(EmissiveColor + DiffuseColor);
    output.color.w = DiffuseColor.w;

and in the remaining case (light and texture), it should be something like:

    // These comments are old but I left them in because they give some idea of how the lighting
    // works.  Lighting is now done in the vertex shader because older 2.0 cards didn't
    // support pixel shader lighting (like radeon 9800)
    // For phong shading, the final color of a pixel is equal to 
    // (sum of influence of lights + ambient constant) * texture color at given tex coord
    // First we find the diffuse light, which is simply the dot product of -1*light direction
    // and the normal.  This gives us the component of the reverse light direction in the
    // direction of the normal.  We then multiply the sum of each lights influence by a 
    // diffuse constant.
    // Now we do a similar strategy for specular light; sum the lights then multiply by
    // a specular constant.  In this formula, for each light, we find the dot product between
    // our viewDirection vector and the vector of reflection for the light ray.  This simulates
    // the glare or shinyness that occurs when looking at an object with a reflective surface
    // and when light can bounce of the surface and hit our eyes.
    // We need to be careful with what values we saturate and clamp, otherwise both sides
    // of the object will be lit, or other strange phenomenon will occur
    // Now we apply the aforementioned phong formulate to get the final color
    output.color.xyz = TextureEnabled ? tex2D(TextureSampler, input.texcoord).xyz  * input.color.xyz
        : input.color.xyz;
    output.color.w   = TextureEnabled ? tex2D(TextureSampler, input.texcoord).w : input.color.w;

Hope it helps


Feb 19, 2007 at 8:44 PM
Edited Feb 19, 2007 at 10:06 PM
Thanks, I'll fix this.

You can report bugs in the "issue tracker" tab.

Actually, setting the w value in the "if" statements crashes the xbox (!!!!!). Also, diffuse materials don't have alpha components in XNA.

So I copied what BasicEffect does and added an Alpha property to BasicPaletteEffect. I fixed the source code by changing the last line to:
        output.color.w   = 
            TextureEnabled ? tex2D(TextureSampler, input.texcoord).w * Alpha : Alpha;