【问题标题】:Problem with xna shaderxna着色器的问题
【发布时间】:2010-12-08 13:42:53
【问题描述】:

尝试在 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();
  }
 }

【问题讨论】:

  • 您遇到的错误是什么?
  • 无,它只是不工作,它显示正常的游戏,除了黑色的后屏,变成紫色

标签: c# xna shader blur glow


【解决方案1】:

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

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的值,但你必须尝试,你也可以看看如何做一个bloom shader,通常效果也很好。

【讨论】:

  • 谢谢你解决了这一切,你能详细说明一下寄存器和 spritesortmode,那到底是做什么的?还有如何减少我的模糊,我只是因为我想让子弹发光而模糊它,但现在我的子弹发光并且模糊
  • @Quincy 已将其添加到答案中
  • 谢谢,我希望花朵看起来更好一点:)
猜你喜欢
  • 2011-11-27
  • 2014-08-05
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
相关资源
最近更新 更多