Skip to content

Commit

Permalink
refactor light target
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 9, 2024
1 parent b930897 commit 9bf17f3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/lights/spot_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions include/threepp/helpers/SpotLightHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ namespace threepp {
public:
void update();

~SpotLightHelper() override;

static std::shared_ptr<SpotLightHelper> create(SpotLight& light, std::optional<Color> color = std::nullopt);

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

std::shared_ptr<LineSegments> cone;
LineSegments* cone;

SpotLightHelper(SpotLight& light, std::optional<Color> color);
};
Expand Down
14 changes: 13 additions & 1 deletion include/threepp/lights/light_interfaces.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ namespace threepp {
class LightWithTarget {

public:
std::shared_ptr<Object3D> target{Object3D::create()};
[[nodiscard]] const Object3D& target() const {

return target_ ? *target_ : defaultTarget;
}

void setTarget(Object3D& target) {

this->target_ = &target;
}

virtual ~LightWithTarget() = default;

private:
Object3D* target_ = nullptr;
Object3D defaultTarget;
};

}// namespace threepp
Expand Down
2 changes: 1 addition & 1 deletion src/threepp/helpers/DirectionalLightHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 12 additions & 17 deletions src/threepp/helpers/SpotLightHelper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "threepp/helpers/SpotLightHelper.hpp"

#include "threepp/lights/SpotLight.hpp"
#include "threepp/lights/Spotlight.hpp"

Check warning on line 4 in src/threepp/helpers/SpotLightHelper.cpp

View workflow job for this annotation

GitHub Actions / vcpkg-on-darwin (macos-12)

non-portable path to file '"threepp/lights/SpotLight.hpp"'; specified path differs in case from file name on disk [-Wnonportable-include-path]

Check failure on line 4 in src/threepp/helpers/SpotLightHelper.cpp

View workflow job for this annotation

GitHub Actions / vcpkg-on-linux (ubuntu-22.04, 11)

threepp/lights/Spotlight.hpp: No such file or directory
#include "threepp/materials/LineBasicMaterial.hpp"
#include "threepp/objects/LineSegments.hpp"

Expand All @@ -11,11 +11,11 @@ using namespace threepp;


SpotLightHelper::SpotLightHelper(SpotLight& light, std::optional<Color> 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();
Expand Down Expand Up @@ -43,8 +43,9 @@ SpotLightHelper::SpotLightHelper(SpotLight& light, std::optional<Color> 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();
}
Expand All @@ -56,15 +57,15 @@ std::shared_ptr<SpotLightHelper> 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);

Expand All @@ -74,12 +75,6 @@ void SpotLightHelper::update() {

} else {

this->cone->material()->as<MaterialWithColor>()->color.copy(this->light.color);
this->cone->material()->as<MaterialWithColor>()->color.copy(this->light->color);
}
}

SpotLightHelper::~SpotLightHelper() {

this->cone->geometry()->dispose();
this->cone->material()->dispose();
}
2 changes: 1 addition & 1 deletion src/threepp/lights/LightShadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void LightShadow::updateMatrices(Light* light) {
shadowCamera->position.copy(_lightPositionWorld);

auto lightWithTarget = dynamic_cast<LightWithTarget*>(light);
_lookTarget.setFromMatrixPosition(*lightWithTarget->target->matrixWorld);
_lookTarget.setFromMatrixPosition(*lightWithTarget->target().matrixWorld);
shadowCamera->lookAt(_lookTarget);
shadowCamera->updateMatrixWorld();

Expand Down
4 changes: 2 additions & 2 deletions src/threepp/renderers/gl/GLLights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void GLLights::setupView(std::vector<Light*>& 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);

Expand All @@ -268,7 +268,7 @@ void GLLights::setupView(std::vector<Light*>& 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);

Expand Down

0 comments on commit 9bf17f3

Please sign in to comment.