/// <summary> Gets childrent for quadkey. </summary> private IEnumerable <QuadKey> GetChildren(QuadKey quadKey) { // TODO can be optimized to avoid string allocations. var quadKeyName = quadKey.ToString(); yield return(QuadKey.FromString(quadKeyName + "0")); yield return(QuadKey.FromString(quadKeyName + "1")); yield return(QuadKey.FromString(quadKeyName + "2")); yield return(QuadKey.FromString(quadKeyName + "3")); }
/// <summary> Builds quadkeys if necessary. Decision is based on visible quadkey and lod level. </summary> private void BuildIfNecessary(Transform planet) { var lod = (int)_zoom; var actualGameObject = GetActual(planet, Coordinate, lod); if (actualGameObject == planet) { return; } var actualQuadKey = QuadKey.FromString(actualGameObject.name); var actualName = actualGameObject.name; var parent = planet; var quadKeys = new List <QuadKey>(); // zoom in int disposedTiles = 0; if (actualQuadKey.LevelOfDetail < lod) { quadKeys.AddRange(GetChildren(actualQuadKey)); var oldParent = actualGameObject.transform.parent; disposedTiles = SafeDestroy(actualQuadKey, actualName) ? 1 : 0; parent = new GameObject(actualName).transform; parent.transform.parent = oldParent; } // zoom out else if (actualQuadKey.LevelOfDetail > lod) { string name = actualName.Substring(0, actualName.Length - 1); var quadKey = QuadKey.FromString(name); // destroy all siblings foreach (var child in GetChildren(quadKey)) { disposedTiles += SafeDestroy(child, child.ToString()) ? 1 : 0; } // destroy current as it might be just placeholder. SafeDestroy(actualQuadKey, name); parent = GetParent(planet, quadKey); quadKeys.Add(quadKey); } UnloadAssets(disposedTiles); BuildQuadKeys(parent, quadKeys); }
/// <summary> Gets parent game object for given quadkey. Creates hierarchy if necessary. </summary> private Transform GetParent(Transform planet, QuadKey quadKey) { // recursion end if (quadKey.LevelOfDetail <= LodRange.Minimum) { return(planet); } string quadKeyName = quadKey.ToString(); string parentName = quadKeyName.Substring(0, quadKeyName.Length - 1); var parent = GameObject.Find(parentName); return(parent != null ? parent.transform : GetParent(planet, QuadKey.FromString(parentName))); }
/// <summary> Gets quadkey which should be downloaded from remote server. </summary> /// <remarks> Sometimes, mapzen returns more precise shapes then we need. </remarks> private QuadKey GetDownloadQuadKey(QuadKey quadKey) { if (!LodMapping.ContainsKey(quadKey.LevelOfDetail)) { return(quadKey); } int targetLod = LodMapping[quadKey.LevelOfDetail]; var isParent = targetLod < quadKey.LevelOfDetail; var lod = quadKey.LevelOfDetail; var newQuadKey = quadKey; while (lod != targetLod) { var str = newQuadKey.ToString(); newQuadKey = QuadKey.FromString(str.Substring(0, str.Length - 1)); lod += (isParent ? -1 : 1); } return(newQuadKey); }