Ejemplo n.º 1
0
        /// <summary>
        /// Default option use EarBased Algorithm (very slow) for huge amount of vertices
        /// </summary>
        /// <param name="p_rectToCalcUV"></param>
        /// <param name="p_triangulationOption"></param>
        /// <returns></returns>
        public static Mesh BakeMesh(this PolygonShape p_polygon, Rect p_rectToCalcUV, TriangulationOptionEnum p_triangulationOption = TriangulationOptionEnum.Default)
        {
            if (p_polygon.Vertices != null && p_polygon.Vertices.Count > 0)
            {
                Mesh v_mesh     = new Mesh();
                var  v_vertices = new List <Vector3>();
                v_vertices.Add(p_polygon.RectBounds.center);

                var v_uvs = new List <Vector2>();
                v_uvs.Add(new Vector2(0.5f, 0.5f));

                var v_colors = new List <Color>();
                v_colors.Add(Color.white);

                //Calculate Triangulation
                var v_indexes = p_triangulationOption == TriangulationOptionEnum.Default ? p_polygon.GetTrianglesWithDefaultOption() : p_polygon.GetTringlesWithUnsafeOption();

                //Map the Vertex and Calculate UVs
                foreach (var v_vertice in p_polygon.Vertices)
                {
                    v_vertices.Add(v_vertice);
                    var v_uv = Rect.PointToNormalized(p_rectToCalcUV, v_vertice);
                    v_uvs.Add(v_uv);
                    v_colors.Add(Color.white);
                }

                //FillMesh
                v_mesh.SetVertices(v_vertices);
                v_mesh.SetUVs(0, v_uvs);
                v_mesh.SetColors(v_colors);
                v_mesh.SetIndices(v_indexes.ToArray(), MeshTopology.Triangles, 0);
                return(v_mesh);
            }
            return(null);
        }