开发者

Problem with alpha blending in XNA

开发者 https://www.devze.com 2023-02-12 06:02 出处:网络
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

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 d开发者_开发知识库o 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
0

精彩评论

暂无评论...
验证码 换一张
取 消