当前位置: 动力学知识库 > 问答 > 编程问答 >

Problem with alpha blending in XNA

问题描述:

Hi

i have a background and two png sprites

I want to make this effect using the provided background and sprites using XNA 3.1

I'm doing something wrong because i only get this As you noticed its not the effect i wanna do

It is possible do this effect with a few lines of code using alpha blending in XNA 3.1? A practical example would be really great!

网友答案:

First, render textures that contain the shapes that you want to be transparent to texture A. The textures containing the shapes should contain black shapes, on a transparent background -- easily constructed with image editing software like Photoshop.

Then take texture A and draw it over top of your scene using an effect (an HLSL shader) that does:

output = float4(0, 0, 0, A.r);

Effectively making the output image's alpha lower where A is darker.

The image will have clear portions where you drew your shapes on A, and will be black everywhere else.

Here are the details of the shader code:

sampler TextureSampler : register(s0);

float4 PS(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
    float4 Color = tex2D(TextureSampler, texCoord); 

    Color = float4(0, 0, 0, Color.r);

    return Color;
}

technique Vicky
{
    pass P0
    {
        PixelShader = compile ps_2_0 PS();
    }
}
网友答案:

If you want a solution without shader.

You first need your fog of war textures to be black with the transparent parts as White.

  1. Render your map and entity normally, to a RenderTarget2D
  2. Clear your background to black
  3. Start sprite batch with Additive blend
  4. Render you fog of war textures
  5. Start a new sprite batch with Multiply blend
  6. Render your map RenderTarget2D on top of the whole screen
分享给朋友:
您可能感兴趣的文章:
随机阅读: