Exemplo n.º 1
0
    private static (List <Vector3>, List <Face>) CalcVerticesAndFaces(RoadNetwork roadNet)
    {
        var vertices = new List <Vector3>();
        var faces    = new List <Face>();

        RoadSegmentMeshInfo[] segmentMeshInfos = new RoadSegmentMeshInfo[roadNet.SegmentCount];

        for (int i = 0; i < roadNet.VertCount; ++i)
        {
            // Construct incoming road edge descriptors
            RoadVertexView roadVert         = roadNet.GetVertex(i);
            List <int>     connSegIndices   = roadVert.Data.ConnectedSegmentIndices; // Assumed connected segments in clockwise order
            var            incRoadEdges     = new RoadSegmentEdges[connSegIndices.Count];
            var            roadMeshEndInfos = new RoadSegmentMeshEndInfo[connSegIndices.Count];
            for (int j = 0; j < connSegIndices.Count; ++j)
            {
                // Extract road segment mesh end info to fill out
                // This is used to assign the vertices we create in this loop to the correct road segment, at the correct end.
                int curSegmentIndex = connSegIndices[j];
                ref RoadSegmentMeshInfo curSegmentMeshInfo = ref segmentMeshInfos[curSegmentIndex];
                if (curSegmentMeshInfo == null)
                {
                    curSegmentMeshInfo = new RoadSegmentMeshInfo();
                }
                RoadSegmentView            curSegment        = roadNet.GetSegment(curSegmentIndex);
                ref RoadSegmentMeshEndInfo curSegMeshEndInfo = ref ((curSegment.StartVertex.Index != i) ? ref curSegmentMeshInfo.startVertIndices : ref curSegmentMeshInfo.endVertIndices);
                curSegMeshEndInfo   = new RoadSegmentMeshEndInfo();
                roadMeshEndInfos[j] = curSegMeshEndInfo;

                // Get adjacent vertex on this road segment
                int            adjVertIdx  = curSegment.StartVertex.Index != i ? curSegment.StartVertex.Index : curSegment.EndVertex.Index;
                RoadVertexView adjRoadVert = roadNet.GetVertex(adjVertIdx);

                // Construct the incoming left and right road edge descriptors
                Vector2          incDir           = (roadVert.Position - adjRoadVert.Position).normalized;
                Vector2          rot90Dir         = new Vector2(-incDir.y, incDir.x); // Rotated 90 degrees counter clockwise
                Vector2          leftOffset       = rot90Dir * curSegment.HalfWidth;
                Vector2          rightOffset      = -rot90Dir * curSegment.HalfWidth;
                RoadSegmentEdges roadSegmentEdges = new RoadSegmentEdges()
                {
                    left = new Line()
                    {
                        start = adjRoadVert.Position + leftOffset,
                        end   = roadVert.Position + leftOffset
                    },
                    right = new Line()
                    {
                        start = adjRoadVert.Position + rightOffset,
                        end   = roadVert.Position + rightOffset
                    }
                };

                // Add incoming edges to array
                incRoadEdges[j] = roadSegmentEdges;
            }