Skip to content

Commit

Permalink
make it possible change the amount of instances to render (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren authored Mar 20, 2024
1 parent 82c68d8 commit 32210bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
21 changes: 11 additions & 10 deletions examples/objects/instancing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ namespace {
}
}
}
mesh.instanceMatrix()->needsUpdate();
mesh.instanceColor()->needsUpdate();
}

}// namespace

int main() {

int amount = 10;
const int maxAmount = 25;

Canvas canvas("Instancing", {{"aa", 4}, {"vsync", false}});
GLRenderer renderer(canvas.size());
Expand All @@ -44,7 +47,7 @@ int main() {

auto scene = Scene::create();
auto camera = PerspectiveCamera::create(60, canvas.aspect(), 0.1f, 10000);
camera->position.set(float(amount), float(amount), float(amount));
camera->position.set(float(maxAmount), float(maxAmount), float(maxAmount));

OrbitControls controls{*camera, canvas};

Expand All @@ -54,8 +57,9 @@ int main() {

auto material = MeshPhongMaterial::create();
auto geometry = IcosahedronGeometry::create(0.5f, 2);
auto mesh = InstancedMesh::create(geometry, material, static_cast<int>(std::pow(amount, 3)));

auto mesh = InstancedMesh::create(geometry, material, static_cast<int>(std::pow(maxAmount, 3)));
mesh->instanceMatrix()->setUsage(DrawUsage::Dynamic);
mesh->setCount(static_cast<int>(std::pow(amount, 3)));
setupInstancedMesh(*mesh, amount);
scene->add(mesh);

Expand All @@ -68,14 +72,11 @@ int main() {
ImGui::SetNextWindowSize({width, 0}, 0);

ImGui::Begin("Settings");
ImGui::SliderInt("Amount", &amount, 2, 25);
ImGui::SliderInt("Amount", &amount, 2, maxAmount);
if (ImGui::IsItemEdited()) {
colorMap.clear();
mesh->removeFromParent();
mesh = InstancedMesh::create(geometry, material, static_cast<int>(std::pow(amount, 3)));
mesh->setCount(static_cast<int>(std::pow(amount, 3)));
setupInstancedMesh(*mesh, amount);
scene->add(mesh);
camera->position.set(float(amount), float(amount), float(amount));
}

ImGui::End();
Expand All @@ -93,7 +94,7 @@ int main() {
const auto font = *fontLoader.load("data/fonts/helvetiker_regular.typeface.json");

TextGeometry::Options opts(font, 20, 2);
auto handle = Text2D(opts, "");
auto handle = Text2D(opts);
handle.setColor(Color::black);
hud.add(handle, HUD::Options()
.setNormalizedPosition({0, 1})
Expand Down Expand Up @@ -135,7 +136,7 @@ int main() {

counter.update(clock.getElapsedTime());
if (it++ % 60 == 0) {
handle.setText("FPS: " + std::to_string(counter.fps), opts);
handle.setText("FPS: " + std::to_string(counter.fps));
hud.needsUpdate(handle);
}

Expand Down
9 changes: 5 additions & 4 deletions include/threepp/objects/InstancedMesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ namespace threepp {
class InstancedMesh: public Mesh {

public:

InstancedMesh(
std::shared_ptr<BufferGeometry> geometry,
std::shared_ptr<Material> material,
size_t count);

[[nodiscard]] size_t count() const;

FloatBufferAttribute* instanceMatrix() const;
void setCount(size_t count);

[[nodiscard]] FloatBufferAttribute* instanceMatrix() const;

FloatBufferAttribute* instanceColor() const;
[[nodiscard]] FloatBufferAttribute* instanceColor() const;

[[nodiscard]] std::string type() const override;

Expand Down Expand Up @@ -50,9 +51,9 @@ namespace threepp {
bool disposed{false};

size_t count_;
size_t maxCount_;
std::unique_ptr<FloatBufferAttribute> instanceMatrix_;
std::unique_ptr<FloatBufferAttribute> instanceColor_ = nullptr;

};

}// namespace threepp
Expand Down
9 changes: 7 additions & 2 deletions src/threepp/objects/InstancedMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ InstancedMesh::InstancedMesh(
std::shared_ptr<Material> material,
size_t count)
: Mesh(std::move(geometry), std::move(material)),
count_(count), instanceMatrix_(FloatBufferAttribute::create(std::vector<float>(count * 16), 16)) {
count_(count), maxCount_(count), instanceMatrix_(FloatBufferAttribute::create(std::vector<float>(count * 16), 16)) {

this->frustumCulled = false;
}
Expand All @@ -32,6 +32,11 @@ size_t InstancedMesh::count() const {
return count_;
}

void InstancedMesh::setCount(size_t count) {

count_ = std::min(maxCount_, count);
}

FloatBufferAttribute* InstancedMesh::instanceMatrix() const {

return instanceMatrix_.get();
Expand Down Expand Up @@ -62,7 +67,7 @@ void InstancedMesh::setColorAt(size_t index, const Color& color) {

if (!this->instanceColor_) {

this->instanceColor_ = FloatBufferAttribute ::create(std::vector<float>(count_ * 3), 3);
this->instanceColor_ = FloatBufferAttribute ::create(std::vector<float>(maxCount_ * 3), 3);
}

color.toArray(this->instanceColor_->array(), index * 3);
Expand Down

0 comments on commit 32210bc

Please sign in to comment.