void DownloadTile(string panoID, int x, int y, PanoSize size, TextureFormat format, Action <Texture2D> onResult = null, Action <Exception> onException = null) { m_Running = true; var url = "https://geo0.ggpht.com/cbk?cb_client=maps_sv.tactile&authuser=0&hl=en" + "&panoid=" + panoID + "&output=tile" + "&x=" + x + "&y=" + y + "&zoom=" + PanoUtility.GetZoomValue(size) + "&nbt&fover=2"; RestRequestAsyncHandle handle = null; m_Handles.Add(handle); var client = new RestClient(); var request = new RestRequest(url, Method.GET); client.ExecuteAsync(request, out handle) .Then(response => { if (!m_Running) { return; } Dispatcher.Enqueue(() => { if (response.IsSuccess()) { var tile = new Texture2D(2, 2, format, true); tile.LoadImage(response.RawBytes); onResult.TryInvoke(tile); } else { onException.TryInvoke(response.GetException()); } }); }) .Catch(exception => { if (!m_Running) { return; } onException.TryInvoke(exception); }); }
// Removes the blank parts of the texture // Similar to StitchTexture, we try to use Graphics.CopyTexture wherever possible // however on VeryLarge PanoSize on Android devices, we use an extenion method called // Texture2D.Crop, which is slower than Graphics.CopyTexture and is a last resort void CropTexture(PanoSize level) { var uRes = PanoUtility.GetUntrimmedResolution(level); var tRes = PanoUtility.DetectBlankBands(m_Texture); tRes = new Vector2( PanoUtility.DetectWidth(m_Texture), tRes.y ); // If the trimmed resolutionm is the same as untrimmed, we don't need to if (tRes.x == uRes.x && tRes.y == uRes.y) { return; } m_Texture.Crop(0, (int)uRes.y - (int)tRes.y, (int)tRes.x, (int)tRes.y); }
/// <summary> /// Downloads the panorama image using the known PanoID /// </summary> /// <param name="panoID">The PanoID to be downloaded</param> /// <param name="size">The <see cref="PanoSize"/> of the image to be downloaded.</param> /// <param name="onResult">Callback containing the Texture2D of the pano image</param> /// <param name="onException">Callback containing the exception when the download fails</param> public void Download(string panoID, PanoSize size, TextureFormat format, Action <Texture32> onResult = null, Action <Exception> onException = null) { var width = PanoUtility.GetUserPanoWidth(size); string url = "https://lh5.googleusercontent.com/p/" + panoID + "=w" + width; m_Running = true; OnStarted.TryInvoke(); new RestClient().ExecuteAsync(new RestRequest(url, Method.GET), out m_Handle) .Then(response => { if (!m_Running) { return; } Dispatcher.Enqueue(() => { if (response.IsSuccess()) { var texture = new Texture2D(1, 1, format, true); texture.LoadImage(response.RawBytes); var result = Texture32.FromTexture2D(texture); MonoBehaviour.Destroy(texture); texture = null; onResult.TryInvoke(result); OnLoaded.TryInvoke(result); } else { OnFailed.TryInvoke(response.GetException()); onException.TryInvoke(response.GetException()); } }); }) .Catch(exception => { if (!m_Running) { return; } onException.TryInvoke(exception); }); }
public void Save() { message.text = "downloading..."; var id = PanoUtility.GetIDFromURL(urlInput.text); downloader.Download(id, size, format) .Then(texture => { try { var dir = Path.Combine(Application.dataPath.Replace("Assets", ""), "SavedPanos"); Directory.CreateDirectory(dir); //File.WriteAllBytes(Path.Combine(dir, id + ".png"), texture.EncodeToPNG()); //File.WriteAllBytes(Path.Combine(dir, id + ".jpg"), texture.EncodeToJPG()); message.text = "Saved to " + dir; } catch (Exception e) { message.text = "Erorr saving the pano: " + e; } }) .Catch(exception => { message.text = "Could not download that pano"; }); }
IEnumerator DownloadCo(string panoID, PanoSize size, TextureFormat format, Action <Texture32> onResult, Action <Exception> onException) { OnStarted?.Invoke(); var uRes = PanoUtility.GetUntrimmedResolution(size); m_Texture = new Texture32((int)uRes.x, (int)uRes.y); var count = PanoUtility.GetTileCount(size); int xCount = (int)count.x; int yCount = (int)count.y; int req = xCount * yCount; int success = 0; int failed = 0; bool done = false; for (int i = 0; i < xCount; i++) { for (int j = 0; j < yCount; j++) { var x = i; var y = j; DownloadTile(panoID, x, y, size, format, tile => { success++; StitchTexture(tile, size, x, ((int)count.y - 1) - y); if (success == req) { done = true; CropTexture(size); onResult?.Invoke(m_Texture); OnLoaded?.Invoke(m_Texture); } }, exception => { if (x == 0 && yCount > y) { yCount = y; req = xCount * yCount; } if (y == 0 && xCount > x) { xCount = x; req = xCount * yCount; } failed++; if (failed == req) { var thrown = new Exception("Could not download the pano image. ID or URL is incorrect."); OnFailed?.Invoke(thrown); onException?.Invoke(thrown); return; } if (success == req && !done) { done = true; CropTexture(size); onResult?.Invoke(m_Texture); OnLoaded?.Invoke(m_Texture); } } ); yield return(null); } } }