/// <summary>
        /// Creates a new array of vertices with values from a @"UnityEngine.ProBuilder.ProBuilderMesh" component.
        /// </summary>
        /// <param name="indexes">An optional list of indexes pointing to the mesh attribute indexes to include in the returned Vertex array.</param>
        /// <returns>An array of vertices.</returns>
        public Vertex[] GetVertices(IList <int> indexes = null)
        {
            int meshVertexCount = vertexCount;
            int vc = indexes != null ? indexes.Count : vertexCount;

            Vertex[] v = new Vertex[vc];

            Vector3[] positions = positionsInternal;
            Color[]   colors    = colorsInternal;
            Vector2[] uv0s      = texturesInternal;
            Vector4[] tangents  = GetTangents();
            Vector3[] normals   = GetNormals();
            Vector2[] uv2s      = mesh != null ? mesh.uv2 : null;

            List <Vector4> uv3s = new List <Vector4>();
            List <Vector4> uv4s = new List <Vector4>();

            GetUVs(2, uv3s);
            GetUVs(3, uv4s);

            bool _hasPositions = positions != null && positions.Count() == meshVertexCount;
            bool _hasColors    = colors != null && colors.Count() == meshVertexCount;
            bool _hasNormals   = normals != null && normals.Count() == meshVertexCount;
            bool _hasTangents  = tangents != null && tangents.Count() == meshVertexCount;
            bool _hasUv0       = uv0s != null && uv0s.Count() == meshVertexCount;
            bool _hasUv2       = uv2s != null && uv2s.Count() == meshVertexCount;
            bool _hasUv3       = uv3s.Count() == meshVertexCount;
            bool _hasUv4       = uv4s.Count() == meshVertexCount;

            for (int i = 0; i < vc; i++)
            {
                v[i] = new Vertex();

                int ind = indexes == null ? i : indexes[i];

                if (_hasPositions)
                {
                    v[i].position = positions[ind];
                }

                if (_hasColors)
                {
                    v[i].color = colors[ind];
                }

                if (_hasNormals)
                {
                    v[i].normal = normals[ind];
                }

                if (_hasTangents)
                {
                    v[i].tangent = tangents[ind];
                }

                if (_hasUv0)
                {
                    v[i].uv0 = uv0s[ind];
                }

                if (_hasUv2)
                {
                    v[i].uv2 = uv2s[ind];
                }

                if (_hasUv3)
                {
                    v[i].uv3 = uv3s[ind];
                }

                if (_hasUv4)
                {
                    v[i].uv4 = uv4s[ind];
                }
            }

            return(v);
        }
        /// <summary>
        /// Set the vertex element arrays on this mesh.
        /// </summary>
        /// <param name="vertices">The new vertex array.</param>
        /// <param name="applyMesh">An optional parameter that will apply elements to the MeshFilter.sharedMesh. Note that this should only be used when the mesh is in its original state, not optimized (meaning it won't affect triangles which can be modified by Optimize).</param>
        public void SetVertices(IList <Vertex> vertices, bool applyMesh = false)
        {
            if (vertices == null)
            {
                throw new ArgumentNullException("vertices");
            }

            var first = vertices.FirstOrDefault();

            if (first == null || !first.HasArrays(MeshArrays.Position))
            {
                Clear();
                return;
            }

            Vector3[]      position;
            Color[]        color;
            Vector3[]      normal;
            Vector4[]      tangent;
            Vector2[]      uv0;
            Vector2[]      uv2;
            List <Vector4> uv3;
            List <Vector4> uv4;

            Vertex.GetArrays(vertices, out position, out color, out uv0, out normal, out tangent, out uv2, out uv3, out uv4);

            m_Positions = position;
            m_Colors    = color;
            m_Tangents  = tangent;
            m_Textures0 = uv0;
            m_Textures2 = uv3;
            m_Textures3 = uv4;

            if (applyMesh)
            {
                Mesh umesh = mesh;

                if (first.HasArrays(MeshArrays.Position))
                {
                    umesh.vertices = position;
                }
                if (first.HasArrays(MeshArrays.Color))
                {
                    umesh.colors = color;
                }
                if (first.HasArrays(MeshArrays.Texture0))
                {
                    umesh.uv = uv0;
                }
                if (first.HasArrays(MeshArrays.Normal))
                {
                    umesh.normals = normal;
                }
                if (first.HasArrays(MeshArrays.Tangent))
                {
                    umesh.tangents = tangent;
                }
                if (first.HasArrays(MeshArrays.Texture1))
                {
                    umesh.uv2 = uv2;
                }
                if (first.HasArrays(MeshArrays.Texture2))
                {
                    umesh.SetUVs(2, uv3);
                }
                if (first.HasArrays(MeshArrays.Texture3))
                {
                    umesh.SetUVs(3, uv4);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Set a collection of mesh attributes with a Vertex.
        /// <br /><br />
        /// Use @"UnityEngine.ProBuilder.ProBuilderMesh.sharedIndexes" and IntArrayUtility.IndexOf to get a shared (or common) index.
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="sharedVertexHandle"></param>
        /// <param name="vertex"></param>
        internal static void SetSharedVertexValues(this ProBuilderMesh mesh, int sharedVertexHandle, Vertex vertex)
        {
            Vertex[] vertices = mesh.GetVertices();

            foreach (var index in mesh.sharedVerticesInternal[sharedVertexHandle])
            {
                vertices[index] = vertex;
            }

            mesh.SetVertices(vertices);
        }