Repository navigation
Expand file tree
/
Copy pathsvg_loader.cpp
More file actions
266 lines (206 loc) · 8.64 KB
/
Copy pathsvg_loader.cpp
File metadata and controls
266 lines (206 loc) · 8.64 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
#include "renderer_factory.hpp"
#include "threepp/extras/imgui/RendererSettings.hpp"
#include "threepp/loaders/SVGLoader.hpp"
#include "threepp/threepp.hpp"
using namespace threepp;
namespace {
class MyUI: public ImguiContext {
public:
explicit MyUI(const Canvas& canvas, Renderer& renderer)
: ImguiContext(canvas, renderer), settings_(renderer) {}
[[nodiscard]] bool newSelection() const {
return lastSelectedIndex != selectedIndex;
}
[[nodiscard]] std::string selected() const {
return names[selectedIndex];
}
protected:
void onRender() override {
lastSelectedIndex = selectedIndex;
ImGui::SetNextWindowPos({}, 0, {});
ImGui::SetNextWindowSize({250 * dpiScale(), 0}, 0);
ImGui::Begin("SVGLoader");
if (ImGui::BeginCombo("SVG file", names[selectedIndex].c_str())) {
for (int i = 0; i < names.size(); ++i) {
const bool isSelected = (selectedIndex == i);
if (ImGui::Selectable(names[i].c_str(), isSelected)) {
selectedIndex = i;
}
}
ImGui::EndCombo();
}
ImGui::Separator();
settings_.drawCollapsed();
ImGui::End();
}
private:
RendererSettings settings_;
int lastSelectedIndex = -1;
int selectedIndex = 0;
std::vector<std::string> names{
"tiger.svg", "threejs.svg", "OpenBridge_Heading.svg",
"hexagon.svg", "lineJoinsAndCaps.svg", "energy.svg",
"style-css-inside-defs.svg", "ordering.svg",
"styles.svg", "units.svg", "ellipseTransform.svg"};
};
auto loadSvg(const std::string& name = "tiger.svg") {
SVGLoader loader;
auto svgData = loader.load(std::string(DATA_FOLDER) + "/models/svg/" + name);
auto svg = Group::create();
svg->name = std::filesystem::path(name).stem().string();
svg->scale.multiplyScalar(0.25f);
svg->position.x = -70;
svg->position.y = 70;
svg->scale.y *= -1;
// The meshes share one plane and write no depth, so the document's
// paint order has to be stated: left to the renderer, transparent
// objects are sorted by the depth of their bounding spheres, which
// reshuffles the layers as soon as the camera leaves the head-on view.
int renderOrder = 0;
for (const auto& data : svgData) {
auto fillColor = data.style.fill;
if (fillColor && *fillColor != "none") {
auto material = MeshBasicMaterial::create(
{{"color", data.path.color},
{"opacity", data.style.fillOpacity},
{"transparent", true},
{"side", Side::Double},
{"depthWrite", false}});
const auto shapes = SVGLoader::createShapes(data);
auto geometry = ShapeGeometry::create(shapes);
auto mesh = Mesh::create(geometry, material);
mesh->name = data.style.id;
mesh->visible = data.style.visibility;
mesh->renderOrder = renderOrder++;
svg->add(mesh);
}
auto strokeColor = data.style.stroke;
if (strokeColor && *strokeColor != "none") {
auto strokeMaterial = MeshBasicMaterial::create(
{{"color", Color().setStyle(*data.style.stroke)},
{"opacity", data.style.strokeOpacity},
{"transparent", true},
{"side", Side::Double},
{"depthWrite", false}});
for (const auto& subPath : data.path.subPaths) {
auto strokeGeometry = SVGLoader::pointsToStroke(subPath->getPoints(), data.style);
if (strokeGeometry) {
auto strokeMesh = Mesh::create(strokeGeometry, strokeMaterial);
strokeMesh->renderOrder = renderOrder++;
svg->add(strokeMesh);
}
}
}
}
return svg;
}
void rotateAround(Object3D& o, const Vector3& axis, Vector3 point, float angle) {
Matrix4 rotation;
Matrix4 origin;
Matrix4 temp;
origin.setPosition(point);
rotation.makeRotationAxis(axis, angle);
rotation.premultiply(origin).multiply(temp.copy(origin).invert());
o.applyMatrix4(rotation);
}
}// namespace
int main(int argc, char** argv) {
// Headless capture (dev): svg_loader --shot <name.png> [--svg tiger.svg]
// [--frames N] [--api gl|vulkan] [--cam x y z]. Renders N frames and saves
// via writeFramebuffer, then exits (layering/parity checks). --cam places
// the camera, still looking at the origin: the layering has to hold from
// an angle too, and the default head-on view cannot show that it does.
std::string shotPath;
std::string shotSvg = "tiger.svg";
int shotFrames = 60, shotFrame = 0;
std::optional<Vector3> shotCam;
std::optional<GraphicsAPI> apiOverride;
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
if (a == "--shot" && i + 1 < argc) shotPath = argv[++i];
else if (a == "--svg" && i + 1 < argc) shotSvg = argv[++i];
else if (a == "--frames" && i + 1 < argc) shotFrames = std::atoi(argv[++i]);
else if (a == "--cam" && i + 3 < argc) {
shotCam = Vector3(std::stof(argv[i + 1]), std::stof(argv[i + 2]), std::stof(argv[i + 3]));
i += 3;
} else if (a == "--api" && i + 1 < argc) {
const std::string v = argv[++i];
if (v == "gl") apiOverride = GraphicsAPI::OpenGL;
else if (v == "vulkan" || v == "vk") apiOverride = GraphicsAPI::Vulkan;
}
}
Canvas canvas("SVGLoader", {{"antialiasing", 4}, {"headless", !shotPath.empty()}});
auto renderer = createRenderer(canvas, apiOverride);
renderer->setClearColor(Color::aliceblue);
auto scene = Scene::create();
auto camera = PerspectiveCamera::create(75, canvas.aspect(), 0.1f, 1000);
camera->position.z = 100;
if (shotCam) {
camera->position.copy(*shotCam);
camera->lookAt({0, 0, 0});
}
auto gridHelper = GridHelper::create(160, 10);
gridHelper->rotation.x = math::PI / 2;
gridHelper->position.z = -0.1f;
scene->add(gridHelper);
std::shared_ptr<Object3D> svg;
canvas.onWindowResize([&](WindowSize size) {
camera->aspect = size.aspect();
camera->updateProjectionMatrix();
renderer->setSize(size);
});
OrbitControls controls{*camera, canvas};
MyUI ui(canvas, *renderer);
IOCapture capture{};
capture.preventMouseEvent = [] {
return ImGui::GetIO().WantCaptureMouse;
};
capture.preventScrollEvent = [] {
return ImGui::GetIO().WantCaptureMouse;
};
canvas.setIOCapture(&capture);
Box3 bb;
auto box3Helper = Box3Helper::create(bb, Color::grey);
box3Helper->material()->opacity = 0.2f;
box3Helper->material()->transparent = true;
scene->add(box3Helper);
Vector3 center;
Clock clock;
canvas.animate([&] {
renderer->render(*scene, *camera);
if (!shotPath.empty()) {
if (!svg) {
svg = loadSvg(shotSvg);
bb.setFromObject(*svg);
scene->add(svg);
}
if (++shotFrame >= shotFrames) {
renderer->writeFramebuffer(shotPath);
std::cout << "wrote " << shotPath << std::endl;
std::exit(0);
}
} else if (ui.newSelection()) {
if (svg) {
svg->removeFromParent();
}
svg = loadSvg(ui.selected());
bb.setFromObject(*svg);
if (svg->name == "OpenBridge_Heading") {
bb.getCenter(center);
svg->worldToLocal(center);
center.sub({-1.25, -4.375, 0});
}
scene->add(svg);
}
float dt = clock.getDelta();
if (svg && svg->name == "OpenBridge_Heading") {
auto cog = svg->getObjectByName("COG_2");
rotateAround(*cog, {0, 0, 1}, center, -0.3f * dt);
auto hdg = svg->getObjectByName("HDG_2");
rotateAround(*hdg, {0, 0, 1}, center, 0.5f * dt);
auto sp = svg->getObjectByName("Shape");
rotateAround(*sp, {0, 0, 1}, center, 0.1f * dt);
}
if (shotPath.empty()) ui.render();
});
}