3

试图在 xna 中制作发光效果,但它根本不显示发光或任何变化。我的背色也是紫色而不是黑色,我也不能改变它:

        GraphicsDevice.Clear(Color.Black);
        GraphicsDevice.SetRenderTarget(bulletRenderTarget);
        spriteBatch.Begin();
        foreach (Bullet bullet in bulletList)
        {
            Texture2D bulletTexture = textures[bullet.bulletType];

            spriteBatch.Draw(
                bulletTexture,
                new Rectangle(
                    (int)bullet.position.X,
                    (int)bullet.position.Y,
                    bulletTexture.Width,
                    bulletTexture.Height
                ),
                null,
                Color.White,
                MathHelper.ToRadians(bullet.angle),
                new Vector2(
                    bulletTexture.Width / 2,
                    bulletTexture.Height / 2
                ),
                SpriteEffects.None,
                0
            );
        }
        spriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);
        GraphicsDevice.Clear(Color.Black);
        postProcessEffect.CurrentTechnique = postProcessEffect.Techniques["Blur"];

        spriteBatch.Begin();
            spriteBatch.Draw(
            bulletRenderTarget,
            new Vector2(0, 0),
            Color.White
        );
        GraphicsDevice.BlendState = BlendState.Additive;
        foreach (EffectPass pass in postProcessEffect.CurrentTechnique.Passes)
        {
            pass.Apply();
            spriteBatch.Draw(
                bulletRenderTarget,
                new Vector2(0,0),
                Color.White
            );
        }

        DrawHud();
        foreach (BaseEntity entity in entityList)
        {
            entity.Draw(gameTime);
        }
        spriteBatch.End();

我只是想让子弹发光。

着色器:

 float BlurDistance = 0.002f;
 sampler ColorMapSampler : register(s1);

 float4 PixelShaderFunction(float2 Tex: TEXCOORD0) : COLOR
 {
  float4 Color;

  // Get the texel from ColorMapSampler using a modified texture coordinate. This
  // gets the texels at the neighbour texels and adds it to Color.
  Color  = tex2D( ColorMapSampler, float2(Tex.x+BlurDistance, Tex.y+BlurDistance));
  Color += tex2D( ColorMapSampler, float2(Tex.x-BlurDistance, Tex.y-BlurDistance));
  Color += tex2D( ColorMapSampler, float2(Tex.x+BlurDistance, Tex.y-BlurDistance));
  Color += tex2D( ColorMapSampler, float2(Tex.x-BlurDistance, Tex.y+BlurDistance));
  // We need to devide the color with the amount of times we added
  // a color to it, in this case 4, to get the avg. color
  Color = Color / 4; 

  // returned the blurred color
  return Color;
 }

 technique Blur
 {
  pass Pass1
  {
   PixelShader = compile ps_2_0 PixelShaderFunction();
  }
 }
4

1 回答 1

2

它是紫色的原因是因为你有

GraphicsDevice.Clear(Color.Black);
GraphicsDevice.SetRenderTarget(bulletRenderTarget);

这应该是相反的,所以把它改成

GraphicsDevice.SetRenderTarget(bulletRenderTarget);
GraphicsDevice.Clear(Color.Black);

修复紫色问题,修复着色器在 fx 文件中更改以下内容

sampler ColorMapSampler : register(s0);

把你的spriteBatch.Begin()变成

spriteBatch.Begin(SpriteSortMode.Immediate, null);

一些额外的信息:

s0指向图形设备上第一个纹理的点,它是由 提供的spriteBatch.Draw,如果你想使用s1你必须GraphicsDevice通过使用将它设置在第一个:

GraphicsDevice.Textures[1] = bulletRenderTarget;

SpriteSortMode.Immediate只是强制立即绘制精灵,spriteBatch.Draw如果你不设置它,它会创建一个批次并一次绘制它们,但这会为时已晚,因为它需要在EffectPass应用时绘制。

至于模糊,你可以降低 的值BlurDistance,但你必须尝试,你也可以看看如何做一个绽放着色器,通常也会产生很好的效果。

于 2010-12-08T17:21:48.567 回答