256 lines
7.1 KiB
C#
256 lines
7.1 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Mapbox.Unity.Map;
|
|
using Mapbox.Utils;
|
|
using System.Collections;
|
|
using Mapbox.Unity.MeshGeneration.Modifiers;
|
|
using Mapbox.Unity.MeshGeneration.Data;
|
|
using Mapbox.Unity.Utilities;
|
|
using System;
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
{
|
|
public class RouteController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
AbstractMap _map;
|
|
[SerializeField]
|
|
MeshModifier SnapModifier;
|
|
[SerializeField]
|
|
Material _material;
|
|
[SerializeField]
|
|
[Range(1, 10)]
|
|
private float UpdateFrequency = 0.1f;
|
|
|
|
private int _counter;
|
|
|
|
GameObject _directionsGO;
|
|
GameObject _mipMapRoute;
|
|
GameObject _pathRoute;
|
|
CyclingController cyclingCotroller;
|
|
private bool initComplete = false;
|
|
|
|
private List<Vector3> vector3s = new List<Vector3>();
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (_map == null)
|
|
{
|
|
_map = FindObjectOfType<AbstractMap>();
|
|
}
|
|
// _map.OnInitialized += _map_OnInitialized; ;
|
|
//_map.OnUpdated += _map_OnUpdated; ;
|
|
|
|
|
|
|
|
}
|
|
private void Update()
|
|
{
|
|
if(!vector3s.Contains(playerController.currentPos))
|
|
vector3s.Add(playerController.currentPos);
|
|
}
|
|
|
|
private void _map_OnUpdated()
|
|
{
|
|
Debug.Log("_map_OnUpdated");
|
|
}
|
|
|
|
private void _map_OnInitialized()
|
|
{
|
|
Debug.Log("_map_OnInitialized");
|
|
CreateRoute();
|
|
}
|
|
|
|
private void _map_OnTileFinished(UnityTile obj)
|
|
{
|
|
|
|
if (!initComplete)
|
|
{
|
|
CreateRoute(obj);
|
|
initComplete = true;
|
|
}
|
|
}
|
|
|
|
|
|
public PlayerController playerController;
|
|
public void Start()
|
|
{
|
|
cyclingCotroller = transform.parent.GetComponent<CyclingController>();
|
|
//playerController = FindObjectOfType<PlayerController>();
|
|
_map.OnTileFinished += _map_OnTileFinished;
|
|
StartCoroutine(QueryTimer());
|
|
//CreateRoute();
|
|
}
|
|
|
|
protected virtual void OnDestroy()
|
|
{
|
|
_map.OnInitialized -= CreateRoute;
|
|
_map.OnUpdated -= CreateRoute;
|
|
}
|
|
|
|
public IEnumerator QueryTimer()
|
|
{
|
|
while (true)
|
|
{
|
|
CreateRoute(null);
|
|
yield return new WaitForSeconds(UpdateFrequency);
|
|
}
|
|
}
|
|
|
|
void CreateRoute(UnityTile tile)
|
|
{
|
|
{
|
|
var meshData = new MeshData();
|
|
var dat = new List<Vector3>();
|
|
var path = new List<Vector3>();
|
|
|
|
var mapData = cyclingCotroller.GetMapData();
|
|
if (mapData != null && playerController != null)
|
|
{
|
|
//foreach (var mapDataItem in mapData.List)
|
|
for (int i = 0; i < mapData.List.Count; i++)
|
|
{
|
|
var point = mapData.List[i].Point;
|
|
Vector3 item = _map.GeoToWorldPosition(new Vector2d(point[0], point[1]));
|
|
item.y += 1f;
|
|
//if (!posInScreen(item) && i > playerController.CurrentIndex)
|
|
//{
|
|
// break;
|
|
//}
|
|
//else if (posInScreen(item))
|
|
//{
|
|
// dat.Add(item);
|
|
//}
|
|
//前后取50个点
|
|
decimal diff = i - playerController.CurrentIndex;
|
|
decimal diffAbs = Math.Abs(diff);
|
|
if (diffAbs <= 200)
|
|
{
|
|
if (!dat.Contains(item))
|
|
dat.Add(item);
|
|
}
|
|
if (diff <= 0 && diff > -200)
|
|
{
|
|
item.y += 0.15f;
|
|
if (!dat.Contains(item))
|
|
path.Add(item);
|
|
}
|
|
}
|
|
var feat = new VectorFeatureUnity();
|
|
feat.Points.Add(dat);
|
|
//处理海拔高度的问题
|
|
//bool foundTile = _map.MapVisualizer.ActiveTiles.TryGetValue(Conversions.LatitudeLongitudeToTileId(_map.CenterLatitudeLongitude.x, _map.CenterLatitudeLongitude.y, (int)_map.Zoom), out tile);
|
|
//if (foundTile)
|
|
//{
|
|
// SnapModifier.Run(feat, meshData, tile);
|
|
//}
|
|
CreatMapRoute(feat);//创建路线
|
|
CreatPathRoute(path);//创建骑行过的线路
|
|
}
|
|
}
|
|
}
|
|
bool posInScreen(Vector3 position)
|
|
{
|
|
Transform camreatra = Camera.main.transform;
|
|
Vector3 viewPos = Camera.main.WorldToViewportPoint(position);
|
|
Vector3 dir = (position - camreatra.localPosition).normalized;
|
|
float dot = Vector3.Dot(camreatra.forward, dir);
|
|
if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1.5 && viewPos.y >= 0 && viewPos.y <= 1.5)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
void CreateRoute()
|
|
{
|
|
UnityTile tile;
|
|
bool foundTile = _map.MapVisualizer.ActiveTiles.TryGetValue(Conversions.LatitudeLongitudeToTileId(_map.CenterLatitudeLongitude.x, _map.CenterLatitudeLongitude.y, (int)_map.Zoom), out tile);
|
|
if (foundTile)
|
|
{
|
|
var meshData = new MeshData();
|
|
var dat = new List<Vector3>();
|
|
|
|
var mapData = cyclingCotroller.GetMapData();
|
|
if (mapData != null)
|
|
{
|
|
foreach (var mapDataItem in mapData.List)
|
|
{
|
|
var point = mapDataItem.Point;
|
|
Vector3 item = _map.GeoToWorldPosition(new Vector2d(point[0], point[1]));
|
|
item.y += 0.3f;
|
|
dat.Add(item);
|
|
}
|
|
var feat = new VectorFeatureUnity();
|
|
feat.Points.Add(dat);
|
|
//处理海拔高度的问题
|
|
//SnapModifier.Run(feat, meshData, tile);
|
|
//CreatMapRoute(feat);//创建路线
|
|
//CreateMiniMapRoute(feat);//创建小地图路线
|
|
|
|
}
|
|
}
|
|
}
|
|
void CreatMapRoute(VectorFeatureUnity feat)
|
|
{
|
|
if (_directionsGO != null)
|
|
{
|
|
_directionsGO.Destroy();
|
|
}
|
|
_directionsGO = new GameObject("MapRoute");
|
|
_directionsGO.transform.parent = transform;
|
|
var lineRender = _directionsGO.AddComponent<LineRenderer>();
|
|
//lineRender.material = new Material(Shader.Find("Sprites/Default"));
|
|
lineRender.material = Resources.Load<Material>("UI/Material/color8");
|
|
var dat = feat.Points[0];
|
|
ColorUtility.TryParseHtmlString("#FF2742", out Color c);
|
|
lineRender.endColor = new Color(1,1,1,0.65f);
|
|
lineRender.startColor = new Color(1, 1, 1, 0.65f);
|
|
|
|
//设置宽度
|
|
lineRender.startWidth = 1f;
|
|
lineRender.endWidth = 1f;
|
|
lineRender.positionCount = dat.Count;
|
|
lineRender.SetPositions(feat.Points[0].ToArray());
|
|
lineRender.loop = false;
|
|
lineRender.numCapVertices = 10;
|
|
lineRender.numCornerVertices = 10;
|
|
|
|
|
|
}
|
|
|
|
void CreatPathRoute(List<Vector3> feat)
|
|
{
|
|
if (_pathRoute != null)
|
|
{
|
|
_pathRoute.Destroy();
|
|
}
|
|
_pathRoute = new GameObject("MapPathRoute");
|
|
_pathRoute.transform.parent = transform;
|
|
var lineRender = _pathRoute.AddComponent<LineRenderer>();
|
|
//lineRender.material = new Material(Shader.Find("Sprites/Default"));
|
|
var color7 = Resources.Load<Material>("UI/Material/color7");
|
|
var Graph2 = Resources.Load<Material>("UI/Material/3dGraph2");
|
|
lineRender.materials = new Material[] { color7, Graph2 };
|
|
lineRender.endColor = new Color(0.9764706f, 0.1882353f, 0.5254902f, 0.65f);
|
|
lineRender.startColor = new Color(0.9764706f, 0.1882353f, 0.5254902f, 0.65f);
|
|
|
|
//设置宽度
|
|
lineRender.startWidth = 1f;
|
|
lineRender.endWidth = 1f;
|
|
lineRender.positionCount = feat.Count;
|
|
lineRender.SetPositions(feat.ToArray());
|
|
lineRender.loop = false;
|
|
//lineRender.numCapVertices = 10;
|
|
lineRender.numCornerVertices = 10;
|
|
//
|
|
lineRender.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
lineRender.allowOcclusionWhenDynamic = false;
|
|
lineRender.textureMode = LineTextureMode.Tile;
|
|
lineRender.motionVectorGenerationMode = MotionVectorGenerationMode.ForceNoMotion;
|
|
lineRender.receiveShadows = false;
|
|
lineRender.rayTracingMode = UnityEngine.Experimental.Rendering.RayTracingMode.Off;
|
|
}
|
|
}
|
|
}
|