XNA Additive particles blending on background
I got a problem with my particle engine that i'm trying to implement in a game i'm currently coding. It looks fine on a black background but I forgot the particles would blend with the background if I added one. Is there any way I can prevent my particles from blending with the background and only let them blend with eachother?
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null);
Main.TileMap.Draw(spriteBatch);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null);
Main.ParticleManager.Draw(spriteBatch);
spriteBatch.End();
Here's some images to show you what I mean:
http://puu.sh/1O6Zf
http://puu.sh/1O6Yl
Edit: I solved it by rendering it to a texture and draw it to the screen. Here's the fixed code:
RenderTarget2D renderTarget;
Texture2D particleMap;
public void LoadContent(ContentManager content)
{
renderTarget = new RenderTarget2D(Main.graphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, true, Main.graphicsDevice.DisplayMode.Format, DepthFormat.Depth24);
}
public void Draw(SpriteBatch spriteBatch)
{
Main.graphicsDevice.SetRenderTarget(renderTarget);
Main.graphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null);
Main.ParticleManager.Draw(spriteBatch);
spriteBatch.End();
Main.graphicsDevice.SetRenderTarget(null);
particleMap = (Texture2D)renderTarget;
Main.graphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
Main.TileMap.Draw(spriteBatch);
spriteBatch.Draw(particleMap, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 1);
spriteBatch.End();
}
maybe this could help you. To make texture half transparent:
spriteBatch.Draw(texture, location, Color.White * 0.5f);
so changing value from 0.0f
to 1.0f
will give you transparency strength.
also make sure you to sent blend state
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend)
链接地址: http://www.djcxy.com/p/31836.html
上一篇: Alpha混合和UIView背景颜色
下一篇: XNA添加剂颗粒在背景上混合