forked from markaren/threepp
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstancing.cpp
More file actions
106 lines (82 loc) · 3.3 KB
/
Copy pathinstancing.cpp
File metadata and controls
106 lines (82 loc) · 3.3 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
#include "threepp/objects/LOD.hpp"
#include "threepp/threepp.hpp"
#include "threepp/extras/imgui/RendererSettings.hpp"
#include <cmath>
using namespace threepp;
namespace {
void setupInstancedMesh(InstancedMesh& mesh, int amount) {
Matrix4 matrix;
Color color;
size_t index = 0;
float offset = static_cast<float>(amount - 1) / 2;
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
for (int z = 0; z < amount; z++) {
matrix.setPosition(offset - static_cast<float>(x), offset - static_cast<float>(y), offset - static_cast<float>(z));
mesh.setMatrixAt(index, matrix);
mesh.setColorAt(index, color);
++index;
}
}
}
mesh.setCount(static_cast<int>(std::pow(amount, 3)));
mesh.instanceMatrix()->needsUpdate();
mesh.instanceColor()->needsUpdate();
mesh.computeBoundingSphere();
}
}// namespace
int main() {
int amount = 10;
constexpr int maxAmount = 25;
Canvas canvas("Instancing", {{"aa", 4}, {"vsync", false}});
auto renderer = createRenderer(canvas);
auto scene = Scene::create();
scene->background = Color::aliceblue;
auto camera = PerspectiveCamera::create(60, canvas.aspect(), 0.1f, 10000);
camera->position.set(static_cast<float>(maxAmount), static_cast<float>(maxAmount), static_cast<float>(maxAmount));
OrbitControls controls{*camera, canvas};
auto light = HemisphereLight::create(0xffffff, 0x888888, 2.5f);
light->position.set(0, 1, 0);
scene->add(light);
auto material = MeshPhongMaterial::create();
auto geometry = IcosahedronGeometry::create(0.5f, 2);
auto mesh = InstancedMesh::create(geometry, material, static_cast<int>(std::pow(maxAmount, 3)));
mesh->instanceMatrix()->setUsage(DrawUsage::Dynamic);
setupInstancedMesh(*mesh, amount);
scene->add(mesh);
std::unordered_map<int, bool> colorMap;
RendererSettingsUi ui(canvas, *renderer, [&] {
ImGui::SliderInt("Amount", &amount, 2, maxAmount);
if (ImGui::IsItemEdited()) {
colorMap.clear();
setupInstancedMesh(*mesh, amount);
}
}, "Settings");
canvas.onWindowResize([&](WindowSize size) {
camera->aspect = size.aspect();
camera->updateProjectionMatrix();
renderer->setSize(size);
});
Vector2 mouse{-Infinity<float>, -Infinity<float>};
MouseMoveListener l([&](auto& pos) {
const auto size = canvas.size();
mouse.x = (pos.x / static_cast<float>(size.width())) * 2 - 1;
mouse.y = -(pos.y / static_cast<float>(size.height())) * 2 + 1;
});
canvas.addMouseListener(l);
Raycaster raycaster;
canvas.animate([&] {
raycaster.setFromCamera(mouse, *camera);
const auto intersects = raycaster.intersectObject(*mesh);
if (!intersects.empty()) {
const auto& instanceId = intersects.front().instanceId;
if (instanceId && !colorMap[*instanceId]) {
mesh->setColorAt(*instanceId, Color().randomize());
mesh->instanceColor()->needsUpdate();
colorMap[*instanceId] = true;
}
}
renderer->render(*scene, *camera);
ui.render();
});
}