Skip to content

Commit 8c7d229

Browse files
committed
update tube example [skip ci]
1 parent 78d0491 commit 8c7d229

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

examples/geometries/tube_geometry.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,25 @@ int main() {
7171

7272
OrbitControls controls{*camera, canvas};
7373

74-
auto tube = makeTubeMesh();
75-
const auto geometry = tube->geometry();
76-
scene->add(tube);
74+
auto tube1 = makeTubeMesh();
75+
const auto geometry1 = tube1->geometry();
76+
scene->add(tube1);
7777

78-
BVH bvh;
79-
bvh.build(*tube->geometry());
78+
BVH bvh1;
79+
bvh1.build(*geometry1);
8080

8181
std::vector<BVHBox3> boxes;
82-
bvh.collectBoxes(boxes);
82+
bvh1.collectBoxes(boxes);
8383

8484
auto boxesGroup = makeBoxes(boxes);
85-
tube->add(boxesGroup);
85+
tube1->add(boxesGroup);
86+
87+
auto tube2 = makeTubeMesh();
88+
tube2->position.x = 10;
89+
scene->add(tube2);
90+
91+
BVH bvh2;
92+
bvh2.build(*tube2->geometry());
8693

8794
KeyAdapter keyAdapter(KeyAdapter::Mode::KEY_PRESSED, [&](const KeyEvent& evt) {
8895
static bool show = boxesGroup->visible;
@@ -101,13 +108,33 @@ int main() {
101108
renderer.setSize(size);
102109
});
103110

111+
auto collisions = InstancedMesh::create(
112+
SphereGeometry::create(0.5f, 16, 8),
113+
MeshBasicMaterial::create({{"color", Color::yellow}}), 10000);
114+
collisions->frustumCulled = false;// Disable frustum culling for collisions
115+
scene->add(collisions);
116+
117+
104118
Clock clock;
105-
const auto count = static_cast<float>(geometry->getIndex()->count());
119+
const auto count = static_cast<float>(geometry1->getIndex()->count());
106120
canvas.animate([&]() {
107-
tube->rotation.y += 1 * clock.getDelta();
121+
tube1->rotation.y += 1 * clock.getDelta();
108122

109123
const auto map = math::mapLinear(std::sin(clock.elapsedTime), -1, 1, 0, count);
110-
geometry->setDrawRange(0, static_cast<int>(map));
124+
geometry1->setDrawRange(0, static_cast<int>(map));
125+
126+
if (BVH::intersects(bvh1, bvh2, *tube1->matrixWorld, *tube2->matrixWorld)) {
127+
static Matrix4 m;
128+
const auto res = BVH::intersect(bvh1, *tube1->matrixWorld, bvh2, *tube2->matrixWorld);
129+
collisions->setCount(res.size());
130+
for (int i = 0; i < res.size(); ++i) {
131+
const auto& intersection = res[i];
132+
collisions->setMatrixAt(i, m.makeTranslation(intersection.position));
133+
}
134+
collisions->instanceMatrix()->needsUpdate();
135+
} else {
136+
collisions->setCount(0);
137+
}
111138

112139
renderer.render(*scene, *camera);
113140
});

0 commit comments

Comments
 (0)