forked from markaren/threepp
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgltf_loader.cpp
More file actions
197 lines (172 loc) · 7.58 KB
/
Copy pathgltf_loader.cpp
File metadata and controls
197 lines (172 loc) · 7.58 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
#include "threepp/animation/AnimationMixer.hpp"
#include "threepp/helpers/SkeletonHelper.hpp"
#include "threepp/loaders/GLTFLoader.hpp"
#include "threepp/loaders/RGBELoader.hpp"
#include "threepp/threepp.hpp"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <iostream>
using namespace threepp;
int main(int argc, char** argv) {
Canvas canvas("GLTF Demo");
auto renderer = createRenderer(canvas);
renderer->toneMapping = ToneMapping::ACESFilmic;
renderer->shadowMap().enabled = true;
auto scene = Scene::create();
scene->background = Color::aliceblue;
auto camera = PerspectiveCamera::create(60, canvas.aspect(), 0.1f, 100.f);
camera->position.set(0, 2, -4);
OrbitControls controls{*camera, canvas};
RGBELoader hdrLoader;
if (auto hdrTexture = hdrLoader.load(std::string(DATA_FOLDER) + "/textures/env/san_giuseppe_bridge/san_giuseppe_bridge_4k.hdr" )) {
scene->background = hdrTexture;
scene->environment = hdrTexture;
}
auto dirLight = DirectionalLight::create(0xffffff, 1.0f);
dirLight->position.set(1, 1, -1);
dirLight->castShadow = true;
scene->add(dirLight);
// gltf_loader [model.gltf|.glb] [--shot out.png] [--frames N] [--zoom f]
// --shot: headless capture — frame the loaded model from its bounding box,
// render N warm-up frames (TAA/denoiser converge), write one PNG, exit.
std::string modelPath = std::string(DATA_FOLDER) + "/models/gltf/Soldier.glb";
std::string shotPath;
int shotFrames = 240;
float shotZoom = 1.f; // <1 = closer (distance scales with it)
bool shotFront = false;// view head-on along the model's thinnest axis (test walls/panels)
bool nonDefaultModelPath = false;
std::string shotAxis; // explicit --front axis: x, -x, z, -z
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--shot") == 0 && i + 1 < argc) {
shotPath = argv[++i];
continue;
}
if (std::strcmp(argv[i], "--frames") == 0 && i + 1 < argc) {
shotFrames = std::atoi(argv[++i]);
continue;
}
if (std::strcmp(argv[i], "--zoom") == 0 && i + 1 < argc) {
shotZoom = static_cast<float>(std::atof(argv[++i]));
continue;
}
if (std::strcmp(argv[i], "--front") == 0) {
shotFront = true;
// optional axis token: x, -x, z, -z (default: thinnest bbox axis)
if (i + 1 < argc && (argv[i + 1][0] == 'x' || argv[i + 1][0] == 'z' || argv[i + 1][0] == '-')) {
shotAxis = argv[++i];
}
continue;
}
std::filesystem::path arg = argv[i];
if (std::filesystem::exists(arg) && std::filesystem::is_regular_file(arg)) {
auto ext = arg.extension().string();
std::ranges::transform(ext, ext.begin(), [](unsigned char c) { return std::tolower(c); });
if (ext == ".gltf" || ext == ".glb") {
modelPath = arg.string();
nonDefaultModelPath = true;
} else {
std::cerr << "Ignoring argument (not a .gltf/.glb file): " << arg << "\n";
}
} else {
std::cerr << "Ignoring argument (file not found): " << arg << "\n";
}
}
const bool capturing = !shotPath.empty();
int shotFrame = 0;
std::cout << "Loading: " << modelPath << "\n";
GLTFLoader loader;
auto result = loader.load(modelPath);
result->scene->traverseType<Mesh>([&](Mesh& mesh) {
mesh.castShadow = true;
});
if (!result) {
std::cerr << "Failed to load model\n";
return 1;
}
scene->add(result->scene);
// Scale the camera frustum and default framing to the loaded model's
// bounding box so near/far clipping and camera distance stay sane
// whether the model is centimeter- or building-scale.
Box3 bbox;
bbox.setFromObject(*result->scene);
Vector3 modelCenter, modelSize;
bbox.getCenter(modelCenter);
bbox.getSize(modelSize);
const float modelMaxDim = std::max({modelSize.x, modelSize.y, modelSize.z, 0.01f});
camera->nearPlane = std::max(0.001f, modelMaxDim * 0.005f);
camera->farPlane = modelMaxDim * 100.f;
camera->updateProjectionMatrix();
controls.target.copy(modelCenter);
if (nonDefaultModelPath) {
// The default Soldier scene keeps its hand-tuned (0, 2, -4) framing;
// custom models get framed from their own bounding box instead.
camera->position.set(modelCenter.x + 0.55f * modelMaxDim,
modelCenter.y + 0.30f * modelMaxDim,
modelCenter.z + 0.55f * modelMaxDim);
camera->lookAt(modelCenter);
}
controls.update();
std::unique_ptr<AnimationMixer> mixer;
if (!result->animations.empty()) {
std::cout << "Loaded " << result->animations.size() << " animation clip(s)." << std::endl;
mixer = std::make_unique<AnimationMixer>(*result->scene);
mixer->clipAction(result->animations.front())->play();
}
if (!nonDefaultModelPath) {
auto skeletonHelper = SkeletonHelper::create(*result->scene);
skeletonHelper->materialAs<LineBasicMaterial>()->linewidth = 2;
scene->add(skeletonHelper);
auto floor = Mesh::create(
BoxGeometry::create(10, 0.1f, 10),
MeshStandardMaterial::create(MeshStandardMaterial::Params{}.color(Color::lightgray)));
floor->position.set(0, 0, 0);
floor->receiveShadow = true;
scene->add(floor);
}
if (capturing) {
// Frame the MODEL (not the whole scene — the helper floor would widen
// the bounds) from a low oblique view.
const float d = modelMaxDim * shotZoom;
if (shotFront) {
// Head-on along the thinnest bbox axis (a wall/panel's facing),
// or the explicitly requested axis.
Vector3 dir = (modelSize.z <= modelSize.x) ? Vector3(0, 0, 1) : Vector3(1, 0, 0);
if (shotAxis == "x") dir.set(1, 0, 0);
else if (shotAxis == "-x") dir.set(-1, 0, 0);
else if (shotAxis == "z") dir.set(0, 0, 1);
else if (shotAxis == "-z") dir.set(0, 0, -1);
camera->position.copy(modelCenter).addScaledVector(dir, 0.9f * d);
camera->lookAt(modelCenter);
std::cout << "[shot] axis=" << (shotAxis.empty() ? "auto" : shotAxis)
<< " center=(" << modelCenter.x << "," << modelCenter.y << "," << modelCenter.z
<< ") size=(" << modelSize.x << "," << modelSize.y << "," << modelSize.z
<< ") cam=(" << camera->position.x << "," << camera->position.y
<< "," << camera->position.z << ")\n";
} else {
camera->position.set(modelCenter.x + 0.55f * d,
modelCenter.y + 0.30f * d,
modelCenter.z + 0.55f * d);
camera->lookAt(Vector3(modelCenter.x, modelCenter.y + 0.05f * d, modelCenter.z));
}
camera->updateMatrixWorld();
}
canvas.onWindowResize([&](WindowSize newSize) {
renderer->setSize(newSize);
camera->aspect = newSize.aspect();
camera->updateProjectionMatrix();
});
Clock clock;
canvas.animate([&] {
renderer->render(*scene, *camera);
if (mixer) mixer->update(clock.getDelta());
if (capturing && ++shotFrame >= shotFrames) {
renderer->writeFramebuffer(shotPath);
std::cout << "wrote " << shotPath << "\n";
std::exit(0);
}
});
return 0;
}