public Vector3 GetPosition(ERRoad road, float distance) { //if (road.roadScript.distances != null && road.roadScript.distances.Count < 3) //{ // Debug.Log(road.roadScript.transform.name); road.roadScript.distances.Clear(); road.SetDistances(); //} Vector3 result = Vector3.zero; int prevIndex = 0, nextIndex = 0; float prevIndexDistance = 0f, nextIndexDistance = 0f; if (road.roadScript.distances.Count < 2) { return(result); } if (distance < road.roadScript.distances[0]) { return(road.roadScript.soSplinePoints[0]); } if (road.roadScript.distances.Count == 2) { prevIndex = 0; nextIndex = 1; } else if (distance > road.roadScript.distances[road.roadScript.distances.Count - 1]) { return(road.roadScript.soSplinePoints[road.roadScript.distances.Count - 1]); } if (road.roadScript.distances.Count > 2) { for (int i = 0; i < road.roadScript.distances.Count - 1; i++) { if (road.roadScript.distances[i] <= distance && road.roadScript.distances[i + 1] >= distance) { prevIndex = i; nextIndex = i + 1; } } } prevIndexDistance = road.roadScript.distances[prevIndex]; nextIndexDistance = road.roadScript.distances[nextIndex]; result = Vector3.Lerp(road.roadScript.soSplinePoints[prevIndex], road.roadScript.soSplinePoints[nextIndex], ((distance - prevIndexDistance) / (nextIndexDistance - prevIndexDistance))); return(result); }
public List <GraphNode> GenerateOneLine(ERRoad road, Road roadLines, float horizontalSep, int definition, int count, bool inverted) { List <GraphNode> nodes = new List <GraphNode>(count * 2); Vector3 vect1 = Vector3.zero, POS = default(Vector3), pos, tempVect = Vector3.zero, prevTempVect = Vector3.zero; GameObject temp; float percent = 0f; GraphNode node; road.roadScript.distances.Clear(); road.SetDistances(); float max = road.roadScript.distances[road.roadScript.distances.Count - 1], min = road.roadScript.distances[0]; int counter = 0; int currentElement = 0; for (int i = 0; i < count; i += 1) { percent = (float)i / (float)count; if (inverted) { percent = 1f - ((float)i / (float)count); } percent = Mathf.Lerp(min, max, percent); if (i == 0) { if (!inverted) { percent = min; } else { percent = max; } } if (i == count - 1) { if (!inverted) { percent = max; } else { percent = min; } } //Debug.Log(road.roadScript.transform.name + " " + currentElement + " " + percent + " "+ road.roadScript.distances.Count); vect1 = GetPosition(road, percent); tempVect = (Quaternion.AngleAxis(90, Vector3.up) * (GetPosition(road, percent + 0.01f) - vect1).normalized) * (horizontalSep * 0.4f); if (inverted && i == 0) { tempVect = (Quaternion.AngleAxis(90, Vector3.up) * (GetPosition(road, percent - 1f) - vect1).normalized * -1f) * (horizontalSep * 0.4f); } if ((!inverted && i != count - 1) || (inverted)) { prevTempVect = tempVect; } else { tempVect = prevTempVect; } vect1 += tempVect; pos = vect1;//(Vector3.Lerp(splinesAtLeft[currentElement], splinesAtRight[currentElement], horizontalSep/Vector3.Distance(splinesAtLeft[currentElement], splinesAtRight[currentElement]))); pos += Vector3.up * 2f; temp = new GameObject("NavNode " + counter, typeof(GraphNode)); nodes.Add(temp.GetComponent <GraphNode>()); node = nodes[nodes.Count - 1]; node.OverrideDefinition = true; node.OverrideDefinitionValue = definition; node.MaxSpeed = 35f; node.Transform.position = pos; temp.transform.SetParent(roadLines.transform); //GameObject tempGo = new GameObject(tempVect.ToString()); //tempGo.transform.position = pos; //tempGo.transform.SetParent(node.transform); counter++; } currentElement = 0; for (int i = 1; i < nodes.Count; i++) { nodes[i - 1].ConnectedNodes.Add(nodes[i]); percent = (float)(i) / (float)count - (1f / (float)count) / 2f; if (inverted) { percent = 1f - (((float)(i) / (float)count) - (1f / (float)count) / 2f); } percent = Mathf.Lerp(min, max, percent); if (i == 1) { if (!inverted) { percent = min; } else { percent = max; } } //if (i == count - 1) //{ // if (!inverted) // { // currentElement = road.GetSplinePointsCenter().Length - 1; // percent = max; // } // else // { // currentElement = 0; // percent = min; // } //} if (inverted && i == 1) { vect1 = Vector3.Lerp(nodes[i - 1].transform.position, nodes[i].transform.position, 0.5f); } else { vect1 = GetPosition(road, percent); vect1 += (Quaternion.AngleAxis(90, Vector3.up) * (GetPosition(road, percent + 0.01f) - vect1).normalized) * (horizontalSep * 0.4f); vect1 += Vector3.up * 2f; } nodes[i - 1].LinksBezierHelpers.Add(vect1); } return(nodes); }