mesh网格导出obj文件

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;


public class GreatMesh : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //创建环形网格
        VertexHelper vh = new VertexHelper();
        float ang = 2 * Mathf.PI / 30;
        for (int i = 0; i < 30; i++)
        {
            float x = Mathf.Sin(ang * i) * 0.3f;
            float z = Mathf.Cos(ang * i) * 0.3f - 0.1f;
            vh.AddVert(new Vector3(x, 0, z), Color.white, Vector2.zero);
            float x0 = Mathf.Sin(ang * i) * 0.5f;
            float z0 = Mathf.Cos(ang * i) * 0.5f - 0.1f;
            vh.AddVert(new Vector3(x0, 0, z0), Color.white, Vector2.zero);

            if (i < 29)
            {
                vh.AddTriangle(i * 2, i * 2 + 1, (i + 1) * 2 + 1);
                vh.AddTriangle(i * 2, (i + 1) * 2 + 1, (i + 1) * 2);
            }
            else
            {
                vh.AddTriangle(i * 2, i * 2 + 1, 1);
                vh.AddTriangle(i * 2, 1, 0);
            }
        }
        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        gameObject.AddComponent<MeshFilter>().mesh = mesh;
        Material material = new Material(Shader.Find("Standard"));
        gameObject.AddComponent<MeshRenderer>().material = material;
        //导出
        MeshToFile(GetComponent<MeshFilter>(), Application.dataPath + "/ring.obj", 1);
    }
    public static void MeshToFile(MeshFilter mf, string filename, float scale)
    {
        using (StreamWriter streamWriter = new StreamWriter(filename))
        {
            streamWriter.Write(MeshToString(mf, scale));
        }
    }

    public static string MeshToString(MeshFilter mf, float scale)
    {
        Mesh mesh = mf.mesh;
        Material[] sharedMaterials = mf.GetComponent<Renderer>().sharedMaterials;
        Vector2 textureOffset = mf.GetComponent<Renderer>().material.GetTextureOffset("_MainTex");
        Vector2 textureScale = mf.GetComponent<Renderer>().material.GetTextureScale("_MainTex");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append("mtllib design.mtl").Append("\n");
        stringBuilder.Append("g ").Append(mf.name).Append("\n");
        Vector3[] vertices = mesh.vertices;
        for (int i = 0; i < vertices.Length; i++)
        {
            Vector3 vector = vertices[i];
            stringBuilder.Append(string.Format("v {0} {1} {2}\n", vector.x * scale, vector.z * scale, vector.y * scale));
        }
        stringBuilder.Append("\n");
        Dictionary<int, int> dictionary = new Dictionary<int, int>();
        if (mesh.subMeshCount > 1)
        {
            int[] triangles = mesh.GetTriangles(1);
            for (int j = 0; j < triangles.Length; j += 3)
            {
                if (!dictionary.ContainsKey(triangles[j]))
                {
                    dictionary.Add(triangles[j], 1);
                }
                if (!dictionary.ContainsKey(triangles[j + 1]))
                {
                    dictionary.Add(triangles[j + 1], 1);
                }
                if (!dictionary.ContainsKey(triangles[j + 2]))
                {
                    dictionary.Add(triangles[j + 2], 1);
                }
            }
        }
        for (int num = 0; num != mesh.uv.Length; num++)
        {
            Vector2 vector2 = Vector2.Scale(mesh.uv[num], textureScale) + textureOffset;
            if (dictionary.ContainsKey(num))
            {
                stringBuilder.Append(string.Format("vt {0} {1}\n", mesh.uv[num].x, mesh.uv[num].y));
            }
            else
            {
                stringBuilder.Append(string.Format("vt {0} {1}\n", vector2.x, vector2.y));
            }
        }
        for (int k = 0; k < mesh.subMeshCount; k++)
        {
            stringBuilder.Append("\n");
            if (k == 0)
            {
                stringBuilder.Append("usemtl ").Append("Material_design").Append("\n");
            }
            if (k == 1)
            {
                stringBuilder.Append("usemtl ").Append("Material_logo").Append("\n");
            }
            int[] triangles2 = mesh.GetTriangles(k);
            for (int l = 0; l < triangles2.Length; l += 3)
            {
                stringBuilder.Append(string.Format("f {0}/{0} {1}/{1} {2}/{2}\n", triangles2[l] + 1, triangles2[l + 1] + 1, triangles2[l + 2] + 1));
            }
        }
        return stringBuilder.ToString();
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
posted @ 2023-03-07 19:06  old_Host  阅读(139)  评论(0编辑  收藏  举报