Ejemplo n.º 1
0
        private void generateOtherSide(float size, myTerrain terrain)
        {
            List <Vector3> otherside;
            List <Vector3> generatedside;

            TerrainInfo ti = terrain.terrainInfo;

            if (side == pavementSide.left)
            {
                otherside     = rightSideVertexes;
                generatedside = leftSideVertexes;
            }
            else
            {
                otherside     = leftSideVertexes;
                generatedside = rightSideVertexes;
            }

            if (otherside.Count == 2)
            {
                Vector3 up      = Vector3.up;
                Vector3 forward = otherside[1] - otherside[0];
                forward.y = 0.0f;
                Vector3 sideVec;
                //if (side == pavementSide.left)*******************************************************
                //    sideVec =  Vector3.Cross(up, forward).normalized;
                //else
                //    sideVec =  Vector3.Cross(forward, up).normalized;
                if (side == pavementSide.left)
                {
                    sideVec = Vector3.Cross(forward, up).normalized;
                }
                else
                {
                    sideVec = Vector3.Cross(up, forward).normalized;
                }



                Vector2 point1 = new Vector2(otherside[0].x, otherside[0].z) + new Vector2(sideVec.x, sideVec.z) * size;
                Vector2 point2 = new Vector2(otherside[1].x, otherside[1].z) + new Vector2(sideVec.x, sideVec.z) * size;

                if ((point1 - point2).magnitude < 6.0f)
                {
                    return;
                }

                generatedside.Add(new Vector3(point1.x, terrain.getTerrainHeight2(point1.y + ti.shiftZ, point1.x + ti.shiftX), point1.y));
                generatedside.Add(new Vector3(point2.x, terrain.getTerrainHeight2(point2.y + ti.shiftZ, point2.x + ti.shiftX), point2.y));
            }

            else
            {
                //Checking if side has enough length to draw a pavement
                float totalsize = 0.0f;
                for (int k = 0; k < otherside.Count - 1; k++)
                {
                    totalsize += (otherside[k + 1] - otherside[k]).magnitude;
                }
                if (totalsize <= 6.0f)
                {
                    return;
                }


                for (int i = 0; i < otherside.Count; i++)
                {
                    Vector3 up       = Vector3.up;
                    Vector3 forward1 = otherside[i + 1] - otherside[i];
                    forward1.y = 0.0f;
                    Vector3 sideVec1;
                    //if (side == pavementSide.left)***************************************************************
                    //    sideVec1 = Vector3.Cross(up, forward1).normalized;
                    //else
                    //    sideVec1 = Vector3.Cross(forward1, up).normalized;
                    if (side == pavementSide.left)
                    {
                        sideVec1 = Vector3.Cross(forward1, up).normalized;
                    }
                    else
                    {
                        sideVec1 = Vector3.Cross(up, forward1).normalized;
                    }

                    Vector3 forward2 = otherside[i + 2] - otherside[i + 1];
                    forward2.y = 0.0f;
                    Vector3 sideVec2;
                    if (side == pavementSide.left)
                    {
                        sideVec2 = Vector3.Cross(forward2, up).normalized;
                    }
                    else
                    {
                        sideVec2 = Vector3.Cross(up, forward2).normalized;
                    }

                    //START POINT WAS ADDED TO GENERATED SIDE
                    if (i == 0)
                    {
                        Vector2 point1 = new Vector2(otherside[i].x, otherside[i].z) + new Vector2(sideVec1.x, sideVec1.z) * size;
                        generatedside.Add(new Vector3(point1.x, terrain.getTerrainHeight2(point1.y + ti.shiftZ, point1.x + ti.shiftX), point1.y));
                    }

                    //MID POINTS WERE ADDED TO GENERATED SIDE

                    //1ST LINE
                    Vector2 p0 = new Vector2(otherside[i].x, otherside[i].z) + new Vector2(sideVec1.x, sideVec1.z) * size;
                    Vector2 p1 = new Vector2(otherside[i + 1].x, otherside[i + 1].z) + new Vector2(sideVec1.x, sideVec1.z) * size;

                    //2ND LINE
                    Vector2 p2 = new Vector2(otherside[i + 1].x, otherside[i + 1].z) + new Vector2(sideVec2.x, sideVec2.z) * size;
                    Vector2 p3 = new Vector2(otherside[i + 2].x, otherside[i + 2].z) + new Vector2(sideVec2.x, sideVec2.z) * size;

                    Vector2 iL = new Vector2();
                    //INTERSECTION
                    if (!Geometry.getInfiniteLineIntersection(ref iL, p0, p1, p2, p3))
                    {
                        generatedside.Add(new Vector3(p1.x, terrain.getTerrainHeight2(p1.y + ti.shiftZ, p1.x + ti.shiftX), p1.y));
                    }
                    else
                    {
                        generatedside.Add(new Vector3(iL.x, terrain.getTerrainHeight2(iL.y + ti.shiftZ, iL.x + ti.shiftX), iL.y));
                    }


                    //ENDING POINT WAS ADDEDD TO GENERATED SIDE
                    if (i == otherside.Count - 3)
                    {
                        Vector2 point1 = new Vector2(otherside[i + 2].x, otherside[i + 2].z) + new Vector2(sideVec2.x, sideVec2.z) * size;
                        generatedside.Add(new Vector3(point1.x, terrain.getTerrainHeight2(point1.y + ti.shiftZ, point1.x + ti.shiftX), point1.y));
                        return;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        //This will be used to drape Highway to Terrain Tiles Diagonally
        private void DiagonalDrape(myTerrain terrain, List<Vector3> leftSideVertexes, List<Vector3> rightSideVertexes)
        {
            TerrainInfo ti = terrain.terrainInfo;

            for (int i = 0; i < (int)leftSideVertexes.Count - 1; i++)
            {

                //LEFT SIDE
                Vector2 pointLeft1 = new Vector2(leftSideVertexes[i].x, leftSideVertexes[i].z);
                Vector2 pointLeft2 = new Vector2(leftSideVertexes[i + 1].x, leftSideVertexes[i + 1].z);

                //RIGHT SIDE
                Vector2 pointRight1 = new Vector2(rightSideVertexes[i].x, rightSideVertexes[i].z);
                Vector2 pointRight2 = new Vector2(rightSideVertexes[i + 1].x, rightSideVertexes[i + 1].z);

                int cnt = 0;

                for (int k = 0, t = 1; k < ti.ColumnCount + ti.RowCount - 3; k++, t++)
                {
                    //Diagonal Line
                    Vector2 pBottom;
                    Vector2 pTop;
                    if (t < ti.ColumnCount)
                        pTop = new Vector2(ti.meterPositions[0, t].y - ti.shiftX, ti.meterPositions[0, t].x - ti.shiftZ);
                    else
                        pTop = new Vector2(ti.meterPositions[1 + t - ti.ColumnCount, ti.ColumnCount - 1].y - ti.shiftX, ti.meterPositions[1 + t - ti.ColumnCount, ti.ColumnCount - 1].x - ti.shiftZ);

                    if (t < ti.RowCount)
                        pBottom = new Vector2(ti.meterPositions[t, 0].y - ti.shiftX, ti.meterPositions[t, 0].x - ti.shiftZ);
                    else
                        pBottom = new Vector2(ti.meterPositions[ti.RowCount - 1, 1 + t - ti.RowCount].y - ti.shiftX, ti.meterPositions[ti.RowCount - 1, t + 1 - ti.RowCount].x - ti.shiftZ);

                    //LEFT SIDE INTERSECTION
                    Vector2 intersectleft = new Vector2();
                    //RIGHT SIDE INTERSECTION
                    Vector2 intersectright = new Vector2();

                    bool isLeft = Geometry.getLineIntersection(ref intersectleft, pTop, pBottom, pointLeft1, pointLeft2);
                    bool isRight = Geometry.getLineIntersection(ref intersectright, pTop, pBottom, pointRight1, pointRight2);

                    if (isLeft && isRight)
                    {
                        leftSideVertexes.Insert(i + 1, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));

                        rightSideVertexes.Insert(i + 1, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                        cnt++;
                    }
                    else if (isLeft)
                    {
                        leftSideVertexes.Insert(i + 1, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));

                        float ratio = (pointLeft1 - intersectleft).magnitude / (pointLeft1 - pointLeft2).magnitude;
                        Vector2 rightPointnew = pointRight1 + (pointRight2 - pointRight1) * ratio;
                        rightSideVertexes.Insert(i + 1, new Vector3(rightPointnew.x, terrain.getTerrainHeight2(rightPointnew.y + ti.shiftZ, rightPointnew.x + ti.shiftX), rightPointnew.y));
                        cnt++;
                    }

                    else if (isRight)
                    {
                        rightSideVertexes.Insert(i + 1, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));

                        float ratio = (pointRight1 - intersectright).magnitude / (pointRight1 - pointRight2).magnitude;
                        Vector2 leftPointnew = pointLeft1 + (pointLeft2 - pointLeft1) * ratio;
                        leftSideVertexes.Insert(i + 1, new Vector3(leftPointnew.x, terrain.getTerrainHeight2(leftPointnew.y + ti.shiftZ, leftPointnew.x + ti.shiftX), leftPointnew.y));
                        cnt++;
                    }
                }
                i += cnt;
            }
        }
Ejemplo n.º 3
0
        //This will be used to drape Highway to Terrain Tiles Horizontally
        private void HorizontalDrape(myTerrain terrain, List<Vector3> leftSideVertexes, List<Vector3> rightSideVertexes)
        {
            TerrainInfo ti = terrain.terrainInfo;

            for (int i = 0; i < leftSideVertexes.Count - 1; i++)
            {

                //LEFT SIDE
                Vector2 pointLeft1 = new Vector2(leftSideVertexes[i].x, leftSideVertexes[i].z);
                Vector2 pointLeft2 = new Vector2(leftSideVertexes[i + 1].x, leftSideVertexes[i + 1].z);
                //RIGHT SIDE
                Vector2 pointRight1 = new Vector2(rightSideVertexes[i].x, rightSideVertexes[i].z);
                Vector2 pointRight2 = new Vector2(rightSideVertexes[i + 1].x, rightSideVertexes[i + 1].z);

                Vector2 previousIntersectLeft = pointLeft1;
                Vector2 previousIntersectRight = pointRight1;

                int leftit = i + 1;
                int rightit = i + 1;
                int count = 0;

                for (int k = ti.bottomIndex, t = 0; k >= ti.topIndex; k--, t++)
                {
                    //Horizontal Line
                    Vector2 pLeft = new Vector2(ti.meterPositions[t, 0].y - ti.shiftX, ti.meterPositions[t, 0].x - ti.shiftZ);
                    Vector2 pRight = new Vector2(ti.meterPositions[t, ti.ColumnCount - 1].y - ti.shiftX, ti.meterPositions[t, ti.ColumnCount - 1].x - ti.shiftZ);

                    Vector2 intersectleft = new Vector2();
                    Vector2 intersectright = new Vector2();
                    bool isleft = Geometry.getLineIntersection(ref intersectleft, pLeft, pRight, pointLeft1, pointLeft2);
                    bool isright = Geometry.getLineIntersection(ref intersectright, pLeft, pRight, pointRight1, pointRight2);

                    if (isleft && isright)
                    {
                        if ((intersectleft - pointLeft1).magnitude > (previousIntersectLeft - pointLeft1).magnitude)
                        {
                            leftSideVertexes.Insert(leftit, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                            leftit++;
                        }
                        else
                        {
                            leftSideVertexes.Insert(i + 1, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                        }

                        if ((intersectright - pointRight1).magnitude > (previousIntersectRight - pointRight1).magnitude)
                        {
                            rightSideVertexes.Insert(rightit, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                            rightit++;
                        }
                        else
                        {
                            rightSideVertexes.Insert(i + 1, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                        }

                        previousIntersectLeft = intersectleft;
                        previousIntersectRight = intersectright;
                        count++;
                    }

                    else if (isleft)
                    {
                        if ((intersectleft - pointLeft1).magnitude > (previousIntersectLeft - pointLeft1).magnitude)
                        {
                            leftSideVertexes.Insert(leftit, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                            leftit++;
                        }
                        else
                        {
                            leftSideVertexes.Insert(i + 1, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                        }

                        float ratio = (pointLeft1 - intersectleft).magnitude / (pointLeft1 - pointLeft2).magnitude;
                        Vector2 rightPointnew = pointRight1 + (pointRight2 - pointRight1) * ratio;
                        rightSideVertexes.Insert(rightit, new Vector3(rightPointnew.x, terrain.getTerrainHeight2(rightPointnew.y + ti.shiftZ, rightPointnew.x + ti.shiftX), rightPointnew.y));

                        count++;
                        rightit++;
                        previousIntersectLeft = intersectleft;
                        previousIntersectRight = rightPointnew;

                    }
                    else if (isright)
                    {

                        if ((intersectright - pointRight1).magnitude > (previousIntersectRight - pointRight1).magnitude)
                        {
                            rightSideVertexes.Insert(rightit, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                            rightit++;
                        }
                        else
                        {
                            rightSideVertexes.Insert(i + 1, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                        }

                        float ratio = (pointRight1 - intersectright).magnitude / (pointRight1 - pointRight2).magnitude;
                        Vector2 leftPointnew = pointLeft1 + (pointLeft2 - pointLeft1) * ratio;
                        leftSideVertexes.Insert(leftit, new Vector3(leftPointnew.x, terrain.getTerrainHeight2(leftPointnew.y + ti.shiftZ, leftPointnew.x + ti.shiftX), leftPointnew.y));

                        previousIntersectLeft = leftPointnew;
                        previousIntersectRight = intersectright;
                        count++;
                        leftit++;
                    }
                    else
                        continue;

                }

                i += count;

            }
        }
Ejemplo n.º 4
0
        //This will be used to drape Highway to Terrain Tiles Vertically
        private void VerticalDrape(myTerrain terrain, List<Vector3> leftSideVertexes, List<Vector3> rightSideVertexes)
        {
            TerrainInfo ti = terrain.terrainInfo;

            for (int i = 0; i < (int)leftSideVertexes.Count - 1; i++)
            {
                Vector2 pointLeft1 = new Vector2(leftSideVertexes[i].x, leftSideVertexes[i].z);
                Vector2 pointLeft2 = new Vector2(leftSideVertexes[i + 1].x, leftSideVertexes[i + 1].z);

                Vector2 pointRight1 = new Vector2(rightSideVertexes[i].x, rightSideVertexes[i].z);
                Vector2 pointRight2 = new Vector2(rightSideVertexes[i + 1].x, rightSideVertexes[i + 1].z);

                int cnt = 0;
                int leftiterator = i + 1;
                int rightiterator = i + 1;

                Vector2 previousIntersectLeft = pointLeft1;
                Vector2 previousIntersectRight = pointRight1;

                for (int k = ti.leftIndex, z = 0; k <= ti.rightIndex; k++, z++)
                {
                    //Vertical Terrain Line
                    Vector2 pTop = new Vector2(ti.meterPositions[0, z].y - ti.shiftX, ti.meterPositions[0, z].x - ti.shiftZ);
                    Vector2 pBottom = new Vector2(ti.meterPositions[ti.RowCount - 1, z].y - ti.shiftX, ti.meterPositions[ti.RowCount - 1, z].x - ti.shiftZ);

                    Vector2 intersectleft = new Vector2();
                    Vector2 intersectright = new Vector2();
                    bool isleft = Geometry.getLineIntersection(ref intersectleft, pTop, pBottom, pointLeft1, pointLeft2);
                    bool isright = Geometry.getLineIntersection(ref intersectright, pTop, pBottom, pointRight1, pointRight2);

                    if (isleft && isright)
                    {

                        if ((intersectleft - pointLeft1).magnitude > (previousIntersectLeft - pointLeft1).magnitude)
                        {
                            leftSideVertexes.Insert(leftiterator, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                            leftiterator++;
                        }
                        else
                        {
                            leftSideVertexes.Insert(i + 1, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                        }

                        if ((intersectright - pointRight1).magnitude > (previousIntersectRight - pointRight1).magnitude)
                        {
                            rightSideVertexes.Insert(rightiterator, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                            rightiterator++;
                        }
                        else
                        {
                            rightSideVertexes.Insert(i + 1, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                        }

                        previousIntersectLeft = intersectleft;
                        previousIntersectRight = intersectright;
                        cnt++;
                    }
                    else if (isleft)
                    {

                        if ((intersectleft - pointLeft1).magnitude > (previousIntersectLeft - pointLeft1).magnitude)
                        {
                            leftSideVertexes.Insert(leftiterator, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                            leftiterator++;
                        }
                        else
                        {
                            leftSideVertexes.Insert(i + 1, new Vector3(intersectleft.x, terrain.getTerrainHeight2(intersectleft.y + ti.shiftZ, intersectleft.x + ti.shiftX), intersectleft.y));
                        }

                        float ratio = (pointLeft1 - intersectleft).magnitude / (pointLeft1 - pointLeft2).magnitude;
                        Vector2 rightPointnew = pointRight1 + (pointRight2 - pointRight1) * ratio;

                        rightSideVertexes.Insert(rightiterator, new Vector3(rightPointnew.x, terrain.getTerrainHeight2(rightPointnew.y + ti.shiftZ, rightPointnew.x + ti.shiftX), rightPointnew.y));
                        previousIntersectLeft = intersectleft;
                        previousIntersectRight = rightPointnew;
                        rightiterator++;
                        cnt++;

                    }
                    else if (isright)
                    {

                        if ((intersectright - pointRight1).magnitude > (previousIntersectRight - pointRight1).magnitude)
                        {
                            rightSideVertexes.Insert(rightiterator, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                            rightiterator++;
                        }
                        else
                        {
                            rightSideVertexes.Insert(i + 1, new Vector3(intersectright.x, terrain.getTerrainHeight2(intersectright.y + ti.shiftZ, intersectright.x + ti.shiftX), intersectright.y));
                        }

                        float ratio = (pointRight1 - intersectright).magnitude / (pointRight1 - pointRight2).magnitude;
                        Vector2 leftPointnew = pointLeft1 + (pointLeft2 - pointLeft1) * ratio;

                        leftSideVertexes.Insert(leftiterator, new Vector3(leftPointnew.x, terrain.getTerrainHeight2(leftPointnew.y + ti.shiftZ, leftPointnew.x + ti.shiftX), leftPointnew.y));
                        previousIntersectRight = intersectright;
                        previousIntersectLeft = leftPointnew;
                        leftiterator++;
                        cnt++;
                    }

                }

                i += cnt;

            }
        }