1 (edited by Loryer 2023-01-18 15:46:35)

Topic: RWT generated mesh not hovering correctly map

Hi Alex, good afternoon,
i'm trying to use both Real World Terrain and Online Maps in the same project,

my goal was to create an interface where i could swap between the 3dimensional mesh created with the first asset and the bidimensional map, more than that i wanted to use the service to get the lat and lon of the pointer position on the screen while being in both these 2 visualization...
The easiest way to do this was to hover the 2 visualization and here is where i'm gettin the problems.

The terrain i create and the bidimensional map don't hover correctly.
There is a pretty huge difference in the coastlines; is it a normal behaviour or am i by any chance setting something different in the assets needed to make the results match?

Is there the possibility to match the 2 maps automatically? Maybe even skipping the part of tuning the zoom slider to set the correct size of the bidimensional map?

Thank you in advance.

Re: RWT generated mesh not hovering correctly map

Hello.

For RWT, you can get the coordinate under the cursor with RealWorldTerrainMonoBase.GetCoordinatesUnderCursor.
http://infinity-code.com/doxygen/real-w … c9b337f452

For Online Maps, you can get the coordinate under the cursor using OnlineMapsControlBase.GetCoords.
https://infinity-code.com/doxygen/onlin … ab8b006546

Unfortunately you don't have a way to automatically make map and terrain match.
You can implement some kind of algorithm (something like below), but it's still not ideal and will have a calculation error.

using System;
using System.Collections;
using InfinityCode.RealWorldTerrain;
using UnityEngine;

public class MatchTerrainSize : MonoBehaviour
{
    public RealWorldTerrainContainer container;

    [Range(0, 1)]
    public float mapAlpha = 1;

    private float _mapAlpha = 1;

    public static void GetCenterPointAndZoom(OnlineMaps map, IEnumerable positions, out OnlineMapsVector2d center, out float zoom, Vector2 inset = default(Vector2))
    {
        center = new OnlineMapsVector2d();
        zoom = OnlineMaps.MINZOOM;
        if (positions == null || map == null) return;

        double minX = double.MaxValue;
        double minY = double.MaxValue;
        double maxX = double.MinValue;
        double maxY = double.MinValue;

        int type = -1; // 0 - Vector2, 1 - OnlineMapsVector2d, 2 - marker2D, 3 - marker3d, 4 - float, 5 - double
        object prev = null;
        int pindex = -1;
        int vindex = 0;
        bool useTwoValues = false;

        foreach (object v in positions)
        {
            pindex++;

            if (type == -1)
            {
                if (v is Vector2) type = 0;
                else if (v is OnlineMapsVector2d) type = 1;
                else if (v is OnlineMapsMarker) type = 2;
                else if (v is OnlineMapsMarker3D) type = 3;
                else if (v is float) type = 4;
                else if (v is double) type = 5;
                else return;

                if (type >= 4) useTwoValues = true;
            }

            if (useTwoValues && pindex % 2 != 1)
            {
                prev = v;
                continue;
            }

            double lng = 0, lat = 0;

            if (type == 0)
            {
                Vector2 p = (Vector2)v;
                lng = p.x;
                lat = p.y;
            }
            else if (type == 1)
            {
                OnlineMapsVector2d p = (OnlineMapsVector2d)v;
                lng = p.x;
                lat = p.y;
            }
            else if (type == 2 || type == 3)
            {
                OnlineMapsMarkerBase p = v as OnlineMapsMarkerBase;
                p.GetPosition(out lng, out lat);
            }
            else if (type == 4)
            {
                lng = (float)prev;
                lat = (float)v;
            }
            else if (type == 5)
            {
                lng = (double)prev;
                lat = (double)v;
            }

            if (vindex > 0)
            {
                double rx = lng - minX;
                if (rx > 180) lng -= 360;
                else if (rx < -180) lng += 360;
            }

            if (lng < minX) minX = lng;
            if (lng > maxX) maxX = lng;

            if (lat < minY) minY = lat;
            if (lat > maxY) maxY = lat;

            vindex++;
        }

        double sx = maxX - minX;
        double sy = maxY - minY;

        center = new OnlineMapsVector2d(sx / 2 + minX, sy / 2 + minY);

        if (center.x < -180) center.x += 360;
        else if (center.x > 180) center.x -= 360;

        if (vindex == 1)
        {
            zoom = OnlineMaps.MAXZOOM;
            return;
        }

        OnlineMapsProjection projection = map.projection;

        double alx, aty, arx, aby;
        projection.CoordinatesToTile(minX, maxY, OnlineMaps.MAXZOOM, out alx, out aty);
        projection.CoordinatesToTile(maxX, minY, OnlineMaps.MAXZOOM, out arx, out aby);

        double mapRangeX = (map.width - inset.x * 2) / OnlineMapsUtils.tileSize;
        double mapRangeY = (map.height - inset.y * 2) / OnlineMapsUtils.tileSize;

        double areaRangeX = arx - alx;
        double areaRangeY = aby - aty;

        double rangeX = areaRangeX / mapRangeX;
        double rangeY = areaRangeY / mapRangeY;

        double zoomOffsetX = Math.Log(rangeX, 2);
        double zoomOffsetY = Math.Log(rangeY, 2);

        zoom = OnlineMaps.MAXZOOM - (float)Math.Max(zoomOffsetX, zoomOffsetY) - 0.5f;
    }

    private void Start()
    {
        if (container == null) return;

        OnlineMaps map = OnlineMaps.instance;
        OnlineMapsVector2d center;
        float zoom;
        GetCenterPointAndZoom(map, new double[]
        {
            container.leftLongitude,
            container.topLatitude,
            container.rightLongitude,
            container.bottomLatitude
        }, out center, out zoom);
        
        map.SetPositionAndZoom(center.x, center.y, zoom);

        OnlineMapsTileSetControl control = map.control as OnlineMapsTileSetControl;
        float size = Mathf.Max(container.size.x, container.size.z);
        control.sizeInScene = new Vector2(size, size);

        map.transform.rotation = Quaternion.Euler(0, 180, 0);
        map.transform.position = container.transform.position + new Vector3((container.size.x - size) / 2, 0, size + (container.size.z - size) / 2);

        control.OnDrawTile += OnDrawTile;
    }

    private void OnDrawTile(OnlineMapsTile tile, Material material)
    {
        Color color = material.color;
        color.a = mapAlpha;
        material.color = color;
    }

    private void Update()
    {
        if (_mapAlpha != mapAlpha)
        {
            OnlineMaps.instance.Redraw();
            _mapAlpha = mapAlpha;
        }
    }
}
Kind Regards,
Infinity Code Team.

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

Re: RWT generated mesh not hovering correctly map

Thanks for the answer and for the script.

What i wanted to make clear is that right now we are matching the map and the terrain by tuning the zoom slider on the map component. What i noticed is the fact the 2 representation aren't actually matching and the difference become greater as we move away from the center.
I started considering the possibility of the 2 modules using different projection methods (?), is this the case?

I attach a screenshot hoping to make clear what i'm referring to.
The first 2 images show the differences, the third is the hovering from an high view point.

Post's attachments

Attachment icon im2.png 1.15 mb, 32 downloads since 2023-01-20 

Re: RWT generated mesh not hovering correctly map

Please attach the coordinates of your area boundaries in RWT. I will check it.

Kind Regards,
Infinity Code Team.

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

Re: RWT generated mesh not hovering correctly map

I'm sending an image with the variables set on the 2 assets, as i stated before there is the zoom component for online maps which confuses me quite a lot and seems by tuning it the easiest way to make the 2 maps match.

Let me know smile

Post's attachments

Attachment icon assetcomparison.png 114.21 kb, 30 downloads since 2023-01-23 

Re: RWT generated mesh not hovering correctly map

I managed to make it match.
Here's a video:
https://www.dropbox.com/s/288783pb6ekwu … T.mp4?dl=0

Kind Regards,
Infinity Code Team.

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

Re: RWT generated mesh not hovering correctly map

Nice! Thanks for the video!
What did you do to make them match? Did you just tune the zoom slider after matching the center?

Do you think there is some kind of automation that i can use to do this?
I ask this because i need to make the same adjustment on like 30 different locations :S

To tune the zoom slider by moving it manually seems a pretty good compromise but i still need to match the center before doing that. Do you have some suggestions? Or is it still ok to use the script you sent me the other day?

Re: RWT generated mesh not hovering correctly map

How I did it:
I used a modified version of the script from post #2 where I added the map to the scene in the same place as the terrain so they overlap.
I changed the terrain material to make it transparent.
The script calculates the center position quite well, and I just need to tweak the zoom a bit.

In most cases, replacing the terrain material is a bad idea because you won't be able to undo it.
It is better to change the transparency of the map.
I added this feature and updated the script in post #2.

How you need to do:
- Add a script to the map.
- Tileset / Materials & Shaders / Tileset Shader - Tileset.
- Start the scene.
- Move the map up (y-axis) to draw the map first.
- Set top view and switch to Ortho projection.
- Adjust the zoom and save the map state.

Kind Regards,
Infinity Code Team.

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

9 (edited by Loryer 2023-01-25 11:19:52)

Re: RWT generated mesh not hovering correctly map

Thanks for your help man!

I created a new project and installed the assets from scratch.
I generated a terrain and created a map with default settings.
I added your script to the map and went play.
Attached is the result obtained (result_overlay01, result_overlay02, result_overlay03).
As you can see the map looks stretched and rotated. From your video everything seemed to be correct, what went wrong?

An other question, in order to cover the size of the example terrain whose data I previously sent you with the OnlineMaps map, I need to increase the pixel dimensions of the map to 16K because otherwise the resolution of the map is too low and I cannot read the written.
Is it correct to increase that parameter to obtain this result?

Thanks so much for your patience

Post's attachments

Attachment icon Proporzioni.jpg 70.3 kb, 26 downloads since 2023-01-25 

Re: RWT generated mesh not hovering correctly map

Rusult_Overlay01

Post's attachments

Attachment icon Rusult_Overlay01.jpg 857.43 kb, 28 downloads since 2023-01-25 

Re: RWT generated mesh not hovering correctly map

Rusult_Overlay02

Post's attachments

Attachment icon Rusult_Overlay02.jpg 567.43 kb, 32 downloads since 2023-01-25 

Re: RWT generated mesh not hovering correctly map

Rusult_Overlay03

Post's attachments

Attachment icon Rusult_Overlay03.jpg 658.07 kb, 30 downloads since 2023-01-25 

Re: RWT generated mesh not hovering correctly map

I dont know. Please attach a video of how you do it. I will check it.

Kind Regards,
Infinity Code Team.

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

Re: RWT generated mesh not hovering correctly map

Ok, i have loaded video on my cloud.
If you need upload other material you can use it.

https://cloud.cetena.it/s/JQ9WCdaQLBYr0Vt

Re: RWT generated mesh not hovering correctly map

You have broken proportions.
I updated the script in post #2 to handle the situation when the terrain is not square.
Here is a video on how to match them in this case:
https://www.dropbox.com/s/y5fz2saf78eqs … 2.mp4?dl=0

Kind Regards,
Infinity Code Team.

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

Re: RWT generated mesh not hovering correctly map

Great Alex, Thanks a lot!