FBX格式mesh解析与加载(二)

2023-11-30 09:48
文章标签 加载 fbx 解析 格式 mesh

本文主要是介绍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解析与加载(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/436571

相关文章

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co