We need the following values in order to do proper geo-referencing operations in Unity:
(The following 2 values are found in TerraLand Downloader’s “Area Location” section)
- public double originLatitude; Origin Latitude coordinate of the database & generated terrain area
- public double originLongitude; Origin Longitude coordinate of the database & generated terrain area
36.87916, -111.5105
(The following 2 values are found in TerraLand Downloader’s “Area Size” section)
- public double areaHeight; Length of the generated area in meters
- public double areaWidth; Width of the generated area in meters
12000, 12000
(The following 4 values are found in TerraLand Downloader’s “Terrain Bounds” tab)
- public double areaTop; Top Latitude coordinate of the generated area
- public double areaBottom; Bottom Latitude coordinate of the generated area
- public double areaLeft; Left Longitude coordinate of the generated area
- public double areaRight; Right Longitude coordinate of the generated area
Top: 36.9330589170472, Bottom: 36.8252610829528, Left: -111.577881816826 & Right: -111.443118183174
And then we can simply convert any Latitude/Longitude points of interests into Unity’s world space in our scene. Here is the following script to get geo-referenced object position in Unity world space and move it to desired Latitude/Longitude coordinates right in the Editor.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [ExecuteInEditMode]
- public class LatLon2Unity : MonoBehaviour
- {
- public double originLatitude;
- public double originLongitude;
- public double areaHeight;
- public double areaWidth;
- public double areaTop;
- public double areaBottom;
- public double areaLeft;
- public double areaRight;
- public bool forceMoveToLatLon = false;
- public double destinationLat;
- public double destinationLon;
- private double currentLatitude;
- private double currentLongitude;
- void Update ()
- {
- if(transform.position.z >= 0)
- currentLatitude = originLatitude + ((areaTop – areaBottom) * (Mathf.InverseLerp(0, (float)areaHeight, transform.position.z)));
- else
- currentLatitude = originLatitude – ((areaTop – areaBottom) * (Mathf.InverseLerp(0, (float)areaHeight, -transform.position.z)));
- if(transform.position.x >= 0)
- currentLongitude = originLongitude + ((areaRight – areaLeft) * (Mathf.InverseLerp(0, (float)areaWidth, transform.position.x)));
- else
- currentLongitude = originLongitude – ((areaRight – areaLeft) * (Mathf.InverseLerp(0, (float)areaWidth, -transform.position.x)));
- print(“Current Lat: ” + currentLatitude + ” Lon: ” + currentLongitude);
- if(forceMoveToLatLon)
- MoveToLatLon();
- }
- private void MoveToLatLon ()
- {
- (
- (float)(((destinationLon – areaLeft) / (areaRight – areaLeft)) * (areaWidth)) – (float)(areaWidth / 2f),
- 0,
- (float)(((destinationLat – areaBottom) / (areaTop – areaBottom)) * (areaHeight)) – (float)(areaHeight / 2f)
- );
- }
- }
Following picture shows a geo-referenced object placed exactly at given coordinates of “36.9005219376448, -111.458823759922”. Location on Google Maps
Here is the free download of full scene for the above implementation:
terraunity.com/freedownload/TerraLand_LatLon2UnityWorldSpace.unitypackage
FYI, the “TerraLand Terrain” component has built-in coordinates converter between “Minute, Degree, Second” (MDS) to Decimal Degrees (DD) GIS formats and vice versa, so no need to go for online converters such as the one you mentioned.