Why are my textures half transparent?

Started by
1 comment, last by Sarkast 13 years, 9 months ago
Hi there,

I just started with XNA and in the current stage it appears I have a bug somewhere in my code.

I wanted to have a grayscale image, which can then be colored (for particles and such), but it appears that a full white png, colored in whatever color is translucent and I can't figure out why.

Here is a (very small :) example
http://image.bayimg.com/lankbaacl.jpg (btw, how do I include image tags?)

The grass is the background, the foreground is this picture colored gray (small note, if I color the white texture white, than it works, but I always want them red, green, whatever)
http://image.bayimg.com/lankcaacl.jpg

As you can see... one can see the background through it, and I don't want that :) I played around with several alpha settings, but I can't make it look the way I want to, I'm hoping there is something obvious I missed.

Here is my shader code, I presume at least the bug is somewhere in there:

technique ColoredTexture
{
pass Pass0
{
ZEnable = false;
AlphaBlendEnable = true;
SrcBlend = SrcAlpha;
DestBlend = DestAlpha;

VertexShader = compile vs_1_1 SimpleVertexShader();
PixelShader = compile ps_1_1 ColoredTexturePixelShader();
}
}

PixelToFrame ColoredTexturePixelShader(VertexToPixel Input)
{
PixelToFrame Output = (PixelToFrame)0;

Output.Color.rgb = tex2D(TextureSampler, Input.TexCoords).rgb*Input.Color.rgb;

Output.Color.a = tex2D(TextureSampler, Input.TexCoords).a;
return Output;
}

VertexToPixel SimpleVertexShader(float4 inputPosition : POSITION, float4 inputColor : COLOR, float2 inputTextureCoords : TEXCOORD0)
{
VertexToPixel Output = (VertexToPixel)0;

Output.Position = mul(inputPosition, xWorldViewProjection);
Output.Color = inputColor;
Output.TexCoords = inputTextureCoords;
return Output;
}

And my surrounding C# code, just in case


public virtual void DrawTexturedQuad(Vector3 position, float scale, Color color, Texture2D texture, Matrix worldTransform)
{
var vertices = CreateVertices(position, scale, color);
_device.VertexDeclaration = _coloredTextureVertexDeclaration;
_effects.CurrentTechnique = _coloredTextureTechnique;
_effects.Parameters["xWorldViewProjection"].SetValue(worldTransform * Statics.Camera.ViewMatrix() * Statics.Camera.ProjectionMatrix);
_effects.Parameters["xTexture"].SetValue(texture);
_effects.Begin();
foreach (var effectPass in _effects.CurrentTechnique.Passes)
{
effectPass.Begin();
_device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3);
effectPass.End();
}
_effects.End();
}


Any help would be greatly appreciated!

( I crossposted this on the xna community board, before I realised it is pretty much dead)

[Edited by - Sarkast on July 18, 2010 8:21:45 AM]
Advertisement
You haven't specified the blend operation in your case likely BlendOp = Add. And I'd set DestBlend to InvSrcAlpha (or One if you want additive blending).

You're talking about a PNG but the image links are JPG. JPG don't have an alpha channel. Can you show us the PNG, I suspect the alpha channel there is not proper.

You could of course reconstruct the alpha from the (calculated) grey value, or just take an arbitrary color channel, if the texture is grayscale.

By the way: In your pixelshader, you can include the alpha in the multiplication:
...Output.Color = tex2D(TextureSampler, Input.TexCoords) * Input.Color;...

this way you can change the overall transparency of your particles, too, not only the tint.
Hello unbird,

[EDIT]

It works! Will do a bit of experimenting what exactly did the trick, but thanks in the mean time!

- Sarkast

This topic is closed to new replies.

Advertisement