Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/controls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

add_example(NAME "drag" WEB)
add_example(NAME "transform" WEB)
add_example(NAME "fly" WEB WEB_EMBED
"${PROJECT_SOURCE_DIR}/data/textures/planets@data/textures/planets"
)
4 changes: 2 additions & 2 deletions examples/controls/drag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ int main() {
DragControls controls(objects, camera, canvas);
controls.rotateSpeed = 2;

struct HoverListener: public EventListener {
struct HoverListener: EventListener {
void onEvent(Event& event) override {

auto target = static_cast<Object3D*>(event.target);
auto target = std::any_cast<Object3D*>(event.target);
auto& color = target->material()->as<MaterialWithColor>()->color;

if (event.type == "hoveron") {
Expand Down
102 changes: 102 additions & 0 deletions examples/controls/transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

#include "threepp/controls/TransformControls.hpp"
#include "threepp/threepp.hpp"

#include <iostream>

using namespace threepp;

int main() {

Canvas canvas(Canvas::Parameters()
.title("Transform controls")
.exitOnKeyEscape(false));

GLRenderer renderer(canvas.size());
renderer.shadowMap().enabled = true;
renderer.shadowMap().type = ShadowMap::PFC;

PerspectiveCamera camera(60, canvas.aspect());
camera.position.set(0,5,5);

Scene scene;
scene.background = Color::aliceblue;

scene.add(AmbientLight::create(0xaaaaaa));

auto light = SpotLight::create(0xffffff, 1.f);
light->position.set(0, 25, 50);
light->angle = math::PI / 9;

light->castShadow = true;
light->shadow->camera->as<PerspectiveCamera>()->nearPlane = 10;
light->shadow->camera->as<PerspectiveCamera>()->farPlane = 100;
light->shadow->mapSize.x = 1024;
light->shadow->mapSize.y = 1024;

scene.add(light);

TextureLoader tl;
auto tex = tl.load(std::string(DATA_FOLDER) + "/textures/crate.gif");

auto material = MeshBasicMaterial::create();
material->transparent = true;
material->opacity = 0.7f;
material->map = tex;
auto object = Mesh::create(BoxGeometry::create(), material);
scene.add(object);


auto grid = GridHelper::create(10, 10);
scene.add(grid);

OrbitControls orbitControls(camera, canvas);

TransformControls controls(camera, canvas);
controls.attach(*object);
scene.add(controls);

LambdaEventListener changeListener([&](Event& event) {
orbitControls.enabled = !std::any_cast<bool>(event.target);
});

controls.addEventListener("dragging-changed", changeListener);

KeyAdapter adapter(KeyAdapter::Mode::KEY_PRESSED, [&](KeyEvent evt) {
switch (evt.key) {
case Key::Q: {
controls.setSpace(controls.getSpace() == "local" ? "world" : "local");
break;
}
case Key::W: {
controls.setMode("translate");
break;
}
case Key::E: {
controls.setMode("rotate");
break;
}
case Key::R: {
controls.setMode("scale");
break;
}
case Key::SPACE: {
controls.enabled = !controls.enabled;
break;
}
}
});
canvas.addKeyListener(adapter);


canvas.onWindowResize([&](WindowSize size) {
camera.aspect = size.aspect();
camera.updateProjectionMatrix();

renderer.setSize(size);
});

canvas.animate([&] {
renderer.render(scene, camera);
});
}
44 changes: 44 additions & 0 deletions include/threepp/controls/TransformControls.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// https://github.com/mrdoob/three.js/blob/r129/examples/jsm/controls/TransformControls.js

#ifndef THREEPP_TRANSFORMCONTROLS_HPP
#define THREEPP_TRANSFORMCONTROLS_HPP

#include "threepp/core/Object3D.hpp"

#include <memory>

namespace threepp {

class Camera;
class PeripheralsEventSource;

class TransformControls: public Object3D {

public:

bool enabled = true;

TransformControls(Camera& camera, PeripheralsEventSource& canvas);

void setSpace(const std::string& space);

[[nodiscard]] std::string getSpace() const;

void setMode(const std::string& mode);

TransformControls& attach(Object3D& object);

TransformControls& detach();

void updateMatrixWorld(bool force) override;

~TransformControls() override;

private:
struct Impl;
std::unique_ptr<Impl> pimpl_;
};

}// namespace threepp

#endif//THREEPP_TRANSFORMCONTROLS_HPP
18 changes: 16 additions & 2 deletions include/threepp/core/EventDispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
#ifndef THREEPP_EVENTDISPATCHER_HPP
#define THREEPP_EVENTDISPATCHER_HPP

#include <any>
#include <string>
#include <unordered_map>
#include <vector>
#include <functional>


namespace threepp {

struct Event {

const std::string type;
void* target;
std::any target;
};

struct EventListener {
Expand All @@ -23,6 +25,18 @@ namespace threepp {
virtual ~EventListener() = default;
};

struct LambdaEventListener: EventListener {

explicit LambdaEventListener(std::function<void(Event&)> f): f_(std::move(f)) {}

void onEvent(Event& event) override {
f_(event);
}

private:
std::function<void(Event&)> f_;
};

class EventDispatcher {

public:
Expand All @@ -32,7 +46,7 @@ namespace threepp {

void removeEventListener(const std::string& type, const EventListener& listener);

void dispatchEvent(const std::string& type, void* target = nullptr);
void dispatchEvent(const std::string& type, std::any target = {});

virtual ~EventDispatcher() = default;

Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(publicHeaders
"threepp/controls/DragControls.hpp"
"threepp/controls/FlyControls.hpp"
"threepp/controls/OrbitControls.hpp"
"threepp/controls/TransformControls.hpp"

"threepp/core/BufferAttribute.hpp"
"threepp/core/BufferGeometry.hpp"
Expand Down Expand Up @@ -273,6 +274,7 @@ set(sources
"threepp/controls/DragControls.cpp"
"threepp/controls/FlyControls.cpp"
"threepp/controls/OrbitControls.cpp"
"threepp/controls/TransformControls.cpp"

"threepp/core/BufferGeometry.cpp"
"threepp/core/Clock.cpp"
Expand Down
Loading
Loading