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()
{
}
}
生成mesh网格并导出obj文件
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 参考Brackeys视频 https://www.youtube.com/watch?v=64NblGkAabk&...
- 前端导出部分: 带参数的话可以把参数写在url里:const baseURL=`/api/export.json?...
- 参考Brackeys视频 https://www.youtube.com/watch?v=eJEpeUH1EMg ...
- FrontISTR是一款知名的免费且开源的有限元分析计算软件。从2005年日本文部省资助此项目开始,至今已经有17...