梦想成真 XNA (3) - SpriteSortMode, BlendState
梦想成真 XNA (3) - SpriteSortMode, BlendState
作者:webabcd
介绍
XNA: SpriteSortMode 和 BlendState
- SpriteSortMode - 精灵在游戏窗口上绘制的排序方式,默认值为:SpriteSortMode.Deferred
- BlendState - 精灵与当前游戏界面的混合方式,默认值为:BlendState.AlphaBlend
示例
1、用于演示 SpriteSortMode 的示例(按键盘 D 键加载此 Demo)
Component/Sprite/SpriteSortModeDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace XNA.Component.Sprite
{
public class SpriteSortModeDemo : Microsoft.Xna.Framework.DrawableGameComponent
{
// 精灵绘制器
SpriteBatch _spriteBatch;
// 精灵对象
Texture2D _spriteSon;
Texture2D _spriteMVP;
public SpriteSortModeDemo(Game game)
: base(game)
{
}
public override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(Game.GraphicsDevice);
_spriteSon = Game.Content.Load<Texture2D>("Image/Son");
_spriteMVP = Game.Content.Load<Texture2D>("Image/MVP");
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Game.GraphicsDevice.Clear(Color.CornflowerBlue);
/*
* SpriteSortMode - 精灵在游戏窗口上绘制的排序方式,默认值为:SpriteSortMode.Deferred
* SpriteSortMode.Deferred - 调用 End() 的时候,绘制精灵,绘制的顺序就是调用 Draw() 方法的顺序
* SpriteSortMode.Immediate - 调用 Draw() 方法时,立即绘制相应的精灵(此模式下在 Begin...End 之间不能再有其他 SpriteBatch 对象,其他模式无此限制)
* SpriteSortMode.FrontToBack - 调用 End() 的时候,绘制精灵,根据层深度(layerDepth)从小到大依次绘制
* SpriteSortMode.BackToFront - 调用 End() 的时候,绘制精灵,根据层深度(layerDepth)从大到小依次绘制
* SpriteSortMode.Texture - 调用 End() 的时候,绘制精灵,按照纹理的优先级依次绘制
*/
_spriteBatch.Begin(SpriteSortMode.Deferred, null);
_spriteBatch.Draw(_spriteSon, Vector2.Zero, Color.White);
_spriteBatch.Draw(_spriteSon, new Vector2(20, 0), Color.Red);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Immediate, null);
_spriteBatch.Draw(_spriteSon, new Vector2(400, 0), Color.White);
_spriteBatch.Draw(_spriteSon, new Vector2(420, 0), Color.Red);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.FrontToBack, null);
Draw(_spriteSon, new Vector2(20, 200), Color.Red, 0.2f);
Draw(_spriteSon, new Vector2(0, 200), Color.White, 0.1f);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.BackToFront, null);
Draw(_spriteSon, new Vector2(420, 200), Color.Red, 0.1f);
Draw(_spriteSon, new Vector2(400, 200), Color.White, 0.2f);
_spriteBatch.End();
// 按纹理优先级依次绘制的 Demo,下面的测试表明文件大小相对小的将被优先绘制
// 官方文档说明:在不需考虑精灵重叠的情况下,建议用此模式,因为其效率较高
_spriteBatch.Begin(SpriteSortMode.Texture, null);
_spriteBatch.Draw(_spriteSon, new Vector2(20, 400), Color.White); // 40.3 KB
_spriteBatch.Draw(_spriteMVP, new Vector2(0, 400), Color.White); // 5.36 KB
_spriteBatch.End();
base.Update(gameTime);
}
private void Draw(Texture2D texture, Vector2 position, Color color, float layerDepth)
{
_spriteBatch.Draw(texture, position, null, color, 0f, Vector2.Zero, 1f, SpriteEffects.None, layerDepth);
}
}
}
2、用于演示 BlendState 的示例(按键盘 E 键加载此 Demo)
Component/Sprite/BlendStateDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace XNA.Component.Sprite
{
public class BlendStateDemo : Microsoft.Xna.Framework.DrawableGameComponent
{
// 精灵绘制器
SpriteBatch _spriteBatch;
// 精灵对象
Texture2D _sprite;
public BlendStateDemo(Game game)
: base(game)
{
}
public override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(Game.GraphicsDevice);
_sprite = Game.Content.Load<Texture2D>("Image/Son");
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Game.GraphicsDevice.Clear(Color.CornflowerBlue);
/*
* BlendState - 精灵与当前游戏界面的混合方式,默认值为:BlendState.AlphaBlend
* BlendState.AlphaBlend - 使精灵的 Alpha 值有效
* BlendState.Additive - 精灵的 Alpha 值有效,同时精灵与当前游戏界面相互混合
* BlendState.Opaque - 使精灵完全不透明
* BlendState.NonPremultiplied - 在 Content Pipeline 没对内容做 Alpha 预处理的情况下,在 Draw 时及时处理 Alpha 值
* 在内容项目中选择一个资源,然后查看其属性,其 Content Processor -> Premultiply Alpha 默认值为 true,即 Content Pipeline 会对内容做 Alpha 预处理
* 如果内容的 Processor -> Premultiply Alpha 设置为 false,则即使在 BlendState.AlphaBlend 模式下,精灵原有的 Alpha 值也不会生效,此时若要使精灵的 Alpha 值生效,则需要设置为 BlendState.NonPremultiplied
*/
_spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
_spriteBatch.Draw(_sprite, Vector2.Zero, Color.White);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
_spriteBatch.Draw(_sprite, new Vector2(200, 0), Color.White);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
_spriteBatch.Draw(_sprite, new Vector2(400, 0), Color.White);
_spriteBatch.End();
base.Update(gameTime);
}
}
}
OK
[源码下载]
浙公网安备 33010602011771号