Author Topic: 2D Minimap with 3D Globe  (Read 787 times)

Fierde

  • Guest
2D Minimap with 3D Globe
« on: August 07, 2019, 01:25:17 PM »
I'm trying to implement a system where, while hovering the main camera around the globe, there will be a flat 2D minimap that highlights whatever is captured in the main camera's viewport corresponding to the 3D Globe. So, the flat minimap shows the entire earth, but whatever section of the globe the main camera is looking at will be highlighted on the minimap with a red rectangle or something. Of course, the highlight will get smaller as the main camera zooms into the globe, thus seeing less of the earth. I don't really have any idea how to project the 3D position of the camera around the globe to a corresponding 2D position on the flat map. If anyone thinks this is an interesting or feasible idea, feel free to provide answers/insight. Thanks!

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: 2D Minimap with 3D Globe
« Reply #1 on: August 08, 2019, 10:48:22 AM »
That's a quite interesting challenge. I'd say there're two scenarios: a) the globe fits within screen edges, b) only a portion of the globe is visible.
In a) case, you could raycast from the center of screen to the globe and convert that hit position into a lat/lon or texture coordinate, knowing that half of sphere is visible (an extents of 180 degrees lat/lon from the raycast hit).
In b) case, you could raycast the corners of the camera frustum and convert the sphere positions of raycast hit into lat/lon or texture coordinates using the Conversion.xxxx methods.

There's a method called GetVisibleCountries which returns which countries are visible from the camera. Another option is to use this method and compute the enclosing rectangle of all country areas (country.rect2D).

Fierde

  • Guest
Re: 2D Minimap with 3D Globe
« Reply #2 on: August 08, 2019, 11:48:07 AM »
Awesome, thanks for the help. I think option B would work best for what I'm going for. One question though: if I'm using he Conversion methods, would I have to make the minimap a WMSK object, or will those methods work with the texture coordinates for any sprite with a texture?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: 2D Minimap with 3D Globe
« Reply #3 on: August 08, 2019, 11:55:10 AM »
The texture coordinates are standard (in the 0..1 range). But the local map positions in WMSK are in the -0.5 .. 0.5 range, so depending on the methdo you use, you may need to add 0.5f to them to map those coordinates to 0..1 tex coords.

Fierde

  • Guest
Re: 2D Minimap with 3D Globe
« Reply #4 on: August 09, 2019, 07:24:47 AM »
OK, after trying this out by myself, I've gotten to about this
Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MiniMap : MonoBehaviour
{
    // Start is called before the first frame update
    Ray topLeft;
    Ray topRight;
    Ray botRight;
    Ray botLeft;
    RaycastHit hitTL;
    RaycastHit hitTR;
    RaycastHit hitBL;
    RaycastHit hitBR;
    Vector3 textureTR;
    Vector3 textureTL;
    Vector3 textureBR;
    Vector3 textureBL;
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        topLeft = Camera.main.ViewportPointToRay(Vector3.zero);
        topRight = Camera.main.ViewportPointToRay(new Vector3(1, 0, 0));
        botRight = Camera.main.ViewportPointToRay(new Vector3(1, 1, 0));
        botLeft = Camera.main.ViewportPointToRay(new Vector3(0, 1, 0));

    }
    private void OnGUI()
    {
        if (Physics.Raycast(topLeft, out hitTL) && Physics.Raycast(topRight, out hitTR) && Physics.Raycast(botRight, out hitBR) && Physics.Raycast(botLeft, out hitBL))
        {
            textureTR = WPM.Conversion.ConvertToTextureCoordinates(hitTR.point, 1, 1);
            textureTL = WPM.Conversion.ConvertToTextureCoordinates(hitTL.point, 1, 1);
            textureBR = WPM.Conversion.ConvertToTextureCoordinates(hitBR.point, 1, 1);
            Debug.Log(textureTR + " " + textureTL + " " + textureBR);
            DrawQuad(new Rect(textureTL.x, textureTR.x, textureTR.y, textureBR.y), Color.red);
        }
       

    }
    void DrawQuad(Rect position, Color color)
    {
        Texture2D texture = new Texture2D(1, 1);
        texture.SetPixel(0, 0, color);
        texture.Apply();
        GUI.skin.box.normal.background = texture;
        GUI.Box(position, GUIContent.none);
    }
}

Obviously this isn't quite correct, I'm just not sure what to do next.

Fierde

  • Guest
Re: 2D Minimap with 3D Globe
« Reply #5 on: August 13, 2019, 11:41:09 AM »
Are you guys able to guide me in the right direction?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: 2D Minimap with 3D Globe
« Reply #6 on: August 14, 2019, 03:47:55 AM »
The raycast hit point property is in world space coordinates.
The Conversion.ConvertToTextureCoordinates API expects a position in local coordinates.
Try passing map.transform.InverseTransformPoint(hitXX.point) instead.

Fierde

  • Guest
Re: 2D Minimap with 3D Globe
« Reply #7 on: August 14, 2019, 06:53:25 AM »
OK, that's done. A couple more questions.
If I have my WorldMapGlobe object scaled up, how should I augment my calculation?
Also, I'm using texturecoordinates, but I see no point at which to actually pass in the texture I want to use. So how would Unity know that I want my minimap texture highlighted?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: 2D Minimap with 3D Globe
« Reply #8 on: August 14, 2019, 07:21:08 AM »
The texture coordinates are invariant of object scale.
At this point, since it's custom development, I could help if you can provide a sample scene that includes what you have achieved so far.

Fierde

  • Guest
Re: 2D Minimap with 3D Globe
« Reply #9 on: August 14, 2019, 08:05:35 AM »
Ok, how do I attach a file to a reply? Clicking on "Attachments and other options" just gives me "return to this topic" and "don't use smileys".

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 5137
    • View Profile
Re: 2D Minimap with 3D Globe
« Reply #10 on: August 14, 2019, 08:20:09 AM »
Yes, you should be able to attach small files. In this case you can zip the project and upload somewhere like Google Drive and send me a pm with the download link.