void ComputeBounds() { Vector2 min = Misc.Vector2max; Vector2 max = Misc.Vector2min; for (int k = 0; k < points.Length; k++) { float x = points [k].x; float y = points [k].y; if (x < min.x) { min.x = x; } if (x > max.x) { max.x = x; } if (y < min.y) { min.y = y; } if (y > max.y) { max.y = y; } } rect2D = new Rect(min.x, min.y, max.x - min.x, max.y - min.y); rect2DArea = rect2D.width * rect2D.height; FastVector.Average(ref min, ref max, ref center); // center = (min + max) * 0.5f; }
void CheckTiles(TileInfo parent, int currentZoomLevel, int xTile, int yTile, int zoomLevel, int subquadIndex) { // Is this tile visible? TileInfo ti; int tileCode = GetTileHashCode(xTile, yTile, zoomLevel); if (!cachedTiles.TryGetValue(tileCode, out ti)) { ti = new TileInfo(xTile, yTile, zoomLevel, subquadIndex, currentEarthTexture); ti.parent = parent; if (parent != null) { if (parent.children == null) { parent.children = new List <TileInfo> (); } parent.children.Add(ti); } for (int k = 0; k < 4; k++) { Vector2 latlon = Conversion.GetLatLonFromTile(xTile + offsets [k].x, yTile + offsets [k].y, zoomLevel); ti.latlons [k] = latlon; ti.cornerLocalPos [k] = Conversion.GetLocalPositionFromLatLon(latlon); } cachedTiles [tileCode] = ti; } // Check if tile is within restricted area if (_tileRestrictToArea) { if (ti.latlons[0].x <_tileMinMaxLatLon.x || ti.latlons[2].x> _tileMinMaxLatLon.z || ti.latlons[0].y > _tileMinMaxLatLon.w || ti.latlons[2].y < _tileMinMaxLatLon.y) { return; } } // Check if any tile corner is visible // Phase I Vector3 minWorldPos = Misc.Vector3max; Vector3 maxWorldPos = Misc.Vector3min; Vector3 tmp = Misc.Vector3zero; for (int c = 0; c < 4; c++) { Vector3 wpos = transform.TransformPoint(ti.cornerLocalPos [c]); ti.cornerWorldPos [c] = wpos; if (wpos.x < minWorldPos.x) { minWorldPos.x = wpos.x; } if (wpos.y < minWorldPos.y) { minWorldPos.y = wpos.y; } if (wpos.z < minWorldPos.z) { minWorldPos.z = wpos.z; } if (wpos.x > maxWorldPos.x) { maxWorldPos.x = wpos.x; } if (wpos.y > maxWorldPos.y) { maxWorldPos.y = wpos.y; } if (wpos.z > maxWorldPos.z) { maxWorldPos.z = wpos.z; } } FastVector.Average(ref minWorldPos, ref maxWorldPos, ref tmp); Bounds bounds = new Bounds(tmp, maxWorldPos - minWorldPos); Vector3 tileMidPoint = bounds.center; #if DEBUG_TILES if (root == null) { root = new GameObject(); root.transform.SetParent(transform); root.transform.localPosition = Vector3.zero; root.transform.localRotation = Misc.QuaternionZero; //Quaternion.Euler (0, 0, 0); } #endif bool insideViewport = false; float minX = currentCamera.pixelWidth * 2f, minY = currentCamera.pixelHeight * 2f; float maxX = -minX, maxY = -minY; for (int c = 0; c < 4; c++) { Vector3 scrPos = currentCamera.WorldToScreenPoint(ti.cornerWorldPos [c]); insideViewport = insideViewport || (scrPos.z > 0 && scrPos.x >= 0 && scrPos.x < currentCamera.pixelWidth && scrPos.y >= 0 && scrPos.y < currentCamera.pixelHeight); if (scrPos.x < minX) { minX = scrPos.x; } if (scrPos.x > maxX) { maxX = scrPos.x; } if (scrPos.y < minY) { minY = scrPos.y; } if (scrPos.y > maxY) { maxY = scrPos.y; } } if (!insideViewport) { insideViewport = GeometryUtility.TestPlanesAABB(cameraPlanes, bounds); if (!insideViewport && _wrapHorizontally && _wrapCamera.enabled) { insideViewport = GeometryUtility.TestPlanesAABB(wrapCameraPlanes, bounds); } } ti.insideViewport = insideViewport; ti.visible = false; if (insideViewport) { if (!ti.created) { CreateTile(ti); } if (!ti.gameObject.activeSelf) { ti.gameObject.SetActive(true); } // Manage hierarchy of tiles float aparentSize = maxY - minY; bool tileIsBig = aparentSize > currentTileSize; #if DEBUG_TILES if (ti.gameObject != null) { ti.gameObject.GetComponent <TileInfoEx> ().bigTile = tileIsBig; ti.gameObject.GetComponent <TileInfoEx> ().zoomLevel = ti.zoomLevel; } #endif if ((tileIsBig || zoomLevel < TILE_MIN_ZOOM_LEVEL) && zoomLevel < _tileMaxZoomLevel) { // Load nested tiles CheckTiles(ti, currentZoomLevel, xTile * 2, yTile * 2, zoomLevel + 1, 0); CheckTiles(ti, currentZoomLevel, xTile * 2 + 1, yTile * 2, zoomLevel + 1, 1); CheckTiles(ti, currentZoomLevel, xTile * 2, yTile * 2 + 1, zoomLevel + 1, 2); CheckTiles(ti, currentZoomLevel, xTile * 2 + 1, yTile * 2 + 1, zoomLevel + 1, 3); ti.renderer.enabled = false; } else { ti.visible = true; // Show tile renderer if (!ti.renderer.enabled) { ti.renderer.enabled = true; } // If parent tile is loaded then use that as placeholder texture if (ti.zoomLevel > TILE_MIN_ZOOM_LEVEL && ti.parent.loadStatus == TILE_LOAD_STATUS.Loaded && !ti.placeholderImageSet) { ti.placeholderImageSet = true; ti.parentTextureCoords = placeHolderUV [ti.subquadIndex]; ti.SetPlaceholderImage(ti.parent.texture); } if (ti.loadStatus == TILE_LOAD_STATUS.Loaded) { if (!ti.hasAnimated) { ti.hasAnimated = true; ti.Animate(1f, AnimationEnded); } } else if (ti.loadStatus == TILE_LOAD_STATUS.Inactive) { ti.distToCamera = FastVector.SqrDistance(ref ti.cornerWorldPos [0], ref currentCameraPosition) * ti.zoomLevel; ti.loadStatus = TILE_LOAD_STATUS.InQueue; ti.queueTime = Time.time; loadQueue.Add(ti); } if (ti.children != null) { for (int k = 0; k < 4; k++) { TileInfo tiChild = ti.children [k]; HideTile(tiChild); } } } } else { HideTile(ti); } }
/// <summary> /// Used internally by the Map Editor. It will recalculate de boundaries and optimize frontiers based on new data of provinces array /// </summary> public void RefreshProvinceGeometry(IAdminEntity province) { lastProvinceLookupCount = -1; float maxVol = 0; if (province.regions == null) { ReadProvincePackedString(province); } if (province.regions == null) { return; } int regionCount = province.regions.Count; Vector2 minProvince = Misc.Vector2one * 10; Vector2 maxProvince = -minProvince; Vector2 min = Misc.Vector2one * 10f; Vector2 max = -min; for (int r = 0; r < regionCount; r++) { Region provinceRegion = province.regions [r]; provinceRegion.entity = province; // just in case one country has been deleted provinceRegion.regionIndex = r; // just in case a region has been deleted int coorCount = provinceRegion.points.Length; min.x = min.y = 10f; max.x = max.y = -10f; for (int c = 0; c < coorCount; c++) { float x = provinceRegion.points [c].x; float y = provinceRegion.points [c].y; if (x < min.x) { min.x = x; } if (x > max.x) { max.x = x; } if (y < min.y) { min.y = y; } if (y > max.y) { max.y = y; } } FastVector.Average(ref min, ref max, ref provinceRegion.center); if (min.x < minProvince.x) { minProvince.x = min.x; } if (min.y < minProvince.y) { minProvince.y = min.y; } if (max.x > maxProvince.x) { maxProvince.x = max.x; } if (max.y > maxProvince.y) { maxProvince.y = max.y; } provinceRegion.rect2D = new Rect(min.x, min.y, max.x - min.x, max.y - min.y); provinceRegion.rect2DArea = provinceRegion.rect2D.width * provinceRegion.rect2D.height; float vol = FastVector.SqrDistance(ref max, ref min); // (max - min).sqrMagnitude; if (vol > maxVol) { maxVol = vol; province.mainRegionIndex = r; province.center = provinceRegion.center; } } province.regionsRect2D = new Rect(minProvince.x, minProvince.y, Math.Abs(maxProvince.x - minProvince.x), Mathf.Abs(maxProvince.y - minProvince.y)); }
/// <summary> /// Unpacks province geodata information. Used by Map Editor. /// </summary> /// <param name="province">Province.</param> public void ReadProvincePackedString(IAdminEntity entity) { Province province = (Province)entity; if (province == null || province.packedRegions == null) { return; } string[] regions = province.packedRegions.Split(SPLIT_SEP_ASTERISK, StringSplitOptions.RemoveEmptyEntries); int regionCount = regions.Length; province.regions = new List <Region> (regionCount); float maxVol = float.MinValue; Vector2 minProvince = Misc.Vector2one * 10; Vector2 maxProvince = -minProvince; for (int r = 0; r < regionCount; r++) { string[] coordinates = regions [r].Split(SPLIT_SEP_SEMICOLON, StringSplitOptions.RemoveEmptyEntries); int coorCount = coordinates.Length; Vector2 min = Misc.Vector2one * 10; Vector2 max = -min; Region provinceRegion = new Region(province, province.regions.Count); provinceRegion.points = new Vector2[coorCount]; for (int c = 0; c < coorCount; c++) { float x, y; GetPointFromPackedString(ref coordinates [c], out x, out y); if (x < min.x) { min.x = x; } if (x > max.x) { max.x = x; } if (y < min.y) { min.y = y; } if (y > max.y) { max.y = y; } provinceRegion.points [c].x = x; provinceRegion.points [c].y = y; } FastVector.Average(ref min, ref max, ref provinceRegion.center); provinceRegion.sanitized = true; province.regions.Add(provinceRegion); // Calculate province bounding rect if (min.x < minProvince.x) { minProvince.x = min.x; } if (min.y < minProvince.y) { minProvince.y = min.y; } if (max.x > maxProvince.x) { maxProvince.x = max.x; } if (max.y > maxProvince.y) { maxProvince.y = max.y; } provinceRegion.rect2D = new Rect(min.x, min.y, max.x - min.x, max.y - min.y); provinceRegion.rect2DArea = provinceRegion.rect2D.width * provinceRegion.rect2D.height; float vol = FastVector.SqrDistance(ref min, ref max); // (max - min).sqrMagnitude; if (vol > maxVol) { maxVol = vol; province.mainRegionIndex = r; province.center = provinceRegion.center; } } province.regionsRect2D = new Rect(minProvince.x, minProvince.y, Math.Abs(maxProvince.x - minProvince.x), Mathf.Abs(maxProvince.y - minProvince.y)); }
/// <summary> /// Used internally by the Map Editor. It will recalculate de boundaries and optimize frontiers based on new data of countries array /// </summary> public void RefreshCountryGeometry(IAdminEntity country) { float maxVol = 0; if (country.regions == null) { return; } int regionCount = country.regions.Count; Vector2 min = Misc.Vector2one * 10; Vector2 max = -min; Vector2 minCountry = Misc.Vector2one * 10; Vector2 maxCountry = -minCountry; for (int r = 0; r < regionCount; r++) { Region countryRegion = country.regions [r]; if (countryRegion.points == null) { continue; } countryRegion.entity = country; // just in case one country has been deleted countryRegion.regionIndex = r; // just in case a region has been deleted int coorCount = countryRegion.points.Length; min.x = min.y = 10f; max.x = max.y = -10; for (int c = 0; c < coorCount; c++) { float x = countryRegion.points [c].x; float y = countryRegion.points [c].y; if (x < min.x) { min.x = x; } if (x > max.x) { max.x = x; } if (y < min.y) { min.y = y; } if (y > max.y) { max.y = y; } } FastVector.Average(ref min, ref max, ref countryRegion.center); // countryRegion.center = (min + max) * 0.5f; // Calculate country bounding rect if (min.x < minCountry.x) { minCountry.x = min.x; } if (min.y < minCountry.y) { minCountry.y = min.y; } if (max.x > maxCountry.x) { maxCountry.x = max.x; } if (max.y > maxCountry.y) { maxCountry.y = max.y; } // Calculate bounding rect countryRegion.rect2D = new Rect(min.x, min.y, max.x - min.x, max.y - min.y); countryRegion.rect2DArea = countryRegion.rect2D.width * countryRegion.rect2D.height; float vol = FastVector.SqrDistance(ref max, ref min); // (max - min).sqrMagnitude; if (vol > maxVol) { maxVol = vol; country.mainRegionIndex = r; country.center = countryRegion.center; } } country.regionsRect2D = new Rect(minCountry.x, minCountry.y, Math.Abs(maxCountry.x - minCountry.x), Mathf.Abs(maxCountry.y - minCountry.y)); }
void DrawTextMeshProLabels() { if (labelsFontTMPro == null) { return; } float mw = mapWidth; float mh = mapHeight; Vector2 center = Misc.Vector2zero; for (int countryIndex = 0; countryIndex < _countries.Length; countryIndex++) { Country country = _countries[countryIndex]; if (country.hidden || !country.labelVisible || country.mainRegionIndex < 0 || country.mainRegionIndex >= country.regions.Count) { continue; } Region region = country.regions[country.mainRegionIndex]; CurvedLabelInfo curvedLabel; if (!ComputeCurvedLabelData(region, out curvedLabel)) { continue; } if (country.labelOffset.x != 0 || country.labelOffset.y != 0) { center = country.center + country.labelOffset; } else { FastVector.Average(ref curvedLabel.axisStart, ref curvedLabel.axisEnd, ref center); // (curvedLabel.axisStart + curvedLabel.axisEnd) * 0.5f; } center.x *= mw; center.y *= mh; // Adjusts country name length string countryName = country.customLabel != null ? country.customLabel : country.name.ToUpper(); if (countryName.Length == 0) { continue; } if (countryName.Length > 15) { countryName = BreakOneLineString(countryName); } // add caption GameObject textObj; TextMeshPro tm; Color labelColor = country.labelColorOverride ? country.labelColor : _countryLabelsColor; if (country.labelTextMeshGO == null) { // create base text textObj = new GameObject(countryName); textObj.hideFlags = HideFlags.DontSave; textObj.hideFlags |= HideFlags.HideInHierarchy; tm = textObj.AddComponent <TextMeshPro>(); tm.alignment = TextAlignmentOptions.Center; tm.enableWordWrapping = false; country.labelTextMeshPro = tm; country.labelTextMeshGO = tm.gameObject; textObj.transform.SetParent(textRoot.transform, false); textObj.layer = textRoot.gameObject.layer; } else { tm = (TextMeshPro)country.labelTextMeshPro; textObj = tm.gameObject; textObj.transform.localPosition = center; } tm.font = (TMP_FontAsset)labelsFontTMPro; tm.color = labelColor; if (_countryLabelsEnableAutomaticFade) { // By using fontMaterial we're forcing to instantiate the material which will enable individual colors and alpha Material mat = tm.fontMaterial; mat.SetColor("_OutlineColor", _countryLabelsOutlineColor); mat.SetFloat("_OutlineWidth", _countryLabelsOutlineWidth); mat.hideFlags = HideFlags.DontSave; } else { tm.fontSharedMaterial.SetColor("_OutlineColor", _countryLabelsOutlineColor); tm.fontSharedMaterial.SetFloat("_OutlineWidth", _countryLabelsOutlineWidth); } tm.text = countryName; textObj.transform.localPosition = center; country.labelMeshWidth = tm.preferredWidth; country.labelMeshHeight = tm.preferredHeight; country.labelMeshCenter = center; float meshWidth = country.labelMeshWidth; float meshHeight = country.labelMeshHeight; // adjusts scale to fit in region Vector2 axis = curvedLabel.axisEnd - curvedLabel.axisStart; float scale; if (country.labelFontSizeOverride) { scale = country.labelFontSize; } else { // axisWidth represents the length of the label along the longest axis float axisWidth = new Vector2(axis.x * mw, axis.y * mh).magnitude; // axisAveragedWidth represents the average length of the region (used as a maximum height for the label) float axisAveragedThickness = new Vector2(curvedLabel.axisAveragedThickness.x * mw, curvedLabel.axisAveragedThickness.y * mh).magnitude; float scaleheight = axisAveragedThickness / meshHeight; float scaleWidth = axisWidth / meshWidth; scale = Mathf.Min(scaleWidth, scaleheight); if (meshHeight * scale < _countryLabelsAbsoluteMinimumSize) { scale = _countryLabelsAbsoluteMinimumSize / meshHeight; } scale *= _countryLabelsSize * 2f; } // apply scale textObj.transform.localScale = new Vector3(scale, scale, 1); // Apply axis rotation or user defined rotation if (country.labelRotation > 0) { textObj.transform.localRotation = Quaternion.Euler(0, 0, country.labelRotation); } else { textObj.transform.localRotation = Quaternion.Euler(0, 0, curvedLabel.axisAngle); } if (_countryLabelsCurvature > 0) { // Compute fitting curve if (labelsCurve == null || labelsCurve.length == 0) { ComputeLabelsCurve(); } tm.havePropertiesChanged = true; // Need to force the TextMeshPro Object to be updated. tm.ForceMeshUpdate(); // Generate the mesh and populate the textInfo with data we can use and manipulate. TMP_TextInfo textInfo = tm.textInfo; int characterCount = textInfo.characterCount; float boundsMinX = tm.bounds.min.x; float boundsMaxX = tm.bounds.max.x; // map bounds to axis length float axisLengthWS = new Vector2(axis.x * mw / scale, axis.y * mh / scale).magnitude; float boundsLength = boundsMaxX - boundsMinX; float boundsMid = (boundsMaxX + boundsMinX) * 0.5f; boundsMinX = boundsMid - (boundsMid - boundsMinX) * axisLengthWS / boundsLength; boundsMaxX = boundsMid + (boundsMaxX - boundsMid) * axisLengthWS / boundsLength; float curveMultiplier = new Vector2(curvedLabel.axisMidDisplacement.x * mw / scale, curvedLabel.axisMidDisplacement.y * mh / scale).magnitude *_countryLabelsCurvature; // check if axisAveragedThickness is above or below axis Vector2 a = axis * 0.5f + curvedLabel.axisMidDisplacement; float dot = a.x * -axis.y + a.y * axis.x; if (dot < 0) { curveMultiplier *= -1f; } float boundsWidth = boundsMaxX - boundsMinX; // Get the index of the mesh used by this character. int materialIndex = textInfo.characterInfo[0].materialReferenceIndex; Vector3[] vertices = textInfo.meshInfo[materialIndex].vertices; for (int i = 0; i < characterCount; i++) { if (!textInfo.characterInfo[i].isVisible) { continue; } int vertexIndex = textInfo.characterInfo[i].vertexIndex; // Compute the baseline mid point for each character Vector2 offsetToMidBaseline = new Vector2((vertices[vertexIndex + 0].x + vertices[vertexIndex + 2].x) / 2, textInfo.characterInfo[i].baseLine); // Apply offset to adjust our pivot point. vertices[vertexIndex + 0].x -= offsetToMidBaseline.x; vertices[vertexIndex + 0].y -= offsetToMidBaseline.y; vertices[vertexIndex + 1].x -= offsetToMidBaseline.x; vertices[vertexIndex + 1].y -= offsetToMidBaseline.y; vertices[vertexIndex + 2].x -= offsetToMidBaseline.x; vertices[vertexIndex + 2].y -= offsetToMidBaseline.y; vertices[vertexIndex + 3].x -= offsetToMidBaseline.x; vertices[vertexIndex + 3].y -= offsetToMidBaseline.y; // Compute the angle of rotation for each character based on the animation curve float x0 = (offsetToMidBaseline.x - boundsMinX) / boundsWidth; // Character's position relative to the bounds of the mesh. float x1 = x0 + 0.01f; float y0 = labelsCurve.Evaluate(x0) * curveMultiplier; float y1 = labelsCurve.Evaluate(x1) * curveMultiplier; Vector2 tangent = new Vector2(x1 * boundsWidth + boundsMinX - offsetToMidBaseline.x, y1 - y0); // - new Vector3 (offsetToMidBaseline.x, y0); dot = Mathf.Acos(tangent.normalized.x) * Mathf.Rad2Deg; float angle = tangent.y > 0 ? dot : 360 - dot; Matrix4x4 matrix = Matrix4x4.TRS(new Vector3(offsetToMidBaseline.x, y0 + offsetToMidBaseline.y, 0), Quaternion.Euler(0, 0, angle), Misc.Vector3one); vertices[vertexIndex + 0] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 0]); vertices[vertexIndex + 1] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 1]); vertices[vertexIndex + 2] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 2]); vertices[vertexIndex + 3] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 3]); // vertices [vertexIndex + 0] += offsetToMidBaseline; // vertices [vertexIndex + 1] += offsetToMidBaseline; // vertices [vertexIndex + 2] += offsetToMidBaseline; // vertices [vertexIndex + 3] += offsetToMidBaseline; } // Upload the mesh with the revised information tm.UpdateVertexData(); } country.labelMeshWidth = tm.bounds.size.x; country.labelMeshHeight = tm.bounds.size.y; // GameObject sphere3 = GameObject.CreatePrimitive (PrimitiveType.Sphere); // sphere3.name = "Axis Middle"; // sphere3.transform.SetParent (o.transform, true); // sphere3.transform.localPosition = (curvedLabel.axisStart + curvedLabel.axisEnd) * 0.5f; // sphere3.transform.localScale *= 0.3f; // // GameObject sphere4 = GameObject.CreatePrimitive (PrimitiveType.Sphere); // sphere4.name = "avgAxisPoint"; // sphere4.transform.SetParent (o.transform, true); // sphere4.transform.localPosition = (curvedLabel.axisStart + curvedLabel.axisEnd) * 0.5f + curvedLabel.axisMidDisplacement; // sphere4.GetComponent<Renderer> ().material.color = Color.red; // sphere4.transform.localScale *= 0.2f; StartCoroutine(RepositionTexts()); } }