diff --git a/examples/lights/spot_light.cpp b/examples/lights/spot_light.cpp index ddef6f31..f7b63c8b 100644 --- a/examples/lights/spot_light.cpp +++ b/examples/lights/spot_light.cpp @@ -62,7 +62,7 @@ int main() { scene->add(helper); auto target = Object3D::create(); - light->target = target; + light->setTarget(*target); scene->add(target); auto knot = createTorusKnot(); diff --git a/include/threepp/helpers/SpotLightHelper.hpp b/include/threepp/helpers/SpotLightHelper.hpp index 80ef68f8..04df1531 100644 --- a/include/threepp/helpers/SpotLightHelper.hpp +++ b/include/threepp/helpers/SpotLightHelper.hpp @@ -19,15 +19,13 @@ namespace threepp { public: void update(); - ~SpotLightHelper() override; - static std::shared_ptr create(SpotLight& light, std::optional color = std::nullopt); private: - SpotLight& light; + SpotLight* light; std::optional color; - std::shared_ptr cone; + LineSegments* cone; SpotLightHelper(SpotLight& light, std::optional color); }; diff --git a/include/threepp/lights/light_interfaces.hpp b/include/threepp/lights/light_interfaces.hpp index d40d7d7f..a69dd771 100644 --- a/include/threepp/lights/light_interfaces.hpp +++ b/include/threepp/lights/light_interfaces.hpp @@ -20,9 +20,21 @@ namespace threepp { class LightWithTarget { public: - std::shared_ptr target{Object3D::create()}; + [[nodiscard]] const Object3D& target() const { + + return target_ ? *target_ : defaultTarget; + } + + void setTarget(Object3D& target) { + + this->target_ = ⌖ + } virtual ~LightWithTarget() = default; + + private: + Object3D* target_ = nullptr; + Object3D defaultTarget; }; }// namespace threepp diff --git a/src/threepp/helpers/DirectionalLightHelper.cpp b/src/threepp/helpers/DirectionalLightHelper.cpp index 13e1ea7c..bb8e2dfb 100644 --- a/src/threepp/helpers/DirectionalLightHelper.cpp +++ b/src/threepp/helpers/DirectionalLightHelper.cpp @@ -48,7 +48,7 @@ void DirectionalLightHelper::update() { static Vector3 _v3; _v1.setFromMatrixPosition(*this->light.matrixWorld); - _v2.setFromMatrixPosition(*this->light.target->matrixWorld); + _v2.setFromMatrixPosition(*this->light.target().matrixWorld); _v3.subVectors(_v2, _v1); this->lightPlane->lookAt(_v2); diff --git a/src/threepp/helpers/SpotLightHelper.cpp b/src/threepp/helpers/SpotLightHelper.cpp index 10beb1d2..f5bce8fb 100644 --- a/src/threepp/helpers/SpotLightHelper.cpp +++ b/src/threepp/helpers/SpotLightHelper.cpp @@ -1,7 +1,7 @@ #include "threepp/helpers/SpotLightHelper.hpp" -#include "threepp/lights/SpotLight.hpp" +#include "threepp/lights/Spotlight.hpp" #include "threepp/materials/LineBasicMaterial.hpp" #include "threepp/objects/LineSegments.hpp" @@ -11,11 +11,11 @@ using namespace threepp; SpotLightHelper::SpotLightHelper(SpotLight& light, std::optional color) - : light(light), color(color) { + : light(&light), color(color) { - this->light.updateMatrixWorld(); + this->light->updateMatrixWorld(); - this->matrix = this->light.matrixWorld; + this->matrix = this->light->matrixWorld; this->matrixAutoUpdate = false; auto geometry = BufferGeometry::create(); @@ -43,8 +43,9 @@ SpotLightHelper::SpotLightHelper(SpotLight& light, std::optional color) material->fog = false; material->toneMapped = false; - this->cone = LineSegments::create(geometry, material); - this->add(this->cone); + auto _cone = LineSegments::create(geometry, material); + this->cone = _cone.get(); + this->add(_cone); this->update(); } @@ -56,15 +57,15 @@ std::shared_ptr SpotLightHelper::create(SpotLight& light, std:: void SpotLightHelper::update() { - this->light.updateMatrixWorld(); + this->light->updateMatrixWorld(); - const auto coneLength = (this->light.distance > 0) ? this->light.distance : 1000; - const auto coneWidth = coneLength * std::tan(this->light.angle); + const auto coneLength = (this->light->distance > 0) ? this->light->distance : 1000; + const auto coneWidth = coneLength * std::tan(this->light->angle); this->cone->scale.set(coneWidth, coneWidth, coneLength); static Vector3 _vector; - _vector.setFromMatrixPosition(*this->light.target->matrixWorld); + _vector.setFromMatrixPosition(*this->light->target().matrixWorld); this->cone->lookAt(_vector); @@ -74,12 +75,6 @@ void SpotLightHelper::update() { } else { - this->cone->material()->as()->color.copy(this->light.color); + this->cone->material()->as()->color.copy(this->light->color); } } - -SpotLightHelper::~SpotLightHelper() { - - this->cone->geometry()->dispose(); - this->cone->material()->dispose(); -} diff --git a/src/threepp/lights/LightShadow.cpp b/src/threepp/lights/LightShadow.cpp index 594af371..2db301c1 100644 --- a/src/threepp/lights/LightShadow.cpp +++ b/src/threepp/lights/LightShadow.cpp @@ -30,7 +30,7 @@ void LightShadow::updateMatrices(Light* light) { shadowCamera->position.copy(_lightPositionWorld); auto lightWithTarget = dynamic_cast(light); - _lookTarget.setFromMatrixPosition(*lightWithTarget->target->matrixWorld); + _lookTarget.setFromMatrixPosition(*lightWithTarget->target().matrixWorld); shadowCamera->lookAt(_lookTarget); shadowCamera->updateMatrixWorld(); diff --git a/src/threepp/renderers/gl/GLLights.cpp b/src/threepp/renderers/gl/GLLights.cpp index 03316cde..ea37b986 100644 --- a/src/threepp/renderers/gl/GLLights.cpp +++ b/src/threepp/renderers/gl/GLLights.cpp @@ -248,7 +248,7 @@ void GLLights::setupView(std::vector& lights, Camera* camera) { direction.setFromMatrixPosition(*light->matrixWorld); Vector3 vector3; - vector3.setFromMatrixPosition(*l->target->matrixWorld); + vector3.setFromMatrixPosition(*l->target().matrixWorld); direction.sub(vector3); direction.transformDirection(viewMatrix); @@ -268,7 +268,7 @@ void GLLights::setupView(std::vector& lights, Camera* camera) { direction.setFromMatrixPosition(*l->matrixWorld); Vector3 vector3; - vector3.setFromMatrixPosition(*l->target->matrixWorld); + vector3.setFromMatrixPosition(*l->target().matrixWorld); direction.sub(vector3); direction.transformDirection(viewMatrix);