Ejemplo n.º 1
0
 static void PrintMeshInfo2()
 {
     foreach (MeshFilter mf in Selection.transforms.Select(x => x.GetComponent <MeshFilter>()))
     {
         if (mf.sharedMesh != null)
         {
             System.Diagnostics.Process.Start(WriteTempFile(MeshUtility.Print(mf.sharedMesh)));
         }
     }
 }
Ejemplo n.º 2
0
 static void PrintMeshInfo()
 {
     foreach (MeshFilter mf in Selection.transforms.Select(x => x.GetComponent <MeshFilter>()))
     {
         if (mf.sharedMesh != null)
         {
             Debug.Log(MeshUtility.Print(mf.sharedMesh));
         }
     }
 }
        /// <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)
        {
            bool usedInParticleSystem = false;

            // 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)
            {
                usedInParticleSystem = MeshUtility.IsUsedInParticleSystem(this);
                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();

            if (usedInParticleSystem)
            {
                MeshUtility.RestoreParticleSystem(this);
            }

            IncrementVersionIndex();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Prints a detailed string summary of the mesh attributes.
        /// </summary>
        /// <param name="mesh">The mesh to print information for.</param>
        /// <returns>A tab-delimited string (positions, normals, colors, tangents, and UV coordinates).</returns>
        public static string Print(Mesh mesh)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

            StringBuilder sb = new StringBuilder();

            Vector3[]      positions = mesh.vertices;
            Vector3[]      normals   = mesh.normals;
            Color[]        colors    = mesh.colors;
            Vector4[]      tangents  = mesh.tangents;
            List <Vector4> uv0       = new List <Vector4>();

            Vector2[]      uv2 = mesh.uv2;
            List <Vector4> uv3 = new List <Vector4>();
            List <Vector4> uv4 = new List <Vector4>();

            mesh.GetUVs(0, uv0);
            mesh.GetUVs(2, uv3);
            mesh.GetUVs(3, uv4);

            sb.AppendLine($"# Sanity Check");
            sb.AppendLine(MeshUtility.SanityCheck(mesh));

            sb.AppendLine($"# Attributes ({mesh.vertexCount})");

            PrintAttribute(sb, $"positions ({positions.Length})", positions, "pos: {0:F2}");
            PrintAttribute(sb, $"normals ({normals.Length})", normals, "nrm: {0:F2}");
            PrintAttribute(sb, $"colors ({colors.Length})", colors, "col: {0:F2}");
            PrintAttribute(sb, $"tangents ({tangents.Length})", tangents, "tan: {0:F2}");
            PrintAttribute(sb, $"uv0 ({uv0.Count})", uv0, "uv0: {0:F2}");
            PrintAttribute(sb, $"uv2 ({uv2.Length})", uv2, "uv2: {0:F2}");
            PrintAttribute(sb, $"uv3 ({uv3.Count})", uv3, "uv3: {0:F2}");
            PrintAttribute(sb, $"uv4 ({uv4.Count})", uv4, "uv4: {0:F2}");

            sb.AppendLine("# Topology");

            for (int i = 0; i < mesh.subMeshCount; i++)
            {
                var topo    = mesh.GetTopology(i);
                var submesh = mesh.GetIndices(i);
                sb.AppendLine($"  Submesh[{i}] ({topo})");

                switch (topo)
                {
                case MeshTopology.Points:
                    for (int n = 0; n < submesh.Length; n += 1)
                    {
                        sb.AppendLine(string.Format("\t{0}", submesh[n]));
                    }
                    break;

                case MeshTopology.Lines:
                    for (int n = 0; n < submesh.Length; n += 2)
                    {
                        sb.AppendLine(string.Format("\t{0}, {1}", submesh[n], submesh[n + 1]));
                    }
                    break;

                case MeshTopology.Triangles:
                    for (int n = 0; n < submesh.Length; n += 3)
                    {
                        sb.AppendLine(string.Format("\t{0}, {1}, {2}", submesh[n], submesh[n + 1], submesh[n + 2]));
                    }
                    break;

                case MeshTopology.Quads:
                    for (int n = 0; n < submesh.Length; n += 4)
                    {
                        sb.AppendLine(string.Format("\t{0}, {1}, {2}, {3}", submesh[n], submesh[n + 1], submesh[n + 2], submesh[n + 3]));
                    }
                    break;
                }
            }

            return(sb.ToString());
        }