132 lines
2.7 KiB
C#
132 lines
2.7 KiB
C#
namespace Mapbox.Unity.MeshGeneration.Factories
|
|
{
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Mapbox.Unity.Map;
|
|
using Data;
|
|
using Modifiers;
|
|
using Mapbox.Utils;
|
|
using Mapbox.Unity.Utilities;
|
|
using System.Collections;
|
|
using Newtonsoft.Json;
|
|
using System.IO;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
|
|
public class RouteController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
AbstractMap _map;
|
|
|
|
[SerializeField]
|
|
MeshModifier[] MeshModifiers;
|
|
[SerializeField]
|
|
Material _material;
|
|
|
|
[SerializeField]
|
|
[Range(1, 10)]
|
|
private float UpdateFrequency = 2;
|
|
|
|
private int _counter;
|
|
|
|
GameObject _directionsGO;
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (_map == null)
|
|
{
|
|
_map = FindObjectOfType<AbstractMap>();
|
|
}
|
|
_map.OnInitialized += CreateRoute;
|
|
_map.OnUpdated += CreateRoute;
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
foreach (var modifier in MeshModifiers)
|
|
{
|
|
modifier.Initialize();
|
|
}
|
|
|
|
StartCoroutine(QueryTimer());
|
|
}
|
|
|
|
protected virtual void OnDestroy()
|
|
{
|
|
_map.OnInitialized -= CreateRoute;
|
|
_map.OnUpdated -= CreateRoute;
|
|
}
|
|
|
|
public IEnumerator QueryTimer()
|
|
{
|
|
while (true)
|
|
{
|
|
yield return new WaitForSeconds(UpdateFrequency);
|
|
CreateRoute();
|
|
}
|
|
}
|
|
void CreateRoute()
|
|
{
|
|
var meshData = new MeshData();
|
|
var dat = new List<Vector3>();
|
|
|
|
List<Vector2d> points = MockData.vl;
|
|
if (points == null)
|
|
{
|
|
points = MockData.GetRideList();
|
|
}
|
|
{
|
|
foreach (var point in points)
|
|
{
|
|
Vector3 item = _map.GeoToWorldPosition(point);//point.x, point.y, _map.CenterMercator, _map.WorldRelativeScale);
|
|
//item.y = (float)(_map.QueryElevationInUnityUnitsAt(point))+0.3f;
|
|
item.y += 0.5f;
|
|
dat.Add(item);
|
|
}
|
|
var feat = new VectorFeatureUnity();
|
|
feat.Points.Add(dat);
|
|
|
|
foreach (MeshModifier mod in MeshModifiers.Where(x => x.Active))
|
|
{
|
|
mod.Run(feat, meshData, _map.WorldRelativeScale * 8);
|
|
|
|
}
|
|
CreateGameObject(meshData);
|
|
}
|
|
}
|
|
|
|
GameObject CreateGameObject(MeshData data)
|
|
{
|
|
if (_directionsGO != null)
|
|
{
|
|
_directionsGO.Destroy();
|
|
}
|
|
_directionsGO = new GameObject("direction waypoint " + " entity");
|
|
var mesh = _directionsGO.AddComponent<MeshFilter>().mesh;
|
|
mesh.subMeshCount = data.Triangles.Count;
|
|
|
|
mesh.SetVertices(data.Vertices);
|
|
_counter = data.Triangles.Count;
|
|
for (int i = 0; i < _counter; i++)
|
|
{
|
|
var triangle = data.Triangles[i];
|
|
mesh.SetTriangles(triangle, i);
|
|
}
|
|
|
|
_counter = data.UV.Count;
|
|
for (int i = 0; i < _counter; i++)
|
|
{
|
|
var uv = data.UV[i];
|
|
mesh.SetUVs(i, uv);
|
|
}
|
|
|
|
mesh.RecalculateNormals();
|
|
_directionsGO.AddComponent<MeshRenderer>().material = _material;
|
|
|
|
return _directionsGO;
|
|
}
|
|
}
|
|
|
|
}
|