From 48db6afe6da5d87c5b8e6bfb357c3210fed294bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Sat, 27 Jul 2024 06:27:49 +0200 Subject: [PATCH] editor: snap to surface transform --- Editor/Editor.cpp | 3 ++- Editor/Translator.cpp | 28 ++++++++++++++++++++++++++-- Editor/Translator.h | 1 + WickedEngine/wiScene.cpp | 2 +- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 334fa37f3..4dd8e4524 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -884,7 +884,8 @@ void EditorComponent::Load() ss += "Select: Left mouse button\n"; ss += "Interact with physics/water/grass: Middle mouse button\n"; ss += "Faster camera: Left Shift button or controller R2/RT\n"; - ss += "Snap transform: Left Ctrl (hold while transforming)\n"; + ss += "Snap to grid transform: Ctrl (hold while transforming)\n"; + ss += "Snap to surface transform: Shift (hold while transforming)\n"; ss += "Camera up: E\n"; ss += "Camera down: Q\n"; ss += "Duplicate entity: Ctrl + D\n"; diff --git a/Editor/Translator.cpp b/Editor/Translator.cpp index 9195ea36a..56740de66 100644 --- a/Editor/Translator.cpp +++ b/Editor/Translator.cpp @@ -453,9 +453,9 @@ void Translator::Update(const CameraComponent& camera, const XMFLOAT4& currentMo transform.Scale(scale); } - if (wi::input::Down(wi::input::BUTTON::KEYBOARD_BUTTON_LCONTROL)) + if (wi::input::Down(wi::input::BUTTON::KEYBOARD_BUTTON_LCONTROL) || wi::input::Down(wi::input::BUTTON::KEYBOARD_BUTTON_RCONTROL)) { - // Snap mode: + // Snap to grid mode: if (isTranslator) { transform.translation_local.x = std::round(transform.translation_local.x / translate_snap) * translate_snap; @@ -469,6 +469,30 @@ void Translator::Update(const CameraComponent& camera, const XMFLOAT4& currentMo transform.scale_local.z = std::max(scale_snap, std::round(transform.scale_local.z / scale_snap) * scale_snap); } } + if (wi::input::Down(wi::input::BUTTON::KEYBOARD_BUTTON_LSHIFT) || wi::input::Down(wi::input::BUTTON::KEYBOARD_BUTTON_RSHIFT)) + { + // Snap to surface mode: + temp_filters.reserve(selected.size()); + for (auto& x : selected) + { + ObjectComponent* object = scene.objects.GetComponent(x.entity); + if (object == nullptr) + continue; + temp_filters.push_back(object->filterMask); + object->filterMask = 0; + } + Ray ray = wi::renderer::GetPickRay((long)currentMouse.x, (long)currentMouse.y, canvas, camera); + wi::scene::Scene::RayIntersectionResult result = scene.Intersects(ray, wi::enums::FILTER_OBJECT_ALL); + transform.translation_local = result.position; + size_t ind = 0; + for (auto& x : selected) + { + ObjectComponent* object = scene.objects.GetComponent(x.entity); + if (object == nullptr) + continue; + object->filterMask = temp_filters[ind++]; + } + } } transform.UpdateTransform(); diff --git a/Editor/Translator.h b/Editor/Translator.h index ff960d51e..63339fe7f 100644 --- a/Editor/Translator.h +++ b/Editor/Translator.h @@ -11,6 +11,7 @@ private: float angle = 0; float angle_start = 0; bool has_selected_transform = false; + wi::vector temp_filters; public: void Update(const wi::scene::CameraComponent& camera, const XMFLOAT4& currentMouse, const wi::Canvas& canvas); diff --git a/WickedEngine/wiScene.cpp b/WickedEngine/wiScene.cpp index 4b298da89..1e5f2fd47 100644 --- a/WickedEngine/wiScene.cpp +++ b/WickedEngine/wiScene.cpp @@ -5333,7 +5333,7 @@ namespace wi::scene return result; } - bool Scene::IntersectsFirst(const wi::primitive::Ray& ray, uint32_t filterMask, uint32_t layerMask, uint32_t lod) const + bool Scene::IntersectsFirst(const Ray& ray, uint32_t filterMask, uint32_t layerMask, uint32_t lod) const { bool result = false;