最近项目内在优化资源,其中模型中的优化有这些方面:模型网格优化
其中,模型的顶点色、法线、切线、uv3/uv4信息都可以通过AutoDesk的FBX SDK直接修改FBX模型文件进行清除。
1.下载安装SDK
没啥可说的,直接去官网下载吧~可以找找适合自己的VS版本的SDK~
2.打开示例工程
直接使用VS打开项目C:\Program Files\Autodesk\FBX\FBX SDK\2019.0\samples(这是默认安装目录~这个文件夹里面有一堆示例代码工程,可以打开学习)
3.构建逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
// 命令行调用入口函数 // argc 为参数个数 // argv 保存命令行参数的字符串指针,其中第0个参数是程序的全名,以后的参数为命令行后面跟的用户输入的参数。 // argv参数是字符串指针数组,其各元素值为命令行中各字符串(参数均按字符串处理)的首地址。 指针数组的长度即为参数个数argc。数组元素初值由系统自动赋予。 int main(int argc, char** argv) { if (argc != 4) { printf("参数格式不对"); return 0; } sInputFile = argv[1]; sOutputFile = argv[2]; sOperator = argv[3]; //FBX SDK Default Manager FbxManager* lSdkManager = NULL; //Scene to load from file FbxScene* lScene = NULL; // Prepare the FBX SDK. InitializeSdkObjects(lSdkManager, lScene); // Load the scene. bool lResult = LoadScene(lSdkManager, lScene, sInputFile); if (lResult == false) { FBXSDK_printf("\n\nAn error occurred while loading the scene..."); } else { bool bSave = false; auto rootNode = lScene->GetRootNode(); for (int i = 0; i < rootNode->GetChildCount(true); i++) { auto lNodeOfInterest = rootNode->GetChild(i); if (lNodeOfInterest) { FbxMesh* lMeshOFInterest = lNodeOfInterest->GetMesh(); if (lMeshOFInterest) { bSave = true; std::cout << "mesh :" << lNodeOfInterest->GetNameOnly() << std::endl; if(FbxString(sOperator) == "-color") RemoveColors(lMeshOFInterest); else if (FbxString(sOperator) == "-uv") RemoveUV34(lMeshOFInterest); else if (FbxString(sOperator) == "-tangent") RemoveTangent(lMeshOFInterest); else if (FbxString(sOperator) == "-normal") RemoveNormal(lMeshOFInterest); } } } if (bSave) { // !< 根据设置转换为相应格式 //SaveScene(lSdkManager, lScene, sOutputFile); // !< 转为二进制文件 SaveScene(lSdkManager, lScene, sOutputFile, 0); } } // Destroy all objects created by the FBX SDK. DestroySdkObjects(lSdkManager, lResult); return 0; } |
命令行程序在调用时main会自动变成两个参数,举个栗子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<iostream> using namespace std; int main(int argc, char* argv[]) { int i; for(i=0;i<argc;i++) { cout<<argv[i]<<endl; } cout<<"argc = " << argc << " ; " << "i = " << i << endl; return 0; } //输出: ./a.out a b c argc = 4 ; i = 4 |
4.打包生成可执行文件exe
这也没啥说的,右击解决方案生成就行了。然后看一下生成到哪了~
5.Unity调用exe进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
[MenuItem("OptimizationTools/去除模型顶点色exe")] static void CFBXUVColor() { string exeFullPath = System.Environment.CurrentDirectory; System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.FileName = exeFullPath + @"\OptimizationTool\ClearUVClr.exe"; string RootPath = Application.dataPath; RootPath = RootPath.Substring(0, RootPath.LastIndexOf("/")) + "/"; var objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets).Where(go => AssetDatabase.GetAssetPath(go).ToLower().Contains(".fbx")); foreach (Object o in objs) { EditorUtility.UnloadUnusedAssetsImmediate(); string fbxPath = AssetDatabase.GetAssetPath(o); GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(fbxPath); ; if (go != null) { bool bNeedAdd = false; MeshFilter[] mrgrp = go.GetComponentsInChildren<MeshFilter>(); for (int i = 0; i < mrgrp.Length; ++i) { if (mrgrp[i] != null && mrgrp[i].sharedMesh != null) { if (mrgrp[i].sharedMesh.uv3.Length != 0 || mrgrp[i].sharedMesh.uv4.Length != 0) { bNeedAdd = true; break; } if (mrgrp[i].sharedMesh.colors.Length != 0) { bNeedAdd = true; break; } } } if (bNeedAdd) { string src = RootPath + fbxPath; string dst = RootPath + fbxPath; psi.Arguments = src + " " + dst + " -color"; System.Diagnostics.Process.Start(psi); } } } } |