1 (edited by galdalali 2025-09-25 13:52:52)

Topic: Transparent overlay

What is the proper way to combine two layers (satellite imagery + transparent streets) to create a hybrid map?
I’m trying to use a back and front overlay approach, but I haven’t had any success so far.

Here's ab example for the transparent layer:
https://services.arcgisonline.com/arcgi … z}/{y}/{x}

Re: Transparent overlay

Hi.

The example is attached.
This is a port script from v3.

using System.Collections.Generic;
using System.Text.RegularExpressions;
using OnlineMaps;
using UnityEngine;
using Cache = OnlineMaps.Cache;

namespace InfinityCode.OnlineMapsSupport
{
    public class CustomWMTSOverlay : MonoBehaviour
    {
        /// <summary>
        /// URL pattern of WMTS tiles
        /// </summary>
        public string url = "https://services.arcgisonline.com/arcgis/rest/services/Reference/World_Boundaries_and_Places/MapServer/tile/{z}/{y}/{x}";

        /// <summary>
        /// Overlay download queue
        /// </summary>
        private Queue<Tile> downloadQueue = new Queue<Tile>();

        /// <summary>
        /// The tile that is downloading now
        /// </summary>
        private Tile activeTile;

        /// <summary>
        /// Adds a tile to the overlay download queue
        /// </summary>
        /// <param name="tile">Tile</param>
        private void AddTileToQueue(Tile tile)
        {
            // Set the count of retry
            tile["countRetry"] = 3;
            downloadQueue.Enqueue(tile);
            TryStartNextDownload();
        }

        /// <summary>
        /// Called when overlay is downloaded
        /// </summary>
        /// <param name="www">Instance of request</param>
        private void OnDownloadComplete(WebRequest www)
        {
            activeTile = null;

            // Get the tile from a request
            Tile tile = www["tile"] as Tile;

            // If the tile is null or disposed, try to download next one
            if (tile == null || tile.status == TileStatus.disposed)
            {
                TryStartNextDownload();
                return;
            }

            // Get tile texture
            Texture2D texture = new Texture2D(1, 1);
            texture.LoadImage(www.bytes);

            // If the texture is the wrong size
            if (texture.width == 8)
            {
                // Destroy the texture to avoid memory leak and try downloading again
                Destroy(texture);
                int countRetry = (int)tile["countRetry"];
                if (countRetry > 0)
                {
                    tile["countRetry"] = countRetry - 1;
                    AddTileToQueue(tile);
                }
            }
            else
            {
                // Set WMS texture to back overlay
                tile.overlayBackTexture = texture;
                texture.wrapMode = TextureWrapMode.Clamp;
                tile.map.Redraw();
            }

            TryStartNextDownload();
        }

        /// <summary>
        /// Called when a tile is loaded from cache
        /// </summary>
        /// <param name="tile">Tile</param>
        private void OnLoadedFromCache(Tile tile)
        {
            AddTileToQueue(tile);
        }

        /// <summary>
        /// Called when a tile is being downloaded from the internet
        /// </summary>
        /// <param name="tile">Tile</param>
        private void OnStartDownloadTile(Tile tile)
        {
            TileManager.StartDownloadTile(tile);
            AddTileToQueue(tile);
        }

        private void Start()
        {
            TileManager.OnStartDownloadTile += OnStartDownloadTile;
            if (Cache.instance) Cache.instance.OnLoadedFromCache += OnLoadedFromCache;
        }

        /// <summary>
        /// Starts downloading the overlay for the tile
        /// </summary>
        /// <param name="tile">Tile</param>
        private void StartDownloadOverlay(Tile tile)
        {
            string _url = Regex.Replace(url, @"{\w+}", match =>
            {
                string v = match.Value.ToLower().Trim('{', '}');

                if (v == "zoom" || v == "z") return tile.zoom.ToString();
                if (v == "x") return tile.x.ToString();
                if (v == "y") return tile.y.ToString();
                return "{" + v + "}";
            });

            // Start download
            WebRequest www = new WebRequest(_url);
            www["tile"] = tile;
            www.OnComplete += OnDownloadComplete;
        }

        /// <summary>
        /// Tries to start the next download
        /// </summary>
        private void TryStartNextDownload()
        {
            // If the overlay is already loading, do nothing
            if (activeTile != null) return;

            // While there are tiles in the queue
            while (downloadQueue.Count > 0)
            {
                // Get the first tile from the queue
                Tile tile = downloadQueue.Dequeue();

                // If the tile is not disposed, start downloading
                if (tile.status != TileStatus.disposed)
                {
                    activeTile = tile;
                    StartDownloadOverlay(tile);
                    return;
                }
            }
        }
    }
}
Post's attachments

Attachment icon img1.png 935.23 kb, 18 downloads since 2025-09-25 

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.