Created
February 12, 2020 14:50
-
-
Save fereria/1e277ffcd08717fbe7ff5ef611f71b37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define NOMINMAX | |
#include <pxr/base/vt/array.h> | |
#include <pxr/base/gf/vec3f.h> | |
#include <pxr/base/vt/value.h> | |
#include <pxr/base/tf/token.h> | |
#include <pxr/usd/usd/stage.h> | |
#include <pxr/usd/usdGeom/mesh.h> | |
#include <pxr/usd/sdf/path.h> | |
#include "fbxsdk.h" | |
#include <iostream> | |
using namespace std; | |
using namespace pxr; | |
template <typename TYPE> | |
void printNode(TYPE *node) | |
{ | |
const char *nodeName = node->GetName(); | |
cout << nodeName << endl; | |
} | |
void usdVtArrayTest() | |
{ | |
char *fbxFile = "D:/work/data_sample/xbot.fbx"; | |
FbxManager *manager = FbxManager::Create(); | |
FbxIOSettings *ioSettings = FbxIOSettings::Create(manager, IOSROOT); | |
FbxImporter *importer = FbxImporter::Create(manager, ""); | |
importer->Initialize(fbxFile, -1, manager->GetIOSettings()); | |
// Fbxにシーンをロード | |
FbxScene *scene = FbxScene::Create(manager, "scene"); | |
importer->Import(scene); | |
importer->Destroy(); | |
auto stage = UsdStage::CreateInMemory(); | |
//シーン内のMeshを取得 | |
int meshCount = scene->GetSrcObjectCount<FbxMesh>(); | |
// Meshの数分出力処理 | |
for (int i = 0; i < meshCount; i++) | |
{ | |
FbxMesh *mesh = scene->GetSrcObject<FbxMesh>(i); | |
int polyCount = mesh->GetPolygonCount(); // Polyの数 | |
int vtxCount = mesh->GetControlPointsCount(); // Vertexの数 | |
FbxVector4 *position = mesh->GetControlPoints(); // 頂点座標 | |
// USDにPrimを作る | |
SdfPath meshPath = SdfPath("/mesh").AppendChild(TfToken(mesh->GetName())); | |
auto usdMesh = UsdGeomMesh::Define(stage, meshPath); | |
// USDにセットするためのArrayを作る | |
VtArray<GfVec3f> points(vtxCount); | |
for (int i = 0; i < vtxCount; i++) | |
{ | |
points[i][0] = position[i][0]; | |
points[i][1] = position[i][1]; | |
points[i][2] = position[i][2]; | |
} | |
// 頂点をセット | |
usdMesh.CreatePointsAttr(VtValue(points)); | |
// トポロジと1Polyあたりの頂点数を取得 | |
VtArray<int> polyVtxAttr(polyCount); | |
VtArray<int> polyIndex(mesh->GetPolygonVertexCount()); | |
int count = 0; | |
for (int i = 0; i < polyCount; i++) | |
{ | |
polyVtxAttr[i] = mesh->GetPolygonSize(i); | |
for (int j = 0; j < polyVtxAttr[i]; j++) | |
{ | |
int index = mesh->GetPolygonVertex(i, j); | |
polyIndex[count] = index; | |
count++; | |
} | |
} | |
usdMesh.CreateFaceVertexCountsAttr(VtValue(polyVtxAttr)); | |
usdMesh.CreateFaceVertexIndicesAttr(VtValue(polyIndex)); | |
} | |
// 保存 | |
stage->GetRootLayer()->Export("D:/test.usda"); | |
manager->Destroy(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment