/// <summary> /// 发射粒子 /// </summary> /// <param name="position">起始位置</param> /// <param name="speed">速率</param> /// <param name="size">起始半径</param> /// <param name="life">生命周期</param> public void SpawnParticle(Point3D position, double speed, double size, double life) { // 不能超过最大粒子数 if (this.particleList.Count > this.MaxParticleCount) { return; } // 建立粒子描述子 ParticleDescriptor3D p = new ParticleDescriptor3D { Position = position, StartLife = life, Life = life, StartSize = size, Size = size }; // 根据速率构造速度向量 float x = 1.0f - (float)rand.NextDouble() * 2.0f; float z = 1.0f - (float)rand.NextDouble() * 2.0f; Vector3D v = new Vector3D(x, z, 0.0); v.Normalize(); v *= ((float)rand.NextDouble() + 0.25f) * (float)speed; p.Velocity = new Vector3D(v.X, v.Y, v.Z); p.Decay = 1.0f; // 添加到渲染列表 this.particleList.Add(p); }
/// <summary> /// 依照更新完的描述子渲染前端 /// </summary> private void UpdateGeometry() { Point3DCollection positions = new Point3DCollection(); Int32Collection indices = new Int32Collection(); PointCollection texcoords = new PointCollection(); for (int i = 0; i < this.particleList.Count; ++i) { int positionIndex = i * 4; ParticleDescriptor3D p = this.particleList[i]; Point3D p1 = new Point3D(p.Position.X, p.Position.Y, p.Position.Z); Point3D p2 = new Point3D(p.Position.X, p.Position.Y + p.Size, p.Position.Z); Point3D p3 = new Point3D(p.Position.X + p.Size, p.Position.Y + p.Size, p.Position.Z); Point3D p4 = new Point3D(p.Position.X + p.Size, p.Position.Y, p.Position.Z); positions.Add(p1); positions.Add(p2); positions.Add(p3); positions.Add(p4); texcoords.Add(renderDirectionT1); texcoords.Add(renderDirectionT2); texcoords.Add(renderDirectionT3); texcoords.Add(renderDirectionT4); indices.Add(positionIndex); indices.Add(positionIndex + 2); indices.Add(positionIndex + 1); indices.Add(positionIndex); indices.Add(positionIndex + 3); indices.Add(positionIndex + 2); } ((MeshGeometry3D)this.particleModel.Geometry).Positions = positions; ((MeshGeometry3D)this.particleModel.Geometry).TriangleIndices = indices; ((MeshGeometry3D)this.particleModel.Geometry).TextureCoordinates = texcoords; }