コード例 #1
0
    public void CreateIntersection(List <Vector3> roadA, List <Vector3> roadB, float width, int size)
    {
        debugPoints = new List <Vector3>();

        var firstList  = roadA;
        var secondList = roadB;

        int firstListIndex  = 0;
        int secondListIndex = 0;

        float distance = float.MaxValue;

        //For a curve, need to fine intersection with brute force
        for (int i = 0; i < firstList.Count; i++)
        {
            for (int j = 0; j < secondList.Count; j++)
            {
                var d = Vector3.Distance(firstList[i], secondList[j]);
                if (d < distance)
                {
                    firstListIndex  = i;
                    secondListIndex = j;
                    distance        = d;
                }
            }
        }

        connections = new List <List <Vector3> >();

        var firstListA = firstList.GetRange(0, firstListIndex);

        firstListA.Reverse();
        connections.Add(firstListA.Skip(size).ToList());
        var firstListB = firstList.GetRange(firstListIndex - 1, firstList.Count() - firstListIndex);

        connections.Add(firstListB.Skip(size).ToList());

        //var lineA1 = new Pair<Vector3>(firstListA.GetRoadPoint(size, -width / 2), firstListB.GetRoadPoint(size, width / 2));
        //var lineA2 = new Pair<Vector3>(firstListA.GetRoadPoint(size, width / 2), firstListB.GetRoadPoint(size, -width / 2));

        var secondListA = secondList.GetRange(0, secondListIndex);

        secondListA.Reverse();
        connections.Add(secondListA.Skip(size).ToList());
        var secondListB = secondList.GetRange(secondListIndex - 1, secondList.Count() - secondListIndex);

        connections.Add(secondListB.Skip(size).ToList());

        //var lineB1 = new Pair<Vector3>(secondListA.GetRoadPoint(size, -width / 2), secondListB.GetRoadPoint(size, width / 2));
        //var lineB2 = new Pair<Vector3>(secondListA.GetRoadPoint(size, width / 2), secondListB.GetRoadPoint(size, -width / 2));

        var side1 = new Pair <Vector3>(firstListA.GetRoadPoint(size, width / 2), firstListA.GetRoadPoint(size, -width / 2));
        var side3 = new Pair <Vector3>(firstListB.GetRoadPoint(size, width / 2), firstListB.GetRoadPoint(size, -width / 2));

        var side4 = new Pair <Vector3>(secondListA.GetRoadPoint(size, width / 2), secondListA.GetRoadPoint(size, -width / 2));
        var side2 = new Pair <Vector3>(secondListB.GetRoadPoint(size, width / 2), secondListB.GetRoadPoint(size, -width / 2));

        var outerPoints = new List <Pair <Vector3> >();

        outerPoints.Add(side1);

        //debugPoints.Add(side2.First);
        //debugPoints.Add(side4.Second);

        outerPoints.Add(side2);
        outerPoints.Add(side3);
        outerPoints.Add(side4);

        LineEquation line1 = new LineEquation(side1.First.To2D(), side3.Second.To2D());
        LineEquation line2 = new LineEquation(side1.Second.To2D(), side3.First.To2D());
        LineEquation line3 = new LineEquation(side2.First.To2D(), side4.Second.To2D());
        LineEquation line4 = new LineEquation(side2.Second.To2D(), side4.First.To2D());

        var innerPoints = new List <Vector3>();

        innerPoints.Add(line1.GetIntersectionWithLine(line3).Value.To3D());
        innerPoints.Add(line2.GetIntersectionWithLine(line3).Value.To3D());
        innerPoints.Add(line2.GetIntersectionWithLine(line4).Value.To3D());
        innerPoints.Add(line1.GetIntersectionWithLine(line4).Value.To3D());

        //debugPoints.Add(line1.GetIntersectionWithLine(line3).Value.To3D());
        //debugPoints.Add(line1.GetIntersectionWithLine(line4).Value.To3D());
        //debugPoints.Add(line2.GetIntersectionWithLine(line3).Value.To3D());
        //debugPoints.Add(line2.GetIntersectionWithLine(line4).Value.To3D());

        var name = "fourwayintersection";

        Mesh mesh = MeshMaker.FourWayIntersection(outerPoints, innerPoints, name);

        DestroyImmediate(GameObject.Find(name));
        GameObject intersection = new GameObject(name);

        intersection.transform.SetParent(transform);
        intersection.transform.position = transform.position;
        intersection.AddComponent <MeshFilter>().sharedMesh = mesh;

        MeshRenderer rend     = intersection.AddComponent <MeshRenderer>();
        Material     material = (Material)Resources.Load("Materials/test_mat", typeof(Material));

        rend.sharedMaterial       = material;
        rend.sharedMaterial.color = Color.white;
    }