示例#1
0
    // Use this for initialization
    void Start()
    {
        geometry = new InputGeometry();

        List <Point> shape = new List <Point> ();

        shape.Add(new Point(0f, 0f));
        shape.Add(new Point(2f, 2f));
        shape.Add(new Point(1f, 4f));
        shape.Add(new Point(3f, 5f));
        shape.Add(new Point(-3f, 5f));
        shape.Add(new Point(-1f, 4f));
        shape.Add(new Point(-2f, 2f));
        geometry.AddRing(shape);

        //it is necessary to put a border around all the points in order to get triangulation to work correctly when holes are used
//        List<Point> border = new List<Point>();
//        border.Add(new Point(distance, verticalDistance));
//        border.Add(new Point(distance, -verticalDistance));
//        border.Add(new Point(-distance, -verticalDistance));
//        border.Add(new Point(-distance, verticalDistance));
//        geometry.AddRing(border);

//        List<Point> outlinePoints = new List<Point>(logoOutline.points.Length);
//        foreach (Vector2 coordinates in logoOutline.points)
//        {
//            outlinePoints.Add(new Point(coordinates));
//        }
//
//        geometry.AddRingAsHole(outlinePoints, 0);
//
//
//        foreach(UPolygon hole in holes)
//        {
//            List<Point> holePoints = new List<Point>(hole.points.Length);
//
//            foreach (Vector2 coordinates in hole.points)
//                holePoints.Add(new Point(coordinates));
//
//            geometry.AddRing(holePoints, 0);
//        }


//        List<Point> points = new List<Point>();
//        for (float offsetX = -distance; offsetX < distance; offsetX += boxDistance)
//        {
//            for (float offsetY = -verticalDistance; offsetY < verticalDistance; offsetY += boxDistance)
//            {
//                Vector2 offset = new Vector2(offsetX, offsetY) + Vector2.one * boxDistance * 0.5f;
//
//                float radians = Random.RandomRange(0, 2 * Mathf.PI);
//                float length = Random.RandomRange(0, circleDistance);
//
//                Vector2 pos = new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * length;
//                pos += offset;
//
//                bool insideOutline = logoOutline.PointInPolygon(pos);
//
//                bool stillAlloved = false;
//                for (int i = 0; i < holes.Length; i++ )
//                {
//                    if (holes[i].PointInPolygon(pos))
//                        stillAlloved = true;
//                }
//
//                if (!insideOutline || stillAlloved)
//                    geometry.AddPoint((float)pos.x, (float)pos.y, 0);
//            }
//        }

        meshRepresentation = new TriangleNet.Mesh();
        meshRepresentation.Triangulate(geometry);

        //generate mesh based on triangulation

        Dictionary <int, float> zOffsets = new Dictionary <int, float>();

//        foreach(KeyValuePair<int, TriangleNet.Data.Vertex> pair in meshRepresentation.vertices)
//        {
//            zOffsets.Add(pair.Key, Random.RandomRange(-zOffset, zOffset));
//        }

        int            triangleIndex   = 0;
        List <Vector3> vertices        = new List <Vector3>(meshRepresentation.triangles.Count * 3);
        List <int>     triangleIndices = new List <int>(meshRepresentation.triangles.Count * 3);

        foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in meshRepresentation.triangles)
        {
            TriangleNet.Data.Triangle triangle = pair.Value;

            TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0);
            TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1);
            TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2);

            Vector3 p0 = new Vector3(vertex0.x, vertex0.y, 0);
            Vector3 p1 = new Vector3(vertex1.x, vertex1.y, 0);
            Vector3 p2 = new Vector3(vertex2.x, vertex2.y, 0);

            vertices.Add(p0);
            vertices.Add(p1);
            vertices.Add(p2);

            triangleIndices.Add(triangleIndex + 2);
            triangleIndices.Add(triangleIndex + 1);
            triangleIndices.Add(triangleIndex);

            triangleIndex += 3;
        }

        mesh           = new Mesh();
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangleIndices.ToArray();
        mesh.RecalculateNormals();
        GetComponent <MeshFilter>().mesh = mesh;
    }
示例#2
0
        protected void GenerateTrisForShape()
        {
            if (vertices.Length >= 6 && vertices.Length % 2 == 0)
            {
                InputGeometry bottomGeometry = new InputGeometry();
                List <Point>  shape          = new List <Point> ();
                int           halfVertLen    = vertices.Length / 2;
                for (int i = 0; i < halfVertLen; i++)
                {
                    shape.Add(new Point(vertices[i].x, vertices[i].z));
                }
                bottomGeometry.AddRing(shape);

                TriangleNet.Mesh bottomMesh = new TriangleNet.Mesh();
                bottomMesh.Triangulate(bottomGeometry);

                List <Vector3> verts           = new List <Vector3>(bottomMesh.triangles.Count * 3);
                List <int>     triangleIndices = new List <int>(bottomMesh.triangles.Count * 3);
                foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in bottomMesh.triangles)
                {
                    TriangleNet.Data.Triangle triangle = pair.Value;

                    TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0);
                    TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1);
                    TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2);

                    Vector3 p0 = new Vector3(vertex0.x, 0, vertex0.y);
                    Vector3 p1 = new Vector3(vertex1.x, 0, vertex1.y);
                    Vector3 p2 = new Vector3(vertex2.x, 0, vertex2.y);

                    verts.Add(p0);
                    verts.Add(p1);
                    verts.Add(p2);

                    triangleIndices.Add(GetIndexForVertex(p0));
                    triangleIndices.Add(GetIndexForVertex(p1));
                    triangleIndices.Add(GetIndexForVertex(p2));
                }

                // Create top Side
                List <int> triangleIndecesReversed = new List <int>();
                for (int i = triangleIndices.Count - 1; i >= 0; i--)
                {
                    triangleIndecesReversed.Add(triangleIndices[i] + halfVertLen);
                }
                triangleIndices.AddRange(triangleIndecesReversed);

                // Add edges
                List <int> edgeIndeces = new List <int>();
                for (int i = 0; i < halfVertLen; i++)
                {
                    int leftBottom  = i;
                    int rightBottom = (i == halfVertLen - 1 ? 0 : i + 1);
                    int leftTop     = i + halfVertLen;
                    int rightTop    = (i == halfVertLen - 1 ? halfVertLen : i + halfVertLen + 1);

                    edgeIndeces.Add(leftTop);
                    edgeIndeces.Add(rightTop);
                    edgeIndeces.Add(leftBottom);

                    edgeIndeces.Add(rightBottom);
                    edgeIndeces.Add(leftBottom);
                    edgeIndeces.Add(rightTop);
                }
                triangleIndices.AddRange(edgeIndeces);

                tris = triangleIndices.ToArray();
            }
            else
            {
                Debug.LogError("[BaseShape.GenerateTrisForShape] Invalid vertices set for shape!");
            }
        }
示例#3
0
    public static void CreateMesh(GameObject meshContainer, string path, string path2, string adfname)
    {
        var subgraphs = _LoadMarkerToSubgraph(path, path2);

        meshContainer.name = adfname + "Mesh";

        InputGeometry geometry = new InputGeometry();

        TriangleNet.Mesh meshRepresentation;
        MeshFilter       mf = meshContainer.GetComponent <MeshFilter> () as MeshFilter;
        Mesh             mesh;
        float            zOffset = 0.1f;

        float[] maxArea = new float[2] {
            0, 0
        };

        List <List <Point> > clusters = new List <List <Point> > ();

        foreach (Graph <Node> s in subgraphs)
        {
            List <Point> points = new List <Point> ();
            foreach (Node n in s.Nodes)
            {
                Point tmp = new Point(n.position.x, n.position.z);
                zOffset = n.position.y;
                points.Add(tmp);
            }
            if (points.Count > 2)
            {
                clusters.Add(points);

                // Calculate areas of the clusters. The largest is the external ring, while the others must be holes.
                float num = CalculateArea(points);
                if (num > maxArea [0])
                {
                    maxArea [0] = num;
                    maxArea [1] = clusters.Count - 1;
                }
            }
            Debug.Log(clusters [clusters.Count - 1].Count);
        }

        geometry.AddRing(clusters[(int)maxArea[1]]);
        clusters.RemoveAt((int)maxArea [1]);

        foreach (List <Point> c in clusters)
        {
            geometry.AddRingAsHole(c);
        }
        meshRepresentation = new TriangleNet.Mesh();
        meshRepresentation.Triangulate(geometry);

        //		Dictionary<int, float> zOffsets = new Dictionary<int, float>();
        //
        //		foreach(KeyValuePair<int, TriangleNet.Data.Vertex> pair in meshRepresentation.vertices)
        //		{
        //			zOffsets.Add(pair.Key, Random.RandomRange(-zOffset, zOffset));
        //		}

        int            triangleIndex   = 0;
        List <Vector3> vertices        = new List <Vector3>(meshRepresentation.triangles.Count * 3);
        List <int>     triangleIndices = new List <int>(meshRepresentation.triangles.Count * 3);

        foreach (KeyValuePair <int, TriangleNet.Data.Triangle> pair in meshRepresentation.triangles)
        {
            TriangleNet.Data.Triangle triangle = pair.Value;

            TriangleNet.Data.Vertex vertex0 = triangle.GetVertex(0);
            TriangleNet.Data.Vertex vertex1 = triangle.GetVertex(1);
            TriangleNet.Data.Vertex vertex2 = triangle.GetVertex(2);

            Vector3 p0 = new Vector3(vertex0.x, zOffset, vertex0.y);
            Vector3 p1 = new Vector3(vertex1.x, zOffset, vertex1.y);
            Vector3 p2 = new Vector3(vertex2.x, zOffset, vertex2.y);

            //			Vector3 p0 = new Vector3( vertex0.x, vertex0.y, zOffsets[vertex0.id]);
            //			Vector3 p1 = new Vector3( vertex1.x, vertex1.y, zOffsets[vertex1.id]);
            //			Vector3 p2 = new Vector3( vertex2.x, vertex2.y, zOffsets[vertex2.id]);

            vertices.Add(p0);
            vertices.Add(p1);
            vertices.Add(p2);

            triangleIndices.Add(triangleIndex + 2);
            triangleIndices.Add(triangleIndex + 1);
            triangleIndices.Add(triangleIndex);

            triangleIndex += 3;
        }
        mesh           = new Mesh();
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangleIndices.ToArray();
        mesh.RecalculateNormals();
        //GetComponent<MeshFilter>().mesh = mesh;
        mf.mesh = mesh;

        Exporter e = meshContainer.GetComponent <Exporter> () as Exporter;

        e.DoExport(true);
//
//		File.WriteAllBytes(Application.persistentDataPath + "/" + adfname + "Mesh", MeshSerializer.WriteMesh (mesh, true));

        meshContainer.SetActive(true);
        meshContainer.GetComponent <NavMeshSurface> ().BuildNavMesh();

        Debug.Log("NavMesh created");

        //MeshSaverEditor.SaveMesh (mesh, "meshtest", true, true);

        //ObjExporterScript.MeshToString(GetComponent<MeshFilter>(),
    }
示例#4
0
    // Use this for initialization
    void Start()
    {
        Map             = new GameMap();
        Map.TerrainData = LowPolyTerrainData.GetRandomMap();

        var building = new Building();

        building.Positions.Add(new Vector2Int(6, 1));
        building.Positions.Add(new Vector2Int(7, 1));
        building.Positions.Add(new Vector2Int(8, 1));
        building.Positions.Add(new Vector2Int(9, 1));
        building.Positions.Add(new Vector2Int(10, 1));
        building.Positions.Add(new Vector2Int(11, 1));
        building.Positions.Add(new Vector2Int(12, 1));
        building.Positions.Add(new Vector2Int(13, 1));
        building.Positions.Add(new Vector2Int(14, 1));
        building.Positions.Add(new Vector2Int(11, 2));
        building.Positions.Add(new Vector2Int(12, 2));
        building.Positions.Add(new Vector2Int(12, 3));
        building.Positions.Add(new Vector2Int(13, 3));
        building.Positions.Add(new Vector2Int(13, 4));
        building.Positions.Add(new Vector2Int(14, 4));
        building.Positions.Add(new Vector2Int(14, 5));
        building.Positions.Add(new Vector2Int(15, 5));
        building.RemoveTrees(Map.TerrainData);
        //Map.Buildings.Add(building);

        building = new Building();
        //building.Positions.Add(new Vector2Int(6, 2));
        building.Positions.Add(new Vector2Int(7, 2));
        building.Positions.Add(new Vector2Int(8, 2));
        building.Positions.Add(new Vector2Int(9, 2));
        building.Positions.Add(new Vector2Int(10, 2));
        building.Positions.Add(new Vector2Int(5, 3));
        building.Positions.Add(new Vector2Int(6, 3));
        building.Positions.Add(new Vector2Int(7, 3));
        building.Positions.Add(new Vector2Int(8, 3));
        building.Positions.Add(new Vector2Int(9, 3));
        building.Positions.Add(new Vector2Int(10, 3));
        building.Positions.Add(new Vector2Int(11, 3));
        //building.Positions.Add(new Vector2Int(5, 4));
        building.Positions.Add(new Vector2Int(6, 4));
        building.Positions.Add(new Vector2Int(7, 4));
        building.Positions.Add(new Vector2Int(8, 4));
        building.Positions.Add(new Vector2Int(9, 4));
        building.Positions.Add(new Vector2Int(10, 4));
        building.Positions.Add(new Vector2Int(11, 4));
        building.Positions.Add(new Vector2Int(10, 6));
        building.Positions.Add(new Vector2Int(11, 6));
        building.Positions.Add(new Vector2Int(12, 6));
        building.Type = "house";
        building.RemoveTrees(Map.TerrainData);
        //Map.Buildings.Add(building);

        building = new Building();

        var floor = new BuildingFloor();

        floor.Positions.Add(new Vector2Int(13, 2));
        floor.Positions.Add(new Vector2Int(14, 2));
        floor.Positions.Add(new Vector2Int(15, 2));
        floor.Positions.Add(new Vector2Int(16, 2));
        floor.Positions.Add(new Vector2Int(14, 3));
        floor.Positions.Add(new Vector2Int(15, 3));
        floor.Positions.Add(new Vector2Int(16, 3));

        floor.WallEdges.Add(new WallEdge(new Vector2Int(13, 2), 2));
        floor.WallEdges.Add(new WallEdge(new Vector2Int(13, 2), 0));

        floor.BaseHeight  = 20.0f;
        floor.FloorHeight = 0.2f;
        floor.Height      = 10.0f;
        floor.Map         = Map;

        building.Floors.Add(floor);

        floor = new BuildingFloor();
        floor.Positions.Add(new Vector2Int(14, 2));
        floor.Positions.Add(new Vector2Int(15, 2));
        floor.Positions.Add(new Vector2Int(16, 2));
        floor.Positions.Add(new Vector2Int(14, 3));
        floor.Positions.Add(new Vector2Int(15, 3));
        floor.Positions.Add(new Vector2Int(16, 3));

        floor.WallEdges.Add(new WallEdge(new Vector2Int(14, 2), 2));
        floor.WallEdges.Add(new WallEdge(new Vector2Int(15, 2), 2));

        floor.BaseHeight  = 30.0f;
        floor.FloorHeight = 0.2f;
        floor.Height      = 10.0f;
        floor.Map         = Map;
        building.Floors.Add(floor);

        floor = new BuildingFloor();
        floor.Positions.Add(new Vector2Int(15, 4));
        floor.Positions.Add(new Vector2Int(16, 4));
        floor.Positions.Add(new Vector2Int(17, 4));
        floor.Positions.Add(new Vector2Int(17, 3));
        floor.Positions.Add(new Vector2Int(18, 3));

        floor.WallEdges.Add(new WallEdge(new Vector2Int(15, 4), 0));
        floor.WallEdges.Add(new WallEdge(new Vector2Int(16, 4), 0));
        floor.WallEdges.Add(new WallEdge(new Vector2Int(17, 4), 1, 2));

        floor.BaseHeight  = 22.0f;
        floor.FloorHeight = 0.2f;
        floor.Height      = 10.0f;
        floor.Map         = Map;
        building.Floors.Add(floor);

        building.Type = "generic";

        building.RemoveTrees(Map.TerrainData);
        Map.Buildings.Add(building);

        Init(Map);

        var geometry = new InputGeometry();

        //it is necessary to put a border around all the points in order to get triangulation to work correctly when holes are used
        var border = new List <Point>();

        border.Add(new Point(10, 6));
        border.Add(new Point(10, -6));
        border.Add(new Point(-10, -6));
        border.Add(new Point(-10, 6));
        geometry.AddRing(border);

        border = new List <Point>();
        border.Add(new Point(7, 4));
        border.Add(new Point(7, 2));
        border.Add(new Point(5, 2));
        border.Add(new Point(5, 4));
        geometry.AddRingAsHole(border);

        border = new List <Point>();
        border.Add(new Point(3, 2));
        border.Add(new Point(3, -6));
        border.Add(new Point(-3, -6));
        border.Add(new Point(-3, 2));
        geometry.AddRingAsHole(border);

        var meshRepresentation = new TriangleNet.Mesh();

        meshRepresentation.Triangulate(geometry);

        var triangleIndex   = 0;
        var vertices        = new List <Vector3>(meshRepresentation.triangles.Count * 3);
        var triangleIndices = new List <int>(meshRepresentation.triangles.Count * 3);

        foreach (var pair in meshRepresentation.triangles)
        {
            var triangle = pair.Value;

            var vertex0 = triangle.GetVertex(0);
            var vertex1 = triangle.GetVertex(1);
            var vertex2 = triangle.GetVertex(2);

            var p0 = new Vector3(vertex0.x, vertex0.y, 0);
            var p1 = new Vector3(vertex1.x, vertex1.y, 0);
            var p2 = new Vector3(vertex2.x, vertex2.y, 0);

            vertices.Add(p0);
            vertices.Add(p1);
            vertices.Add(p2);

            triangleIndices.Add(triangleIndex + 2);
            triangleIndices.Add(triangleIndex + 1);
            triangleIndices.Add(triangleIndex);

            triangleIndex += 3;
        }

        var mesh = new Mesh();

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangleIndices.ToArray();
        mesh.RecalculateNormals();

        var newObj = new GameObject();

        newObj.name             = "Test";
        newObj.transform.parent = transform;
        newObj.AddComponent <MeshFilter>();
        newObj.AddComponent <MeshRenderer>();
        newObj.GetComponent <MeshFilter>().mesh = mesh;

        /*
         * var path = new List<ClipperLib.IntPoint>();
         * path.Add(new ClipperLib.IntPoint(-3000, 0));
         * path.Add(new ClipperLib.IntPoint(1000, 0));
         * path.Add(new ClipperLib.IntPoint(6000, 3000));
         * path.Add(new ClipperLib.IntPoint(6000, -3000));
         * path.Add(new ClipperLib.IntPoint(1000, 0));
         * //path.Add(new ClipperLib.IntPoint(5000, 0));
         * //path.Add(new ClipperLib.IntPoint(1000, -3500));
         * //path.Add(new ClipperLib.IntPoint(-3000, 0));
         *
         * var co = new ClipperLib.ClipperOffset();
         * co.AddPath(path, ClipperLib.JoinType.jtSquare, ClipperLib.EndType.etClosedLine);
         *
         * var result = new List<List<ClipperLib.IntPoint>>();
         * co.Execute(ref result, 500);
         * foreach (var intpoint in path)
         * {
         *  var point = new Vector3((float)intpoint.X / 1000.0f, 0, (float)intpoint.Y / 1000.0f);
         *  Debug.DrawLine(point, point + Vector3.up * 3, Color.cyan, 100);
         * }
         *
         * foreach (var intpath in result)
         * {
         *  foreach (var intpoint in intpath)
         *  {
         *      var point = new Vector3((float)intpoint.X / 1000.0f, 0, (float)intpoint.Y / 1000.0f);
         *      Debug.DrawLine(point, point + Vector3.up * 3, Color.red, 100);
         *  }
         * }
         */
    }