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 void UpdateMap() { if (this.texture == null) { this.texture = new Texture2D((int)this.size.x, (int)this.size.y, TextureFormat.ARGB32, false); } if (this.changed == false) { return; } this.texture = HeatmapVisualizer.Create(this.texture, this.GetPoints(), this.size); this.changed = false; }
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 static Texture2D Create(Texture2D map, List <UnityEngine.UI.Windows.Plugins.Heatmap.Core.HeatmapSettings.Point> normalizedPoints, Vector2 size, int radius = 10) { 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 poitn. Color color = new Color(1f, 1f, 1f, pointAlpha); int lineWidth = 1; //(int)(radius * .05f); Dictionary <Vector2, Color> pixelAlpha = new Dictionary <Vector2, Color>(); for (int i = 0; i < normalizedPoints.Count; i++) // generate alpha add for each point and a specified circumference { pixelAlpha.Clear(); 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)(normalizedPoints[i].GetAbsoluteX(size.x)); y2 = (int)(r * Mathf.Sin(angle)) + (int)(normalizedPoints[i].GetAbsoluteY(size.y)); // This could be sped up for (int y = y2; y > y2 - lineWidth; y--) { for (int x = x2; x < x2 + lineWidth; x++) { Vector2 coord = new Vector2(x, y); if (pixelAlpha.ContainsKey(coord)) { pixelAlpha[coord] = color; } else { pixelAlpha.Add(new Vector2(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 <Vector2, Color> keyval in pixelAlpha) { Vector2 coord = keyval.Key; Color previousColor = map.GetPixel((int)coord.x, (int)coord.y); Color newColor = keyval.Value; map.SetPixel((int)coord.x, (int)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.SetPixels(Colorize(map.GetPixels(0)), 0); map.Apply(); return(map); }