public void GetContainingFaceTest()
        {
            var dcel = new DCEL();
            var v1   = dcel.AddVertex(new DCELVertex(0, 0));
            var v2   = dcel.AddVertex(new DCELVertex(10, 10));
            var v3   = dcel.AddVertex(new DCELVertex(20, 0));

            dcel.AddEdge(v1, v2);
            dcel.AddEdge(v2, v3);
            dcel.AddEdge(v3, v1);
            var expFace = dcel.InnerFaces.FirstOrDefault();

            Assert.AreEqual(expFace, dcel.GetContainingFace(new Vector2(10, 5)));
            Assert.AreEqual(dcel.OuterFace, dcel.GetContainingFace(new Vector2(-1, 0)));
        }
示例#2
0
        /// <summary>
        /// Updates the mesh according to the Voronoi DCEL.
        /// </summary>
        private void UpdateMesh()
        {
            if (m_meshFilter.mesh == null)
            {
                // create initial mesh
                m_meshFilter.mesh = new Mesh {
                    subMeshCount = 2
                };
                m_meshFilter.mesh.MarkDynamic();
            }
            else
            {
                // clear old mesh
                m_meshFilter.mesh.Clear();
                m_meshFilter.mesh.subMeshCount = 2;
            }

            // build vertices and triangle list
            var vertices  = new List <Vector3>();
            var triangles = new List <int>[2] {
                new List <int>(),
                new List <int>()
            };

            // iterate over vertices and create triangles accordingly
            foreach (var inputNode in m_delaunay.Vertices)
            {
                // dont draw anything for unowned vertices
                if (m_ownership[inputNode] == EOwnership.UNOWNED)
                {
                    continue;
                }

                // get ownership of node
                var playerIndex = m_ownership[inputNode] == EOwnership.PLAYER1 ? 0 : 1;

                var face = m_DCEL.GetContainingFace(inputNode);

                // cant triangulate outer face
                if (face.IsOuter)
                {
                    continue;
                }

                // triangulate face polygon
                var triangulation = Triangulator.Triangulate(face.Polygon.Outside);

                // add triangles to correct list
                foreach (var triangle in triangulation.Triangles)
                {
                    int curCount = vertices.Count;

                    // add triangle vertices
                    vertices.Add(new Vector3(triangle.P0.x, 0, triangle.P0.y));
                    vertices.Add(new Vector3(triangle.P1.x, 0, triangle.P1.y));
                    vertices.Add(new Vector3(triangle.P2.x, 0, triangle.P2.y));

                    // add triangle to mesh according to owner
                    triangles[playerIndex].Add(curCount);
                    triangles[playerIndex].Add(curCount + 1);
                    triangles[playerIndex].Add(curCount + 2);
                }
            }

            // update mesh
            m_meshFilter.mesh.vertices = vertices.ToArray();
            m_meshFilter.mesh.SetTriangles(triangles[0], 0);
            m_meshFilter.mesh.SetTriangles(triangles[1], 1);
            m_meshFilter.mesh.RecalculateBounds();

            // set correct uv
            var newUVs = new List <Vector2>();

            foreach (var vertex in vertices)
            {
                newUVs.Add(new Vector2(vertex.x, vertex.z));
            }
            m_meshFilter.mesh.uv = newUVs.ToArray();
        }