WIP: add select on click

This commit is contained in:
SweetId 2023-11-21 20:06:18 +05:30
parent e79a629257
commit 6487acadca
2 changed files with 49 additions and 7 deletions

View File

@ -28,15 +28,19 @@ namespace Nz
auto& registry = m_world->GetRegistry();
for (auto&& entity : registry.storage<entt::entity>().each())
{
Nz::GraphicsComponent* component = registry.try_get<Nz::GraphicsComponent>(std::get<entt::entity>(entity));
if (component != nullptr)
Nz::GraphicsComponent* graphics = registry.try_get<Nz::GraphicsComponent>(std::get<entt::entity>(entity));
Nz::NodeComponent* transform = registry.try_get<Nz::NodeComponent>(std::get<entt::entity>(entity));
if (transform != nullptr && graphics != nullptr)
{
BoundingVolumef boundingVolume(graphics->GetAABB());
boundingVolume.Update(transform->GetTransformMatrix());
float distance = 0;
if (ray.Intersect(component->GetAABB(), &distance))
if (ray.Intersect(boundingVolume, &distance))
{
entities.push_back({
.entity = entt::handle(registry, std::get<entt::entity>(entity)),
.position = ray.origin + ray.direction * distance,
.position = ray.GetPoint(distance),
.distance = distance
});
}

View File

@ -1,7 +1,9 @@
#include <NazaraEditor/Core/Components/CameraComponent.hpp>
#include <NazaraEditor/Core/Application/BaseApplication.hpp>
#include <Nazara/Graphics/Components/CameraComponent.hpp>
#include <Nazara/Math/Ray.hpp>
namespace
{
// Unity SmoothDamp function
@ -43,8 +45,10 @@ namespace
namespace Nz
{
EditorCameraComponent::EditorCameraComponent()
: m_targetAngles(Nz::EulerAnglesf::Zero())
EditorCameraComponent::EditorCameraComponent(Nz::Camera& camera, Nz::DebugDrawer& debugDrawer)
: m_camera(camera)
, m_debugDrawer(debugDrawer)
, m_targetAngles(Nz::EulerAnglesf::Zero())
, m_targetPosition(Nz::Vector3f::Zero())
, m_currentVelocity(Nz::Vector3f::Zero())
, m_moveSpeed(3.f)
@ -52,6 +56,14 @@ namespace Nz
{
auto& handler = EditorBaseApplication::Instance()->GetWindow()->GetEventHandler();
m_onMouseClicked.Connect(handler.OnMouseButtonReleased, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseButtonEvent& event)
{
if (event.button == Nz::Mouse::Button::Left)
{
RaycastSelection(event.x, event.y);
}
});
m_onMouseMoved.Connect(handler.OnMouseMoved, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
{
if (Nz::Mouse::IsButtonPressed(Nz::Mouse::Button::Right))
@ -103,5 +115,31 @@ namespace Nz
node.SetPosition(currentPosition);
node.SetRotation(currentRotation);
if (!m_debugClock.IsPaused())
{
if (m_debugClock.GetElapsedTime().AsSeconds() > 100)
m_debugClock.Pause();
m_debugDrawer.DrawLine(m_lastRay.origin, m_lastRay.direction * 500 + m_lastRay.origin, Nz::Color::Green());
}
}
void EditorCameraComponent::RaycastSelection(int x, int y)
{
auto near = m_camera.Unproject(Nz::Vector3f(x, y, 0));
auto far = m_camera.Unproject(Nz::Vector3f(x, y, 1));
Nz::Rayf ray(near, (far - near).Normalize());
auto entities = EditorBaseApplication::Instance()->GetLevel().Raycast(ray);
if (!entities.empty())
{
EditorBaseApplication::Instance()->OnEntitySelected(entities.front().entity);
}
m_lastRay = ray;
m_debugClock.Restart();
}
}