[TASK] Initial commit with basic product setup

This commit is contained in:
2019-08-18 13:50:14 +02:00
commit 01a66a8e1f
2548 changed files with 167528 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using Mapbox.Map;
using Mapbox.Unity.Map.Interfaces;
using UnityEngine;
namespace Mapbox.Unity.Map.TileProviders
{
public class ExtentArgs : EventArgs
{
//TODO: Override GetHashCode for UnwrappedTileId
public HashSet<UnwrappedTileId> activeTiles;
}
public abstract class AbstractTileProvider : MonoBehaviour, ITileProvider
{
public event EventHandler<ExtentArgs> ExtentChanged;
protected IMap _map;
protected ExtentArgs _currentExtent = new ExtentArgs();
protected ITileProviderOptions _options;
public ITileProviderOptions Options
{
get
{
return _options;
}
}
public virtual void Initialize(IMap map)
{
_map = map;
OnInitialized();
}
public virtual void OnExtentChanged()
{
ExtentChanged(this, _currentExtent);
}
public abstract void OnInitialized();
public abstract void UpdateTileExtent();
public abstract bool Cleanup(UnwrappedTileId tile);
public virtual void SetOptions(ITileProviderOptions options)
{
_options = options;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b7386ebbea7d44bb29c2107b2c909409
timeCreated: 1494606252
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
using Mapbox.Map;
using Mapbox.Utils;
namespace Mapbox.Unity.Map.TileProviders
{
public class GlobeTileProvider : AbstractTileProvider
{
public override void OnInitialized()
{
_currentExtent.activeTiles = new HashSet<UnwrappedTileId>();
}
public override void UpdateTileExtent()
{
// HACK: don't allow too many tiles to be requested.
if (_map.AbsoluteZoom > 5)
{
throw new System.Exception("Too many tiles! Use a lower zoom level!");
}
var tileCover = TileCover.Get(Vector2dBounds.World(), _map.AbsoluteZoom);
foreach (var tile in tileCover)
{
_currentExtent.activeTiles.Add(new UnwrappedTileId(tile.Z, tile.X, tile.Y));
}
OnExtentChanged();
}
public override bool Cleanup(UnwrappedTileId tile)
{
return (!_currentExtent.activeTiles.Contains(tile));
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: a718f313c2f574a4aad42d7b52867205
timeCreated: 1500047799
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,39 @@
using Mapbox.Map;
using Mapbox.Unity.Map.Interfaces;
using Mapbox.Unity.MeshGeneration.Data;
using Mapbox.Unity.Utilities;
using Mapbox.Utils;
using UnityEngine;
namespace Mapbox.Unity.Map.TileProviders
{
//TODO : obsolete.
[CreateAssetMenu(menuName = "Mapbox/MapVisualizer/QuadTreeMapVisualizer")]
public class QuadTreeMapVisualizer : AbstractMapVisualizer
{
protected override void PlaceTile(UnwrappedTileId tileId, UnityTile tile, IMapReadable map)
{
//get the tile covering the center (Unity 0,0,0) of current extent
UnwrappedTileId centerTile = TileCover.CoordinateToTileId(map.CenterLatitudeLongitude, map.AbsoluteZoom);
//get center WebMerc corrdinates of tile covering the center (Unity 0,0,0)
Vector2d centerTileCenter = Conversions.TileIdToCenterWebMercator(centerTile.X, centerTile.Y, map.AbsoluteZoom);
//calculate distance between WebMerc center coordinates of center tile and WebMerc coordinates exactly at center
Vector2d shift = map.CenterMercator - centerTileCenter;
var unityTileSize = map.UnityTileSize;
// get factor at equator to avoid shifting errors at higher latitudes
float factor = Conversions.GetTileScaleInMeters(0f, map.AbsoluteZoom) * 256.0f / unityTileSize;
var scaleFactor = Mathf.Pow(2, (map.InitialZoom - map.AbsoluteZoom));
//position the tile relative to the center tile of the current viewport using the tile id
//multiply by tile size Unity units (unityTileScale)
//shift by distance of current viewport center to center of center tile
float shiftX = (float)shift.x / factor;
float shiftY = (float)shift.y / factor;
Vector3 position = new Vector3(
((tileId.X - centerTile.X) * unityTileSize - shiftX) * scaleFactor
, 0
, ((centerTile.Y - tileId.Y) * unityTileSize - shiftY) * scaleFactor);
tile.transform.localPosition = position;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1dfd965733118434481fedf30fa380c1
timeCreated: 1508892338
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,199 @@
using System;
using System.Collections.Generic;
using Mapbox.Map;
using Mapbox.Unity.Utilities;
using Mapbox.Utils;
using UnityEngine;
namespace Mapbox.Unity.Map.TileProviders
{
public class QuadTreeTileProvider : AbstractTileProvider
{
private Plane _groundPlane;
private bool _shouldUpdate;
private CameraBoundsTileProviderOptions _cbtpOptions;
//private List<UnwrappedTileId> _toRemove;
//private HashSet<UnwrappedTileId> _tilesToRequest;
private Vector2dBounds _viewPortWebMercBounds;
#region Tile decision and raycasting fields
private HashSet<UnwrappedTileId> _tiles;
private HashSet<CanonicalTileId> _canonicalTiles;
private Ray _ray00;
private Ray _ray01;
private Ray _ray10;
private Ray _ray11;
private Vector3[] _hitPnt = new Vector3[4];
private bool _isFirstLoad;
#endregion
public override void OnInitialized()
{
_tiles = new HashSet<UnwrappedTileId>();
_canonicalTiles = new HashSet<CanonicalTileId>();
_cbtpOptions = (CameraBoundsTileProviderOptions)_options;
if (_cbtpOptions.camera == null)
{
_cbtpOptions.camera = Camera.main;
}
_cbtpOptions.camera.transform.hasChanged = false;
_groundPlane = new Plane(Vector3.up, 0);
_shouldUpdate = true;
_currentExtent.activeTiles = new HashSet<UnwrappedTileId>();
}
public override void UpdateTileExtent()
{
if (!_shouldUpdate)
{
return;
}
//update viewport in case it was changed by switching zoom level
_viewPortWebMercBounds = getcurrentViewPortWebMerc();
_currentExtent.activeTiles = GetWithWebMerc(_viewPortWebMercBounds, _map.AbsoluteZoom);
OnExtentChanged();
}
public HashSet<UnwrappedTileId> GetWithWebMerc(Vector2dBounds bounds, int zoom)
{
_tiles.Clear();
_canonicalTiles.Clear();
if (bounds.IsEmpty()) { return _tiles; }
//stay within WebMerc bounds
Vector2d swWebMerc = new Vector2d(Math.Max(bounds.SouthWest.x, -Utils.Constants.WebMercMax), Math.Max(bounds.SouthWest.y, -Utils.Constants.WebMercMax));
Vector2d neWebMerc = new Vector2d(Math.Min(bounds.NorthEast.x, Utils.Constants.WebMercMax), Math.Min(bounds.NorthEast.y, Utils.Constants.WebMercMax));
//UnityEngine.Debug.LogFormat("swWebMerc:{0}/{1} neWebMerc:{2}/{3}", swWebMerc.x, swWebMerc.y, neWebMerc.x, neWebMerc.y);
UnwrappedTileId swTile = WebMercatorToTileId(swWebMerc, zoom);
UnwrappedTileId neTile = WebMercatorToTileId(neWebMerc, zoom);
//UnityEngine.Debug.LogFormat("swTile:{0} neTile:{1}", swTile, neTile);
for (int x = swTile.X; x <= neTile.X; x++)
{
for (int y = neTile.Y; y <= swTile.Y; y++)
{
UnwrappedTileId uwtid = new UnwrappedTileId(zoom, x, y);
//hack: currently too many tiles are created at lower zoom levels
//investigate formulas, this worked before
if (!_canonicalTiles.Contains(uwtid.Canonical))
{
//Debug.LogFormat("TileCover.GetWithWebMerc: {0}/{1}/{2}", zoom, x, y);
_tiles.Add(uwtid);
_canonicalTiles.Add(uwtid.Canonical);
}
}
}
return _tiles;
}
public UnwrappedTileId WebMercatorToTileId(Vector2d webMerc, int zoom)
{
var tileCount = Math.Pow(2, zoom);
var dblX = webMerc.x / Utils.Constants.WebMercMax;
var dblY = webMerc.y / Utils.Constants.WebMercMax;
int x = (int)Math.Floor((1 + dblX) / 2 * tileCount);
int y = (int)Math.Floor((1 - dblY) / 2 * tileCount);
return new UnwrappedTileId(zoom, x, y);
}
private Vector2dBounds getcurrentViewPortWebMerc(bool useGroundPlane = true)
{
if (useGroundPlane)
{
// rays from camera to groundplane: lower left and upper right
_ray00 = _cbtpOptions.camera.ViewportPointToRay(new Vector3(0, 0));
_ray01 = _cbtpOptions.camera.ViewportPointToRay(new Vector3(0, 1));
_ray10 = _cbtpOptions.camera.ViewportPointToRay(new Vector3(1, 0));
_ray11 = _cbtpOptions.camera.ViewportPointToRay(new Vector3(1, 1));
_hitPnt[0] = getGroundPlaneHitPoint(_ray00);
_hitPnt[1] = getGroundPlaneHitPoint(_ray01);
_hitPnt[2] = getGroundPlaneHitPoint(_ray10);
_hitPnt[3] = getGroundPlaneHitPoint(_ray11);
}
// Find min max bounding box.
// TODO : Find a better way of doing this.
float minX = float.MaxValue;
float minZ = float.MaxValue;
float maxX = float.MinValue;
float maxZ = float.MinValue;
for (int i = 0; i < 4; i++)
{
if (_hitPnt[i] == Vector3.zero)
{
continue;
}
else
{
if (minX > _hitPnt[i].x)
{
minX = _hitPnt[i].x;
}
if (minZ > _hitPnt[i].z)
{
minZ = _hitPnt[i].z;
}
if (maxX < _hitPnt[i].x)
{
maxX = _hitPnt[i].x;
}
if (maxZ < _hitPnt[i].z)
{
maxZ = _hitPnt[i].z;
}
}
}
Vector3 hitPntLL = new Vector3(minX, 0, minZ);
Vector3 hitPntUR = new Vector3(maxX, 0, maxZ);
//Debug.Log(hitPntLL + " - " + hitPntUR);
var llLatLong = _map.WorldToGeoPosition(hitPntLL);
var urLatLong = _map.WorldToGeoPosition(hitPntUR);
Vector2dBounds tileBounds = new Vector2dBounds(Conversions.LatLonToMeters(llLatLong), Conversions.LatLonToMeters(urLatLong));
// Bounds debugging.
#if UNITY_EDITOR
Debug.DrawLine(_cbtpOptions.camera.transform.position, hitPntLL, Color.blue);
Debug.DrawLine(_cbtpOptions.camera.transform.position, hitPntUR, Color.red);
#endif
return tileBounds;
}
private Vector3 getGroundPlaneHitPoint(Ray ray)
{
float distance;
if (!_groundPlane.Raycast(ray, out distance)) { return Vector3.zero; }
return ray.GetPoint(distance);
}
public virtual void Update()
{
if (_cbtpOptions != null && _cbtpOptions.camera != null && _cbtpOptions.camera.transform.hasChanged)
{
UpdateTileExtent();
_cbtpOptions.camera.transform.hasChanged = false;
}
}
public override bool Cleanup(UnwrappedTileId tile)
{
return (!_currentExtent.activeTiles.Contains(tile));
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 756a01a914208439d8d2fc8dcd68b229
timeCreated: 1508278498
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,79 @@
using System.Collections.Generic;
using Mapbox.Map;
using UnityEngine;
namespace Mapbox.Unity.Map.TileProviders
{
public class RangeAroundTransformTileProvider : AbstractTileProvider
{
private RangeAroundTransformTileProviderOptions _rangeTileProviderOptions;
private bool _initialized = false;
private UnwrappedTileId _currentTile;
private bool _waitingForTargetTransform = false;
public override void OnInitialized()
{
_rangeTileProviderOptions = (RangeAroundTransformTileProviderOptions)Options;
if (_rangeTileProviderOptions.targetTransform == null)
{
Debug.LogError("TransformTileProvider: No location marker transform specified.");
_waitingForTargetTransform = true;
}
else
{
_initialized = true;
}
//_toRemove = new List<UnwrappedTileId>(((_rangeTileProviderOptions.visibleBuffer * 2) + 1) * ((_rangeTileProviderOptions.visibleBuffer * 2) + 1));
_currentExtent.activeTiles = new HashSet<UnwrappedTileId>();
_map.OnInitialized += UpdateTileExtent;
_map.OnUpdated += UpdateTileExtent;
}
public override void UpdateTileExtent()
{
if (!_initialized) return;
_currentExtent.activeTiles.Clear();
//_toRemove.Clear();
_currentTile = TileCover.CoordinateToTileId(_map.WorldToGeoPosition(_rangeTileProviderOptions.targetTransform.localPosition), _map.AbsoluteZoom);
for (int x = _currentTile.X - _rangeTileProviderOptions.visibleBuffer; x <= (_currentTile.X + _rangeTileProviderOptions.visibleBuffer); x++)
{
for (int y = _currentTile.Y - _rangeTileProviderOptions.visibleBuffer; y <= (_currentTile.Y + _rangeTileProviderOptions.visibleBuffer); y++)
{
_currentExtent.activeTiles.Add(new UnwrappedTileId(_map.AbsoluteZoom, x, y));
}
}
OnExtentChanged();
}
public virtual void Update()
{
if (_waitingForTargetTransform && !_initialized)
{
if (_rangeTileProviderOptions.targetTransform != null)
{
_initialized = true;
}
}
if (_rangeTileProviderOptions != null && _rangeTileProviderOptions.targetTransform != null && _rangeTileProviderOptions.targetTransform.hasChanged)
{
UpdateTileExtent();
_rangeTileProviderOptions.targetTransform.hasChanged = false;
}
}
public override bool Cleanup(UnwrappedTileId tile)
{
bool dispose = false;
dispose = tile.X > _currentTile.X + _rangeTileProviderOptions.disposeBuffer || tile.X < _currentTile.X - _rangeTileProviderOptions.disposeBuffer;
dispose = dispose || tile.Y > _currentTile.Y + _rangeTileProviderOptions.disposeBuffer || tile.Y < _currentTile.Y - _rangeTileProviderOptions.disposeBuffer;
return (dispose);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 15eed3c0528a3498dae4ff8f7b8ae77f
timeCreated: 1505936421
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,58 @@
using System.Collections.Generic;
using Mapbox.Map;
namespace Mapbox.Unity.Map.TileProviders
{
public class RangeTileProvider : AbstractTileProvider
{
private RangeTileProviderOptions _rangeTileProviderOptions;
private bool _initialized = false;
//private List<UnwrappedTileId> _toRemove;
//private HashSet<UnwrappedTileId> _tilesToRequest;
public override void OnInitialized()
{
if (Options != null)
{
_rangeTileProviderOptions = (RangeTileProviderOptions)Options;
}
else
{
_rangeTileProviderOptions = new RangeTileProviderOptions();
}
_initialized = true;
//_toRemove = new List<UnwrappedTileId>((_rangeTileProviderOptions.east + _rangeTileProviderOptions.west) * (_rangeTileProviderOptions.north + _rangeTileProviderOptions.south));
_currentExtent.activeTiles = new HashSet<UnwrappedTileId>();
}
public override void UpdateTileExtent()
{
if (!_initialized || Options == null)
{
return;
}
_currentExtent.activeTiles.Clear();
//_toRemove.Clear();
var centerTile = TileCover.CoordinateToTileId(_map.CenterLatitudeLongitude, _map.AbsoluteZoom);
_currentExtent.activeTiles.Add(new UnwrappedTileId(_map.AbsoluteZoom, centerTile.X, centerTile.Y));
for (int x = (centerTile.X - _rangeTileProviderOptions.west); x <= (centerTile.X + _rangeTileProviderOptions.east); x++)
{
for (int y = (centerTile.Y - _rangeTileProviderOptions.north); y <= (centerTile.Y + _rangeTileProviderOptions.south); y++)
{
_currentExtent.activeTiles.Add(new UnwrappedTileId(_map.AbsoluteZoom, x, y));
}
}
OnExtentChanged();
}
public override bool Cleanup(UnwrappedTileId tile)
{
return (!_currentExtent.activeTiles.Contains(tile));
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7d7ff8ed3b76a4ee5a451d64a5393e3f
timeCreated: 1494606252
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Mapbox.Map;
using UnityEngine;
using UnityEngine.Events;
namespace Mapbox.Unity.Map.TileProviders
{
/// <summary>
/// Monobehavior Script to handle TileErrors.
/// There's an OnTileError event on AbstractMapVisualizer, AbstractTileFactory and UnityTile classes that one can subscribe to to listen to tile errors
/// </summary>
public class TileErrorHandler : MonoBehaviour
{
[SerializeField]
private AbstractMap _mapInstance;
public TileErrorEvent OnTileError;
protected virtual void OnEnable()
{
if (_mapInstance == null)
{
_mapInstance = GetComponent<AbstractMap>();
}
_mapInstance.OnInitialized += MapInstance_OnInitialized;
}
private void MapInstance_OnInitialized()
{
_mapInstance.OnInitialized -= MapInstance_OnInitialized;
_mapInstance.MapVisualizer.OnTileError += _OnTileErrorHandler;
}
private void _OnTileErrorHandler(object sender, TileErrorEventArgs e)
{
// check if request has been aborted: show warning not error
if (e.Exceptions.Count > 0)
{
// 1. aborted is always the first exception
// additional exceptions are always caused by the request being aborted
// show all of them as warnings
// 2. 'Unable to write data' is another exception associated
// with aborted requests: request finshed successfully but
// was aborted during filling of local buffer, also show as warning
if (
e.Exceptions[0].Message.Contains("Request aborted")
|| e.Exceptions[0].Message.Equals("Unable to write data")
)
{
Debug.LogWarning(printMessage(e.Exceptions, e));
}
else
{
Debug.LogError(printMessage(e.Exceptions, e));
}
}
if (OnTileError != null)
{
OnTileError.Invoke(e);
}
}
private string printMessage(List<Exception> exceptions, TileErrorEventArgs e)
{
return string.Format(
"{0} Exception(s) caused on the tile. Tile ID:{1} Tile Type:{4}{2}{3}"
, exceptions.Count
, e.TileId
, Environment.NewLine
, string.Join(Environment.NewLine, exceptions.Select(ex => ex.Message).ToArray())
, e.TileType
);
}
protected virtual void OnDisable()
{
_mapInstance.MapVisualizer.OnTileError -= _OnTileErrorHandler;
}
}
[System.Serializable]
public class TileErrorEvent : UnityEvent<TileErrorEventArgs> { }
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 76a256726e16d4966a3f30b97a17e1d5
timeCreated: 1509146285
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: