Skip to content

Instantly share code, notes, and snippets.

@colin-young
Last active December 29, 2018 03:03
Show Gist options
  • Save colin-young/2883b0dd9fdf2b5925d56590e2b4ebd5 to your computer and use it in GitHub Desktop.
Save colin-young/2883b0dd9fdf2b5925d56590e2b4ebd5 to your computer and use it in GitHub Desktop.
Mapbox/Unity/EasyRoads3D Integration Problem
using System;
using System.Collections.Generic;
using System.Linq;
using EasyRoads3Dv3;
using Mapbox.Unity;
using Mapbox.Unity.MeshGeneration.Data;
using Mapbox.Unity.MeshGeneration.Modifiers;
using Mapbox.Unity.Utilities;
using UnityEngine;
[CreateAssetMenu(menuName = "Mapbox/Modifiers/EasyRoads3D Mesh Modifier")]
public class EasyRoads3DModifier : MeshModifier
{
[SerializeField]
ERRoadNetwork _roadNetwork;
public override ModifierType Type { get { return ModifierType.Preprocess; } }
public override void Run(VectorFeatureUnity feature, MeshData md, float scale)
{
if (_roadNetwork == null) _roadNetwork = new ERRoadNetwork();
CreateRoad(feature);
}
public override void Run(VectorFeatureUnity feature, MeshData md, UnityTile tile = null)
{
if (_roadNetwork == null) _roadNetwork = new ERRoadNetwork();
CreateRoad(feature);
}
private void CreateRoad(VectorFeatureUnity feature)
{
if (feature.Points.Count < 1)
return;
var roadName = feature.Properties.ContainsKey("name")
? feature.Properties["name"].ToString()
: Guid.NewGuid().ToString();
var roadId = feature.Properties.ContainsKey("@id")
? feature.Properties["@id"].ToString()
: "";
if (roadName.Equals("Iron Age Street"))
{
// TODO extract lanes
// TODO extract road type
foreach (var roadSegment in feature.Points)
{
Debug.Log(string.Format("Creating road '{0}' - [{2}] from points {1}",
roadName,
roadSegment.Select(s => s.ToString()).Aggregate((a, c) => a + ", " + c),
roadId));
_roadNetwork.CreateRoad(roadName, roadSegment.ToArray());
}
}
}
}
private void ExtureLine(VectorFeatureUnity feature, MeshData md)
{
if (feature.Points.Count < 1)
return;
// ---- START added code ----
var roadName = feature.Properties.ContainsKey("name")
? feature.Properties["name"].ToString()
: Guid.NewGuid().ToString();
var camera = Camera.main.gameObject.transform;
// ---- END added code ----
foreach (var roadSegment in feature.Points)
{
var mdVertexCount = md.Vertices.Count;
var roadSegmentCount = roadSegment.Count;
for (int i = 1; i < roadSegmentCount * 2; i++)
{
md.Edges.Add(mdVertexCount + i);
md.Edges.Add(mdVertexCount + i - 1);
}
md.Edges.Add(mdVertexCount);
md.Edges.Add(mdVertexCount + (roadSegmentCount * 2) - 1);
var newVerticeList = new Vector3[roadSegmentCount * 2];
var newNorms = new Vector3[roadSegmentCount * 2];
var uvList = new Vector2[roadSegmentCount * 2];
var newTangents = new Vector4[roadSegmentCount * 2];
Vector3 norm;
var lastUv = 0f;
var p1 = Constants.Math.Vector3Zero;
var p2 = Constants.Math.Vector3Zero;
var p3 = Constants.Math.Vector3Zero;
// ---- START added code ----
foreach (var segment in roadSegment)
{
Color rayColor = Color.black;
Debug.DrawRay(camera.position, segment - camera.position, rayColor, 30, true);
}
// ---- END added code ----
for (int i = 1; i < roadSegmentCount; i++)
{
p1 = roadSegment[i - 1];
p2 = roadSegment[i];
p3 = p2;
if (i + 1 < roadSegmentCount)
{
p3 = roadSegment[i + 1];
}
if (i == 1)
{
norm = GetNormal(p1, p1, p2) * _scaledWidth; //road width
newVerticeList[0] = (p1 + norm);
newVerticeList[roadSegmentCount * 2 - 1] = (p1 - norm);
newNorms[0] = Constants.Math.Vector3Up;
newNorms[roadSegmentCount * 2 - 1] = Constants.Math.Vector3Up;
uvList[0] = new Vector2(0, 0);
uvList[roadSegmentCount * 2 - 1] = new Vector2(1, 0);
newTangents[0] = new Vector4(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z, 1).normalized;
newTangents[roadSegmentCount * 2 - 1] = newTangents[0];
}
var dist = Vector3.Distance(p1, p2);
lastUv += dist;
norm = GetNormal(p1, p2, p3) * _scaledWidth;
newVerticeList[i] = (p2 + norm);
newVerticeList[2 * roadSegmentCount - 1 - i] = (p2 - norm);
newNorms[i] = Constants.Math.Vector3Up;
newNorms[2 * roadSegmentCount - 1 - i] = Constants.Math.Vector3Up;
uvList[i] = new Vector2(0, lastUv);
uvList[2 * roadSegmentCount - 1 - i] = new Vector2(1, lastUv);
newTangents[i] = new Vector4(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z, 1).normalized;
newTangents[2 * roadSegmentCount - 1 - i] = newTangents[i];
}
md.Vertices.AddRange(newVerticeList);
md.Normals.AddRange(newNorms);
md.UV[0].AddRange(uvList);
md.Tangents.AddRange(newTangents);
var lineTri = new List<int>();
var n = roadSegmentCount;
for (int i = 0; i < n - 1; i++)
{
lineTri.Add(mdVertexCount + i);
lineTri.Add(mdVertexCount + i + 1);
lineTri.Add(mdVertexCount + 2 * n - 1 - i);
lineTri.Add(mdVertexCount + i + 1);
lineTri.Add(mdVertexCount + 2 * n - i - 2);
lineTri.Add(mdVertexCount + 2 * n - i - 1);
}
if (md.Triangles.Count < 1)
md.Triangles.Add(new List<int>());
md.Triangles[0].AddRange(lineTri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment