SpriteBatchでもαブレンディング(色加算限定)
■SpriteBatchで色加算をあきらめかけていたのですが、やり方がわかりました
Game1.cs
namespace WindowsGame { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; SpriteBatch sprite; Texture2D texture; const int particleMax = 500; Particle[] particle = new Particle[particleMax]; public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void Initialize() { Random ran = new Random(); for (int i = 0; i < particleMax; i++) particle[i] = new Particle( (float)ran.Next(700), (float)ran.Next(500), (float)ran.Next(360) * 3.14159f / 180.0f); base.Initialize(); } protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { sprite = new SpriteBatch(graphics.GraphicsDevice); texture = content.Load<Texture2D>("ball"); } } protected override void Update(GameTime gameTime) { foreach (Particle p in particle) p.Move(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.Black); sprite.Begin(SpriteBlendMode.Additive); foreach (Particle p in particle) sprite.Draw(texture, p.Pos, Color.White); sprite.End(); base.Draw(gameTime); } } }
Particle.cs
using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; namespace WindowsGame { class Particle { public Vector2 Pos; public Vector2 Direction; public Particle(float x, float y, float ang) { Pos = new Vector2(x, y); Direction = new Vector2((float)Math.Sin(ang) * 1.0f, (float)Math.Cos(ang) * 1.0f); } public void Move() { Pos += Direction; if ((Pos.X < 0) || (Pos.X > 800-64)) Direction.X = -Direction.X; if ((Pos.Y < 0) || (Pos.Y > 600-64)) Direction.Y = -Direction.Y; } } }
■説明
アルファ入りはいつもは、こうなのですが
sprite.Begin(SpriteBlendMode.AlphaBlend);
色換算のときは、このように書けばできるようです。
sprite.Begin(SpriteBlendMode.Additive);
ちなみに、このように設定すると、アルファは無視されます。
sprite.Begin(SpriteBlendMode.None);
「sprite.Begin();」は「sprite.Begin(SpriteBlendMode.AlphaBlend);」と同じ動作のようです。