Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 10, 2024
1 parent 9a77789 commit 92fb63e
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 41 deletions.
10 changes: 5 additions & 5 deletions examples/geometries/box_geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace {
auto geometry = BoxGeometry::create(params);
auto material = MeshBasicMaterial::create();

Mesh mesh(geometry, material);
mesh.add(createWireframe(*geometry));
auto mesh = Mesh::create(geometry, material);
mesh->add(createWireframe(*geometry));

return mesh;
}
Expand Down Expand Up @@ -79,16 +79,16 @@ int main() {
canvas.animate([&]() {
float dt = clock.getDelta();

mesh.rotation.y += 0.8f * dt;
mesh.rotation.x += 0.5f * dt;
mesh->rotation.y += 0.8f * dt;
mesh->rotation.x += 0.5f * dt;

renderer.render(scene, camera);

ui.render();

if (paramsChanged) {
paramsChanged = false;
updateGroupGeometry(mesh, params);
updateGroupGeometry(*mesh, params);
}
});
}
2 changes: 1 addition & 1 deletion examples/misc/morphtargets_sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main() {

mesh->material()->as<MaterialWithMorphTargets>()->morphTargets = true;

auto points = Points::create(mesh->shared_geometry(), pointsMaterial);
auto points = Points::create(mesh->geometry()->shared_from_this(), pointsMaterial);
points->copyMorphTargetInfluences(&mesh->morphTargetInfluences());
mesh->add(points);
});
Expand Down
2 changes: 1 addition & 1 deletion examples/objects/decal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ int main() {

Vector3 scale = Vector3::ONES() * math::randFloat(0.6f, 1.2f);

auto mat = decalMat->clone()->as<MeshPhongMaterial>();
auto mat = decalMat->clone()->as_shared<MeshPhongMaterial>();
mat->color.randomize();
orientation.z = math::PI * math::randFloat();
auto m = Mesh::create(DecalGeometry::create(*mesh, position, orientation, scale), mat);
Expand Down
2 changes: 1 addition & 1 deletion examples/objects/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main() {
material->map = loader.load("data/textures/three.png");
material->map->offset.set(0.5, 0.5);

auto pickMaterial = material->clone()->as<SpriteMaterial>();
auto pickMaterial = material->clone()->as_shared<SpriteMaterial>();

auto sprites = createSprites(material);
scene->add(sprites);
Expand Down
7 changes: 6 additions & 1 deletion include/threepp/core/BufferGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace threepp {

class BufferGeometry: public EventDispatcher {
class BufferGeometry: public EventDispatcher, public std::enable_shared_from_this<BufferGeometry> {

public:
const unsigned int id{++_id};
Expand All @@ -35,6 +35,11 @@ namespace threepp {

BufferGeometry();

BufferGeometry(const BufferGeometry&) = delete;
BufferGeometry& operator=(const BufferGeometry&) = delete;
BufferGeometry(BufferGeometry&&) = delete;
BufferGeometry& operator=(BufferGeometry&&) = delete;

[[nodiscard]] virtual std::string type() const {

return "BufferGeometry";
Expand Down
5 changes: 4 additions & 1 deletion include/threepp/core/Object3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace threepp {
Object3D();

Object3D(Object3D&& source) noexcept;
Object3D& operator=(Object3D&& other) = delete;
Object3D& operator=(Object3D&&) = delete;
Object3D(const Object3D&) = delete;
Object3D& operator=(const Object3D&) = delete;

Expand Down Expand Up @@ -231,6 +231,9 @@ namespace threepp {
template<class T>
T* as() {

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

return dynamic_cast<T*>(this);
}

Expand Down
26 changes: 21 additions & 5 deletions include/threepp/materials/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ namespace threepp {

std::unordered_map<std::string, UniformValue> defaultAttributeValues;

unsigned int version = 0;

Material(Material&&) = delete;
Material& operator=(Material&&) = delete;
Material(const Material&) = delete;
Material& operator=(const Material&) = delete;

std::string uuid() const;

[[nodiscard]] unsigned int version() const;

void setValues(const std::unordered_map<std::string, MaterialValue>& values);

void dispose();
Expand All @@ -91,7 +94,19 @@ namespace threepp {
[[nodiscard]] virtual std::string type() const = 0;

template<class T>
std::shared_ptr<T> as() {
T* as() {

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

return dynamic_cast<T*>(this);
}

template<class T>
std::shared_ptr<T> as_shared() {

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

auto m = shared_from_this();
return std::dynamic_pointer_cast<T>(m);
Expand All @@ -112,15 +127,15 @@ namespace threepp {

void copyInto(Material* m) const;

Color extractColor(const MaterialValue& value) {
static Color extractColor(const MaterialValue& value) {
if (std::holds_alternative<int>(value)) {
return std::get<int>(value);
} else {
return std::get<Color>(value);
}
}

float extractFloat(const MaterialValue& value) {
static float extractFloat(const MaterialValue& value) {
if (std::holds_alternative<int>(value)) {
return std::get<int>(value);
} else {
Expand All @@ -133,6 +148,7 @@ namespace threepp {
private:
bool disposed_ = false;
std::string uuid_;
unsigned int version_ = 0;
inline static unsigned int materialId = 0;
};

Expand Down
2 changes: 2 additions & 0 deletions include/threepp/objects/Line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace threepp {
public:
Line(std::shared_ptr<BufferGeometry> geometry, std::shared_ptr<Material> material);

Line(Line&&) = delete;

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

BufferGeometry* geometry() override;
Expand Down
7 changes: 1 addition & 6 deletions include/threepp/objects/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ namespace threepp {
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);

Mesh(Mesh&& other) noexcept;
Mesh(Mesh&& other) = delete;

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

BufferGeometry* geometry() override;

std::shared_ptr<BufferGeometry> shared_geometry() {

return geometry_;
}

[[nodiscard]] const BufferGeometry* geometry() const;

void setGeometry(const std::shared_ptr<BufferGeometry>& geometry);
Expand Down
2 changes: 2 additions & 0 deletions include/threepp/objects/Points.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace threepp {
public:
Points(std::shared_ptr<BufferGeometry> geometry, std::shared_ptr<Material> material);

Points(Points&&) = delete;

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

BufferGeometry* geometry() override;
Expand Down
2 changes: 2 additions & 0 deletions include/threepp/objects/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace threepp {

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

Sprite(Sprite&&) = delete;

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

void raycast(const Raycaster& raycaster, std::vector<Intersection>& intersects) override;
Expand Down
4 changes: 3 additions & 1 deletion include/threepp/textures/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ namespace threepp {
Encoding encoding{Encoding::Linear};

Texture(const Texture&) = delete;
Texture operator=(const Texture&) = delete;
Texture& operator=(const Texture&) = delete;
Texture(Texture&&) = delete;
Texture& operator=(Texture&&) = delete;

std::optional<std::function<void(Texture&)>> onUpdate;

Expand Down
2 changes: 1 addition & 1 deletion src/threepp/lights/PointLightShadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace threepp;


PointLightShadow::PointLightShadow()
: LightShadow(std::make_unique<PerspectiveCamera>(90, 1, 0.5f, 500)),
: LightShadow(std::make_unique<PerspectiveCamera>(90.f, 1.f, 0.5f, 500.f)),
_cubeDirections({Vector3(1, 0, 0), Vector3(-1, 0, 0), Vector3(0, 0, 1),
Vector3(0, 0, -1), Vector3(0, 1, 0), Vector3(0, -1, 0)}),
_cubeUps({Vector3(0, 1, 0), Vector3(0, 1, 0), Vector3(0, 1, 0),
Expand Down
9 changes: 8 additions & 1 deletion src/threepp/materials/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@

using namespace threepp;


Material::Material()
: uuid_(math::generateUUID()) {}


std::string Material::uuid() const {

return uuid_;
}

unsigned int Material::version() const {

return version_;
}

void Material::dispose() {
if (!disposed_) {
disposed_ = true;
Expand All @@ -25,7 +32,7 @@ void Material::dispose() {

void Material::needsUpdate() {

this->version++;
this->version_++;
}

void Material::copyInto(Material* m) const {
Expand Down
4 changes: 0 additions & 4 deletions src/threepp/objects/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ Mesh::Mesh(std::shared_ptr<BufferGeometry> geometry, std::vector<std::shared_ptr
: geometry_(std::move(geometry)), ObjectWithMaterials{std::move(materials)} {
}

Mesh::Mesh(Mesh&& other) noexcept: Object3D(std::move(other)), ObjectWithMaterials(std::move(other)) {
geometry_ = std::move(other.geometry_);
}

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

if (material() == nullptr) return;
Expand Down
4 changes: 2 additions & 2 deletions src/threepp/renderers/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ struct GLRenderer::Impl {
bool isInstancedMesh = object->type() == "InstancedMesh";
bool isSkinnedMesh = object->type() == "SkinnedMesh";

if (material->version == materialProperties->version) {
if (material->version() == materialProperties->version) {

if (materialProperties->needsLights && (materialProperties->lightsStateVersion != lights.state.version)) {

Expand Down Expand Up @@ -802,7 +802,7 @@ struct GLRenderer::Impl {
} else {

needsProgramChange = true;
materialProperties->version = material->version;
materialProperties->version = material->version();
}

//
Expand Down
22 changes: 11 additions & 11 deletions src/threepp/renderers/gl/GLMaterials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,54 +484,54 @@ struct GLMaterials::Impl {

} else if (type == "MeshLambertMaterial") {

auto m = material->as<MeshLambertMaterial>().get();
auto m = material->as<MeshLambertMaterial>();
refreshUniformsCommon(uniforms, m);
refreshUniformsLambert(uniforms, m);

} else if (type == "MeshToonMaterial") {

auto m = material->as<MeshToonMaterial>().get();
auto m = material->as<MeshToonMaterial>();
refreshUniformsCommon(uniforms, m);
refreshUniformsToon(uniforms, m);

} else if (type == "MeshPhongMaterial") {

auto m = material->as<MeshPhongMaterial>().get();
auto m = material->as<MeshPhongMaterial>();
refreshUniformsCommon(uniforms, m);
refreshUniformsPhong(uniforms, m);

} else if (type == "MeshStandardMaterial") {

auto m = material->as<MeshStandardMaterial>().get();
auto m = material->as<MeshStandardMaterial>();
refreshUniformsCommon(uniforms, material);
refreshUniformsStandard(uniforms, m);

} else if (type == "MeshMatcapMaterial") {

auto m = material->as<MeshMatcapMaterial>().get();
auto m = material->as<MeshMatcapMaterial>();
refreshUniformsCommon(uniforms, m);
refreshUniformsMatcap(uniforms, m);

} else if (type == "MeshDepthMaterial") {

auto m = material->as<MeshDepthMaterial>().get();
auto m = material->as<MeshDepthMaterial>();
refreshUniformsCommon(uniforms, m);
refreshUniformsDepth(uniforms, m);

} else if (type == "MeshDistanceMaterial") {

auto m = material->as<MeshDistanceMaterial>();
refreshUniformsCommon(uniforms, m.get());
refreshUniformsDistance(uniforms, m.get());
refreshUniformsCommon(uniforms, m);
refreshUniformsDistance(uniforms, m);

} else if (type == "LineBasicMaterial") {

auto m = material->as<LineBasicMaterial>();
refreshUniformsLine(uniforms, m.get());
refreshUniformsLine(uniforms, m);

} else if (type == "PointsMaterial") {

auto m = material->as<PointsMaterial>().get();
auto m = material->as<PointsMaterial>();
refreshUniformsPoints(uniforms, m, pixelRatio, static_cast<float>(height));

} else if (type == "ShadowMaterial") {
Expand All @@ -543,7 +543,7 @@ struct GLMaterials::Impl {
} else if (type == "SpriteMaterial") {

auto m = material->as<SpriteMaterial>();
refreshUniformsSprites(uniforms, m.get());
refreshUniformsSprites(uniforms, m);


} else if (type == "ShaderMaterial") {
Expand Down

0 comments on commit 92fb63e

Please sign in to comment.