/// <summary> /// Request a texture download. The download will be processed asynchronously. If there is /// already a request in progress for this URL a second request will *not* be queued. /// </summary> /// <param name="url">URL to download.</param> /// <param name="layerNameToSet">Layer identifier for this request.</param> /// <param name="dateTimeToSet">Date of this request.</param> /// <param name="handler">Callback that will be invoked when the texture is ready.</param> /// <param name="prepareForRendering">True if the texture will be used for rendering. If /// true mipmaps will be generated and an aniso bias set. If false the texture will be /// loaded and returned with no further processing.</param> /// <param name="queueNum">The download queue to use for this request. Queues are identified /// by index, and all downloads in a queue can be cancelled without affecting other /// queues.</param> public void RequestTexture(string url, string layerNameToSet, DateTime dateTimeToSet, TextureDownloadHandler handler, bool prepareForRendering = true, int queueNum = 0) { DownloadStatus status; bool haveEntry = downloadRequests.TryGetValue(url, out status); bool requestInProgress = haveEntry && status == DownloadStatus.InProgress; if (!requestInProgress) { DownloadTask globeTileLayerSet = new DownloadTask { url = url, layerName = layerNameToSet, date = dateTimeToSet, handler = handler, prepareTextureForRendering = prepareForRendering }; downloadRequests[url] = DownloadStatus.InProgress; SwarmManager.Instance.EnqueueRequest(new DownloadRequest { url = url, callbackAction = t => { Texture2D myTexture = new Texture2D(2, 2, TextureFormat.RGB24, true, linearTextures); myTexture.LoadImage(t.downloadHandler.data, false); //HACK: Unity does not provide a callback if the image for the texture is unrecognized. //The default texture for an unrecognized image is a red question mark that is 8x8. //Here we check for that and swap it for a black texture. if (myTexture.height == 8) { myTexture = Texture2D.blackTexture; } if (globeTileLayerSet.prepareTextureForRendering) { myTexture.wrapMode = TextureWrapMode.Clamp; myTexture.mipMapBias = globalMipMapBias; myTexture.anisoLevel = globalAnisoLevel; myTexture.Apply(true, false); } globeTileLayerSet.texture = myTexture; globeTileTexturesToLoadQueue.Enqueue(globeTileLayerSet); } }); if (!globeTileQueueLookup.ContainsKey(url)) { globeTileQueueLookup.Add(url, globeTileLayerSet); } } else { DownloadTask globeTileLayerSet = globeTileQueueLookup[url]; globeTileLayerSet.handler += handler; globeTileLayerSet.prepareTextureForRendering = globeTileLayerSet.prepareTextureForRendering || prepareForRendering; } }
/// <summary> /// Request a texture download. The download will be processed asynchronously. If there is /// already a request in progress for this URL a second request will *not* be queued. /// </summary> /// <param name="url">URL to download.</param> /// <param name="layerNameToSet">Layer identifier for this request.</param> /// <param name="dateTimeToSet">Date of this request.</param> /// <param name="handler">Callback that will be invoked when the texture is ready.</param> /// <param name="prepareForRendering">True if the texture will be used for rendering. If /// true mipmaps will be generated and an aniso bias set. If false the texture will be /// loaded and returned with no further processing.</param> /// <param name="queueNum">The download queue to use for this request. Queues are identified /// by index, and all downloads in a queue can be cancelled without affecting other /// queues.</param> public void RequestTexture(string url, string layerNameToSet, DateTime dateTimeToSet, TextureDownloadHandler handler, bool prepareForRendering = true, int queueNum = 0) { DownloadStatus status; bool haveEntry = downloadRequests.TryGetValue(url, out status); bool requestInProgress = haveEntry && status == DownloadStatus.InProgress; if (!requestInProgress) { DownloadTask globeTileLayerSet = new DownloadTask { url = url, layerName = layerNameToSet, date = dateTimeToSet, handler = handler, prepareTextureForRendering = prepareForRendering }; downloadRequests[url] = DownloadStatus.InProgress; SwarmManager.Instance.EnqueueRequest(new DownloadRequest { url = url, callbackAction = t => { Texture2D myTexture = new Texture2D(2, 2, TextureFormat.RGB24, true, linearTextures); myTexture.LoadImage(t.downloadHandler.data, false); if (globeTileLayerSet.prepareTextureForRendering) { myTexture.wrapMode = TextureWrapMode.Clamp; myTexture.mipMapBias = globalMipMapBias; myTexture.anisoLevel = globalAnisoLevel; myTexture.Apply(true, true); } globeTileLayerSet.texture = myTexture; globeTileTexturesToLoadQueue.Enqueue(globeTileLayerSet); } }); if (!globeTileQueueLookup.ContainsKey(url)) { globeTileQueueLookup.Add(url, globeTileLayerSet); } } else { DownloadTask globeTileLayerSet = globeTileQueueLookup[url]; globeTileLayerSet.handler += handler; globeTileLayerSet.prepareTextureForRendering = globeTileLayerSet.prepareTextureForRendering || prepareForRendering; } }
private void RequestTexture(Layer layer, DateTime date, TextureDownloadHandler handler, int layerNum) { url = layer.WmtsToUrl(coords, date); //Debug.Log(url); // Load from cache if possible if (globe.tileTextureCache.ExistsInCache(url)) { handler(layer.identifier, date, globe.tileTextureCache.GetTexture(url)); } else { globe.downloader.RequestTexture(url, layer.identifier, date, handler, queueNum: layerNum + 1); } }