开发者

Problem with xna shader

Trying to make a glow effect in xna but it doesn't show the glow or any change at all. Also my back color is purple instead of black and I can't change that either :

        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();

I'm only trying to get the bullets to glow.

shader :

 float BlurDistance = 0.002f;
 sampler ColorMapSamp开发者_运维技巧ler : 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();
  }
 }


The reason it's purple is because you have

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

which should be the other way around, so changing that into

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

fixes the purple problem, to fix the shader change the following in the fx file

sampler ColorMapSampler : register(s0);

And change your spriteBatch.Begin() into

spriteBatch.Begin(SpriteSortMode.Immediate, null);

Some extra info:

the s0 points to the first texture on the graphics device, which is the one that supplied by spriteBatch.Draw, if you want to use s1 you'd have to set it on the GraphicsDevice first by using:

GraphicsDevice.Textures[1] = bulletRenderTarget;

the SpriteSortMode.Immediate just forces the spriteBatch.Draw to draw the sprite immediately, if you don't set it it'll create a batch and draw them all at once, but this will be to late because it needs to be drawn when the EffectPass is being applied.

As for the blur you could lower the value of BlurDistance, but you'd have to try, you can also look up how to do a bloom shader, usually gives a nice effect too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜