Created
July 31, 2018 13:07
-
-
Save Naphier/8828d7957e2fea41411b102150b6f8df to your computer and use it in GitHub Desktop.
MapboxController: AbstractMap Mapbox v1.3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* VR Rehab, Inc Confidential | |
* __________________ | |
* | |
* Copyright 2016 - VR Rehab, Inc | |
* All Rights Reserved. | |
* | |
* NOTICE: All information contained herein is, and remains | |
* the property of VR Rehab, Inc and its subsidiaries, | |
* if any. The intellectual and technical concepts contained | |
* herein are proprietary to VR Rehab, Inc | |
* and its subsidiaries and may be covered by U.S. and Foreign Patents, | |
* patents in process, and are protected by trade secret or copyright law. | |
* Dissemination of this information or reproduction of this material | |
* is strictly forbidden unless prior written permission is obtained | |
* from VR Rehab, Inc. | |
* contact: [email protected] | |
* | |
*/ | |
using UnityEngine; | |
using UnityEngine.Events; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using Mapbox.Unity.MeshGeneration.Data; | |
using Mapbox.Unity.MeshGeneration.Interfaces; | |
using Mapbox.Unity.Utilities; | |
using Mapbox.Unity.Map; | |
using Mapbox.Map; | |
namespace VRR | |
{ | |
/// <summary> | |
/// Generates Mapbox terrain - VRR implementation. | |
/// </summary> | |
public class MapboxController : AbstractMap | |
{ | |
public Rect ReferenceTileRect { get; private set; } | |
public float WorldScaleFactor { get; private set; } | |
public VectorLayerVisualizer MapVisualization; | |
public Material defaultMaterial; | |
public float TileSize = 200; | |
public bool SnapYToZero = true; | |
//public int Zoom = 17; | |
public Vector4 Range = new Vector4(5, 5, 5, 5); | |
//public GameObject Root { get; private set; } | |
private Dictionary<Vector2, UnityTile> _tiles = new Dictionary<Vector2, UnityTile>(); | |
public UnityEvent OnExecuteStart; | |
public UnityEvent OnExecuteFinish; | |
public Action<float> OnProgressUpdate; | |
public Dictionary<Vector2, UnityTile> GetTiles() | |
{ | |
return _tiles; | |
} | |
protected override void Awake() | |
{ | |
_unityTileSize = TileSize; | |
InitializeListeners(); | |
base.Awake(); | |
} | |
protected override void OnDestroy() | |
{ | |
if (MapVisualizer != null) | |
MapVisualizer.OnMapVisualizerStateChanged -= OnMapVisualizerStateChanged; | |
base.OnDestroy(); | |
} | |
private void InitializeListeners() | |
{ | |
MapVisualizer.OnMapVisualizerStateChanged += OnMapVisualizerStateChanged; | |
//Mapbox.Unity.RequestCounter.OnFirstRequest += OnFirstRequest; | |
//Mapbox.Unity.RequestCounter.OnAllRequestsFinished += OnAllRequestsFinished; | |
} | |
bool didSendFirstRequest = false; | |
bool didFinish = false; | |
private void OnMapVisualizerStateChanged(ModuleState moduleState) | |
{ | |
Debug.LogWarning(transform.GetFullPath() + ".OnMapVisualizerStateChanged :" + moduleState.ToString()); | |
switch (moduleState) | |
{ | |
case ModuleState.Initialized: | |
OnFirstRequest(); | |
break; | |
case ModuleState.Working: | |
break; | |
case ModuleState.Finished: | |
OnAllRequestsFinished(); | |
break; | |
default: | |
throw new NotImplementedException(moduleState.ToString()); | |
} | |
} | |
// TODO - this is not an ideal way to check if the terrain is loaded, but for some reason the | |
// OnMapVisualizerStateChanged does not always work. | |
int children = -1; | |
private void Update() | |
{ | |
if (didFinish) | |
return; | |
if (Root.childCount != children) { | |
children = Root.childCount; | |
//Debug.Log("children: " + children); | |
if (children > 120) | |
OnAllRequestsFinished(); | |
} | |
} | |
private void OnFirstRequest() | |
{ | |
if (didSendFirstRequest) | |
return; | |
didSendFirstRequest = true; | |
//Mapbox.Unity.RequestCounter.OnFirstRequest -= OnFirstRequest; | |
OnExecuteStart.Invoke(); | |
//progressHandlerRef = ProgressHandler(); | |
//StartCoroutine(progressHandlerRef); | |
Debug.LogWarning(transform.GetFullPath() + ".MapboxController.OnFirstRequest()"); | |
} | |
private void OnAllRequestsFinished() | |
{ | |
if (didFinish) | |
return; | |
didFinish = true; | |
//Mapbox.Unity.RequestCounter.OnAllRequestsFinished -= OnAllRequestsFinished; | |
//Root.gameObject.SetLayerRecursive(Constants.TERRAIN_LAYER); | |
//SnapToYZero(); | |
OnExecuteFinish.Invoke(); | |
/* | |
if (progressHandlerRef != null) | |
{ | |
StopCoroutine(progressHandlerRef); | |
progressHandlerRef = null; | |
} | |
*/ | |
MapVisualizer.OnMapVisualizerStateChanged -= OnMapVisualizerStateChanged; | |
Debug.LogWarning(transform.GetFullPath() + ".MapboxController.OnAllRequestsFinished()"); | |
} | |
/* | |
private IEnumerator progressHandlerRef; | |
private IEnumerator ProgressHandler() | |
{ | |
while (true) | |
{ | |
if (OnProgressUpdate != null) | |
//OnProgressUpdate.Invoke(Mapbox.Unity.RequestCounter.GetProgress()); | |
yield return new WaitForEndOfFrame(); | |
} | |
} | |
*/ | |
public void SnapToYZero() | |
{ | |
if (SnapYToZero && Root != null) | |
{ | |
var ray = new Ray(new Vector3(0, 1000, 0), Vector3.down); | |
RaycastHit rayhit; | |
if (Physics.Raycast(ray, out rayhit)) | |
{ | |
Root.transform.position = new Vector3(0, -rayhit.point.y, 0); | |
SnapYToZero = false; | |
//Debug.LogErrorFormat("Snap to y zero done. y: {0} who: {1}", _root.transform.position.y, rayhit.collider.gameObject.name); | |
} | |
} | |
} | |
public double Latitude | |
{ | |
get | |
{ | |
return lat; | |
} | |
} | |
public double Longitude | |
{ | |
get | |
{ | |
return lng; | |
} | |
} | |
#pragma warning disable 0414 | |
[Header("For debugging inspection")] | |
[SerializeField] | |
private double lat = 0; | |
[SerializeField] | |
private double lng = 0; | |
#pragma warning restore | |
public void Execute(double lat, double lng) | |
{ | |
Initialize(new Vector2d(lat, lng), (int)Zoom); | |
} | |
/// <summary> | |
/// Deprecated 2018-02-22 | |
/// </summary> | |
/// <param name="lat"></param> | |
/// <param name="lng"></param> | |
/// <param name="zoom"></param> | |
/// <param name="frame"></param> | |
public void Execute(double lat, double lng, int zoom, Vector4 frame) | |
{ | |
Initialize(new Vector2d(lat, lng), zoom); | |
} | |
[SerializeField] | |
bool _useRelativeScale = true; | |
public override void Initialize(Vector2d latLon, int zoom) | |
{ | |
_worldHeightFixed = false; | |
_centerLatitudeLongitude = latLon; | |
_zoom = zoom; | |
_initialZoom = zoom; | |
var referenceTileRect = Conversions.TileBounds(TileCover.CoordinateToTileId(_centerLatitudeLongitude, AbsoluteZoom)); | |
_centerMercator = referenceTileRect.Center; | |
_worldRelativeScale = _useRelativeScale ? Mathf.Cos(Mathf.Deg2Rad * (float)_centerLatitudeLongitude.x) : 1f; | |
_mapVisualizer.Initialize(this, _fileSource); | |
_tileProvider.Initialize(this); | |
SendInitialized(); | |
OnFirstRequest(); | |
Debug.Log(transform.GetFullPath() + ".MapboxController.Initialize: " + latLon.ToString()); | |
lat = latLon.x; | |
lng = latLon.y; | |
} | |
// For some reason this freezes the app. | |
IEnumerator LateInit(Vector2d latLon) | |
{ | |
yield return new WaitForSecondsRealtime(1); | |
_mapVisualizer.Initialize(this, _fileSource); | |
_tileProvider.Initialize(this); | |
SendInitialized(); | |
Debug.Log(transform.GetFullPath() + ".MapboxController.Initialize: " + latLon.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment