Skip to content

Instantly share code, notes, and snippets.

@fereria
Created March 6, 2020 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fereria/2c773a4d16001fc67195c19ab402b611 to your computer and use it in GitHub Desktop.
Save fereria/2c773a4d16001fc67195c19ab402b611 to your computer and use it in GitHub Desktop.
#define NOMINMAX
#include <iostream>
#include <vector>
#include <pxr/base/gf/vec3f.h>
#include <pxr/base/tf/token.h>
#include <pxr/base/vt/array.h>
#include <pxr/base/vt/value.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/mesh.h>
#include <pxr/usd/usdShade/material.h>
#include <pxr/usd/usdShade/materialBindingAPI.h>
#include <pxr/usd/usdShade/shader.h>
#include "cppBasic/classTest.h"
#include "usd/usdTest.h"
using namespace std;
using namespace pxr;
void
printData(USDMaterialData data) {
cout << data.nodeName << endl;
}
void
main() {
auto stage = UsdStage::CreateInMemory();
ClassTest* hoge = new ClassTest("D:/cube.fbx");
FbxArray<FbxMesh*>* meshArray = new FbxArray<FbxMesh*>();
hoge->getAllMesh(*meshArray);
vector<USDMaterialData>* materialsData = new vector<USDMaterialData>();
hoge->getMaterials(*materialsData);
for_each(materialsData->begin(), materialsData->end(), [stage](USDMaterialData data) {
// Materials/Shaderを作る
auto mat = UsdShadeMaterial::Define(stage, SdfPath("/mat/" + data.nodeName + "Mat"));
auto shader = UsdShadeShader::Define(stage, SdfPath("/mat/" + data.nodeName + "Mat/" + data.nodeName));
// カラーとかをセット
auto color = shader.CreateInput(TfToken("diffuseColor"), SdfValueTypeNames->Color3f);
color.ConnectToSource(shader, TfToken("rgb"));
color.Set(GfVec3f(data.diffuseColor[0], data.diffuseColor[1], data.diffuseColor[2]));
mat.CreateSurfaceOutput().ConnectToSource(shader, TfToken("surface"));
});
// 取得してきた情報でPrimを作る
for (int i = 0; i < meshArray->Size(); i++) {
USDMeshData* data = new USDMeshData();
hoge->getMeshData(meshArray->GetAt(i), data);
SdfPath meshPath = SdfPath(data->sdfPath);
UsdGeomMesh usdMesh = UsdGeomMesh::Define(stage, meshPath);
usdMesh.CreatePointsAttr(VtValue(data->vtxPoints));
usdMesh.CreateFaceVertexCountsAttr(VtValue(data->polyVtx));
usdMesh.CreateFaceVertexIndicesAttr(VtValue(data->polyIndex));
}
stage->GetRootLayer()->Export("D:/test.usda");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment