Skip to content

Commit

Permalink
Rework how clone/copy works (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren authored Apr 4, 2024
1 parent e465778 commit 454869e
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 72 deletions.
28 changes: 21 additions & 7 deletions examples/objects/lod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ int main() {

OrbitControls controls{camera, canvas};

LOD lod;
scene.add(lod);
LOD lod1;
scene.add(lod1);

float radius = 0.5;
auto material = MeshBasicMaterial::create({{"wireframe", true}});
for (int z = 0; z <= 5; z++) {
int detail = 6 - z;
auto obj = Mesh::create(IcosahedronGeometry::create(radius, detail), material);
lod.addLevel(obj, static_cast<float>(z));
lod1.addLevel(obj, static_cast<float>(z));
}

LOD lod2;
lod2.copy(lod1);
scene.add(lod2);

float spacing = 1;
lod1.position.x = spacing;
lod2.position.x = -spacing;

canvas.onWindowResize([&](WindowSize size) {
camera.aspect = size.aspect();
camera.updateProjectionMatrix();
Expand All @@ -38,12 +46,18 @@ int main() {
const auto font = *fontLoader.load("data/fonts/gentilis_bold.typeface.json");

TextGeometry::Options opts(font, 20, 5);
auto handle = Text2D(opts, "");
handle.setColor(Color::gray);
hud.add(handle);

Text2D handle1(opts);
handle1.setColor(Color::gray);
hud.add(handle1, HUD::Options().setNormalizedPosition({0.f, 0.05f}));

Text2D handle2(opts);
handle2.setColor(Color::gray);
hud.add(handle2);

canvas.animate([&]() {
handle.setText("LOD level: " + std::to_string(lod.getCurrentLevel()), opts);
handle1.setText("LOD1 level: " + std::to_string(lod1.getCurrentLevel()));
handle2.setText("LOD2 level: " + std::to_string(lod2.getCurrentLevel()));

renderer.clear();
renderer.render(scene, camera);
Expand Down
26 changes: 24 additions & 2 deletions include/threepp/core/Object3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,40 @@ namespace threepp {
return dynamic_cast<T*>(this);
}

template<class T>
const T* as() const {

static_assert(std::is_base_of<Object3D, typename std::remove_cv<typename std::remove_pointer<T>::type>::type>::value,
"T must be a base class of Object3D");

return dynamic_cast<const T*>(this);
}

template<class T>
[[nodiscard]] bool is() const {

return dynamic_cast<const T*>(this) != nullptr;
}

void copy(const Object3D& source, bool recursive = true);
virtual void copy(const Object3D& source, bool recursive = true);

virtual std::shared_ptr<Object3D> clone(bool recursive = true);
template<class T = Object3D>
std::shared_ptr<T> clone(bool recursive = true) {

auto clone = createDefault();
clone->copy(*this, recursive);

return std::dynamic_pointer_cast<T>(clone);
}

~Object3D() override;

protected:
virtual std::shared_ptr<Object3D> createDefault() {

return std::make_shared<Object3D>();
}

private:
inline static unsigned int _object3Did{0};

Expand Down
6 changes: 6 additions & 0 deletions include/threepp/objects/Bone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ namespace threepp {

return std::make_shared<Bone>();
}

protected:
std::shared_ptr<Object3D> createDefault() override {

return create();
}
};

}// namespace threepp
Expand Down
5 changes: 3 additions & 2 deletions include/threepp/objects/Group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ namespace threepp {
public:
[[nodiscard]] std::string type() const override;

std::shared_ptr<Object3D> clone(bool recursive = true) override;

static std::shared_ptr<Group> create();

~Group() override = default;

protected:
std::shared_ptr<Object3D> createDefault() override;
};

}// namespace threepp
Expand Down
5 changes: 5 additions & 0 deletions include/threepp/objects/LOD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ namespace threepp {

void update(Camera& camera);

void copy(const Object3D& source, bool recursive = false) override;

static std::shared_ptr<LOD> create();

protected:
std::shared_ptr<Object3D> createDefault() override;

private:
size_t _currentLevel = 0;
std::vector<Level> levels;
Expand Down
6 changes: 4 additions & 2 deletions include/threepp/objects/Line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ namespace threepp {

virtual void computeLineDistances();

void raycast(const Raycaster& raycaster, std::vector<Intersection>& intersects) override;
void copy(const Object3D& source, bool recursive = true) override;

std::shared_ptr<Object3D> clone(bool recursive = true) override;
void raycast(const Raycaster& raycaster, std::vector<Intersection>& intersects) override;

static std::shared_ptr<Line> create(const std::shared_ptr<BufferGeometry>& geometry = nullptr, const std::shared_ptr<Material>& material = nullptr);

protected:
std::shared_ptr<BufferGeometry> geometry_;

std::shared_ptr<Object3D> createDefault() override;
};

}// namespace threepp
Expand Down
2 changes: 0 additions & 2 deletions include/threepp/objects/LineLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace threepp {

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

std::shared_ptr<Object3D> clone(bool recursive = true) override;

static std::shared_ptr<LineLoop> create(
const std::shared_ptr<BufferGeometry>& geometry = nullptr,
const std::shared_ptr<Material>& material = nullptr);
Expand Down
2 changes: 0 additions & 2 deletions include/threepp/objects/LineSegments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace threepp {

void computeLineDistances() override;

std::shared_ptr<Object3D> clone(bool recursive = true) override;

static std::shared_ptr<LineSegments> create(
const std::shared_ptr<BufferGeometry>& geometry = nullptr,
const std::shared_ptr<Material>& material = nullptr);
Expand Down
4 changes: 3 additions & 1 deletion include/threepp/objects/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace threepp {

void raycast(const Raycaster& raycaster, std::vector<Intersection>& intersects) override;

std::shared_ptr<Object3D> clone(bool recursive = true) override;
void copy(const Object3D& source, bool recursive = true) override;

static std::shared_ptr<Mesh> create(
std::shared_ptr<BufferGeometry> geometry = nullptr,
Expand All @@ -43,6 +43,8 @@ namespace threepp {

protected:
std::shared_ptr<BufferGeometry> geometry_;

std::shared_ptr<Object3D> createDefault() override;
};

}// namespace threepp
Expand Down
6 changes: 4 additions & 2 deletions include/threepp/objects/Points.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ namespace threepp {

void setGeometry(const std::shared_ptr<BufferGeometry>& geometry);

std::shared_ptr<Object3D> clone(bool recursive = true) override;

void raycast(const Raycaster& raycaster, std::vector<Intersection>& intersects) override;

void copy(const Object3D& source, bool recursive = true) override;

static std::shared_ptr<Points> create(
std::shared_ptr<BufferGeometry> geometry = BufferGeometry::create(),
std::shared_ptr<Material> material = PointsMaterial::create());

protected:
std::shared_ptr<BufferGeometry> geometry_;

std::shared_ptr<Object3D> createDefault() override;
};

}// namespace threepp
Expand Down
5 changes: 5 additions & 0 deletions include/threepp/objects/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ namespace threepp {

void setMaterial(const std::shared_ptr<SpriteMaterial>& material);

void copy(const Object3D& source, bool recursive = true) override;

static std::shared_ptr<Sprite> create(const std::shared_ptr<SpriteMaterial>& material = nullptr);

protected:
std::shared_ptr<Object3D> createDefault() override;

private:
std::shared_ptr<BufferGeometry> _geometry;
std::shared_ptr<SpriteMaterial> _material;
Expand Down
10 changes: 1 addition & 9 deletions src/threepp/core/Object3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,22 +442,14 @@ void Object3D::copy(const Object3D& source, bool recursive) {

if (recursive) {

for (auto& child : source.children) {
for (const auto& child : source.children) {

auto clone = child->clone();
this->add(clone);
}
}
}

std::shared_ptr<Object3D> Object3D::clone(bool recursive) {

auto clone = std::make_shared<Object3D>();
clone->copy(*this, recursive);

return clone;
}

Object3D::Object3D(Object3D&& source) noexcept: Object3D() {

this->name = std::move(source.name);
Expand Down
2 changes: 1 addition & 1 deletion src/threepp/loaders/OBJLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ struct OBJLoader::Impl {

auto cached = cache_[path.string()];
if (!cached.expired()) {
return std::dynamic_pointer_cast<Group>(cached.lock()->clone());
return cached.lock()->clone<Group>();
} else {
cache_.erase(path.string());
}
Expand Down
10 changes: 4 additions & 6 deletions src/threepp/objects/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ std::string Group::type() const {
return "Group";
}

std::shared_ptr<Object3D> Group::clone(bool recursive) {
auto clone = create();
clone->copy(*this, recursive);
std::shared_ptr<Group> Group::create() {

return clone;
return std::make_shared<Group>();
}

std::shared_ptr<Group> Group::create() {
std::shared_ptr<Object3D> Group::createDefault() {

return std::make_shared<Group>();
return create();
}
19 changes: 19 additions & 0 deletions src/threepp/objects/LOD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,22 @@ void LOD::update(Camera& camera) {
}
}
}

void LOD::copy(const Object3D& source, bool recursive) {
Object3D::copy(source, false);

if (const auto l = source.as<LOD>()) {

for (auto level : l->levels) {

this->addLevel(level.object->clone(), level.distance);
}

this->autoUpdate = l->autoUpdate;
}
}

std::shared_ptr<Object3D> LOD::createDefault() {

return create();
}
22 changes: 15 additions & 7 deletions src/threepp/objects/Line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ void Line::setGeometry(const std::shared_ptr<BufferGeometry>& geometry) {
this->geometry_ = geometry;
}

std::shared_ptr<Object3D> Line::clone(bool recursive) {
auto clone = create(geometry_, materials_.front());
clone->copy(*this, recursive);

return clone;
}

std::shared_ptr<Line> Line::create(const std::shared_ptr<BufferGeometry>& geometry, const std::shared_ptr<Material>& material) {

return std::make_shared<Line>(geometry, (material));
Expand Down Expand Up @@ -172,3 +165,18 @@ void Line::raycast(const Raycaster& raycaster, std::vector<Intersection>& inters
}
}
}

void Line::copy(const Object3D& source, bool recursive) {
Object3D::copy(source, recursive);

if (const auto l = source.as<Line>()) {

materials_ = l->materials_;
geometry_ = l->geometry_;
}
}

std::shared_ptr<Object3D> Line::createDefault() {

return create();
}
7 changes: 0 additions & 7 deletions src/threepp/objects/LineLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ std::string LineLoop::type() const {
return "LineLoop";
}

std::shared_ptr<Object3D> LineLoop::clone(bool recursive) {
auto clone = create();
clone->copy(*this, recursive);

return clone;
}

std::shared_ptr<LineLoop> LineLoop::create(const std::shared_ptr<BufferGeometry>& geometry, const std::shared_ptr<Material>& material) {

return std::make_shared<LineLoop>(geometry, (material));
Expand Down
7 changes: 0 additions & 7 deletions src/threepp/objects/LineSegments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ void LineSegments::computeLineDistances() {
}
}

std::shared_ptr<Object3D> LineSegments::clone(bool recursive) {
auto clone = create();
clone->copy(*this, recursive);

return clone;
}

std::shared_ptr<LineSegments> LineSegments::create(
const std::shared_ptr<BufferGeometry>& geometry,
const std::shared_ptr<Material>& material) {
Expand Down
24 changes: 16 additions & 8 deletions src/threepp/objects/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,6 @@ void Mesh::raycast(const Raycaster& raycaster, std::vector<Intersection>& inters
}
}

std::shared_ptr<Object3D> Mesh::clone(bool recursive) {

auto clone = create(geometry_, materials_);
clone->copy(*this, recursive);

return clone;
}

std::string Mesh::type() const {

return "Mesh";
Expand Down Expand Up @@ -370,3 +362,19 @@ std::shared_ptr<Mesh> Mesh::create(std::shared_ptr<BufferGeometry> geometry, std

return std::make_shared<Mesh>(std::move(geometry), std::move(materials));
}

void Mesh::copy(const Object3D& source, bool recursive) {
Object3D::copy(source, recursive);

if (const auto m = source.as<Mesh>()) {

// TODO morphs

materials_ = m->materials_;
geometry_ = m->geometry_;
}
}

std::shared_ptr<Object3D> Mesh::createDefault() {
return create();
}
Loading

0 comments on commit 454869e

Please sign in to comment.