-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathdemo.cpp
More file actions
180 lines (137 loc) · 5.45 KB
/
Copy pathdemo.cpp
File metadata and controls
180 lines (137 loc) · 5.45 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
#include "threepp/canvas/Monitor.hpp"
#include "threepp/extras/imgui/ImguiContext.hpp"
#include "threepp/objects/TextSprite.hpp"
#include "threepp/threepp.hpp"
using namespace threepp;
struct MyGui final: ImguiContext {
bool colorChanged = false;
explicit MyGui(const Canvas& canvas, Renderer& renderer, const MeshBasicMaterial& m)
: ImguiContext(canvas, renderer) {
colorBuf_[0] = m.color.r;
colorBuf_[1] = m.color.g;
colorBuf_[2] = m.color.b;
colorBuf_[3] = m.opacity;
}
void onRender() override {
ImGui::SetNextWindowPos({0, 0}, 0, {0, 0});
ImGui::SetNextWindowSize({0, 0}, 0);
ImGui::Begin("Plane transform");
ImGui::SliderFloat3("position", posBuf_.data(), -5.f, 5.f);
ImGui::SliderFloat3("rotation", eulerBuf_.data(), -180.f, 180.f);
ImGui::ColorEdit4("Color", colorBuf_.data());
colorChanged = ImGui::IsItemEdited();
ImGui::End();
}
const Vector3& position() {
pos_.fromArray(posBuf_);
return pos_;
}
const Euler& rotation() {
euler_.set(math::DEG2RAD * eulerBuf_[0], math::DEG2RAD * eulerBuf_[1], math::DEG2RAD * eulerBuf_[2]);
return euler_;
}
[[nodiscard]] const std::array<float, 4>& color() const {
return colorBuf_;
}
private:
Vector3 pos_;
Euler euler_;
std::array<float, 3> posBuf_{};
std::array<float, 3> eulerBuf_{};
std::array<float, 4> colorBuf_{0, 0, 0, 1};
};
auto createBox() {
const auto boxGeometry = BoxGeometry::create();
const auto boxMaterial = MeshBasicMaterial::create();
boxMaterial->color.setRGB(1, 0, 0);
boxMaterial->transparent = true;
boxMaterial->opacity = 0.1f;
auto box = Mesh::create(boxGeometry, boxMaterial);
auto wiredBox = LineSegments::create(WireframeGeometry::create(*boxGeometry));
wiredBox->materialAs<LineBasicMaterial>()->depthTest = false;
wiredBox->materialAs<LineBasicMaterial>()->color = Color::gray;
box->add(wiredBox);
return box;
}
auto createSphere() {
const auto sphereGeometry = SphereGeometry::create(0.5f);
const auto sphereMaterial = MeshBasicMaterial::create();
sphereMaterial->color.setHex(0x00ff00);
sphereMaterial->wireframe = true;
auto sphere = Mesh::create(sphereGeometry, sphereMaterial);
sphere->position.setX(-1);
return sphere;
}
auto createPlane() {
const auto planeGeometry = PlaneGeometry::create(5, 5);
const auto planeMaterial = MeshBasicMaterial::create();
planeMaterial->color.setHex(Color::yellow);
planeMaterial->transparent = true;
planeMaterial->opacity = 0.5f;
planeMaterial->side = Side::Double;
auto plane = Mesh::create(planeGeometry, planeMaterial);
plane->position.setZ(-2);
return plane;
}
int main() {
Canvas canvas("threepp demo", {{"aa", 4}});
auto renderer = createRenderer(canvas);
auto scene = Scene::create();
scene->background = Color::aliceblue;
auto camera = PerspectiveCamera::create(75, canvas.aspect(), 0.1f, 1000);
camera->position.z = 5;
auto box = createBox();
scene->add(box);
auto sphere = createSphere();
box->add(sphere);
auto plane = createPlane();
auto planeMaterial = plane->materialAs<MeshBasicMaterial>();
scene->add(plane);
FontLoader fontLoader;
const auto font1 = fontLoader.defaultFont();
const auto font2 = *fontLoader.load(std::string(DATA_FOLDER) + "/fonts/typeface/gentilis_regular.typeface.json");
// Screen-space text labels — Sprite::screenSpace + screenAnchor route
// these through the renderer's ortho overlay automatically, no separate
// HUD scene / camera / autoClear ritual. Anchor (0,0) = bottom-left of
// the viewport; (1,1) = top-right. position.xy is the pixel offset from
// the anchor (negative = "from the opposite edge", CSS-style). Resize
// is implicit — the renderer samples viewport size each frame.
auto hudText1 = TextSprite::create(font1, 40 * monitor::contentScale().first);
hudText1->setText("Hello World!");
hudText1->setColor(Color::black);
hudText1->setVerticalAlignment(TextSprite::VerticalAlignment::Above);
hudText1->screenSpace = true;
hudText1->screenAnchor.set(0.f, 0.f);
hudText1->position.set(5.f, 5.f, 0.f);
scene->add(hudText1);
auto hudText2 = TextSprite::create(font2, 10 * monitor::contentScale().first);
hudText2->setColor(Color::red);
hudText2->setVerticalAlignment(TextSprite::VerticalAlignment::Below);
hudText2->setHorizontalAlignment(TextSprite::HorizontalAlignment::Right);
hudText2->screenSpace = true;
hudText2->screenAnchor.set(1.f, 1.f);
hudText2->position.set(-5.f, -5.f, 0.f);
scene->add(hudText2);
canvas.onWindowResize([&](WindowSize size) {
camera->aspect = size.aspect();
camera->updateProjectionMatrix();
renderer->setSize(size);
});
MyGui ui(canvas, *renderer, *planeMaterial);
Clock clock;
canvas.animate([&] {
const auto dt = clock.getDelta();
box->rotation.y += 0.5f * dt;
hudText2->setText("Delta=" + std::to_string(dt));
renderer->render(*scene, *camera);
ui.render();
plane->position.copy(ui.position());
plane->rotation.copy(ui.rotation());
if (ui.colorChanged) {
const auto& c = ui.color();
planeMaterial->color.fromArray(c);
planeMaterial->opacity = c[3];
planeMaterial->transparent = c[3] != 1;
}
});
}