本文主要是介绍FBX格式mesh解析与加载(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
直接上代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ImportFbx
{
public class FBXImporter
{
private static FBXImporter fbxImporter;
public static FBXImporter Instance
{
get
{
if (fbxImporter == null) fbxImporter = new FBXImporter();
return fbxImporter;
}
}
private Vector3[] vertices;public Vector3[] Vertices {get {return vertices;}}private int[] vertexIndex;public int[] VertexIndex {get {return vertexIndex;}}private Vector3[] normal;public Vector3[] Normal {get {return normal;}}private Vector2[] uv;public Vector2[] UV{get{return uv;}}public void OpenFBXFile(string path){if (!System.IO.File.Exists(path)) return;System.IO.FileStream file = new System.IO.FileStream(path,System.IO.FileMode.Open);byte[] bytes = new byte[file.Length];file.Read(bytes, 0, (int)file.Length);string data = System.Text.Encoding.UTF8.GetString(bytes);UnityEngine.Debug.Log(data);file.Dispose();file.Close();string[] ObjectStr = data.Split(new string[] { "Objects:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[] a = ObjectStr[1].Split(new string[] { "Vertices:" }, 2 ,StringSplitOptions.RemoveEmptyEntries);UnityEngine.Debug.Log(a[1]);string[] aa = a[1].Split(new string[] { "a:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[] aaa = aa[1].Split(new char[] { '}' }, 2, StringSplitOptions.RemoveEmptyEntries);string[] verticesStr = aaa[0].Split(new char[] { ',' });UnityEngine.Debug.Log(verticesStr.Length);string[]b = ObjectStr[1].Split(new string[] { "PolygonVertexIndex:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[]bb = b[1].Split(new string[] { "a:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[]bbb = bb[1].Split(new char[] { '}' }, 2, StringSplitOptions.RemoveEmptyEntries);string[] vertexIdStr = bbb[0].Split(new char[] { ',' });UnityEngine.Debug.Log(vertexIdStr.Length);string[]c = ObjectStr[1].Split(new string[] { "Normals:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[]cc = c[1].Split(new string[] { "a:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[]ccc = cc[1].Split(new char[] { '}' }, 2, StringSplitOptions.RemoveEmptyEntries);string[] normalStr = ccc[0].Split(new char[] { ',' });UnityEngine.Debug.Log(normalStr.Length);string[] d = ObjectStr[1].Split(new string[] { "UV:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[] dd = d[1].Split(new string[] { "a:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[] ddd = dd[1].Split(new char[] { '}' }, 2, StringSplitOptions.RemoveEmptyEntries);string[] uvStr = ddd[0].Split(new char[] { ',' });UnityEngine.Debug.Log(uvStr.Length);string[] e = ObjectStr[1].Split(new string[] { "UVIndex:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[] ee = e[1].Split(new string[] { "a:" }, 2, StringSplitOptions.RemoveEmptyEntries);string[] eee = ee[1].Split(new char[] { '}' }, 2, StringSplitOptions.RemoveEmptyEntries);string[] uvIndexStr = eee[0].Split(new char[] { ',' });UnityEngine.Debug.Log(uvIndexStr.Length);vertices = new Vector3[verticesStr.Length/3];for (int i = 0; i < verticesStr.Length; i+=3){float.TryParse(verticesStr[i], out vertices[i/3].x);float.TryParse(verticesStr[i + 1], out vertices[i/3].y);float.TryParse(verticesStr[i + 2], out vertices[i/3].z);}int[] vertexIndexTemp = new int[vertexIdStr.Length];int q = 0;bool isSquareMesh = false;for (int i = 0; i < vertexIdStr.Length; i++){int.TryParse(vertexIdStr[i], out vertexIndexTemp[i]);q++;if (vertexIndexTemp[i] < 0 && q == 3){q = 0;vertexIndexTemp[i] = -vertexIndexTemp[i] - 1;}else if (vertexIndexTemp[i] < 0 && q == 4){q = 0;vertexIndexTemp[i] = -vertexIndexTemp[i] - 1;isSquareMesh = true;}}if (isSquareMesh) {List<int> triangles = new List<int>();for (int i = 0; i < vertexIdStr.Length; i+=4){triangles.AddRange(new int[3] { vertexIndexTemp[i], vertexIndexTemp[i + 1], vertexIndexTemp[i + 3] });triangles.AddRange(new int[3] { vertexIndexTemp[i + 1], vertexIndexTemp[i + 2], vertexIndexTemp[i + 3] });}vertexIndex = triangles.ToArray();}else{vertexIndex = vertexIndexTemp;}normal = new Vector3[vertices.Length];Vector3[] normTemp = new Vector3[normalStr.Length / 3];for (int i = 0; i < normalStr.Length; i+=3){float.TryParse(normalStr[i], out normTemp[i/3].x);float.TryParse(normalStr[i + 1], out normTemp[i/3].y);float.TryParse(normalStr[i + 2], out normTemp[i/3].z);}for (int i = 0; i < vertices.Length; i++){for (int j = 0; j < vertexIndexTemp.Length; j++){if (vertexIndexTemp[j] == i){normal[i] = normTemp[j];break;}}}uv = new Vector2[vertices.Length];Vector2[] uvTemp = new Vector2[uvStr.Length / 2];//UVVector2[] uvIndexTemp = new Vector2[uvIndexStr.Length];//all UVfor (int i = 0; i < uvStr.Length; i += 2){float.TryParse(uvStr[i], out uvTemp[i / 2].x);float.TryParse(uvStr[i + 1], out uvTemp[i / 2].y);}for (int i = 0; i < uvIndexStr.Length; i++){int index;int.TryParse(uvIndexStr[i], out index);uvIndexTemp[i] = uvTemp[index];}for (int i = 0; i < vertices.Length; i++){for (int j = 0; j < vertexIndexTemp.Length; j++){if (vertexIndexTemp[j] == i){uv[i] = uvIndexTemp[j];break;}}}}
}
}
放在unity中运行:
模型2是通过常规的导入方式导入场景中作为参考的模型。
模型1是运行时导入的结果,也就是代码运行的结果。
现在还有些问题为解决:
1.没有获取fbx中的transform,所以场景中的transform是错误的。
2.如果出现四边面,uv的计算是错误的,triangles的排序是错误的。
3.当前没有考虑模型有子模型的情况。
这篇关于FBX格式mesh解析与加载(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!