Skip to content

Commit

Permalink
Some refactoring related to material handling (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren authored Mar 9, 2024
1 parent 86d9956 commit b930897
Show file tree
Hide file tree
Showing 29 changed files with 140 additions and 149 deletions.
6 changes: 3 additions & 3 deletions examples/objects/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int main() {
Sprite* lastPicked = nullptr;
canvas.animate([&]() {
if (lastPicked) {
lastPicked->material = material;
lastPicked->setMaterial(material);
lastPicked->scale.set(1, 1, 1);
}

Expand All @@ -120,12 +120,12 @@ int main() {
raycaster.setFromCamera(mouse, *camera);
auto intersects = raycaster.intersectObjects(sprites->children, true);
if (!intersects.empty()) {
auto& intersection = intersects.front();
const auto& intersection = intersects.front();
helper->position.copy(intersection.point);
helper->visible = true;

lastPicked = intersection.object->as<Sprite>();
lastPicked->material = pickMaterial;
lastPicked->setMaterial(pickMaterial);
lastPicked->scale.set(1.2, 1.2, 1.2);
}

Expand Down
10 changes: 0 additions & 10 deletions include/threepp/core/Object3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,6 @@ namespace threepp {
return nullptr;
}

virtual std::vector<Material*> materials() {

return {};
}

[[nodiscard]] virtual const Material* material() const {

return nullptr;
}

template<class T>
T* as() {

Expand Down
2 changes: 0 additions & 2 deletions include/threepp/helpers/AxesHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace threepp {
class AxesHelper: public LineSegments {

public:
~AxesHelper() override;

static std::shared_ptr<AxesHelper> create(float size);

protected:
Expand Down
2 changes: 0 additions & 2 deletions include/threepp/helpers/GridHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace threepp {
class GridHelper: public LineSegments {

public:
~GridHelper() override;

static std::shared_ptr<GridHelper> create(
unsigned int size = 10,
unsigned int divisions = 10,
Expand Down
3 changes: 1 addition & 2 deletions include/threepp/helpers/PointLightHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#ifndef THREEPP_POINTLIGHTHELPER_HPP
#define THREEPP_POINTLIGHTHELPER_HPP

#include "threepp/geometries/SphereGeometry.hpp"
#include "threepp/objects/Mesh.hpp"

#include <memory>
Expand All @@ -22,7 +21,7 @@ namespace threepp {

private:
std::optional<Color> color;
PointLight& light;
PointLight* light;

PointLightHelper(PointLight& light, float sphereSize, std::optional<Color> color);
};
Expand Down
8 changes: 2 additions & 6 deletions include/threepp/objects/Line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include "threepp/core/BufferGeometry.hpp"
#include "threepp/core/Object3D.hpp"
#include "threepp/materials/Material.hpp"
#include "threepp/objects/ObjectWithMaterials.hpp"

#include <memory>
#include <utility>
#include <vector>

namespace threepp {

class Line: public Object3D {
class Line: public virtual Object3D, public ObjectWithMaterials {

public:
Line(std::shared_ptr<BufferGeometry> geometry, std::shared_ptr<Material> material);
Expand All @@ -24,10 +25,6 @@ namespace threepp {

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

Material* material() override;

std::vector<Material*> materials() override;

virtual void computeLineDistances();

void raycast(const Raycaster& raycaster, std::vector<Intersection>& intersects) override;
Expand All @@ -38,7 +35,6 @@ namespace threepp {

protected:
std::shared_ptr<BufferGeometry> geometry_;
std::shared_ptr<Material> material_;
};

}// namespace threepp
Expand Down
15 changes: 2 additions & 13 deletions include/threepp/objects/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
#include "threepp/core/BufferGeometry.hpp"
#include "threepp/core/Object3D.hpp"
#include "threepp/materials/Material.hpp"
#include "threepp/objects/ObjectWithMaterials.hpp"
#include "threepp/objects/ObjectWithMorphTargetInfluences.hpp"


namespace threepp {

// Class representing triangular polygon mesh based objects.
class Mesh: public Object3D, public ObjectWithMorphTargetInfluences {
class Mesh: public virtual Object3D, public ObjectWithMorphTargetInfluences, public ObjectWithMaterials {

public:

explicit Mesh(std::shared_ptr<BufferGeometry> geometry = nullptr, std::shared_ptr<Material> material = nullptr);
Mesh(std::shared_ptr<BufferGeometry> geometry, std::vector<std::shared_ptr<Material>> materials);

Expand All @@ -34,16 +34,6 @@ namespace threepp {

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

Material* material() override;

[[nodiscard]] std::vector<Material*> materials() override;

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

void setMaterials(const std::vector<std::shared_ptr<Material>>& materials);

[[nodiscard]] size_t numMaterials() const;

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

std::shared_ptr<Object3D> clone(bool recursive = true) override;
Expand All @@ -60,7 +50,6 @@ namespace threepp {

protected:
std::shared_ptr<BufferGeometry> geometry_;
std::vector<std::shared_ptr<Material>> materials_;
};

}// namespace threepp
Expand Down
34 changes: 34 additions & 0 deletions include/threepp/objects/ObjectWithMaterials.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

#ifndef THREEPP_OBJECTWITHMATERIALS_HPP
#define THREEPP_OBJECTWITHMATERIALS_HPP

#include "threepp/core/Object3D.hpp"
#include "threepp/materials/Material.hpp"


#include <vector>

namespace threepp {

class ObjectWithMaterials: public virtual Object3D {

public:
Material* material() override;

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

std::vector<Material*> materials();

void setMaterials(const std::vector<std::shared_ptr<Material>>& materials);

[[nodiscard]] size_t numMaterials() const;

protected:
std::vector<std::shared_ptr<Material>> materials_;

explicit ObjectWithMaterials(std::vector<std::shared_ptr<Material>> materials);
};

}// namespace threepp

#endif//THREEPP_OBJECTWITHMATERIALS_HPP
5 changes: 4 additions & 1 deletion include/threepp/objects/ObjectWithMorphTargetInfluences.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
#ifndef THREEPP_OBJECTWITHMORPHTARGETINFLUENCES_HPP
#define THREEPP_OBJECTWITHMORPHTARGETINFLUENCES_HPP

#include "threepp/core/Object3D.hpp"

#include <vector>

namespace threepp {

struct ObjectWithMorphTargetInfluences {
class ObjectWithMorphTargetInfluences: public virtual Object3D {

public:
std::vector<float>& morphTargetInfluences() {

if (copyMorphTargetInfluences_) {
Expand Down
8 changes: 2 additions & 6 deletions include/threepp/objects/Points.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include "threepp/core/BufferGeometry.hpp"
#include "threepp/core/Object3D.hpp"
#include "threepp/materials/PointsMaterial.hpp"
#include "threepp/objects/ObjectWithMaterials.hpp"
#include "threepp/objects/ObjectWithMorphTargetInfluences.hpp"

namespace threepp {

class Points: public Object3D, public ObjectWithMorphTargetInfluences {
class Points: public virtual Object3D, public ObjectWithMorphTargetInfluences, public ObjectWithMaterials {

public:
Points(std::shared_ptr<BufferGeometry> geometry, std::shared_ptr<Material> material);
Expand All @@ -21,10 +22,6 @@ namespace threepp {

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

Material* material() override;

std::vector<Material*> materials() override;

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

void raycast(const Raycaster& raycaster, std::vector<Intersection>& intersects) override;
Expand All @@ -35,7 +32,6 @@ namespace threepp {

protected:
std::shared_ptr<BufferGeometry> geometry_;
std::shared_ptr<Material> material_;
};

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

public:
Vector2 center{0.5f, 0.5f};
std::shared_ptr<SpriteMaterial> material;

explicit Sprite(const std::shared_ptr<SpriteMaterial>& material);

Expand All @@ -23,10 +22,15 @@ namespace threepp {

BufferGeometry* geometry() override;

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

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

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

private:
std::shared_ptr<BufferGeometry> _geometry;
std::shared_ptr<SpriteMaterial> _material;
};

}// namespace threepp
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ set(publicHeaders
"threepp/objects/LineSegments.hpp"
"threepp/objects/LOD.hpp"
"threepp/objects/Mesh.hpp"
"threepp/objects/ObjectWithMaterials.hpp"
"threepp/objects/ObjectWithMorphTargetInfluences.hpp"
"threepp/objects/ParticleSystem.hpp"
"threepp/objects/Sky.hpp"
Expand Down Expand Up @@ -371,6 +372,7 @@ set(sources
"threepp/objects/LOD.cpp"
"threepp/objects/InstancedMesh.cpp"
"threepp/objects/Mesh.cpp"
"threepp/objects/ObjectWithMaterials.cpp"
"threepp/objects/ParticleSystem.cpp"
"threepp/objects/Points.cpp"
"threepp/objects/Skeleton.cpp"
Expand Down
8 changes: 1 addition & 7 deletions src/threepp/helpers/AxesHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ AxesHelper::AxesHelper(float size): LineSegments(BufferGeometry::create(), LineB
geometry_->setAttribute("position", FloatBufferAttribute::create(vertices, 3));
geometry_->setAttribute("color", FloatBufferAttribute::create(colors, 3));

material_->vertexColors = true;
materials_.front()->vertexColors = true;
}

std::shared_ptr<AxesHelper> AxesHelper::create(float size) {

return std::shared_ptr<AxesHelper>(new AxesHelper(size));
}

AxesHelper::~AxesHelper() {

this->geometry_->dispose();
this->material_->dispose();
}
2 changes: 1 addition & 1 deletion src/threepp/helpers/Box3Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Box3Helper::Box3Helper(const Box3& box, const Color& color)

std::vector<float> positions{1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1};

auto lineMaterial = material_->as<LineBasicMaterial>();
auto lineMaterial = material()->as<LineBasicMaterial>();
lineMaterial->color.copy(color);
lineMaterial->toneMapped = false;

Expand Down
2 changes: 1 addition & 1 deletion src/threepp/helpers/BoxHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BoxHelper::BoxHelper(Object3D& object, const Color& color)
geometry_->setIndex(indices);
geometry_->setAttribute("position", FloatBufferAttribute::create(positions, 3));

material_->setValues({{"color", color}, {"toneMapped", false}});
material()->setValues({{"color", color}, {"toneMapped", false}});

this->matrixAutoUpdate = false;

Expand Down
2 changes: 1 addition & 1 deletion src/threepp/helpers/CameraHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct CameraHelper::Impl {
explicit Impl(CameraHelper& scope, Camera& camera)
: scope(scope), camera(camera) {

auto m = scope.material_->as<LineBasicMaterial>();
auto m = scope.material()->as<LineBasicMaterial>();
m->toneMapped = false;
m->vertexColors = true;
m->color = 0xffffff;
Expand Down
20 changes: 5 additions & 15 deletions src/threepp/helpers/GridHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,15 @@ GridHelper::GridHelper(unsigned int size, unsigned int divisions, const Color& c
k += step;
}

auto geometry = BufferGeometry::create();
geometry->setAttribute("position", FloatBufferAttribute::create(vertices, 3));
geometry->setAttribute("color", FloatBufferAttribute::create(colors, 3));
geometry_->setAttribute("position", FloatBufferAttribute::create(vertices, 3));
geometry_->setAttribute("color", FloatBufferAttribute::create(colors, 3));

auto material = LineBasicMaterial::create();
material->vertexColors = true;
material->toneMapped = false;

material_ = material;
geometry_ = geometry;
auto m = material()->as<LineBasicMaterial>();
m->vertexColors = true;
m->toneMapped = false;
}

std::shared_ptr<GridHelper> GridHelper::create(unsigned int size, unsigned int divisions, const Color& color1, const Color& color2) {

return std::shared_ptr<GridHelper>(new GridHelper(size, divisions, color1, color2));
}

GridHelper::~GridHelper() {

this->geometry_->dispose();
this->material_->dispose();
}
2 changes: 1 addition & 1 deletion src/threepp/helpers/HemisphereLightHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ std::shared_ptr<HemisphereLightHelper> threepp::HemisphereLightHelper::create(He
return std::shared_ptr<HemisphereLightHelper>(new HemisphereLightHelper(light, size, color));
}

threepp::HemisphereLightHelper::~HemisphereLightHelper() = default;
HemisphereLightHelper::~HemisphereLightHelper() = default;
6 changes: 3 additions & 3 deletions src/threepp/helpers/PlaneHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ PlaneHelper::PlaneHelper(const Plane& plane, float size, const Color& color)
geometry_->setAttribute("position", FloatBufferAttribute::create(positions, 3));
geometry_->computeBoundingSphere();

auto material = dynamic_cast<LineBasicMaterial*>(material_.get());
material->color.copy(color);
material->toneMapped = false;
auto _material = material()->as<MaterialWithColor>();
_material->color.copy(color);
_material->toneMapped = false;

std::vector<float> positions2{1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1};

Expand Down
Loading

0 comments on commit b930897

Please sign in to comment.