80 lines
3.3 KiB
C#
80 lines
3.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Globalization;
|
||
using System.Text;
|
||
|
||
namespace XNet.Business.Parser
|
||
{
|
||
public class SimpleObjParser
|
||
{
|
||
public class ObjData
|
||
{
|
||
public required float[] Vertices { get; set; }
|
||
public required int[] Indices { get; set; }
|
||
}
|
||
|
||
public static ObjData Parse(string filePath)
|
||
{
|
||
var verts = new List<float>();
|
||
var indices = new List<int>();
|
||
|
||
if (!File.Exists(filePath)) throw new FileNotFoundException("Obj file not found", filePath);
|
||
var lines = File.ReadAllLines(filePath);
|
||
|
||
foreach (var line in lines)
|
||
{
|
||
var trimmed = line.Trim();
|
||
if (string.IsNullOrEmpty(trimmed) || trimmed.StartsWith("#")) continue;
|
||
var parts = trimmed.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||
if (parts.Length == 0) continue;
|
||
|
||
if (parts[0] == "v")
|
||
{
|
||
// 修改为 (翻转 Z 轴并交换 Y/Z,视具体 OBJ 情况尝试):
|
||
float x = float.Parse(parts[1], CultureInfo.InvariantCulture);
|
||
float y = float.Parse(parts[2], CultureInfo.InvariantCulture);
|
||
float z = float.Parse(parts[3], CultureInfo.InvariantCulture);
|
||
|
||
// 方案 A: 标准 Unity 导出 (无需修改,保持 x, y, z) - 如果 Size Y 正常,用这个。
|
||
verts.Add(x);
|
||
verts.Add(y);
|
||
verts.Add(z);
|
||
|
||
// 方案 B: 修正翻转 (如果你的地图竖起来了,或者 X 轴镜像了)
|
||
//verts.Add(x);
|
||
//verts.Add(z); // 把 Z 变成 Y
|
||
//verts.Add(y); // 把 Y 变成 Z
|
||
}
|
||
else if (parts[0] == "f")
|
||
{
|
||
if (parts.Length >= 4)
|
||
{
|
||
indices.Add(int.Parse(parts[1].Split('/')[0]) - 1);
|
||
indices.Add(int.Parse(parts[2].Split('/')[0]) - 1);
|
||
indices.Add(int.Parse(parts[3].Split('/')[0]) - 1);
|
||
}
|
||
|
||
//if (parts.Length >= 4)
|
||
//{
|
||
// int v1 = int.Parse(parts[1].Split('/')[0]) - 1;
|
||
// int v2 = int.Parse(parts[2].Split('/')[0]) - 1;
|
||
// int v3 = int.Parse(parts[3].Split('/')[0]) - 1;
|
||
|
||
// // --- 修改开始 ---
|
||
// // 尝试 1: 默认顺序 (你当前的代码)
|
||
// // indices.Add(v1); indices.Add(v2); indices.Add(v3);
|
||
|
||
// // 尝试 2: [强力推荐] 交换 v2 和 v3 (反转面朝向)
|
||
// // 如果之前生成为 0,这行代码通常能救命
|
||
// indices.Add(v1);
|
||
// indices.Add(v3); // 注意:这里换成了 v3
|
||
// indices.Add(v2); // 注意:这里换成了 v2
|
||
// // --- 修改结束 ---
|
||
//}
|
||
}
|
||
}
|
||
return new ObjData { Vertices = verts.ToArray(), Indices = indices.ToArray() };
|
||
}
|
||
}
|
||
}
|