-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfbx_writer.cpp
More file actions
290 lines (244 loc) · 13.5 KB
/
fbx_writer.cpp
File metadata and controls
290 lines (244 loc) · 13.5 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdexcept>
#include <filesystem>
#include <cassert>
#include <plateau/mesh_writer/fbx_writer.h>
#include <fbxsdk.h>
#include <set>
namespace fs = std::filesystem;
namespace {
void copyTexture(const std::string& fbx_path, const std::string& texture_url) {
auto src_path = fs::u8path(texture_url);
auto t_path = src_path.filename();
auto dst_path = fs::u8path(fbx_path).parent_path();
dst_path /= src_path.parent_path().filename();
create_directories(dst_path);
dst_path /= t_path;
constexpr auto copy_options = fs::copy_options::skip_existing;
copy(src_path, dst_path, copy_options);
}
}
namespace plateau::meshWriter {
class FbxWriterImpl {
public:
FbxWriterImpl()
: manager_(FbxManager::Create()) {
}
~FbxWriterImpl() {
manager_->Destroy();
}
bool write(const std::string& fbx_file_path, const plateau::polygonMesh::Model& model, const FbxWriteOptions& options) {
FbxIOSettings* ios = FbxIOSettings::Create(manager_, IOSROOT);
manager_->SetIOSettings(ios);
ios->SetBoolProp(EXP_FBX_MATERIAL, true);
ios->SetBoolProp(EXP_FBX_TEXTURE, true);
ios->SetBoolProp(EXP_FBX_SHAPE, true);
ios->SetBoolProp(EXP_FBX_GOBO, true);
ios->SetBoolProp(EXP_FBX_GLOBAL_SETTINGS, true);
ios->SetBoolProp(EXP_ASCIIFBX, options.file_format == FbxFileFormat::ASCII);
const auto fbx_scene = FbxScene::Create(manager_, "");
FbxAxisSystem axis_system;
switch (options.coordinate_system) {
case geometry::CoordinateSystem::ENU:
axis_system = FbxAxisSystem(
FbxAxisSystem::EUpVector::eZAxis,
FbxAxisSystem::EFrontVector::eParityEven,
FbxAxisSystem::eRightHanded);
break;
case geometry::CoordinateSystem::ESU:
axis_system = FbxAxisSystem(
FbxAxisSystem::EUpVector::eZAxis,
FbxAxisSystem::EFrontVector::eParityEven,
FbxAxisSystem::eLeftHanded);
break;
case geometry::CoordinateSystem::WUN:
axis_system = FbxAxisSystem(
FbxAxisSystem::EUpVector::eYAxis,
FbxAxisSystem::EFrontVector::eParityEven,
FbxAxisSystem::eRightHanded);
break;
case geometry::CoordinateSystem::EUN:
axis_system = FbxAxisSystem(
FbxAxisSystem::EUpVector::eYAxis,
FbxAxisSystem::EFrontVector::eParityEven,
FbxAxisSystem::eLeftHanded);
break;
}
axis_system.ConvertScene(fbx_scene);
// create scene info
FbxDocumentInfo* SceneInfo = FbxDocumentInfo::Create(manager_, "SceneInfo");
fbx_scene->SetSceneInfo(SceneInfo);
std::filesystem::path path = std::filesystem::u8path(fbx_file_path);
if (path.is_relative()) {
auto pathCurrent = std::filesystem::current_path();
pathCurrent /= path;
pathCurrent.swap(path);
}
std::filesystem::path pathFile = path.filename();
for (int i = 0; i < model.getRootNodeCount(); ++i) {
const auto& node = model.getRootNodeAt(i);
processNodeRecursive(node, fbx_scene->GetRootNode(), fbx_scene, fs::absolute(path).u8string());
}
const auto exporter = FbxExporter::Create(manager_, "");
int FileFormat =
options.file_format == FbxFileFormat::ASCII
? manager_->GetIOPluginRegistry()->FindWriterIDByDescription("FBX ascii (*.fbx)")
: manager_->GetIOPluginRegistry()->GetNativeWriterFormat();
if (!exporter->Initialize(fbx_file_path.c_str(), FileFormat, manager_->GetIOSettings())) {
return false;
}
if (!exporter->Export(fbx_scene)) {
return false;
}
exporter->Destroy();
// テクスチャファイルコピー
for (const auto& texture_path : required_textures_) {
copyTexture(fs::absolute(path).u8string(), texture_path);
}
return true;
}
void processNodeRecursive(const polygonMesh::Node& node, FbxNode* parent_fbx_node, FbxScene* fbx_scene, const std::string& fbx_path) {
const auto fbx_node = FbxNode::Create(fbx_scene, node.getName().c_str());
parent_fbx_node->AddChild(fbx_node);
// ノード位置をローカル座標で指定
auto local_pos = node.getLocalPosition();
fbx_node->LclTranslation.Set(FbxDouble3(local_pos.x, local_pos.y, local_pos.z));
// ノードのローカルスケールを指定
auto local_scale = node.getLocalScale();
fbx_node->LclScaling.Set(FbxDouble3(local_scale.x, local_scale.y, local_scale.z));
// ノードのローカル向きを指定
// 参考 : https://stackoverflow.com/questions/47753266/fbxsdk-using-quaternions-to-set-rotation-keys
auto local_rotation = node.getLocalRotation();
auto local_quaternion = FbxQuaternion(local_rotation.getX(), local_rotation.getY(), local_rotation.getZ(), local_rotation.getW());
FbxAMatrix rotation_matrix;
rotation_matrix.SetQ(local_quaternion);
auto rotation_euler = rotation_matrix.GetR();
fbx_node->LclRotation.Set(rotation_euler);
const auto mesh = node.getMesh();
if (mesh != nullptr)
addMesh(*mesh, fbx_scene, fbx_node, fbx_path);
for (size_t i = 0; i < node.getChildCount(); ++i) {
const auto& child_node = node.getChildAt(i);
processNodeRecursive(child_node, fbx_node, fbx_scene, fbx_path);
}
}
void addMesh(const polygonMesh::Mesh& mesh, FbxScene* fbx_scene, FbxNode* fbx_node, const std::string& fbx_path) {
const auto fbx_mesh = FbxMesh::Create(fbx_scene, "");
// Create control points.
unsigned VertCount(mesh.getVertices().size());
fbx_mesh->InitControlPoints(VertCount);
FbxVector4* ControlPoints = fbx_mesh->GetControlPoints();
// Set the normals on Layer 0.
FbxLayer* Layer = fbx_mesh->GetLayer(0);
if (Layer == nullptr) {
fbx_mesh->CreateLayer();
Layer = fbx_mesh->GetLayer(0);
}
FbxLayerElementNormal* LayerElementNormal = FbxLayerElementNormal::Create(fbx_mesh, "");
LayerElementNormal->SetMappingMode(FbxLayerElement::eByControlPoint);
// Set the normal values for every control point.
LayerElementNormal->SetReferenceMode(FbxLayerElement::eDirect);
// Create UV for Diffuse channel.
// FbxLayerElementUV* UVDiffuseLayer = FbxLayerElementUV::Create(fbx_mesh, "DiffuseUV");
// UVDiffuseLayer->SetMappingMode(FbxLayerElement::eByControlPoint);
// UVDiffuseLayer->SetReferenceMode(FbxLayerElement::eDirect);
// Layer->SetUVs(UVDiffuseLayer, FbxLayerElement::eTextureDiffuse);
std::vector<FbxGeometryElementUV*> UVs = {
fbx_mesh->CreateElementUV("DiffuseUV"),
fbx_mesh->CreateElementUV("UV2"),
fbx_mesh->CreateElementUV("UV3"),
fbx_mesh->CreateElementUV("UV4")
};
for (auto uv : UVs) {
uv->SetMappingMode(FbxGeometryElement::eByControlPoint);
uv->SetReferenceMode(FbxGeometryElement::eDirect);
}
for (unsigned VertexIdx = 0; VertexIdx < VertCount; ++VertexIdx) {
const auto& vertex = mesh.getVertices()[VertexIdx];
const auto& uv = mesh.getUV1()[VertexIdx];
ControlPoints[VertexIdx] = FbxVector4(vertex.x, vertex.y, vertex.z);
UVs.at(0)->GetDirectArray().Add(FbxVector2(uv.x, uv.y));
UVs.at(1)->GetDirectArray().Add(FbxVector2(0, 0));
UVs.at(2)->GetDirectArray().Add(FbxVector2(0, 0));
const auto& src_uv4 = mesh.getUV4();
if (VertexIdx < src_uv4.size()) {
auto& uv4 = src_uv4.at(VertexIdx);
UVs.at(3)->GetDirectArray().Add(FbxVector2(uv4.x, uv4.y));
}
}
for (auto uv : UVs) {
uv->GetIndexArray().SetCount(VertCount);
}
// 頂点カラーをセットします。
auto dst_vertex_colors = fbx_mesh->CreateElementVertexColor();
dst_vertex_colors->SetMappingMode(FbxGeometryElement::eByControlPoint);
dst_vertex_colors->SetReferenceMode(FbxGeometryElement::eDirect);
auto src_vert_colors = mesh.getVertexColors();
for(auto src_color : src_vert_colors) {
dst_vertex_colors->GetDirectArray().Add(FbxColor(src_color.r, src_color.g, src_color.b));
}
Layer->SetVertexColors(dst_vertex_colors);
// Build list of Indices re-used multiple times to lookup Normals, UVs, other per face vertex information
const auto& indices = mesh.getIndices();
FbxLayerElementMaterial* MatLayer = FbxLayerElementMaterial::Create(fbx_mesh, "");
MatLayer->SetMappingMode(FbxLayerElement::eByPolygon);
MatLayer->SetReferenceMode(FbxLayerElement::eIndexToDirect);
Layer->SetMaterials(MatLayer);
const auto& sub_meshes = mesh.getSubMeshes();
for (const auto& sub_mesh : sub_meshes) {
FbxSurfaceMaterial* fbx_material;
const auto& texture_path = sub_mesh.getTexturePath();
if (texture_path.empty()) {
// マテリアル名の末尾は "-(gameMaterialID)" である必要があります。これはUnityの「Assetsへ変換」機能を動かすために必要です。
FbxString default_material_name = ("Default-Material-" + std::to_string(sub_mesh.getGameMaterialID())).c_str();
fbx_material = fbx_scene->GetMaterial(default_material_name);
if (!fbx_material) {
fbx_material = FbxSurfacePhong::Create(fbx_scene, default_material_name.Buffer());
((FbxSurfacePhong*)fbx_material)->Diffuse.Set(FbxDouble3(0.72, 0.72, 0.72));
((FbxSurfacePhong*)fbx_material)->DiffuseFactor.Set(1.);
}
} else {
FbxString material_name = (fs::u8path(texture_path).filename().replace_extension("").u8string() + "-" + std::to_string(sub_mesh.getGameMaterialID())).c_str();
fbx_material = fbx_scene->GetMaterial(material_name); // 同じマテリアルがすでにあればそれを利用します。
if(!fbx_material) {
// 同じマテリアルがなければ生成します。
fbx_material = FbxSurfacePhong::Create(fbx_scene, material_name);
FbxProperty FbxColorProperty = fbx_material->FindProperty(FbxSurfaceMaterial::sDiffuse);
if (FbxColorProperty.IsValid()) {
//Create a fbx property
FbxFileTexture* lTexture = FbxFileTexture::Create(fbx_scene, fs::u8path(texture_path).filename().u8string().c_str());
auto dst_path = fs::u8path(fbx_path).parent_path();
dst_path /= fs::u8path(texture_path).parent_path().filename();
dst_path /= fs::u8path(texture_path).filename();
lTexture->SetFileName(dst_path.u8string().c_str());
lTexture->SetTextureUse(FbxTexture::eStandard);
lTexture->SetMappingType(FbxTexture::eUV);
lTexture->ConnectDstProperty(FbxColorProperty);
required_textures_.insert(texture_path);
}
}
}
auto material_index = fbx_node->GetMaterialIndex(fbx_material->GetName());
if(material_index < 0) {
material_index = fbx_node->AddMaterial(fbx_material);
}
const unsigned triangle_count = (sub_mesh.getEndIndex() - sub_mesh.getStartIndex() + 1) / 3;
// Copy over the index buffer into the FBX polygons set.
for (unsigned triangle_index = 0; triangle_index < triangle_count; ++triangle_index) {
fbx_mesh->BeginPolygon(material_index);
for (unsigned point_index = 0; point_index < 3; point_index++) {
const auto vert_index = indices[sub_mesh.getStartIndex() + triangle_index * 3 + point_index];
fbx_mesh->AddPolygon(vert_index);
}
fbx_mesh->EndPolygon();
}
fbx_node->SetNodeAttribute(fbx_mesh);
}
}
private:
FbxManager* manager_;
std::set<std::string> required_textures_;
};
bool FbxWriter::write(const std::string& fbx_file_path, const plateau::polygonMesh::Model& model, const FbxWriteOptions& options) {
return FbxWriterImpl().write(fbx_file_path, model, options);
}
}