コード例 #1
0
        //generates the ends of the platform
        Mesh GenerateEndCaps(Vert[] vertices, bool isStart)
        {
            var vertices2D = new Vector2[vertices.Length];

            if (isStart)
            {
                vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.Vector.x, x.Vector.y));
            }
            else
            {
                vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.Vector.y, x.Vector.x));
            }

            // Use the triangulator to get indices for creating triangles
            var tr = new PlatformHelper.Triangulator(vertices2D);

            int[] indices = tr.Triangulate();

            // Create the mesh
            Mesh msh = new Mesh();

            msh.vertices  = vertices.Select(x => x.Vector).ToArray();;
            msh.triangles = indices;
            return(msh);
        }
コード例 #2
0
        //generates a rectangular mesh with verts, tris, and uvs.
        Mesh GenerateMeshFromPoints(Vector3[] vertices, float uvLengthDistance, float uvWidthDistance, Vector2[] previousUvs, Vector2[] previousWidthUvs)
        {
            var vertices2D = new Vector2[vertices.Length];
            var cross      = Vector3.Cross(vertices[1] - vertices[0], vertices[2] - vertices[1]);

            if (cross.x != 0)
            {
                if (cross.x < 0)
                {
                    vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.z, x.y));
                }
                else
                {
                    vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.y, x.z));
                }
            }
            else if (cross.y != 0)
            {
                if (cross.y < 0)
                {
                    vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.x, x.z));
                }
                else
                {
                    vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.z, x.x));
                }
            }
            else if (cross.z != 0)
            {
                if (cross.z < 0)
                {
                    vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.x, x.y));
                }
                else
                {
                    vertices2D = Array.ConvertAll(vertices, x => new Vector2(x.y, x.x));
                }
            }

            // Use the triangulator to get indices for creating triangles
            var tr = new PlatformHelper.Triangulator(vertices2D);

            int[] indices = tr.Triangulate();

            //uvs
            var currentUvLengthDistance = (Mathf.Abs(Vector3.Distance(vertices[0], vertices[3])) / uvLengthDistance);
            var currentUvWidthDistance  = (Mathf.Abs(Vector3.Distance(vertices[0], vertices[1])) / uvWidthDistance);
            var uvs = new Vector2[4];

            uvs[3] = new Vector2(previousUvs[0].x, previousWidthUvs[2].y);
            uvs[2] = new Vector2(previousUvs[1].x, previousWidthUvs[1].y + currentUvWidthDistance);
            uvs[1] = new Vector2(previousUvs[0].x + currentUvLengthDistance, previousWidthUvs[2].y + currentUvWidthDistance);
            uvs[0] = new Vector2(previousUvs[1].x + currentUvLengthDistance, previousWidthUvs[1].y);

            // Create the mesh
            Mesh msh = new Mesh();

            msh.vertices  = vertices;
            msh.triangles = indices;
            msh.uv        = uvs;
            return(msh);
        }