Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections;
using Mapbox.BaseModule.Data.DataFetchers;
using Mapbox.BaseModule.Data.Tiles;

namespace Mapbox.BaseModule.Data.Interfaces
{
public interface ITerrainLayerModule : ILayerModule
{
float QueryElevation(CanonicalTileId tileId, float x, float y);
IEnumerator LoadTileData(CanonicalTileId tileId, Action<TerrainData> callback = null);
}
}
13 changes: 11 additions & 2 deletions Runtime/Mapbox/BaseModule/Map/MapInformationStaticMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ public static Vector3 ConvertLatLngToPositionForScale(this IMapInformation mapIn
return scaledDeltaMercatorVector3;
}

public static Vector3 ConvertLatLngToPosition(this IMapInformation mapInfo, LatitudeLongitude latlng)
public static Vector3 ConvertLatLngToPosition(this IMapInformation mapInfo, LatitudeLongitude latlng, bool queryElevation = false)
{
var mercator = Conversions.LatitudeLongitudeToWebMercator(latlng);
var deltaMercator = mercator - mapInfo.CenterMercator;
var scaledDeltaMercator = deltaMercator / mapInfo.Scale;
var scaledDeltaMercatorVector3 = scaledDeltaMercator.ToVector3xz();
var elevation = 0f;

if (queryElevation)
{
var tileId = Conversions.LatitudeLongitudeToTileId(latlng, 14).Canonical;
var pointPosition = Conversions.LatitudeLongitudeToInTile01(latlng, tileId);
elevation = mapInfo.QueryElevation(tileId, pointPosition.x, pointPosition.y);
}

var scaledDeltaMercatorVector3 = new Vector3((float)scaledDeltaMercator.x, elevation, (float)scaledDeltaMercator.y);

return scaledDeltaMercatorVector3;
}
Expand Down
79 changes: 79 additions & 0 deletions Runtime/Mapbox/BaseModule/Map/MapboxMapExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Mapbox.BaseModule.Data.Tiles;
using Mapbox.BaseModule.Data.Vector2d;
using Mapbox.BaseModule.Unity;
using Mapbox.BaseModule.Utilities;

namespace Mapbox.BaseModule.Map
{
public static class MapboxMapExtensions
{
public static bool TryGetMapTile(this MapboxMap map, UnwrappedTileId tileId, out UnityMapTile tile)
{
if (map.MapVisualizer.ActiveTiles.TryGetValue(tileId, out tile))
{
return true;
}

tile = null;
return false;
}

public static bool TryGetMapTile(this MapboxMap map, LatitudeLongitude coordinates, out UnityMapTile tile, int maxZoomLevel = 20)
{
var maxTileId = Conversions.LatitudeLongitudeToTileId(coordinates, maxZoomLevel);
for (int i = maxZoomLevel; i > 1; i--)
{
if (map.MapVisualizer.ActiveTiles.TryGetValue(maxTileId, out tile))
{
return true;
}
else
{
maxTileId = maxTileId.Parent;
}
}

tile = null;
return false;
}

/// <summary>
/// Only queries active tiles / visible world
/// </summary>
/// <param name="map"></param>
/// <param name="location"></param>
/// <param name="elevation"></param>
/// <param name="maxZoomLevel"></param>
/// <param name="minZoomLevel"></param>
/// <returns></returns>
public static bool TryGetElevation(this MapboxMap map, LatitudeLongitude location, out float elevation, int maxZoomLevel = 20, int minZoomLevel = 2)
{
var maxTileId = Conversions.LatitudeLongitudeToTileId(location, maxZoomLevel);
UnityMapTile tile = null;
for (int i = maxZoomLevel; i >= minZoomLevel; i--)
{
if (map.MapVisualizer.ActiveTiles.TryGetValue(maxTileId, out tile))
{
break;
}
else
{
maxTileId = maxTileId.Parent;
}
}

// Check tile and nested properties for null to prevent NullReferenceException
if (tile != null &&
tile.TerrainContainer != null &&
tile.TerrainContainer.TerrainData != null)
{
var tilePos = Conversions.LatitudeLongitudeToInTile01(location, tile.TerrainContainer.TerrainData.TileId);
elevation = tile.TerrainContainer.QueryHeightData(tilePos.x, tilePos.y);
return true;
}

elevation = float.MinValue;
return false;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Mapbox/BaseModule/Map/MapboxMapExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Mapbox/BaseModule/Map/MapboxMapVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ public void OnDestroy()
public bool TryGetLayerModule<T>(out T module) where T : ILayerModule
{
module = (T)LayerModules.FirstOrDefault(x => x is T);
if (module == null)
{
var composite = LayerModules.FirstOrDefault(x => x is CompositeLayerModule) as CompositeLayerModule;
if (composite != null)
{
module = (T)composite.LayerModules.FirstOrDefault(x => x is T);
}
}
return module != null;
}

Expand Down
8 changes: 8 additions & 0 deletions Runtime/Mapbox/CustomImageryModule.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions Runtime/Mapbox/CustomImageryModule/CustomApiLayerModuleScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Mapbox.BaseModule.Data.Interfaces;
using Mapbox.BaseModule.Map;
using Mapbox.BaseModule.Unity;
using Mapbox.BaseModule.Utilities;
using Mapbox.ImageModule;
using Mapbox.UnityMapService;
using UnityEngine;

namespace Mapbox.CustomImageryModule
{
public class CustomApiLayerModuleScript : ModuleConstructorScript
{
public CustomSourceSettings CustomSourceSettings;
public StaticLayerModuleSettings Settings = new StaticLayerModuleSettings()
{
RejectTilesOutsideZoom = new Vector2(2, 25),
DataSettings = new ImageSourceSettings()
{
ClampDataLevelToMax = 25
}
};
public override ILayerModule ModuleImplementation { get; protected set; }

private void Start()
{

}

public override ILayerModule ConstructModule(MapService service, IMapInformation mapInformation,
UnityContext unityContext)
{
if (Settings.SourceType == ImagerySourceType.None)
{

}
else if (Settings.SourceType == ImagerySourceType.Custom)
{
Settings.DataSettings.TilesetId = Settings.CustomSourceId;
}
else
{
var imageryTileset = MapboxDefaultImagery.GetParameters(Settings.SourceType);
Settings.DataSettings.TilesetId = imageryTileset.Id;
}

var unityService = service as MapUnityService;
var source = new CustomSource(CustomSourceSettings, unityService.FetchingManager, unityService.CacheManager, new ImageSourceSettings()
{
TilesetId = "CustomImagery"
});
ModuleImplementation = new StaticApiLayerModule(source, Settings);
return ModuleImplementation;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions Runtime/Mapbox/CustomImageryModule/CustomSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Mapbox.BaseModule.Data.DataFetchers;
using Mapbox.BaseModule.Data.Platform.Cache;
using Mapbox.BaseModule.Data.Tiles;
using Mapbox.BaseModule.Map;
using Mapbox.UnityMapService.DataSources;

namespace Mapbox.CustomImageryModule
{
public class CustomSource : ImageSource<RasterData>
{
protected CustomSourceSettings _customSourceSettings;
protected ImageSourceSettings _settings;

public CustomSource(CustomSourceSettings customSourceSettings, DataFetchingManager dataFetchingManager,
MapboxCacheManager mapboxCacheManager, ImageSourceSettings settings)
: base(dataFetchingManager, mapboxCacheManager, settings)
{
_settings = settings;
_customSourceSettings = customSourceSettings;
}

protected override RasterTile CreateTile(CanonicalTileId tileId, string tilesetId)
{
if (_customSourceSettings.InvertY)
{
return new CustomTMSTile(_customSourceSettings.UrlFormat, tileId, tilesetId, true);
}
else
{
return new RasterTile(tileId, tilesetId, true);
}
}

protected override RasterData CreateRasterDataWrapper(RasterTile tile)
{
RasterData rasterData;
rasterData = new RasterData()
{
TileId = tile.Id,
TilesetId = tile.TilesetId,
Texture = tile.Texture2D,
CacheType = tile.FromCache,
Data = tile.Data,
ETag = tile.ETag,
ExpirationDate = tile.ExpirationDate
};

return rasterData;
}

protected override void CheckExpiration(RasterData cacheItem)
{
//not checking for expiration and updating for custom sources
//just using whatever is in cache
return;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Mapbox/CustomImageryModule/CustomSource.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Runtime/Mapbox/CustomImageryModule/CustomSourceSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using UnityEngine;

namespace Mapbox.CustomImageryModule
{
[Serializable]
public class CustomSourceSettings
{
[Tooltip("Url format string, structured as C# string format with '{}' fields for X/Y/Z coordinates. {0}=Z, {1}=X, {2}=Y")]
public string UrlFormat;
[Tooltip("Invert Y axis coordinates for TMS coordinate system, which starts from bottom left and grows to top-right")]
public bool InvertY;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Runtime/Mapbox/CustomImageryModule/CustomTMSTile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Mapbox.BaseModule.Data.DataFetchers;
using Mapbox.BaseModule.Data.Platform;
using Mapbox.BaseModule.Data.Tiles;
using UnityEngine;

namespace Mapbox.CustomImageryModule
{
/// <summary>
/// TMS tile in the name means Y axis is inverted in the tile ids
/// </summary>
public class CustomTMSTile : RasterTile
{
private string _urlFormat;
public CustomTMSTile(string urlFormat, CanonicalTileId tileId, string tilesetId, bool useNonReadableTexture) : base(tileId, tilesetId, useNonReadableTexture)
{
_urlFormat = urlFormat;
}

public override void Initialize(IFileSource fileSource, Action<DataFetchingResult> p)
{
TileState = TileState.Loading;
_callback = p;

var invertY = (Mathf.Pow(2, Id.Z))- Id.Y - 1;
_generatedUrl = string.Format(_urlFormat, Id.Z, Id.X, (int)invertY);
DoTheRequest(fileSource);
}

protected override void DoTheRequest(IFileSource fileSource)
{
_webRequest = fileSource.CustomImageRequest(_generatedUrl, HandleTileResponse, ETag, 10, IsTextureNonreadable);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Mapbox/CustomImageryModule/CustomTMSTile.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Mapbox.BaseModule.Data.Interfaces;
using Mapbox.BaseModule.Map;
using Mapbox.BaseModule.Unity;
using Mapbox.ImageModule.Terrain;
using Mapbox.UnityMapService;
using UnityEngine;

namespace Mapbox.CustomImageryModule
{
public class CustomTerrainLayerModuleScript : ModuleConstructorScript
{
public CustomSourceSettings CustomSourceSettings;
public TerrainLayerModuleSettings Settings = new TerrainLayerModuleSettings()
{
RejectTilesOutsideZoom = new Vector2(2, 25),
DataSettings = new ImageSourceSettings()
{
ClampDataLevelToMax = 25
}
};
public override ILayerModule ModuleImplementation { get; protected set; }

private void Start()
{

}

public override ILayerModule ConstructModule(MapService service, IMapInformation mapInformation,
UnityContext unityContext)
{
Settings.DataSettings.TilesetId = "CustomTerrain";

var unityService = service as MapUnityService;
var source = new CustomTerrainSource(CustomSourceSettings, unityService.FetchingManager, unityService.CacheManager, new ImageSourceSettings()
{
TilesetId = "CustomTerrain"
});
ModuleImplementation = new TerrainLayerModule(source, Settings);
return ModuleImplementation;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading