private void EmitNewParticle(ParticleEffect particleEffect, float lerpAmount, ref Particle particle) { if (Emit(lerpAmount, ref particle.Position, ref particle.Velocity)) { particle.Alpha = 1; particle.Rotation = Rotation.Min + (float)random.NextDouble() * (Rotation.Max - Rotation.Min); particle.Duration = Duration.Min + (float)random.NextDouble() * (Duration.Max - Duration.Min); particle.Velocity *= Speed.Min + (float)random.NextDouble() * (Speed.Max - Speed.Min); particle.Size = Size.Min + (float)random.NextDouble() * (Size.Max - Size.Min); particle.Color = Microsoft.Xna.Framework.Color.Lerp(Color.Min, Color.Max, (float)random.NextDouble()); Vector3.Transform(ref particle.Position, ref transform, out particle.Position); Vector3.TransformNormal(ref particle.Velocity, ref transform, out particle.Velocity); particleEffect.Emit(ref particle); } }
static void UpdateAsync(ParticleEffect particleEffect) { if (ActiveUpdates == null) { ParticleQueueSyncEvent = new AutoResetEvent(false); ActiveUpdates = new ConcurrentQueue <ParticleEffect>(); #if WINRT Windows.System.Threading.ThreadPool.RunAsync(op => ParticleUpdateWorker(), Windows.System.Threading.WorkItemPriority.Normal, Windows.System.Threading.WorkItemOptions.TimeSliced); #else var ParticleThread = new Thread((ThreadStart)ParticleUpdateWorker); ParticleThread.IsBackground = true; ParticleThread.Name = "ParticleEffect"; ParticleThread.Start(); #endif } ActiveUpdates.Enqueue(particleEffect); ParticleQueueSyncEvent.Set(); }
/// <summary> /// Updates the emitter, emits any new particles during the update. /// </summary> public bool Update(ParticleEffect particleEffect, float elapsedSeconds) { if (!Enabled || elapsedTime < 0) { return(false); } // Check for delay elapsedTime += elapsedSeconds; if (elapsedTime < Delay.TotalSeconds) { return(false); } if (elapsedTime > Lifetime.TotalSeconds + Delay.TotalSeconds) { return(true); } Particle particle = new Particle(); if (EmitCount > 0) { for (int i = 0; i < EmitCount; ++i) { EmitNewParticle(particleEffect, 0, ref particle); } return(true); } // Emit when the particle emitter has just started. if (!firstParticleEmitted) { EmitNewParticle(particleEffect, 0, ref particle); firstParticleEmitted = true; } // Work out how much time has passed since the previous update. float timeBetweenParticles = 1.0f / Emission; // If we had any time left over that we didn't use during the // previous update, add that to the current elapsed time. float timeToSpend = timeLeftOver + elapsedSeconds; // Counter for looping over the time interval. float currentTime = -timeLeftOver; // Create particles as long as we have a big enough time interval. while (timeToSpend > timeBetweenParticles) { currentTime += timeBetweenParticles; timeToSpend -= timeBetweenParticles; // Work out the optimal position for this particle. This will produce // evenly spaced particles regardless of the object speed, particle // creation frequency, or game update rate. float mu = currentTime / elapsedSeconds; // Emit a new particle EmitNewParticle(particleEffect, mu, ref particle); } // Store any time we didn't use, so it can be part of the next update. timeLeftOver = timeToSpend; return(false); }