using UnityEngine; using UnityEngine.UI; using sam; public class WorldMapDataVisualizer : MonoBehaviour { public float tileWidth = 0.5f; float htw; //half tile width AreaVariableSubsession avs; Image mapImage; Texture2D texture; int texW; int texH; int scaleFactor; Color[] origImage; ProductData data; ProductVariable prodvar; void Start() { avs = GetComponentInParent(); htw = 0.5f * tileWidth; mapImage = GetComponent(); texture = mapImage.mainTexture as Texture2D; texW = texture.width; texH = texture.height; scaleFactor = texW / 720; if ((texH != (scaleFactor*360)) || (texW != (2*texH))) { Debug.LogError($"WorldMapDataVisualizer: invalid texture size {texW} x {texH}"); } if (origImage == null) { origImage = texture.GetPixels().Clone() as Color[]; } } public void OnProductSelected(ProductData pd) { data = pd; } public void OnVariableSelected(int vi) { prodvar = data.profile.variables[vi]; Color[] img = origImage.Clone() as Color[]; foreach (ProductDataPoint dp in avs.latLonDictionaries[0].Values) { RasterizeDataPoint(dp, img); } texture.SetPixels(img); texture.Apply(); } void RasterizeDataPoint(ProductDataPoint dp, Color[] img) { Color c = avs.GetDataPointColor(dp); float lat = dp.latitude + 90.0f - htw; // y axis float lon = dp.longitude + 180f - htw; // x axis int ilat = Mathf.RoundToInt(lat * 2 * scaleFactor); int ilon = Mathf.RoundToInt(lon * 2 * scaleFactor); int p = (ilat * texW) + ilon; for (int j=0; j < scaleFactor; j++, p += texW) { for (int i=0; i < scaleFactor; i++) { img[p + i] = Color.Lerp(img[p + i], c, c.a); } } } private void OnDestroy() { texture.SetPixels(origImage); texture.Apply(); } }