XNA-粒子系统简介

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

爆炸效果实现思路
粒子效果类
在项目中创建名为ParticleExplosion的新类。确保在文件顶部有下 列的命名空间: using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics;
爆炸效果类的成员变量
// Particle arrays and vertex buffer VertexPositionTexture[] verts; Vector3[] vertexDirectionArray; Color[] vertexColorArray; VertexBuffer particleVertexBuffer; // Position Vector3 position;
// Vertex and graphics info GraphicsDevice graphicsDevice; // Settings ParticleSettings particleSettings; // Effect Effect particleEffect; // Textures Texture2D particleColorsTexture; // Array indices int endOfLiveParticlesIndex = 0; int endOfDeadParticlesIndex = 0;
// Life int lifeLeft;
// Rounds and particle counts int numParticlesPerRound; int maxParticles; static Random rnd = new Random(); int roundTime; int timeSinceLastRound = 0;
XNA游戏开发
粒子系统
粒子系统的基本概念 XNA实现简单3D爆炸效果
粒子系统的作用来自游戏开发人员可以利用粒子特效创造令人兴奋的、真 实的特殊效果——开发人员把它们用在例如烟、火焰、爆 炸、魔幻以及其它特效上,可以使游戏经历更令人兴奋, 也更有吸引力。
粒子是什么?
在游戏开发术语中,粒子通常代表粒子特效中的 一个单一成分。火焰中的一个火花,烟流中的一个分 子,以及魔幻效果中的一个闪光,都是粒子的实例。 在一个特殊效果中集合了多个粒子就叫做粒子特效。 粒子引擎是粒子特效的驱动器。通过施加外力, 例如重力或其它因素,粒子引擎操纵多个粒子创造出 粒子特效,从而使粒子逼近现实中的运动方式。
粒子纹理
简单粒子纹理
包含随机颜色的粒子纹理
粒子系统的基本概念 XNA实现简单3D爆炸效果

粒子效果设置
class ParticleSettings { // Size of particle public int maxSize = 2; } class ParticleExplosionSettings { // Life of particles public int minLife = 1000; public int maxLife = 2000; // Particles per round public int minParticlesPerRound = 100; public int maxParticlesPerRound = 600; // Round time public int minRoundTime = 16;//毫秒 public int maxRoundTime = 50; // Number of particles public int minParticles = 2000; public int maxParticles = 3000; }
爆炸效果类的构造方法
// Multiply by NextDouble to make sure that // all particles move at random speeds direction *= (float)rnd.NextDouble();
// Set direction of particle vertexDirectionArray[i] = direction;
// Create a random velocity/direction Vector3 direction = new Vector3( (float)rnd.NextDouble() * 2 - 1, (float)rnd.NextDouble() * 2 - 1, (float)rnd.NextDouble() * 2 - 1); direction.Normalize();
爆炸效果类的构造方法
public ParticleExplosion(GraphicsDevice graphicsDevice, Vector3 position, int lifeLeft, int roundTime, int numParticlesPerRound, int maxParticles, Texture2D particleColorsTexture, ParticleSettings particleSettings, Effect particleEffect) { this.position = position; this.lifeLeft = lifeLeft; this.numParticlesPerRound = numParticlesPerRound; this.maxParticles = maxParticles; this.roundTime = roundTime; this.graphicsDevice = graphicsDevice; this.particleSettings = particleSettings; this.particleEffect = particleEffect; this.particleColorsTexture = particleColorsTexture; InitializeParticleVertices(); }
爆炸效果类的构造方法
// Loop until max particles for (int i = 0; i < maxParticles; ++i) { float size = (float)rnd.NextDouble() * particleSettings.maxSize; // Set position, direction and size of particle verts[i * 4] = new VertexPositionTexture(position, new Vector2(0, 0)); verts[(i * 4) + 1] = new VertexPositionTexture(new Vector3(position.X, position.Y + size, position.Z), new Vector2(0, 1)); verts[(i * 4) + 2] = new VertexPositionTexture(new Vector3(position.X + size, position.Y, position.Z), new Vector2(1, 0)); verts[(i * 4) + 3] = new VertexPositionTexture(new Vector3(position.X + size, position.Y + size, position.Z), new Vector2(1, 1));
爆炸效果类的更新方法
if (lifeLeft <= 0) { // Increment end of dead particles index each // round until end of list is reached if (endOfDeadParticlesIndex < maxParticles) { endOfDeadParticlesIndex += numParticlesPerRound; if (endOfDeadParticlesIndex > maxParticles) endOfDeadParticlesIndex = maxParticles; } } }
爆炸效果类的更新方法
// Update positions of all live particles for (int i = endOfDeadParticlesIndex; i < endOfLiveParticlesIndex; ++i) { verts[i * 4].Position += vertexDirectionArray[i]; verts[(i * 4) + 1].Position += vertexDirectionArray[i]; verts[(i * 4) + 2].Position += vertexDirectionArray[i]; verts[(i * 4) + 3].Position += vertexDirectionArray[i]; } }
爆炸效果类的更新方法
public void Update(GameTime gameTime) { // Decrement life left until it's gone if (lifeLeft > 0) lifeLeft -= liseconds; // Time for new round? timeSinceLastRound += liseconds; if (timeSinceLastRound > roundTime) { // New round - add and remove particles timeSinceLastRound -= roundTime; // Increment end of live particles index each // round until end of list is reached if (endOfLiveParticlesIndex < maxParticles) { endOfLiveParticlesIndex += numParticlesPerRound; if (endOfLiveParticlesIndex > maxParticles) endOfLiveParticlesIndex = maxParticles; }
爆炸效果类的构造方法
private void InitializeParticleVertices() { // Instantiate all particle arrays verts = new VertexPositionTexture[maxParticles * 4]; vertexDirectionArray = new Vector3[maxParticles]; vertexColorArray = new Color[maxParticles]; // Get color data from colors texture Color[] colors = new Color[particleColorsTexture.Width * particleColorsTexture.Height]; particleColorsTexture.GetData(colors);
// Set color of particle by getting a random color from the texture vertexColorArray[i] = colors[(rnd.Next(0, particleColorsTexture.Height) * particleColorsTexture.Width) + rnd.Next(0, particleColorsTexture.Width)]; } // Instantiate vertex buffer particleVertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionTexture), verts.Length, BufferUsage.None); }
相关文档
最新文档