首页 > 其他分享 >导出mesh网格

导出mesh网格

时间:2023-03-10 18:44:35浏览次数:43  
标签:mf dictionary stringBuilder 导出 网格 mesh Append triangles

 

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 导出Mash网格
/// </summary>
public class ExportMesh
{
    //导出
    //ExportMesh.MeshToFile(GetComponent<MeshFilter>(), Application.dataPath + "/SwordSpirit.obj", 1);
    public static void MeshToFile(MeshFilter mf, string filename, float scale)
    {
        using (StreamWriter streamWriter = new StreamWriter(filename))
        {
            streamWriter.Write(MeshToString(mf, scale));
        }
        AssetDatabase.Refresh();
    }

    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();
    }
}

 

标签:mf,dictionary,stringBuilder,导出,网格,mesh,Append,triangles
From: https://www.cnblogs.com/z816/p/17204411.html

相关文章

  • Redis 数据导入导出
    一、redis-dump方式1、Ruby安装(RockyLinux9)dnf-yinstallrubyruby-devel2、修改Rubygems默认源#列出默认源gemsources#移除默认源gemsource......
  • Easyexcel导出多个表格
    Easyexcel导出多个表格ExcelWriterexcelWriter=null;try{excelWriter=EasyExcel.write(response.getOutputStream()).build();......
  • Excel多线程导出大数据量
    以下是Java的EasyExcelAPI实现多线程Excel导出的demo。在这个demo中,我们使用EasyExcelAPI来导出Excel文件。我们将表头和内容定义为动态的,以便能够处理大量的数据。我......
  • JS关于导出文件流方法封装的使用
    如果需要频繁地在JavaScript中导出文件流,可以将上述的方法封装为函数,以便在不同的代码中调用。/***导出文件*@param{string|Uint8Array}data-文件内容*@para......
  • JAVA 实现导入导出Excel-03随笔
    还是摸鱼的一天疯狂星期四的舆论越来越少了,于我而言:KFC并没有特别价值值得众人去推崇和关注的地方到公司打完卡之后,"偷偷"跑下楼吃了一份早餐,又"偷偷"的做在工位上,看见......
  • un-resolved CFD-DEM网格尺寸为颗粒直径的3倍以上
    依据颗粒与流体网格的尺寸,目前在未解析CFD-DEM模型中,流体空隙率算法主要有三种:PCM(ParticleCentroidMethod)、DPVM(DividedParticleVolume Method)和SKM(StatisticalKe......
  • Unity SkinMesh之如何分割SubMesh
    可参考:http://answers.unity3d.com/questions/1213025/separating-submeshes-into-unique-meshes.html代码如下:1usingUnityEditor;2usingUnityEngine;3usi......
  • Mysql导出文本文件
    使用mysqldump命令导出文本文件mysqldump-uroot-pPassword-T目标目录dbname[tables][option];其中:Password参数表示root用户的密码,密码紧挨着-p选项。-T:只有......
  • ParaView插件—————CCM+网格的读取(更新)
    使用方法见原博文:https://www.cnblogs.com/liusuanyatong/p/16183872.html 插件需要用自己编译的ParaView,我编译的ParaView下载链接: https://pan.baidu.com/s/1kW7......
  • JAVA 实现导入导出Excel-02随笔
    忙碌的一天第一天写博客就让我煞费苦心,从上午完成的第一篇开始,具目前为止已经是下午18:41了;读者可能好奇为什么写这一篇随笔,我只能回答说太无聊了哈哈哈;其实不然,刚到......