Проблема с шейдером xna

Попытка создать эффект свечения в 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();
  }
 }

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, обычно тоже дает хороший эффект.

Другие вопросы по тегам