Topic: Interception receiving elevations
Sometimes you want to use elevation data obtained from your own sources.
In this example, I will show you how to make Real World Terrain use your own elevation data.
How to use this:
Place the script in Editor folder.
Modify OnGetElevation and OnGetElevationRange methods for your source.
To start intercepting values, select RWT / Intercept Elevation and click Intercept.
After generating the terrains, click Release.
using InfinityCode.RealWorldTerrain.Generators;
using UnityEditor;
using UnityEngine;
public class InterceptGetElevationExample : EditorWindow
{
public static bool isIntercepted = false;
private void OnGUI()
{
EditorGUI.BeginDisabledGroup(isIntercepted);
if (GUILayout.Button("Intercept"))
{
// Subscribe to OnGetElevation event, to intercept receiving elevation from the provider.
RealWorldTerrainElevationGenerator.OnGetElevation += OnGetElevation;
// If you want to use the range of values from the selected elevation provider, just do not subscribe to RealWorldTerrainElevationGenerator.OnGetElevationRange.
RealWorldTerrainElevationGenerator.OnGetElevationRange += OnGetElevationRange;
isIntercepted = true;
}
EditorGUI.EndDisabledGroup();
EditorGUI.BeginDisabledGroup(!isIntercepted);
if (GUILayout.Button("Release"))
{
RealWorldTerrainElevationGenerator.OnGetElevation -= OnGetElevation;
RealWorldTerrainElevationGenerator.OnGetElevationRange -= OnGetElevationRange;
isIntercepted = false;
}
EditorGUI.EndDisabledGroup();
}
/// <summary>
/// This method will be called when the RWT wants to get the range of elevation values.
/// If you want to use the range of values from the selected elevation provider, just do not subscribe to RealWorldTerrainElevationGenerator.OnGetElevationRange.
/// </summary>
/// <param name="minEl">Minimum elevation for the area (meters)</param>
/// <param name="maxEl">Maximum elevation for the area (meters)</param>
private void OnGetElevationRange(out double minEl, out double maxEl)
{
minEl = maxEl = 0;
// Here you calculate the maximum and minimum elevation value,
// or return the pre-calculated values.
}
/// <summary>
/// This method will be called when the RWT wants to get the elevation value from the provider
/// </summary>
/// <param name="mx">Mercator position X (0-1)</param>
/// <param name="my">Mercator position Y (0-1)</param>
/// <returns>Elevation value in meters or null (if you need to get elevation value from the provider)</returns>
private double? OnGetElevation(double mx, double my)
{
// Mercator position is a tile position for zoom 0.
// https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/
// https://docs.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system?redirectedfrom=MSDN
// Here you calculate your elevation value in meters, and return that value.
// Or return null if you need to get elevation value from the provider.
// Points of interest:
// Use RealWorldTerrainUtils.MercatToLatLong if you need to convert Mercator position to geographic coordinates.
return null;
}
[MenuItem("RWT/Intercept Elevation")]
private static void OpenWindow()
{
GetWindow<InterceptGetElevationExample>("Intercept Elevation");
}
}
Infinity Code Team.
Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.