using System; using System.Collections.Generic; using System.Diagnostics; using Azimuth; using Blacklight.Core.Cameras; namespace Konis.Testbed { /// /// Works out what the selected effect, or all of them, cost this frame, counting every emitter an effect runs. /// public class StatsCollector { /// /// The side in pixels of the squares the screen is divided into to estimate coverage. /// public const int CellPixels = 4; private byte[] covered = new byte[0]; private int columns; private int rows; private long hits; private long uniqueCells; private void ResetGrid(int width, int height) { columns = (width + CellPixels - 1) / CellPixels; rows = (height + CellPixels - 1) / CellPixels; if (covered.Length != columns * rows) { covered = new byte[columns * rows]; } else { for (int i = 0; i < covered.Length; i++) { covered[i] = 0; } } hits = 0; uniqueCells = 0; } // A cell counts for a sprite when its center is inside the sprite, which is a square turned by its rotation. private void Cover(StandardEmitter emitter, ViewProjection projection, Camera camera, double pixelsPerWorld, int width, int height) { Particle[] particles = emitter.Store.Items; Vector3 origin = emitter.Position; for (int i = 0; i < emitter.Store.Count; i++) { Vector scene = projection.Project(new Vector3( origin.X + particles[i].PositionX, origin.Y + particles[i].PositionY, origin.Z + particles[i].PositionZ)); Vector centered = camera.WorldToProjection.TransformPoint(scene); double centerX = centered.X + width / 2.0; double centerY = height / 2.0 - centered.Y; double half = emitter.SizeOf(i) * pixelsPerWorld / 2; double angle = emitter.RotationOf(i) * 2.0 * Math.PI; double cos = Math.Cos(angle); double sin = Math.Sin(angle); double reach = half * (Math.Abs(cos) + Math.Abs(sin)); int firstColumn = Math.Max(0, (int)Math.Floor((centerX - reach) / CellPixels)); int lastColumn = Math.Min(columns - 1, (int)Math.Floor((centerX + reach) / CellPixels)); int firstRow = Math.Max(0, (int)Math.Floor((centerY - reach) / CellPixels)); int lastRow = Math.Min(rows - 1, (int)Math.Floor((centerY + reach) / CellPixels)); for (int row = firstRow; row <= lastRow; row++) { for (int column = firstColumn; column <= lastColumn; column++) { // The offset of the cell's center, with y up as the sprite's rotation counts it. double across = (column + 0.5) * CellPixels - centerX; double up = centerY - (row + 0.5) * CellPixels; double along = across * cos + up * sin; double athwart = -across * sin + up * cos; if (Math.Abs(along) > half || Math.Abs(athwart) > half) { continue; } hits++; if (covered[row * columns + column] == 0) { covered[row * columns + column] = 1; uniqueCells++; } } } } } private static double PixelsPerWorldUnit(Camera camera) { Vector origin = camera.ProjectionToWorld.TransformPoint(new Vector(0, 0)); Vector across = camera.ProjectionToWorld.TransformPoint(new Vector(1, 0)); double worldPerPixel = Math.Sqrt((across.X - origin.X) * (across.X - origin.X) + (across.Y - origin.Y) * (across.Y - origin.Y)); return worldPerPixel == 0 ? 1 : 1 / worldPerPixel; } private static int IndexOf(IReadOnlyList effects, SpawnedEffect effect) { for (int i = 0; i < effects.Count; i++) { if (effects[i] == effect) { return i; } } return -1; } /// /// Measures , or every one of when it is null, as the /// camera shows them through , in a window pixels wide /// and high. /// public EmitterStats Measure(IReadOnlyList effects, SpawnedEffect selected, ViewProjection projection, Camera camera, int width, int height) { var returnMe = new EmitterStats(); returnMe.IsTotal = selected == null; returnMe.Title = selected == null ? $"All emitters ({effects.Count})" : $"{Effects.All[selected.EffectIndex].Name} {IndexOf(effects, selected) + 1}"; ResetGrid(width, height); double pixelsPerWorld = PixelsPerWorldUnit(camera); double millisecondsPerTick = 1000.0 / Stopwatch.Frequency; foreach (SpawnedEffect effect in effects) { if (selected != null && effect != selected) { continue; } returnMe.Age = effect.Emitter.Age; returnMe.Lifespan = effect.Emitter.Lifespan; returnMe.WriteMilliseconds += effect.AverageWriteTicks * millisecondsPerTick; foreach (Emitter running in effect.Running.Emitters) { var emitter = (StandardEmitter)running; returnMe.Particles += emitter.Store.Count; returnMe.Capacity += emitter.Store.Capacity; returnMe.Rate += emitter.Rate * emitter.Intensity; returnMe.DroppedBirths += emitter.DroppedBirths; returnMe.UpdateMilliseconds += emitter.AverageUpdateTicks * millisecondsPerTick; returnMe.Bytes += emitter.Store.ByteSize; Cover(emitter, projection, camera, pixelsPerWorld, width, height); } } returnMe.FillPixels = hits * CellPixels * CellPixels; returnMe.CoveredPixels = uniqueCells * CellPixels * CellPixels; returnMe.ScreenFraction = (double)returnMe.CoveredPixels / Math.Max(1, (long)width * height); returnMe.Overdraw = uniqueCells == 0 ? 0 : (double)hits / uniqueCells; return returnMe; } } }