简单添加鼠标拾取功能

This commit is contained in:
2025-06-06 21:49:43 +08:00
parent 8c0eb4314c
commit de75ee9481
12 changed files with 461 additions and 116 deletions

View File

@ -6,20 +6,23 @@ layout(location = 1) in vec4 a_Color;
layout(location = 2) in vec2 a_TexCoord;
layout(location = 3) in float a_TexIndex;
layout(location = 4) in float a_TilingFactor;
layout(location = 5) in int a_EntityID;
uniform mat4 u_ViewProjection;
//uniform mat4 u_Transform;
out vec2 v_TexCoord;
out vec4 v_Color;
out float v_TexIndex;
out flat float v_TexIndex;
out float v_TilingFactor;
out flat int v_EntityID;
void main() {
v_TexCoord = a_TexCoord;
v_Color = a_Color;
v_TexIndex = a_TexIndex;
v_TilingFactor = a_TilingFactor;
v_EntityID = a_EntityID;
gl_Position = u_ViewProjection * vec4(a_Position, 1.0f);
}
@ -28,11 +31,13 @@ void main() {
#version 460 core
layout(location = 0) out vec4 color;
layout(location = 1) out int color2;
in vec2 v_TexCoord;
in vec4 v_Color;
in float v_TexIndex;
in flat float v_TexIndex;
in float v_TilingFactor;
in flat int v_EntityID;
uniform vec4 u_Color;
uniform sampler2D u_Textures[32];
@ -41,4 +46,5 @@ void main() {
color = texture(u_Textures[int(v_TexIndex)], v_TexCoord * v_TilingFactor) * v_Color;
// color = v_Color;
color2 = v_EntityID;
}

View File

@ -27,6 +27,7 @@ namespace Hazel
FrameBufferSpecification spec;
spec.Width = Application::Get().GetWindow().GetWidth();
spec.Height = Application::Get().GetWindow().GetHeight();
spec.Attachments = { FrameBufferTextureFormat::RGBA8, FrameBufferTextureFormat::RED_INTEGER,FrameBufferTextureFormat::DEPTH };
m_FrameBuffer = FrameBuffer::Create(spec);
m_ViewPortSize = { spec.Width, spec.Height };
@ -73,12 +74,28 @@ namespace Hazel
RendererCommand::SetClearColor(m_BackgroundColor);
RendererCommand::Clear();
m_FrameBuffer->ClearAttachment(1, -1);
// Renderer2D::BeginScene(m_CameraController.GetCamera());
// update Scene
m_ActiveScene->OnUpdateEditor(ts, m_EditorCamera);
auto [mx, my] = ImGui::GetMousePos();
mx -= m_ViewPortBounds[0].x;
my -= m_ViewPortBounds[0].y;
const glm::vec2 viewPortSize = m_ViewPortBounds[1] - m_ViewPortBounds[0];
my = viewPortSize.y - my;
int mouseX = (int)mx;
int mouseY = (int)my;
if (mouseX >= 0 && mouseY >= 0 && mouseX < m_ViewPortSize.x && mouseY < m_ViewPortSize.y)
{
int pixelData = m_FrameBuffer->ReadPixel(1, mouseX, mouseY);
m_HoveredEntity = pixelData == -1 ? Entity{} : Entity{ (entt::entity)pixelData, m_ActiveScene.get()};
}
// Renderer2D::DrawQuad({0, 0.5f}, {1.0f, 1.0f}, {0.2f, 0.3f, 0.8f, 1.0f});
// Renderer2D::DrawQuad({0, -0.5f}, {1.0f, 1.0f}, {0.8f, 0.2f, 0.2f, 1.0f});
// Renderer2D::DrawQuad({-1, 0}, {1, 1}, m_LogoTexture);
@ -138,9 +155,9 @@ namespace Hazel
ImGui::PopStyleVar(2);
// Submit the DockSpace
ImGuiIO& io = ImGui::GetIO();
ImGuiStyle& style = ImGui::GetStyle();
style.WindowMinSize.x = 350.0f;
ImGuiIO& io = ImGui::GetIO();
ImGuiStyle& style = ImGui::GetStyle();
style.WindowMinSize.x = 350.0f;
if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable)
{
ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
@ -156,14 +173,14 @@ namespace Hazel
// ImGui::MenuItem("Fullscreen", NULL, &opt_fullscreen);
// ImGui::MenuItem("Padding", NULL, &opt_padding);
// ImGui::Separator();
if (ImGui::MenuItem("New", "Ctrl+N"))
NewScene();
if (ImGui::MenuItem("Open...", "Ctrl+O"))
OpenScene();
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S"))
SaveScene();
if (ImGui::MenuItem("New", "Ctrl+N"))
NewScene();
if (ImGui::MenuItem("Open...", "Ctrl+O"))
OpenScene();
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S"))
SaveScene();
ImGui::Separator();
ImGui::Separator();
if (ImGui::MenuItem("Exit")) { Hazel::Application::Get().Close(); }
ImGui::EndMenu();
@ -172,10 +189,10 @@ namespace Hazel
ImGui::EndMenuBar();
}
// Scene Hierachy Panel
m_SceneHierachyPanel.OnImGuiRender();
// Scene Hierachy Panel
m_SceneHierachyPanel.OnImGuiRender();
// Render Status
// Render Status
{
ImGui::Begin("Render Status");
@ -189,15 +206,25 @@ namespace Hazel
ImGui::Separator();
ImGui::Text("viewPortSize: (%.2f, %.2f)", m_ViewPortSize.x, m_ViewPortSize.y);
ImGui::Separator();
std::string name = "none";
if (m_HoveredEntity)
name = m_HoveredEntity.GetComponent<TagComponent>().Tag;
ImGui::Text("Hovered Entity: %s", name.c_str());
ImGui::End();
}
// ViewPort
// ViewPort
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0.0f, 0.0f});
ImGui::Begin("Viewport");
m_ViewportFocused = ImGui::IsWindowFocused();
m_ViewportHovered = ImGui::IsWindowHovered();
auto viewportOffet = ImGui::GetCursorPos();
m_ViewportFocused = ImGui::IsWindowFocused();
m_ViewportHovered = ImGui::IsWindowHovered();
ImVec2 viewPortPanelSize = ImGui::GetContentRegionAvail();
@ -209,21 +236,34 @@ namespace Hazel
ImGui::Image(m_FrameBuffer->GetColorAttachmentID(), {m_ViewPortSize.x, m_ViewPortSize.y}, {0, 1},
{1, 0});
Entity selectedEntity = m_SceneHierachyPanel.GetSelectedEntity();
if (selectedEntity)
{
ImGuizmo::SetOrthographic(false);
ImGuizmo::SetDrawlist();
const float windowWidth = ImGui::GetWindowWidth();
const float windowHeight = ImGui::GetWindowHeight();
auto windowsSize = ImGui::GetWindowSize();
ImVec2 minBound = ImGui::GetWindowPos();
const ImVec2 windowPos = ImGui::GetWindowPos();
ImGuizmo::SetRect(windowPos.x,windowPos.y,windowWidth,windowHeight);
minBound.x += viewportOffet.x;
minBound.y += viewportOffet.y + m_ViewPortSize.y - windowsSize.y;
// auto cameraEntity = m_ActiveScene->GetPrimaryCameraEntity();
if (m_GizmoType != -1)
ImVec2 maxBound = {minBound.x + windowsSize.x, minBound.y + windowsSize.y};
m_ViewPortBounds[0] = {minBound.x, minBound.y};
m_ViewPortBounds[1] = {maxBound.x, maxBound.y};
// ImGuizmo
Entity selectedEntity = m_SceneHierachyPanel.GetSelectedEntity();
if (selectedEntity)
{
ImGuizmo::SetOrthographic(false);
ImGuizmo::SetDrawlist();
const float windowWidth = ImGui::GetWindowWidth();
const float windowHeight = ImGui::GetWindowHeight();
const ImVec2 windowPos = ImGui::GetWindowPos();
ImGuizmo::SetRect(windowPos.x, windowPos.y, windowWidth, windowHeight);
// auto cameraEntity = m_ActiveScene->GetPrimaryCameraEntity();
if (m_GizmoType != -1)
{
// const auto& camera = cameraEntity.GetComponent<CameraComponent>().Camera;
auto& tc = selectedEntity.GetComponent<TransformComponent>();
@ -231,38 +271,40 @@ namespace Hazel
// const glm::mat4& cameraProjection = camera.GetProjection();
// glm::mat4 cameraView = glm::inverse(cameraEntity.GetComponent<TransformComponent>().GetTransform());
glm::mat4 cameraProjection = m_EditorCamera.GetProjection();
glm::mat4 cameraView = m_EditorCamera.GetViewMatrix();
glm::mat4 transform = tc.GetTransform();
bool snap = SDL_GetModState() & SDL_KMOD_CTRL;
float snapValue = 0.5f;
if (m_GizmoType == ImGuizmo::OPERATION::TRANSLATE)
snapValue = 0.25f;
else if (m_GizmoType == ImGuizmo::OPERATION::ROTATE)
snapValue = 15.0f;
else if (m_GizmoType == ImGuizmo::OPERATION::SCALE)
snapValue = 0.25f;
glm::mat4 cameraProjection = m_EditorCamera.GetProjection();
glm::mat4 cameraView = m_EditorCamera.GetViewMatrix();
glm::mat4 transform = tc.GetTransform();
bool snap = SDL_GetModState() & SDL_KMOD_CTRL;
float snapValue = 0.5f;
if (m_GizmoType == ImGuizmo::OPERATION::TRANSLATE)
snapValue = 0.25f;
else if (m_GizmoType == ImGuizmo::OPERATION::ROTATE)
snapValue = 15.0f;
else if (m_GizmoType == ImGuizmo::OPERATION::SCALE)
snapValue = 0.25f;
float snapValues[3] = { snapValue, snapValue, snapValue };
float snapValues[3] = {snapValue, snapValue, snapValue};
if (ImGuizmo::Manipulate(glm::value_ptr(cameraView), glm::value_ptr(cameraProjection), ImGuizmo::OPERATION(m_GizmoType), ImGuizmo::LOCAL,glm::value_ptr(transform), nullptr, snap ? snapValues : nullptr))
if (ImGuizmo::Manipulate(glm::value_ptr(cameraView), glm::value_ptr(cameraProjection),
ImGuizmo::OPERATION(m_GizmoType), ImGuizmo::LOCAL,
glm::value_ptr(transform), nullptr, snap ? snapValues : nullptr))
{
if (ImGuizmo::IsUsing())
{
glm::vec3 translation, rotation, scale;
glm::vec3 translation, rotation, scale;
Hazel::Math::DecomposeTransform(transform, translation, rotation, scale);
Hazel::Math::DecomposeTransform(transform, translation, rotation, scale);
glm::vec3 deltaRotation = rotation - tc.Rotation;
glm::vec3 deltaRotation = rotation - tc.Rotation;
tc.Translation = translation;
tc.Rotation += deltaRotation;
tc.Scale = scale;
tc.Translation = translation;
tc.Rotation += deltaRotation;
tc.Scale = scale;
}
}
}
}
}
ImGui::End();
ImGui::PopStyleVar();
@ -273,7 +315,7 @@ namespace Hazel
void EditorLayer::SaveScene() const
{
std::string filepath = FileDiaglogs::SaveFile("Hazel Scene (*.scene)\0*.scene\0");
std::string filepath = FileDiaglogs::SaveFile("Hazel Scene (*.scene,*.yaml)\0*.scene;*.yaml\0*\0*.*\0\0");
if (!filepath.empty())
{
SceneSerializer serializer(m_ActiveScene);
@ -283,7 +325,7 @@ namespace Hazel
void EditorLayer::OpenScene()
{
std::string filepath = FileDiaglogs::OpenFile("Hazel Scene (*.scene)\0*.scene\0");
std::string filepath = FileDiaglogs::OpenFile("Scene(*.scene, *.yaml)\0*.scene;*.yaml\0All files\0*.*\0\0");
if (!filepath.empty())
{
m_ActiveScene = CreateRef<Scene>();
@ -302,11 +344,10 @@ namespace Hazel
m_SceneHierachyPanel.SetContext(m_ActiveScene);
}
void EditorLayer::ChangeOptMode(unsigned int mode)
void EditorLayer::ChangeOptMode(unsigned int mode)
{
if (m_ViewportHovered)
m_GizmoType = mode;
m_GizmoType = mode;
}
void EditorLayer::OnEvent(SDL_Event& e)
@ -314,46 +355,45 @@ namespace Hazel
if (m_ViewportFocused && m_ViewportHovered)
{
m_CameraController.OnEvent(e);
m_EditorCamera.OnEvent(e);
m_EditorCamera.OnEvent(e);
}
#define SHORTCUT_NEW (SDL_KMOD_CTRL | SDLK_N)
#define SHORTCUT_OPEN (SDL_KMOD_CTRL | SDLK_O)
#define SHORTCUT_SAVE_ALL (SDL_KMOD_CTRL | SDL_KMOD_SHIFT | SDLK_S)
const auto mod = SDL_GetModState();
const auto ctrl = (mod & SDL_KMOD_CTRL) ? SDL_KMOD_CTRL : 0;
const auto shift = (mod & SDL_KMOD_SHIFT) ? SDL_KMOD_SHIFT : 0;
const auto mod = SDL_GetModState();
const auto ctrl = (mod & SDL_KMOD_CTRL) ? SDL_KMOD_CTRL : 0;
const auto shift = (mod & SDL_KMOD_SHIFT) ? SDL_KMOD_SHIFT : 0;
switch (ctrl | shift | e.key.key)
{
case SHORTCUT_NEW:
NewScene();
break;
case SHORTCUT_OPEN:
OpenScene();
break;
case SHORTCUT_SAVE_ALL:
SaveScene();
break;
{
case SHORTCUT_NEW:
NewScene();
break;
case SHORTCUT_OPEN:
OpenScene();
break;
case SHORTCUT_SAVE_ALL:
SaveScene();
break;
// GIZMO
case SDLK_Q:
ChangeOptMode(-1);
break;
case SDLK_W:
ChangeOptMode(ImGuizmo::OPERATION::TRANSLATE);
break;
case SDLK_E:
ChangeOptMode(ImGuizmo::OPERATION::SCALE);
break;
case SDLK_R:
ChangeOptMode(ImGuizmo::OPERATION::ROTATE);
break;
// GIZMO
case SDLK_Q:
ChangeOptMode(-1);
break;
case SDLK_W:
ChangeOptMode(ImGuizmo::OPERATION::TRANSLATE);
break;
case SDLK_E:
ChangeOptMode(ImGuizmo::OPERATION::SCALE);
break;
case SDLK_R:
ChangeOptMode(ImGuizmo::OPERATION::ROTATE);
break;
default:
break;
}
default:
break;
}
}
}

View File

@ -37,11 +37,14 @@ namespace Hazel
Ref<Scene> m_ActiveScene;
EditorCamera m_EditorCamera;
Entity m_HoveredEntity;
glm::vec4 m_BackgroundColor = { 0.2f, 0.2f, 0.2f, 1.0f };
glm::vec4 m_SquareColor = { 0.2f, 0.2f, 0.2f, 1.0f };
bool m_ViewportFocused = false, m_ViewportHovered = false;
glm::vec2 m_ViewPortSize = {0, 0};
glm::vec2 m_ViewPortBounds[2];
Ref<FrameBuffer> m_FrameBuffer;