Beispiel #1
0
        /// <summary>
        /// Rebuild the mesh positions and submeshes. If vertex count matches new positions array the existing attributes are kept, otherwise the mesh is cleared. UV2 is the exception, it is always cleared.
        /// </summary>
        /// <param name="preferredTopology">Triangles and Quads are supported.</param>
        public void ToMesh(MeshTopology preferredTopology = MeshTopology.Triangles)
        {
            // if the mesh vertex count hasn't been modified, we can keep most of the mesh elements around
            if (mesh == null)
            {
#if ENABLE_DRIVEN_PROPERTIES
                SerializationUtility.RegisterDrivenProperty(this, this, "m_Mesh");
#endif
                mesh = new Mesh();
            }
            else if (mesh.vertexCount != vertexCount)
            {
                mesh.Clear();
            }

            mesh.indexFormat = vertexCount > ushort.MaxValue ? Rendering.IndexFormat.UInt32 : Rendering.IndexFormat.UInt16;
            mesh.vertices    = m_Positions;
            mesh.uv2         = null;

            if (m_MeshFormatVersion < k_MeshFormatVersion)
            {
                if (m_MeshFormatVersion < k_MeshFormatVersionSubmeshMaterialRefactor)
                {
                    Submesh.MapFaceMaterialsToSubmeshIndex(this);
                }
                if (m_MeshFormatVersion < k_MeshFormatVersionAutoUVScaleOffset)
                {
                    UvUnwrapping.UpgradeAutoUVScaleOffset(this);
                }
                m_MeshFormatVersion = k_MeshFormatVersion;
            }

            m_MeshFormatVersion = k_MeshFormatVersion;

            int materialCount = MaterialUtility.GetMaterialCount(renderer);

            Submesh[] submeshes = Submesh.GetSubmeshes(facesInternal, materialCount, preferredTopology);

            mesh.subMeshCount = materialCount;

            for (int i = 0; i < mesh.subMeshCount; i++)
            {
#if DEVELOPER_MODE
                if (i >= materialCount)
                {
                    Log.Warning("Submesh index " + i + " is out of bounds of the MeshRenderer materials array.");
                }
                if (submeshes[i] == null)
                {
                    throw new Exception("Attempting to assign a null submesh. " + i + "/" + materialCount);
                }
#endif
                mesh.SetIndices(submeshes[i].m_Indexes, submeshes[i].m_Topology, i, false);
            }

            mesh.name = string.Format("pb_Mesh{0}", id);

            EnsureMeshFilterIsAssigned();
        }
Beispiel #2
0
        public void RefreshUV(IEnumerable <Face> facesToRefresh)
        {
            // If the UV array has gone out of sync with the positions array, reset all faces to Auto UV so that we can
            // correct the texture array.
            if (!HasArrays(MeshArrays.Texture0))
            {
                m_Textures0 = new Vector2[vertexCount];
                foreach (Face f in facesInternal)
                {
                    f.manualUV = false;
                }
                facesToRefresh = facesInternal;
            }

            s_CachedHashSet.Clear();

            foreach (var face in facesToRefresh)
            {
                if (face.manualUV || face.indexesInternal?.Length < 3)
                {
                    continue;
                }

                int textureGroup = face.textureGroup;

                if (!IsValidTextureGroup(textureGroup))
                {
                    UvUnwrapping.Unwrap(this, face);
                }
                else if (s_CachedHashSet.Add(textureGroup))
                {
                    UvUnwrapping.ProjectTextureGroup(this, textureGroup, face.uv);
                }
            }

            mesh.uv = m_Textures0;

            if (HasArrays(MeshArrays.Texture2))
            {
                mesh.SetUVs(2, m_Textures2);
            }
            if (HasArrays(MeshArrays.Texture3))
            {
                mesh.SetUVs(3, m_Textures3);
            }

            IncrementVersionIndex();
        }