Beispiel #1
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        // Button to generate the mesh again
        if (GUILayout.Button("Regenerate Mesh"))
        {
            Sidewalk sidewalk = target as Sidewalk;
            sidewalk.CalculateVertices();
            sidewalk.CreateMesh();
        }
    }
    /// <summary>
    /// Gets all the points that will form a sidewalk
    /// </summary>
    /// <param name="startIntersection"></param>
    /// <param name="roadSegment"></param>
    /// <param name="checks"></param>
    /// <param name="parent"></param>
    private Sidewalk CreateSidewalk(Intersection startIntersection, RoadSegment roadSegment, HashSet <SidewalkCheckHelper> checks, GameObject parent)
    {
        Intersection current     = startIntersection;
        Intersection next        = roadSegment.GetOtherSide(startIntersection);
        RoadSegment  nextSegment = null;

        GameObject go = new GameObject();

        go.name = "Sidewalk Mesh";
        Sidewalk sidewalk = go.AddComponent <Sidewalk>();

        sidewalk.OriginalVertices.Add(roadSegment.GetVertexWorldPosition(startIntersection, false));
        SidewalkCheckHelper check = new SidewalkCheckHelper();

        while (next != startIntersection)
        {
            int nextIndex = (next.Roads.IndexOf(roadSegment) + 1) % (next.Roads.Count);
            nextSegment = next.Roads[nextIndex];

            check.Intersection = next;
            check.Road         = nextSegment;

            checks.Add(check);

            if (next.Roads.Count == 1)
            {
                sidewalk.OriginalVertices.Add(nextSegment.GetVertexWorldPosition(next, true));
                sidewalk.OriginalVertices.Add(nextSegment.GetVertexWorldPosition(next, false));
            }
            else if (!(Mathf.Approximately(roadSegment.GetVertexWorldPosition(next, true).x, nextSegment.GetVertexWorldPosition(next, false).x) &&
                       Mathf.Approximately(roadSegment.GetVertexWorldPosition(next, true).z, nextSegment.GetVertexWorldPosition(next, false).z)))
            {
                sidewalk.OriginalVertices.Add(roadSegment.GetVertexWorldPosition(next, true));
                sidewalk.OriginalVertices.Add(nextSegment.GetVertexWorldPosition(next, false));
            }
            else
            {
                sidewalk.OriginalVertices.Add(nextSegment.GetVertexWorldPosition(next, false));
            }

            current     = next;
            next        = nextSegment.GetOtherSide(next);
            roadSegment = nextSegment;
        }

        if (nextSegment != null &&
            !(Mathf.Approximately(sidewalk.OriginalVertices[0].x, nextSegment.GetVertexWorldPosition(next, true).x) &&
              Mathf.Approximately(sidewalk.OriginalVertices[0].z, nextSegment.GetVertexWorldPosition(next, true).z)))
        {
            sidewalk.OriginalVertices.Add(nextSegment.GetVertexWorldPosition(next, true));
        }


        Vector3 sum = Vector3.zero;

        for (int i = 0; i < sidewalk.OriginalVertices.Count; i++)
        {
            sum += sidewalk.OriginalVertices[i];
        }

        go.transform.position     = sum / sidewalk.OriginalVertices.Count;
        sidewalk.SidewalkMaterial = city.SidewalkMaterial;
        sidewalk.InnerMaterial    = city.InnerBlockMaterial;
        sidewalk.FixMeshOffset();
        sidewalk.FindDirection();
        sidewalk.CalculateVertices();
        sidewalk.CreateMesh();
        go.transform.parent = parent.transform;
        go.isStatic         = true;
        return(sidewalk);
    }