protected override void Draw(CanvasDrawingSession drawingSession #if WINDOWS_UWP , CanvasSpriteBatch spriteBatch #endif ) { // 逆向遍历队列,可以让新粒子绘制在旧粒子下方,这样当很多粒子在同一个位置生成时,效果较好 for (int i = ActiveParticles.Count - 1; i >= 0; i--) { Particle particle = ActiveParticles[i]; // NormalizedLifeTime 是一个0到1之间的值,用来表示粒子在生命周期中的进度,这个值接近0或接近1时, // 粒子将会渐隐/渐显,使用它来计算粒子的透明度和缩放 float normalizedLifetime = particle.TimeSinceStart / 4; if (normalizedLifetime > 1) { normalizedLifetime = 1; } // We want particles to fade in and fade out, so we'll calculate alpha to be // (normalizedLifetime) * (1 - normalizedLifetime). This way, when normalizedLifetime // is 0 or 1, alpha is 0. The maximum value is at normalizedLifetime = .5, and is: // // (normalizedLifetime) * (1-normalizedLifetime) // (.5) * (1-.5) // .25 // // Since we want the maximum alpha to be 1, not .25, we'll scale the entire equation by 4. float alpha = (float)EasingHelper.QuinticEase(Windows.UI.Xaml.Media.Animation.EasingMode.EaseOut, normalizedLifetime); var x = particle.ScaleX; var y = particle.ScaleY; // Make particles grow as they age. // They'll start at 75% of their size, and increase to 100% once they're finished. if (isImmersive) { alpha *= 0.8f; x *= 1.2f; y *= 1.2f; } #if WINDOWS_UWP if (spriteBatch != null) { spriteBatch.Draw(smokeSurfaces[particle.Key], particle.Position, new Vector4(1, 1, 1, alpha), bitmapCenter, particle.Rotation, new Vector2(x, y), CanvasSpriteFlip.None); } else #endif { // Compute a transform matrix for this particle. var transform = Matrix3x2.CreateRotation(particle.Rotation, bitmapCenter) * Matrix3x2.CreateScale(x, y, bitmapCenter) * Matrix3x2.CreateTranslation(particle.Position - bitmapCenter); // Draw the particle. drawingSession.DrawImage(smokeSurfaces[particle.Key], 0, 0, bitmapBounds, alpha, CanvasImageInterpolation.Linear, new Matrix4x4(transform)); } } }
public void Update() { if (surfaceLoaded) { if (nowFrame < inFrames) { var progress = nowFrame / (float)inFrames; progress = (float)EasingHelper.QuinticEase(Windows.UI.Xaml.Media.Animation.EasingMode.EaseOut, progress); position.X = (float)(xOffset * (progress - 0.5)) / 2; position.Y = (float)(yOffset * (progress - 0.5)) / 2; opcity = 0.8f * progress; } nowFrame++; rotation = 0.000174532922222222f * nowFrame; } }
public void update(Vector2 size) { if (tempSurface != null && canDraw) { scale.X = (float)(size.X / bound.Width > size.Y / bound.Height ? size.X / bound.Width : size.Y / bound.Height); scale.Y = scale.X; position = size / 2; if (isImmersive) { nowFrame++; if (nowFrame <= inFrames) { opacity = (float)EasingHelper.QuinticEase(Windows.UI.Xaml.Media.Animation.EasingMode.EaseOut, (double)nowFrame / inFrames); } } else if (nowFrame != 0) { nowFrame -= 1; opacity = (float)EasingHelper.QuinticEase(Windows.UI.Xaml.Media.Animation.EasingMode.EaseOut, (double)nowFrame / inFrames); if (nowFrame == 0) { canDraw = false; } } if (enableBlur) { blurFrame++; if (blurFrame <= inFrames) { blur.BlurAmount = blurAmount * (float)EasingHelper.CircleEase(Windows.UI.Xaml.Media.Animation.EasingMode.EaseOut, (double)blurFrame / inFrames); } } else if (blurFrame != 0) { blurFrame--; blur.BlurAmount = blurAmount * (float)EasingHelper.CircleEase(Windows.UI.Xaml.Media.Animation.EasingMode.EaseIn, (double)blurFrame / inFrames); } } }
private double GetElasticFactor(double percent) { return(ElasticFactor * (1 - EasingHelper.QuinticEase(EasingMode.EaseOut, percent))); }