public virtual void GetHeatmapData(int screenId, int screenWidth, int screenHeight, UserFilter filter, System.Action <HeatmapResult> onResult) { var result = new HeatmapResult(); result.points = new HeatmapResult.Point[Random.Range(200, 500)]; for (int i = 0; i < result.points.Length; ++i) { result.points[i] = new HeatmapResult.Point(); result.points[i].tag = 5; result.points[i].x = Random.Range(0f, 1f); result.points[i].y = Random.Range(0f, 1f); result.points[i].weight = Random.Range(1, 100); } onResult(result); }
public static void GenerateTextureFromData(int screenWidth, int screenHeight, HeatmapResult data, System.Action<Texture2D> onResult) { var texture = new Texture2D(screenWidth, screenHeight, TextureFormat.ARGB32, false); texture = HeatmapVisualizer.Create(texture, data.points, new Vector2(screenWidth, screenHeight)); onResult(texture); }
public static Texture2D Create(Texture2D map, HeatmapResult.Point[] points, Vector2 size) { float maxWeight = 0f; for (int i = 0; i < points.Length; ++i) { if (points[i].weight > maxWeight) { maxWeight = (float)points[i].weight; } } var radius = HeatmapVisualizer.GetRadius(); if (size == Vector2.zero) { return null; } // Create new texture // Texture2D map = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false); if (map == null) { map = new Texture2D((int)size.x, (int)size.y, TextureFormat.ARGB32, false); } // Set texture to alpha-fied state map.SetPixels(HeatmapVisualizer.ColorArray(new Color(1f, 1f, 1f, 0f), map.width * map.height), 0); /*** Generate Grayscale Values ***/ { int x2; // the offset x val in img coordinates int y2; // the offset y val in img coordinates (0,0) - (maxX, maxY) float pointAlpha = .9f; // The alpha that the darkest pixel will be in a point. Color color = new Color(1f, 1f, 1f, pointAlpha); int lineWidth = 1;//(int)(radius * .05f); Dictionary<Vector2Int, Color> pixelAlpha = new Dictionary<Vector2Int, Color>(); for (int i = 0; i < points.Length; ++i) { // generate alpha add for each point and a specified circumference pixelAlpha.Clear(); var weight = points[i].weight / maxWeight; pointAlpha = weight; for (int r = 0; r < radius; r += lineWidth) { // draw and fill them circles for (int angle = 0; angle < 360; ++angle) { x2 = (int)(r * Mathf.Cos(angle)) + (int)(points[i].realPoint.x); y2 = (int)(r * Mathf.Sin(angle)) + (int)(points[i].realPoint.y); // This could be sped up for (int y = y2; y > y2 - lineWidth; --y) { for (int x = x2; x < x2 + lineWidth; ++x) { var coord = new Vector2Int(x, y); if (pixelAlpha.ContainsKey(coord) == true) { pixelAlpha[coord] = color; } else { pixelAlpha.Add(new Vector2Int(x, y), color); } } } } color = new Color(color.r, color.g, color.b, color.a - (pointAlpha / ((float)radius / lineWidth))); } // Since the radial fill code overwrites it's own pixels, make sure to only add finalized alpha to // old values. foreach (KeyValuePair<Vector2Int, Color> keyval in pixelAlpha) { var coord = keyval.Key; Color previousColor = map.GetPixel((int)coord.x, (int)coord.y); Color newColor = keyval.Value; map.SetPixel(coord.x, coord.y, new Color(newColor.r, newColor.b, newColor.g, newColor.a + previousColor.a)); } // Reset color for next point color = new Color(color.r, color.g, color.b, pointAlpha); } } map.Apply(); map.SetPixels32(Colorize(map.GetPixels32(0)), 0); map.Apply(); return map; }
public virtual void GetHeatmapData(int screenId, int screenWidth, int screenHeight, UserFilter filter, System.Action<HeatmapResult> onResult) { var result = new HeatmapResult(); result.points = new HeatmapResult.Point[Random.Range(200, 500)]; for (int i = 0; i < result.points.Length; ++i) { result.points[i] = new HeatmapResult.Point(); result.points[i].tag = 5; result.points[i].x = Random.Range(0f, 1f); result.points[i].y = Random.Range(0f, 1f); result.points[i].weight = Random.Range(1, 100); } onResult(result); }