Detection of water by texture

Back
Tags:
Math
Detect the location on the water or on the land by additional texture.
Usage instructions:
Link to additional Texture
Add this script to map GameObject, select additional texture and start the scene.
Important: Make sure «Read / Write Enabled - ON» in Texture Importer.
Move your mouse over the map and click "P" to check whether there is water at the point under the cursor.
The result will be shown in the console.
DetectWaterByTextureExample.cs
/*         INFINITY CODE         */
/*   https://infinity-code.com   */

using System;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    /// <summary>
    /// Example of detection of water by texture.
    /// This example requires a texture:
    /// http://infinity-code.com/atlas/online-maps/images/mapForDetectWaterBW4.jpg
    /// </summary>
    [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/DetectWaterByTextureExample")]
    public class DetectWaterByTextureExample : MonoBehaviour
    {
        /// <summary>
        /// Reference to the map. If not specified, the current instance will be used.
        /// </summary>
        public OnlineMaps map;
        
        /// <summary>
        /// Color of water on the texture.
        /// </summary>
        public Color32 waterColor = Color.black;

        // Set map 2048x2048, with Read / Write Enabled
        public Texture2D mapForDetectWater;

        private void Start()
        {
            if (map == null) map = OnlineMaps.instance;
        }

        private void Update()
        {
            // Check if the P key is pressed
            if (Input.GetKeyUp(KeyCode.P))
            {
                // Get the coordinates under the cursor.
                Vector2 mouseCoords = map.control.GetCoords();
                
                // Check if there is water at this point
                bool hasWater = HasWater(mouseCoords.x, mouseCoords.y);
                Debug.Log(hasWater ? "Has Water" : "No Water");
            }
        }

        private bool HasWater(float lng, float lat)
        {
            // Convert geo coordinates to tile coordinates
            double tx, ty;
            map.projection.CoordinatesToTile(lng, lat, 3, out tx, out ty);

            const int countTileRowCol = 8;

            // Convert tile coordinates to texture coordinates (UV)
            tx /= countTileRowCol;
            ty /= countTileRowCol;

            // Check pixel color
            Color color = mapForDetectWater.GetPixelBilinear((float)tx, (float)(1 - ty));
            Debug.Log(tx + "   " + ty);

            return color == waterColor;
        }
    }
}