155 lines
3.9 KiB
C#
155 lines
3.9 KiB
C#
using System;
|
|
using GISTech.GISTerrainLoader;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GeoRef : MonoBehaviour
|
|
{
|
|
public Dropdown Projections;
|
|
|
|
public Dropdown DisplayFormat_DropDown;
|
|
|
|
public Text LatLonText;
|
|
|
|
public Text ElevationText;
|
|
|
|
public TerrainSourceMode terrainSourceMode = TerrainSourceMode.FromPlayMode;
|
|
|
|
private GISTerrainLoaderPrefs Prefs;
|
|
|
|
private GISTerrainLoaderRuntimePrefs RuntimePrefs;
|
|
|
|
private RuntimeTerrainGenerator runtimeGenerator;
|
|
|
|
public LayerMask TerrainLayer;
|
|
|
|
private Terrain m_terrain;
|
|
|
|
private RaycastHit hitInfo;
|
|
|
|
public int From_EPSG;
|
|
|
|
public int To_EPSG;
|
|
|
|
public Terrain terrain
|
|
{
|
|
get
|
|
{
|
|
return m_terrain;
|
|
}
|
|
set
|
|
{
|
|
if (m_terrain != value)
|
|
{
|
|
m_terrain = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
RuntimePrefs = GISTerrainLoaderMonoSingleton<GISTerrainLoaderRuntimePrefs>.Get;
|
|
Prefs = RuntimePrefs.Prefs;
|
|
runtimeGenerator = GISTerrainLoaderMonoSingleton<RuntimeTerrainGenerator>.Get;
|
|
RuntimeTerrainGenerator.OnFinish += OnTerrainGenerated;
|
|
if ((bool)Projections)
|
|
{
|
|
Projections.onValueChanged.AddListener(OnProjectionChanged);
|
|
}
|
|
if ((bool)DisplayFormat_DropDown)
|
|
{
|
|
DisplayFormat_DropDown.onValueChanged.AddListener(OnDisplayFormatChanged);
|
|
}
|
|
if (terrainSourceMode == TerrainSourceMode.FromEditMode)
|
|
{
|
|
GISTerrainContainer gISTerrainContainer = UnityEngine.Object.FindObjectOfType<GISTerrainContainer>();
|
|
if ((bool)gISTerrainContainer)
|
|
{
|
|
gISTerrainContainer.GetStoredHeightmap();
|
|
GISTerrainLoaderMonoSingleton<RuntimeTerrainGenerator>.Get.SetGeneratedTerrain(gISTerrainContainer);
|
|
}
|
|
else
|
|
{
|
|
terrainSourceMode = TerrainSourceMode.FromPlayMode;
|
|
Debug.LogError("Terrain not found in the Current scene, generate a terrain in the editor mode or set the'Origin Projection Mode' to PlayMode ");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
RayCastMousePosition();
|
|
}
|
|
|
|
private void RayCastMousePosition()
|
|
{
|
|
hitInfo = default(RaycastHit);
|
|
if (!Camera.main || !Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, float.PositiveInfinity, TerrainLayer))
|
|
{
|
|
return;
|
|
}
|
|
if (terrain == null)
|
|
{
|
|
terrain = hitInfo.collider.transform.gameObject.GetComponent<Terrain>();
|
|
ElevationText.text = "0";
|
|
}
|
|
if (terrain != null && !string.Equals(hitInfo.collider.transform.name, terrain.name))
|
|
{
|
|
terrain = hitInfo.collider.transform.gameObject.GetComponent<Terrain>();
|
|
}
|
|
DVector3 dVector = GISTerrainLoaderGeoConversion.UnityWorldSpaceToRealWorldCoordinates(hitInfo.point, runtimeGenerator.GeneratedContainer, GetElevation: true, RealWorldElevation.Elevation);
|
|
if ((bool)LatLonText)
|
|
{
|
|
DVector2 dVector2 = ConvertCoordinatesTo(dVector.ToDVector2(), To_EPSG);
|
|
if (To_EPSG == 4326)
|
|
{
|
|
LatLonText.text = "GEO WGS84 " + GISTerrainLoaderGeoConversion.ToDisplayFormat(dVector2, Prefs.CoordinatesDisplayFormat);
|
|
}
|
|
if (To_EPSG != 4326)
|
|
{
|
|
LatLonText.text = Projections.captionText.text + " { " + dVector2.x + " / " + dVector2.y + "}";
|
|
}
|
|
}
|
|
if ((bool)ElevationText)
|
|
{
|
|
ElevationText.text = Math.Round(dVector.z, 2) + " m ";
|
|
}
|
|
}
|
|
|
|
private DVector2 ConvertCoordinatesTo(DVector2 coor, int To_epsg)
|
|
{
|
|
if (From_EPSG != To_epsg)
|
|
{
|
|
return GISTerrainLoaderGeoConversion.ConvertCoordinatesFromTo(coor, From_EPSG, To_epsg);
|
|
}
|
|
return coor;
|
|
}
|
|
|
|
private void OnProjectionChanged(int value)
|
|
{
|
|
To_EPSG = Prefs.ProjectionsData_SO.GetEPSG(Projections.options[value].text);
|
|
}
|
|
|
|
private void OnDisplayFormatChanged(int value)
|
|
{
|
|
Prefs.CoordinatesDisplayFormat = (DisplayFormat)value;
|
|
}
|
|
|
|
private void OnTerrainGenerated(GISTerrainContainer m_container)
|
|
{
|
|
Projections.ClearOptions();
|
|
Projections.AddOptions(Prefs.ProjectionsData_SO.GetOptions());
|
|
From_EPSG = runtimeGenerator.GeneratedContainer.data.EPSG;
|
|
if (From_EPSG == 0)
|
|
{
|
|
From_EPSG = 4326;
|
|
}
|
|
To_EPSG = From_EPSG;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
RuntimeTerrainGenerator.OnFinish -= OnTerrainGenerated;
|
|
}
|
|
}
|