Browse Source

Updated ERVertexPath builder to latest version (merged ERCurbBuilder and TrafficChase changes)

master
madc0der 4 years ago
parent
commit
df8d1d46c4
  1. 24
      ERVertexPath/ERMultiRoadPathCreator.cs
  2. 69
      ERVertexPath/ERPathAdapter.cs
  3. 2
      ERVertexPath/ERPathToVertexPathWrapper.cs

24
ERVertexPath/ERMultiRoadPathCreator.cs

@ -4,7 +4,6 @@ using System.Linq;
using EasyRoads3Dv3; using EasyRoads3Dv3;
using UnityEngine; using UnityEngine;
using UnityEngine.Assertions; using UnityEngine.Assertions;
using UnityEngine.PlayerLoop;
namespace ERVertexPath namespace ERVertexPath
{ {
@ -56,16 +55,18 @@ namespace ERVertexPath
DebugLog($"Search connections for road: {road.GetName()}"); DebugLog($"Search connections for road: {road.GetName()}");
var endConnector = road.GetConnectionAtEnd(out _); var endConnector = road.GetConnectionAtEnd(out _);
if (endConnector != null) if (endConnector == null)
{ {
for (var i = 0; i < endConnector.GetConnectionCount(); i++) continue;
}
for (var i = 0; i < endConnector.GetConnectionCount(); i++)
{
var connectedRoad = endConnector.GetConnectedRoad(i, out _);
if (connectedRoad != null && connectedRoad != road)
{ {
var connectedRoad = endConnector.GetConnectedRoad(i, out _); road = connectedRoad;
if (connectedRoad != null && connectedRoad != road) break;
{
road = connectedRoad;
break;
}
} }
} }
@ -82,6 +83,7 @@ namespace ERVertexPath
private void BuildUnionVertexPath(IEnumerable<ERPathToVertexPathWrapper> wrappers) private void BuildUnionVertexPath(IEnumerable<ERPathToVertexPathWrapper> wrappers)
{ {
var firstRoadWidth = 0f;
var vertexList = new List<Vector3>(); var vertexList = new List<Vector3>();
var directionsList = new List<Vector3>(); var directionsList = new List<Vector3>();
var normalsList = new List<Vector3>(); var normalsList = new List<Vector3>();
@ -93,6 +95,7 @@ namespace ERVertexPath
var positionIndex = 0; var positionIndex = 0;
foreach (var wrapper in wrappers) foreach (var wrapper in wrappers)
{ {
firstRoadWidth = wrapper.Width;
vertexList.AddRange(wrapper.Positions); vertexList.AddRange(wrapper.Positions);
directionsList.AddRange(wrapper.Directions); directionsList.AddRange(wrapper.Directions);
normalsList.AddRange(wrapper.Normals); normalsList.AddRange(wrapper.Normals);
@ -125,7 +128,8 @@ namespace ERVertexPath
} }
unionAdapter = gameObject.AddComponent<ERPathAdapter>(); unionAdapter = gameObject.AddComponent<ERPathAdapter>();
unionAdapter.InitFromData(totalDistance, vertexList.ToArray(), directionsList.ToArray(), unionAdapter.InitFromData(firstRoadWidth,
totalDistance, vertexList.ToArray(), directionsList.ToArray(),
normalsList.ToArray(), rotationsList.ToArray(), distanceList.ToArray(), startIndexToRoad); normalsList.ToArray(), rotationsList.ToArray(), distanceList.ToArray(), startIndexToRoad);
DebugLog($"Created union adapter for roads with mask = {roadNameMask}, distance = {unionAdapter.TotalDistance}, vertex count = {vertexList.Count}"); DebugLog($"Created union adapter for roads with mask = {roadNameMask}, distance = {unionAdapter.TotalDistance}, vertex count = {vertexList.Count}");

69
ERVertexPath/ERPathAdapter.cs

@ -6,6 +6,7 @@ namespace ERVertexPath
{ {
public class ERPathAdapter : MonoBehaviour public class ERPathAdapter : MonoBehaviour
{ {
private float roadWidth;
private float totalDistance; private float totalDistance;
private Vector3[] positions; private Vector3[] positions;
private Vector3[] directions; private Vector3[] directions;
@ -17,6 +18,8 @@ namespace ERVertexPath
private readonly LinkedLines lastBestSection = new LinkedLines(); private readonly LinkedLines lastBestSection = new LinkedLines();
private readonly PathPoint pointInstance = new PathPoint(); private readonly PathPoint pointInstance = new PathPoint();
public float RoadWidth => roadWidth;
public float TotalDistance => totalDistance; public float TotalDistance => totalDistance;
public Vector3[] Positions => positions; public Vector3[] Positions => positions;
@ -36,14 +39,17 @@ namespace ERVertexPath
public void InitFromWrapper(ERPathToVertexPathWrapper wrapper) public void InitFromWrapper(ERPathToVertexPathWrapper wrapper)
{ {
InitFromData(wrapper.TotalDistance, wrapper.Positions, wrapper.Directions, wrapper.Normals, InitFromData(wrapper.Width,
wrapper.TotalDistance, wrapper.Positions, wrapper.Directions, wrapper.Normals,
wrapper.Rotations, wrapper.Distances); wrapper.Rotations, wrapper.Distances);
} }
public void InitFromData(float totalDistance, Vector3[] positions, Vector3[] directions, Vector3[] normals, public void InitFromData(float roadWidth,
float totalDistance, Vector3[] positions, Vector3[] directions, Vector3[] normals,
Quaternion[] rotations, float[] distances, Quaternion[] rotations, float[] distances,
Dictionary<int, GameObject> startIndexToRoadMap = null) Dictionary<int, GameObject> startIndexToRoadMap = null)
{ {
this.roadWidth = roadWidth;
this.totalDistance = totalDistance; this.totalDistance = totalDistance;
this.positions = positions; this.positions = positions;
this.directions = directions; this.directions = directions;
@ -72,6 +78,7 @@ namespace ERVertexPath
var t = (clampedDistance - d1) / (d2 - d1); var t = (clampedDistance - d1) / (d2 - d1);
pointInstance.set( pointInstance.set(
clampedDistance,
distance, distance,
Vector3.Lerp(positions[i1], positions[i2], t), Vector3.Lerp(positions[i1], positions[i2], t),
Vector3.Lerp(directions[i1], directions[i2], t), Vector3.Lerp(directions[i1], directions[i2], t),
@ -143,14 +150,22 @@ namespace ERVertexPath
public float GetClosestDistanceAlongPath(Vector3 p) public float GetClosestDistanceAlongPath(Vector3 p)
{ {
var bestSection = findLinkedLines(p); var bestSection = FindLinkedLines(p, true);
if (bestSection == null)
{
bestSection = FindLinkedLines(p, false);
}
projectPointOnBestSection(bestSection, p, out var distanceOnPath, out _); projectPointOnBestSection(bestSection, p, out var distanceOnPath, out _);
return distanceOnPath; return distanceOnPath;
} }
public Vector3 GetClosestPointOnPath(Vector3 p, out float closestDistance) public Vector3 GetClosestPointOnPath(Vector3 p, out float closestDistance)
{ {
var bestSection = findLinkedLines(p); var bestSection = FindLinkedLines(p, true);
if (bestSection == null)
{
bestSection = FindLinkedLines(p, false);
}
return projectPointOnBestSection(bestSection, p, out closestDistance, out _); return projectPointOnBestSection(bestSection, p, out closestDistance, out _);
} }
@ -241,11 +256,16 @@ namespace ERVertexPath
} }
} }
private LinkedLines findLinkedLines(Vector3 p) private LinkedLines FindLinkedLines(Vector3 p, bool strictHeight)
{ {
var bestSection = lastBestSection; var bestSection = lastBestSection;
var minDistance = float.MaxValue; var minDistance = float.MaxValue;
//Assume any bridge will have vertical offset > 10 (i.e. 16)
const float verticalDistanceThreshold = 10f;
var found = false;
for (var i = 0; i < positions.Length; i++) for (var i = 0; i < positions.Length; i++)
{ {
var isFirstPoint = i == 0; var isFirstPoint = i == 0;
@ -258,9 +278,12 @@ namespace ERVertexPath
var p1p = p - p1; var p1p = p - p1;
var distance = p1p.sqrMagnitude; var distance = p1p.sqrMagnitude;
if (distance < minDistance) var verticalDistance = Mathf.Abs(p.y - p1.y);
if (distance < minDistance && (!strictHeight || verticalDistance < verticalDistanceThreshold))
{ {
found = true;
minDistance = distance; minDistance = distance;
bestSection.p0 = positions[i0]; bestSection.p0 = positions[i0];
bestSection.p1 = p1; bestSection.p1 = p1;
bestSection.p2 = positions[i1]; bestSection.p2 = positions[i1];
@ -270,7 +293,8 @@ namespace ERVertexPath
if (isFirstPoint) if (isFirstPoint)
{ {
bestSection.d2 = totalDistance + distances[i1]; bestSection.d2 = totalDistance + distances[i1];
} else if (isLastPoint) }
else if (isLastPoint)
{ {
bestSection.d2 = totalDistance + distances[i1]; bestSection.d2 = totalDistance + distances[i1];
} }
@ -285,6 +309,10 @@ namespace ERVertexPath
} }
} }
if (!found)
{
return null;
}
return bestSection; return bestSection;
} }
} }
@ -298,6 +326,7 @@ namespace ERVertexPath
public class PathPoint public class PathPoint
{ {
public float clampedDistance;
public float distance; public float distance;
public Vector3 position; public Vector3 position;
public Vector3 direction; public Vector3 direction;
@ -306,8 +335,16 @@ namespace ERVertexPath
public int index1; public int index1;
public int index2; public int index2;
public void set(float distance, Vector3 position, Vector3 direction, Vector3 normal, Quaternion rotation, int index1, int index2) public void set(float clampedDistance,
float distance,
Vector3 position,
Vector3 direction,
Vector3 normal,
Quaternion rotation,
int index1,
int index2)
{ {
this.clampedDistance = clampedDistance;
this.distance = distance; this.distance = distance;
this.position = position; this.position = position;
this.direction = direction; this.direction = direction;
@ -319,8 +356,22 @@ namespace ERVertexPath
public PathPoint copyFrom(PathPoint src) public PathPoint copyFrom(PathPoint src)
{ {
set(src.distance, src.position, src.direction, src.normal, src.rotation, src.index1, src.index2); set(src.clampedDistance,
src.distance,
src.position,
src.direction,
src.normal,
src.rotation,
src.index1,
src.index2);
return this; return this;
} }
public static PathPoint CopyFrom(PathPoint src)
{
var pathPoint = new PathPoint();
pathPoint.copyFrom(src);
return pathPoint;
}
} }
} }

2
ERVertexPath/ERPathToVertexPathWrapper.cs

@ -35,6 +35,8 @@ namespace ERVertexPath
public float[] Distances => distances; public float[] Distances => distances;
public float Width => road.GetWidth();
public void Init(ERModularRoad modularRoad) public void Init(ERModularRoad modularRoad)
{ {
road = new ERRoadNetwork().GetRoadByGameObject(modularRoad.gameObject); road = new ERRoadNetwork().GetRoadByGameObject(modularRoad.gameObject);

Loading…
Cancel
Save