D3D11 How not to draw the pixel when alpha in texture is 0

Started by
4 comments, last by pseudomarvin 8 years, 11 months ago

I am making a 2D game in D3D11 and I am implementing my own renderer which works like this:

1. A sprite has a texture, size and a position

2. To draw the sprite I create a rectangle which reflects the screen position and size of the sprite and map it to the vertex buffer

3. Vertex shader just applies orthographics projection to the rectangle coordinates

4. In the pixel shader I sample the sprite's texture and output it as color

Let's say that I am drawing a triangle. The texture is square but the part which does not contain the triangle has alpha = 0. I expected that only the triangle itself would be drawn and the rest of the square texture which has alpha = 0 would not be drawn but that is not the case, it is also drawn but it is just white. I think I am missing something important but I'm not sure what it is.

I am saving the loaded textures in a DDS format if that makes any difference. Thanks.


// PIXEL SHADER

Texture2D shaderTexture;
SamplerState texSampler;

struct PixelShaderInput
{
	float4 position : SV_POSITION;
	float2 texCoord : TEXCOORD;
};

float4 main(PixelShaderInput input) : SV_TARGET
{
	float4 textureColor = shaderTexture.Sample(texSampler, input.texCoord);
	return textureColor;
}
Advertisement

You can use something like this:


if(textureColor.a < 0.1)
  discard;

Another alternative is alpha blending. If you don't use depth-testing, your sprites are drawn at 1:1 scale, and there is only 1.0 or 0.0 values for the alpha of a pixel the results will be identical.

Thanks, discard is exactly what I was looking for.

I'm pretty inexperienced at 3D graphics... that said, I'm pretty sure alpha blending is what you are actually looking for.

If you want an alpha of 0.5 to make a pixel 50% transparent, then you should use alpha blending.

I'm pretty inexperienced at 3D graphics... that said, I'm pretty sure alpha blending is what you are actually looking for.

If you want an alpha of 0.5 to make a pixel 50% transparent, then you should use alpha blending.

Alpha blending and discard both have uses.
Mainly, discard should be used in order to prevent updating the depth buffer.
If he is writing a retro-style game like Super Mario Bros. then his sprites will have either 1 or 0 alpha, so in this case discard would be best. The depth buffer will match exactly the actual pixels drawn to the screen and without values between 1 and 0 blending is inappropriate.

Both blending and discard come at a performance cost, and discard’s cost is greater than just blending, so careful thought should always be given when considering which way is best for your specific needs. You could mimic Super Mario Bros. by disabling depth testing entirely, using only blending, and drawing in a specific order, for example (then again discard would be almost free in this case, defeating the purpose of the ad-hoc setup, but you get the point).


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks for the suggestions, I will probably try using alpha blending later to implement a kind of disappearing/fading effect on certain in game objects. I do not currently even have a depth buffer, I thought there was no use for one in a 2D game.

This topic is closed to new replies.

Advertisement