/* calculate margins for all incoming road * plus render node*/ public void updateMargins() { Destroy(smoothInstance); smoothPolygonEdges.Clear(); connection = connection.OrderBy(r => startof(r.curve) ? r.curve.angle_ending(true) : r.curve.angle_ending(false)).ToList(); if (connection.Count > 1) { smoothInstance = Instantiate(indicatorInst.roadIndicatorPrefab, transform); for (int i = 0; i != connection.Count; ++i) { int i_hat = (i == connection.Count - 1) ? 0 : i + 1; //List<Curve> localSmootheners; var margins = smoothenCrossing(connection[i], connection[i_hat], out List <Curve> localSmootheners); if (smoothPolygonEdges.Count > 0 && localSmootheners.Count > 0) { addIfNotNull(smoothPolygonEdges, Line.TryInit(smoothPolygonEdges.Last().at_ending_2d(false), localSmootheners.First().at_ending_2d(true))); } smoothPolygonEdges.AddRange(localSmootheners); if (position.y > 0) { generateNodeFence(localSmootheners); } if (startof(connection[i].curve)) { connection[i].margin0LLength = margins.First; } else { connection[i].margin1RLength = margins.First; } if (startof(connection[i_hat].curve)) { connection[i_hat].margin0RLength = margins.Second; } else { connection[i_hat].margin1LLength = margins.Second; } } if (smoothPolygonEdges.Count > 0) { //Debug.Assert(smoothPolygonEdges.Count > 1); addIfNotNull(smoothPolygonEdges, Line.TryInit(smoothPolygonEdges.Last().at_ending_2d(false), smoothPolygonEdges.First().at_ending_2d(true))); RoadRenderer smoothObjConfig = smoothInstance.GetComponent <RoadRenderer>(); smoothObjConfig.generate(new Polygon(smoothPolygonEdges), position.y, "roadsurface"); } } updateDirectionLaneRange(); }
public void createRoadObject(Road r) { Destroy(r.roadObject); GameObject roadInstance = Instantiate(road, transform); RoadRenderer roadConfigure = roadInstance.GetComponent <RoadRenderer>(); r.roadObject = roadInstance; roadConfigure.generate(r); }
void generateNodeFence(List <Curve> smoothener) { foreach (Curve nodeSide in smoothener) { nodeSide.z_start = position.y; RoadRenderer smoothObjConfig = smoothInstance.GetComponent <RoadRenderer>(); smoothObjConfig.generate(nodeSide, new List <String> { "singlefence" }); nodeSide.z_start = 0f; } }
void addAngleDrawing(Vector3 positionMaybeOnRoad, Vector3 anotherPosition) { Node n; List <Vector2> neighborDirs; if (roadManager.findNodeAt(positionMaybeOnRoad, out n)) { neighborDirs = n.getNeighborDirections(Algebra.toVector2(anotherPosition - positionMaybeOnRoad)); } else { Road tar; roadManager.approxNodeToExistingRoad(positionMaybeOnRoad, out tar); if (tar == null) { return; } float?param = tar.curve.paramOf(positionMaybeOnRoad); if (param == null) { return; } neighborDirs = new List <Vector2>() { tar.curve.direction_2d((float)param), -tar.curve.direction_2d((float)param) }; } foreach (Vector2 dir in neighborDirs) { Debug.Assert(!Algebra.isclose(dir, Vector2.zero)); Vector3 textPosition = positionMaybeOnRoad + ((anotherPosition - positionMaybeOnRoad).normalized + Algebra.toVector3(dir).normalized).normalized * textDistance; GameObject textObj = Instantiate(degreeTextPrefab, textPosition, Quaternion.Euler(90f, 0f, 0f)); textObj.transform.SetParent(transform); textObj.GetComponent <TextMesh>().text = Mathf.RoundToInt(Mathf.Abs(Vector2.Angle(Algebra.toVector2(anotherPosition - positionMaybeOnRoad), dir))).ToString(); degreeTextInstance.Add(textObj); GameObject indicatorObj = Instantiate(roadIndicatorPrefab, transform); RoadRenderer indicatorConfigure = indicatorObj.GetComponent <RoadRenderer>(); indicatorConfigure.generate(Line.TryInit(positionMaybeOnRoad, positionMaybeOnRoad + Algebra.toVector3(dir) * textDistance * 2), new List <string> { "solid_blueindi" }); neighborIndicatorInstance.Add(indicatorObj); } }
public void Update() { clearAngleDrawing(); laneConfig = GameObject.FindWithTag("UI/laneconfig").GetComponent <LaneConfigPanelBehavior>().laneconfigresult; interestedApproxLines.Clear(); if (pointer >= 1) { interestedApproxLines.Add(Line.TryInit(controlPoint[pointer - 1] + Vector3.back * Algebra.InfLength, controlPoint[pointer - 1] + Vector3.forward * Algebra.InfLength)); interestedApproxLines.Add(Line.TryInit(controlPoint[pointer - 1] + Vector3.left * Algebra.InfLength, controlPoint[pointer - 1] + Vector3.right * Algebra.InfLength)); if (targetRoad != null && !Algebra.isProjectionClose(controlPoint[pointer - 1], targetRoad.curve.AttouchPoint(controlPoint[pointer - 1]))) { interestedApproxLines.Add(Line.TryInit(controlPoint[pointer - 1], targetRoad.curve.AttouchPoint(controlPoint[pointer - 1]))); } } if (indicatorType == IndicatorType.none) { nodeIndicator.transform.localScale = Vector3.zero; } if (controlPoint[pointer].x != Vector3.negativeInfinity.x && indicatorType != IndicatorType.none) { Vector3 adjustedAttach; if (targetRoad != null) { adjustedAttach = targetRoad.at(targetRoad.curve.paramOf(targetRoad.curve.AttouchPoint(controlPoint[pointer])).Value); } else { adjustedAttach = controlPoint[pointer]; } nodeIndicator.transform.position = new Vector3(adjustedAttach.x, adjustedAttach.y / 2 + 0.1f, adjustedAttach.z); nodeIndicator.transform.localScale = new Vector3(1.5f, Mathf.Max(1f, adjustedAttach.y / 2), 1.5f); if (indicatorType == IndicatorType.line) { if (pointer == 1) { Destroy(roadIndicator); addAngleDrawing(controlPoint[1], controlPoint[0]); addAngleDrawing(controlPoint[0], controlPoint[1]); Road cp0_targetRoad; roadManager.approxNodeToExistingRoad(controlPoint[0], out cp0_targetRoad); if (cp0_targetRoad != null) { //perpendicular interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d((float)cp0_targetRoad.curve.paramOf(controlPoint[0])) + Mathf.PI / 2) * Algebra.InfLength)); interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d((float)cp0_targetRoad.curve.paramOf(controlPoint[0])) - Mathf.PI / 2) * Algebra.InfLength)); //extension if (Algebra.isclose(cp0_targetRoad.curve.at_ending(true), controlPoint[0])) { Node crossingRoad; roadManager.findNodeAt(cp0_targetRoad.curve.at_ending(true), out crossingRoad); Debug.Assert(crossingRoad != null); interestedApproxLines.AddRange(crossingRoad.directionalLines(Algebra.InfLength, reverse: true)); } else { if (Algebra.isclose(cp0_targetRoad.curve.at_ending(false), controlPoint[0])) { Node crossingRoad; roadManager.findNodeAt(cp0_targetRoad.curve.at_ending(false), out crossingRoad); Debug.Assert(crossingRoad != null); interestedApproxLines.AddRange(crossingRoad.directionalLines(Algebra.InfLength, reverse: true)); } } } if (!Algebra.isProjectionClose(controlPoint[0], controlPoint[1])) { roadIndicator = Instantiate(roadIndicatorPrefab, transform); RoadRenderer roadConfigure = roadIndicator.GetComponent <RoadRenderer>(); roadConfigure.generate(Line.TryInit(controlPoint[0], controlPoint[1]), laneConfig); } } if (pointer == 2) { roadManager.addRoad(Line.TryInit(controlPoint[0], controlPoint[1]), laneConfig); reset(); } } if (indicatorType == IndicatorType.bezeir) { if (pointer == 1) { Destroy(roadIndicator); addAngleDrawing(controlPoint[1], controlPoint[0]); addAngleDrawing(controlPoint[0], controlPoint[1]); interestedApproxLines.Add(Line.TryInit(controlPoint[0] + Vector3.back * Algebra.InfLength, controlPoint[0] + Vector3.forward * Algebra.InfLength)); interestedApproxLines.Add(Line.TryInit(controlPoint[0] + Vector3.left * Algebra.InfLength, controlPoint[0] + Vector3.right * Algebra.InfLength)); Road cp0_targetRoad; roadManager.approxNodeToExistingRoad(controlPoint[0], out cp0_targetRoad); if (cp0_targetRoad != null) { interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d((float)cp0_targetRoad.curve.paramOf(controlPoint[0])) + Mathf.PI / 2) * Algebra.InfLength)); interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d((float)cp0_targetRoad.curve.paramOf(controlPoint[0])) - Mathf.PI / 2) * Algebra.InfLength)); if (Algebra.isclose(cp0_targetRoad.curve.at_ending(true), controlPoint[0])) { Node crossingRoad; roadManager.findNodeAt(cp0_targetRoad.curve.at_ending(true), out crossingRoad); Debug.Assert(crossingRoad != null); interestedApproxLines.AddRange(crossingRoad.directionalLines(Algebra.InfLength, reverse: true)); } else { if (Algebra.isclose(cp0_targetRoad.curve.at_ending(false), controlPoint[0])) { Node crossingRoad; roadManager.findNodeAt(cp0_targetRoad.curve.at_ending(false), out crossingRoad); Debug.Assert(crossingRoad != null); interestedApproxLines.AddRange(crossingRoad.directionalLines(Algebra.InfLength, reverse: true)); } } } if (!Algebra.isProjectionClose(controlPoint[0], controlPoint[1])) { roadIndicator = Instantiate(roadIndicatorPrefab, transform); RoadRenderer roadConfigure = roadIndicator.GetComponent <RoadRenderer>(); roadConfigure.generate(Line.TryInit(controlPoint[0], controlPoint[1]), laneConfig); } } if (pointer == 2) { if (!Geometry.Parallel(controlPoint[1] - controlPoint[0], controlPoint[2] - controlPoint[1]) && !Algebra.isRoadNodeClose(controlPoint[2], controlPoint[1])) { Destroy(roadIndicator); addAngleDrawing(controlPoint[2], controlPoint[1]); roadIndicator = Instantiate(roadIndicatorPrefab, transform); RoadRenderer roadConfigure = roadIndicator.GetComponent <RoadRenderer>(); roadConfigure.generate(Bezeir.TryInit(controlPoint[0], controlPoint[1], controlPoint[2]), laneConfig); } } if (pointer == 3) { if (!Geometry.Parallel(controlPoint[1] - controlPoint[0], controlPoint[2] - controlPoint[1]) && !Algebra.isRoadNodeClose(controlPoint[2], controlPoint[1])) { roadManager.addRoad(Bezeir.TryInit(controlPoint[0], controlPoint[1], controlPoint[2]), laneConfig); reset(); } else { pointer = 2; } } } if (indicatorType == IndicatorType.arc) { if (pointer == 1) { Road cp0_targetRoad; roadManager.approxNodeToExistingRoad(controlPoint[0], out cp0_targetRoad); if (cp0_targetRoad != null) { addAngleDrawing(controlPoint[0], controlPoint[1]); if (Algebra.isclose(cp0_targetRoad.curve.at_ending(true), controlPoint[0])) { interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d(0f) + Mathf.PI / 2) * Algebra.InfLength)); interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d(0f) - Mathf.PI / 2) * Algebra.InfLength)); Node crossingRoad; roadManager.findNodeAt(cp0_targetRoad.curve.at_ending(true), out crossingRoad); Debug.Assert(crossingRoad != null); interestedApproxLines.AddRange(crossingRoad.directionalLines(Algebra.InfLength, reverse: true)); } else { if (Algebra.isclose(cp0_targetRoad.curve.at_ending(false), controlPoint[0])) { interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d(1f) + Mathf.PI / 2) * Algebra.InfLength)); interestedApproxLines.Add(Line.TryInit(controlPoint[0], controlPoint[0] + Algebra.angle2dir_3d(cp0_targetRoad.curve.Angle_2d(1f) - Mathf.PI / 2) * Algebra.InfLength)); Node crossingRoad; roadManager.findNodeAt(cp0_targetRoad.curve.at_ending(false), out crossingRoad); Debug.Assert(crossingRoad != null); interestedApproxLines.AddRange(crossingRoad.directionalLines(Algebra.InfLength, reverse: true)); } } } /*ind[0] is start, ind[1] isorigin*/ Destroy(roadIndicator); if (!Algebra.isProjectionClose(controlPoint[0], controlPoint[1])) { roadIndicator = Instantiate(roadIndicatorPrefab, transform); RoadRenderer roadConfigure = roadIndicator.GetComponent <RoadRenderer>(); roadConfigure.generate(Line.TryInit(controlPoint[1], controlPoint[0]), laneConfig); if (!Algebra.isclose(controlPoint[1], controlPoint[0])) { roadConfigure.generate(Arc.TryInit(controlPoint[1], controlPoint[0], 1.999f * Mathf.PI), laneConfig); } } } if (pointer == 2) { Vector3 basedir = controlPoint[0] - controlPoint[1]; Vector3 towardsdir = controlPoint[2] - controlPoint[1]; interestedApproxLines.Add(Arc.TryInit(controlPoint[1], controlPoint[0], Mathf.PI * 1.999f)); if (!Algebra.isProjectionClose(Vector3.zero, towardsdir) && !Algebra.isProjectionClose(controlPoint[1], controlPoint[0]) && !Geometry.Parallel(basedir, towardsdir)) { Destroy(roadIndicator); roadIndicator = Instantiate(roadIndicatorPrefab, transform); RoadRenderer roadConfigure = roadIndicator.GetComponent <RoadRenderer>(); roadConfigure.generate(Arc.TryInit(Algebra.toVector2(controlPoint[1]), Algebra.toVector2(controlPoint[0]), Mathf.Deg2Rad * Vector2.SignedAngle(Algebra.toVector2(basedir), Algebra.toVector2(towardsdir)), controlPoint[0].y, controlPoint[2].y), laneConfig); roadConfigure.generate(Arc.TryInit(controlPoint[1], controlPoint[1] + Vector3.right, 1.999f * Mathf.PI), laneConfig); } } if (pointer == 3) { Vector3 basedir = controlPoint[0] - controlPoint[1]; Vector3 towardsdir = controlPoint[2] - controlPoint[1]; if (Algebra.isclose(0, towardsdir.magnitude)) { pointer = 2; } else { roadManager.addRoad(Arc.TryInit(Algebra.toVector2(controlPoint[1]), Algebra.toVector2(controlPoint[0]), Mathf.Deg2Rad * Vector2.SignedAngle(Algebra.toVector2(basedir), Algebra.toVector2(towardsdir)), controlPoint[0].y, controlPoint[2].y), laneConfig); reset(); } } } if (indicatorType == IndicatorType.delete) { if (pointer == 0) { Destroy(roadIndicator); if (targetRoad != null) { roadIndicator = Instantiate(roadIndicatorPrefab, transform); RoadRenderer roadConfigure = roadIndicator.GetComponent <RoadRenderer>(); roadConfigure.generate(targetRoad.marginedOutCurve, new List <string> { "removal_" + targetRoad.width }); } } else { if (targetRoad != null) { roadManager.deleteRoad(targetRoad); reset(); } else { pointer = 0; } } } } }