Beispiel #1
0
        //---------------------------------------------------------------
        #endregion
        //---------------------------------------------------------------

        //---------------------------------------------------------------
        #region Methods
        //---------------------------------------------------------------
        /// <summary>
        /// Updates the particle system.
        /// </summary>
        /// <param name="deltaTime">The time since the last update.</param>
        public override void Update(float deltaTime)
        {
            base.Update(deltaTime);

            Matrix4 m     = Device.Instance.Transformations.View;
            Vector3 right = m.RightVector;
            Vector3 up    = m.UpVector;

            // update streams
            PositionStream posStream     = (PositionStream)vertexUnit[typeof(PositionStream)];
            ColorStream    colorStream   = (ColorStream)vertexUnit[typeof(ColorStream)];
            TextureStream  textureStream = (TextureStream)vertexUnit[typeof(TextureStream)];

            // Update all particles
            for (int i = 0; i < particles.Count; i++)
            {
                int         offset     = i * 4;
                IParticle   particle   = particles[i] as IParticle;
                IParticle3d particle3d = particles[i] as IParticle3d;
                if (particle3d != null)
                {
                    Vector3 position = particle3d.Position;
                    posStream[offset]     = position - particle.Size.X * right + particle.Size.Y * up;
                    posStream[offset + 1] = position + particle.Size.X * right + particle.Size.Y * up;
                    posStream[offset + 2] = position + particle.Size.X * right - particle.Size.Y * up;
                    posStream[offset + 3] = position - particle.Size.X * right - particle.Size.Y * up;
                }
                IParticleColor particleColor = particles[i] as IParticleColor;
                if (particleColor != null)
                {
                    colorStream[offset]     = particleColor.Color;
                    colorStream[offset + 1] = particleColor.Color;
                    colorStream[offset + 2] = particleColor.Color;
                    colorStream[offset + 3] = particleColor.Color;
                }
                IParticleIndex            particleIndex = particles[i] as IParticleIndex;
                System.Drawing.RectangleF tc;
                if (particleIndex == null || subTextures == null)
                {
                    tc = (Texture as ITexture2d).TextureCoordinates;
                }
                else
                {
                    tc = subTextures[(int)particleIndex.TextureIndex % subTextures.Length].TextureCoordinates;
                }
                textureStream[offset]     = new Vector2(tc.Left, tc.Top);
                textureStream[offset + 1] = new Vector2(tc.Right, tc.Top);
                textureStream[offset + 2] = new Vector2(tc.Right, tc.Bottom);
                textureStream[offset + 3] = new Vector2(tc.Left, tc.Bottom);
            }
            posStream.Upload();
            colorStream.Upload();
            textureStream.Upload();
        }
Beispiel #2
0
        //---------------------------------------------------------------
        #endregion
        //---------------------------------------------------------------

        //---------------------------------------------------------------
        #region Methods
        //---------------------------------------------------------------
        /// <summary>
        /// Updates the particle system.
        /// </summary>
        /// <param name="deltaTime">The time since the last update.</param>
        public override void Update(float deltaTime)
        {
            UpdateAge(deltaTime);
            // delete dead particles
            FixedRoundBuffer buffer = particles as FixedRoundBuffer;

            while (buffer.Count > 0 && !(buffer[0] as IParticle).IsAlive)
            {
                buffer.RemoveFirst();
            }

            UpdateParticles(deltaTime);
            EmitParticles(deltaTime);

            ITexture2d texture = Texture;

            if (particles.Count >= 2)
            {
                // update streams
                PositionStream posStream     = (PositionStream)vertexUnit[typeof(PositionStream)];
                ColorStream    colorStream   = (ColorStream)vertexUnit[typeof(ColorStream)];
                TextureStream  textureStream = (TextureStream)vertexUnit[typeof(TextureStream)];
                // Update all particles
                for (int i = 0; i < particles.Count; i++)
                {
                    int         offset     = i * 2;
                    IParticle   particle   = particles[i] as IParticle;
                    IParticle3d particle3d = particles[i] as IParticle3d;
                    if (particle3d != null)
                    {
                        Vector3 position = particle3d.Position;
                        posStream[offset]     = position + particle.Size.X * (particle as IParticleOrientation).Orientation;
                        posStream[offset + 1] = position - particle.Size.X * (particle as IParticleOrientation).Orientation;
                    }
                    IParticleColor particleColor = particles[i] as IParticleColor;
                    if (particleColor != null)
                    {
                        colorStream[offset]     = particleColor.Color;
                        colorStream[offset + 1] = particleColor.Color;
                    }
                    float tx = texture.TextureCoordinates.Left + texture.TextureCoordinates.Width * particle.Age / particle.LifeTime;
                    textureStream[offset]     = new Vector2(tx, texture.TextureCoordinates.Top);
                    textureStream[offset + 1] = new Vector2(tx, texture.TextureCoordinates.Bottom);
                }
                posStream.Upload();
                colorStream.Upload();
                textureStream.Upload();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Sets the position of the particle.
        /// </summary>
        /// <param name="particle">Particle for which to set position.</param>
        /// <param name="position">Position to set.</param>
        public static void SetPosition(IParticle particle, Vector3 position)
        {
            IParticle3d p3d = (particle as IParticle3d);

            if (p3d != null)
            {
                p3d.Position = position;
            }
            else
            {
                IParticle2d p2d = (particle as IParticle2d);
                if (p2d != null)
                {
                    p2d.Position = position.Vector2;
                }
            }
        }