/// <summary> /// When you drop the marker onto the start marker /// It creates a smooth(ish) overlap by adding two extra control points /// and aligning them with points 2 and 3 /// </summary> /// <param name="Point"></param> public void JoinSection(Vector3 Point) { if (Rd.Sectns.Count < 4) { return; } int SegCount = Mathf.CeilToInt(Vector3.Distance(Point, Bez.CtrlPts[Bez.CtrlPts.Count - 2].Pos) / 0.84f); Road.Instance.IsCircular = true; RoadSectn Sectn = new RoadSectn(); int NewIdx = Road.Instance.Sectns.Count; Sectn.Idx = NewIdx; Sectn.name = "RoadSection" + (NewIdx); Rd.Sectns[NewIdx - 1].AddXSecs(SegCount); //Add XSecs to the PREVIOUS Section Sectn.CreateGameObjects(); Road.Instance.Sectns.Add(Sectn); //Move the hidden control point to the penultimate position: Bez.CtrlPts[0].Pos = Bez.CtrlPts[Bez.CtrlPts.Count - 2].Pos; //this doesnt work any more - had to fix it in CalcXSecs //Add a control point at the join Bez.AddControlPoint(Point, 0, SegCount); //We add a couple more control points so we can recalculate the curve after the join //We will remove one of these two in a sec RoadSectn OverlapSectn = new RoadSectn(); //OverlapSectn.AddXSecs(Rd.Sectns[1].Segments.Count); Bez.AddControlPoint(Bez.CtrlPts[2].Pos, Bez.CtrlPts[2].BankAngle, 20); Bez.AddControlPoint(Bez.CtrlPts[3].Pos, Bez.CtrlPts[3].BankAngle, 20); //Recalculate Section 1 that goes from CtrlPt1 to CtrlPt2 Bez.Interp(1); //It doesn't interp the Section before CtrlPt1 //Interp the path two before the end Bez.Interp(NewIdx - 1); //Interp the path just before the end Bez.Interp(NewIdx); Bez.AlignAllRoadMarkers(); XSecCalculator.CalcXSecs(2, RoadWidth); //Bez.RemoveLastControlPoint(); //Bez.CtrlPts[Bez.CtrlPts.Count - 1] = Bez.CtrlPts[Bez.CtrlPts.Count - 2]; XSecCalculator.CalcXSecs(NewIdx - 1, RoadWidth); XSecCalculator.CalcXSecs(NewIdx, RoadWidth); //NOt sure we need this next bit - We only need to calculate the XSec for one path point. // Commented out because it crashed - XSecCalculator.CalcXSecs(NewIdx + 1, RoadWidth); //Remove the 2 extra control points and their paths Bez.RemoveLastControlPoint(); Bez.RemoveLastControlPoint(); Bez.CtrlPts[Bez.CtrlPts.Count - 1].Pos = Bez.CtrlPts[2].Pos; //new bit for (int Idx = Bez.CtrlPts[NewIdx - 1].SegStartIdx; Idx < Bez.CtrlPts[NewIdx].SegStartIdx; Idx++) //create the segs ahead of the marker but don't mesh them { RoadSegment seg = new RoadSegment(); seg.Idx = Idx; seg.SectnIdx = NewIdx - 1; Rd.Sectns[NewIdx - 1].Segments.Add(seg); seg.CreateGameObjects(); seg.goSeg.name = "RoadSeg" + Idx; Rd.Segments.Add(seg); seg.SetMaterial(RoadMat); seg.LFenceType = LFenceType; seg.RFenceType = RFenceType; seg.goSeg.transform.SetParent(Rd.Sectns[NewIdx - 1].goSectn.transform); } Rd.Sectns[NewIdx - 1].SetMaterial(RoadMat); //bugfix 6/2/18 cos we now store RdMat in the section not the segment Rd.Sectns[NewIdx - 1].LFenceType = LFenceType; //Longstanding bug fixed Rd.Sectns[NewIdx - 1].RFenceType = RFenceType; //Longstanding bug fixed XSecCalculator.AdjustHairpin(Bez, NewIdx - 2); XSecCalculator.AdjustHairpin(Bez, NewIdx - 1); XSecCalculator.AdjustHairpin(Bez, 2); Road.Instance.Sectns[NewIdx - 3].CalcVisibleFenceVerts(); BuildQueue.Enqueue(NewIdx - 3); Road.Instance.Sectns[NewIdx - 2].CalcVisibleFenceVerts(); BuildQueue.Enqueue(NewIdx - 2); Road.Instance.Sectns[NewIdx - 1].CalcVisibleFenceVerts(); BuildQueue.Enqueue(NewIdx - 1); Road.Instance.Sectns[NewIdx].CalcVisibleFenceVerts(); //Join the beginning of the first Segment to the end of the last //Road.Instance.XSecs[0].KerbL = Road.Instance.XSecs[(Road.Instance.Sectns.Count - 2) * 20].KerbL; //Road.Instance.XSecs[0].KerbR = Road.Instance.XSecs[(Road.Instance.Sectns.Count - 2) * 20].KerbR; Road.Instance.Sectns[1].CalcVisibleFenceVerts(); BuildQueue.Enqueue(1); //Remove the Path points that were created when we added the second extra control point (The first one is needed because the SegVerts calculation needs it) //Bez.Path.RemoveRange(Bez.Path.Count - 20, 20); //This line commentd out now because RemoveCtrlPt also removes the Path Points Bez.DrawLine(); }