-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
to-renderscene-main.cc
224 lines (191 loc) · 6.74 KB
/
to-renderscene-main.cc
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
// SPDX-License-Identifier: Apache 2.0
// Copyright 2022-Present Light Transport Entertainment Inc.
//
// Command-line tool to convert USD Stage to RenderScene(glTF-like data
// structure)
//
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include "io-util.hh"
#include "pprinter.hh"
#include "prim-pprint.hh"
#include "str-util.hh"
#include "tinyusdz.hh"
#include "tydra/obj-export.hh"
#include "tydra/render-data.hh"
#include "tydra/scene-access.hh"
#include "tydra/shader-network.hh"
#include "tydra/usd-export.hh"
#include "usdShade.hh"
#include "value-pprint.hh"
#include "value-types.hh"
int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " input.usd [OPTIONS].\n";
std::cout << "\n\nOptions\n\n";
std::cout << " --timecode VALUE: Specify timecode value(e.g. 3.14)\n";
std::cout << " --noidxbuild: Do not rebuild vertex indices\n";
std::cout << " --notri: Do not triangulate mesh\n";
std::cout << " --nousdprint: Do not print parsed USD\n";
std::cout
<< " --dumpobj: Dump mesh as wavefront .obj(for visual debugging)\n";
std::cout << " --dumpusd: Dump scene as USD(USDA Ascii)\n";
return EXIT_FAILURE;
}
// When Xform, Mesh, Material, etc. have time-varying values,
// values are evaluated at `timecode` time(except for animation values in
// SkelAnimation)
double timecode = tinyusdz::value::TimeCode::Default();
bool build_indices = true;
bool triangulate = true;
bool export_obj = false;
bool export_usd = false;
bool no_usdprint = false;
std::string filepath;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--notri") == 0) {
triangulate = false;
} else if (strcmp(argv[i], "--noidxbuild") == 0) {
build_indices = false;
} else if (strcmp(argv[i], "--nousdprint") == 0) {
no_usdprint = true;
} else if (strcmp(argv[i], "--dumpobj") == 0) {
export_obj = true;
} else if (strcmp(argv[i], "--dumpusd") == 0) {
export_usd = true;
} else if (strcmp(argv[i], "--timecode") == 0) {
if ((i + 1) >= argc) {
std::cerr << "arg is missing for --timecode flag.\n";
return -1;
}
timecode = std::stod(argv[i + 1]);
std::cout << "Use timecode: " << timecode << "\n";
i++;
} else {
filepath = argv[i];
}
}
std::string warn;
std::string err;
tinyusdz::Stage stage;
if (!tinyusdz::IsUSD(filepath)) {
std::cerr << "File not found or not a USD format: " << filepath << "\n";
}
bool ret = tinyusdz::LoadUSDFromFile(filepath, &stage, &warn, &err);
if (!warn.empty()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}
if (!ret) {
std::cerr << "Failed to load USD file: " << filepath << "\n";
return EXIT_FAILURE;
}
bool is_usdz = tinyusdz::IsUSDZ(filepath);
if (!no_usdprint) {
std::string s = stage.ExportToString();
std::cout << s << "\n";
std::cout << "--------------------------------------"
<< "\n";
}
// RenderScene: Scene graph object which is suited for GL/Vulkan renderer
tinyusdz::tydra::RenderScene render_scene;
tinyusdz::tydra::RenderSceneConverter converter;
tinyusdz::tydra::RenderSceneConverterEnv env(stage);
std::cout << "Triangulate : " << (triangulate ? "true" : "false") << "\n";
env.mesh_config.triangulate = triangulate;
std::cout << "Rebuild vertex indices : " << (build_indices ? "true" : "false")
<< "\n";
env.mesh_config.build_vertex_indices = build_indices;
// Add base directory of .usd file to search path.
std::string usd_basedir = tinyusdz::io::GetBaseDir(filepath);
std::cout << "Add seach path: " << usd_basedir << "\n";
tinyusdz::USDZAsset usdz_asset;
if (is_usdz) {
// Setup AssetResolutionResolver to read a asset(file) from memory.
if (!tinyusdz::ReadUSDZAssetInfoFromFile(filepath, &usdz_asset, &warn,
&err)) {
std::cerr << "Failed to read USDZ assetInfo from file: " << err << "\n";
exit(-1);
}
if (warn.size()) {
std::cout << warn << "\n";
}
tinyusdz::AssetResolutionResolver arr;
// NOTE: Pointer address of usdz_asset must be valid until the call of
// RenderSceneConverter::ConvertToRenderScene.
if (!tinyusdz::SetupUSDZAssetResolution(arr, &usdz_asset)) {
std::cerr << "Failed to setup AssetResolution for USDZ asset\n";
exit(-1);
};
env.asset_resolver = arr;
} else {
env.set_search_paths({usd_basedir});
// TODO: Add example to set user-defined AssetResolutionResolver
// AssetResolutionResolver arr;
// ...
// env.asset_resolver(arr);
}
if (!tinyusdz::value::TimeCode(timecode).is_default()) {
std::cout << "Use timecode : " << timecode << "\n";
}
env.timecode = timecode;
ret = converter.ConvertToRenderScene(env, &render_scene);
if (!ret) {
std::cerr << "Failed to convert USD Stage to RenderScene: \n"
<< converter.GetError() << "\n";
return EXIT_FAILURE;
}
if (converter.GetWarning().size()) {
std::cout << "ConvertToRenderScene warn: " << converter.GetWarning()
<< "\n";
}
std::cout << DumpRenderScene(render_scene) << "\n";
if (export_obj) {
std::cout << "Dump RenderMesh as wavefront .obj\n";
for (size_t i = 0; i < render_scene.meshes.size(); i++) {
std::string obj_str;
std::string mtl_str;
if (!tinyusdz::tydra::export_to_obj(render_scene, i, obj_str, mtl_str,
&warn, &err)) {
std::cerr << "obj export error: " << err << "\n";
exit(-1);
}
std::string obj_filename =
std::to_string(i) + render_scene.meshes[i].prim_name + ".obj";
std::string mtl_filename =
std::to_string(i) + render_scene.meshes[i].prim_name + ".mtl";
{
std::ofstream obj_ofs(obj_filename);
obj_ofs << obj_str;
}
{
std::ofstream mtl_ofs(mtl_filename);
mtl_ofs << mtl_str;
}
std::cout << " Wrote " << obj_filename << "\n";
}
}
if (export_usd) {
std::string ext = tinyusdz::io::GetFileExtension(filepath);
std::string usd_basename = tinyusdz::io::GetBaseFilename(filepath);
std::string usd_filename =
tinyusdz::removeSuffix(usd_basename, ext) + "export.usda";
std::string usda_str;
if (!tinyusdz::tydra::export_to_usda(render_scene, usda_str, &warn, &err)) {
std::cerr << "Failed to export RenderScene to USDA: " << err << "\n";
}
if (warn.size()) {
std::cout << "WARN: " << warn << "\n";
}
{
std::ofstream ofs(usd_filename);
ofs << usda_str;
}
std::cout << "Exported RenderScene as USDA: " << usd_filename << "\n";
}
return EXIT_SUCCESS;
}