添加组件圆,添加可视化碰撞器渲染

This commit is contained in:
2025-06-24 17:24:56 +08:00
parent 0032814ed6
commit a32ce57503
19 changed files with 635 additions and 58 deletions

View File

@ -0,0 +1,72 @@
// Basic circle Texture Shader
#type vertex
#version 450 core
layout(location = 0) in vec3 a_WorldPosition;
layout(location = 1) in vec3 a_LocalPosition;
layout(location = 2) in vec4 a_Color;
layout(location = 3) in float a_Thickness;
layout(location = 4) in float a_Fade;
layout(location = 5) in int a_EntityID;
layout(std140, binding = 0) uniform Camera
{
mat4 u_ViewProjection;
};
struct VertexOutput
{
vec3 LocalPosition;
vec4 Color;
float Thickness;
float Fade;
};
layout (location = 0) out VertexOutput Output;
layout (location = 4) out flat int v_EntityID;
void main()
{
Output.LocalPosition = a_LocalPosition;
Output.Color = a_Color;
Output.Thickness = a_Thickness;
Output.Fade = a_Fade;
v_EntityID = a_EntityID;
gl_Position = u_ViewProjection * vec4(a_WorldPosition, 1.0);
}
#type fragment
#version 450 core
layout(location = 0) out vec4 o_Color;
layout(location = 1) out int o_EntityID;
struct VertexOutput
{
vec3 LocalPosition;
vec4 Color;
float Thickness;
float Fade;
};
layout (location = 0) in VertexOutput Input;
layout (location = 4) in flat int v_EntityID;
void main()
{
float distance = 1.0f - length(Input.LocalPosition);
float circle = smoothstep(0.0f, Input.Fade, distance);
circle *= smoothstep(Input.Thickness + Input.Fade, Input.Thickness, distance);
if(circle == 0.0f)
discard;
o_Color = Input.Color;
o_Color.a *= circle;
o_EntityID = v_EntityID;
}

View File

@ -0,0 +1,49 @@
// Basic Line Texture Shader
#type vertex
#version 450 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
layout(location = 2) in int a_EntityID;
layout(std140, binding = 0) uniform Camera
{
mat4 u_ViewProjection;
};
struct VertexOutput
{
vec4 Color;
};
layout (location = 0) out VertexOutput Output;
layout (location = 1) out flat int v_EntityID;
void main()
{
Output.Color = a_Color;
v_EntityID = a_EntityID;
gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
}
#type fragment
#version 450 core
layout(location = 0) out vec4 o_Color;
layout(location = 1) out int o_EntityID;
struct VertexOutput
{
vec4 Color;
};
layout (location = 0) in VertexOutput Input;
layout (location = 1) in flat int v_EntityID;
void main()
{
o_Color = Input.Color;
o_EntityID = v_EntityID;
}

View File

@ -1,4 +1,4 @@
// Basic Texture Shader
// Basic quad Texture Shader
#type vertex
#version 450 core

View File

@ -96,7 +96,6 @@ namespace Hazel
break;
}
// Renderer2D::BeginScene(m_CameraController.GetCamera());
auto [mx, my] = ImGui::GetMousePos();
mx -= m_ViewPortBounds[0].x;
@ -104,8 +103,8 @@ namespace Hazel
const glm::vec2 viewPortSize = m_ViewPortBounds[1] - m_ViewPortBounds[0];
my = viewPortSize.y - my;
int mouseX = (int)mx;
int mouseY = (int)my;
const int mouseX = (int)mx;
const int mouseY = (int)my;
if (mouseX >= 0 && mouseY >= 0 && mouseX < m_ViewPortSize.x && mouseY < m_ViewPortSize.y)
{
@ -117,11 +116,54 @@ namespace Hazel
// 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);
// Renderer2D::DrawQuad({1, 0}, {1, 1}, m_CheckerBoardTexture);
OnOverLayRender();
// Renderer2D::EndScene();
m_FrameBuffer->UnBind();
}
void EditorLayer::OnOverLayRender()
{
if (m_SceneState == SceneState::Play)
{
Entity camera = m_ActiveScene->GetPrimaryCameraEntity();
Renderer2D::BeginScene(camera.GetComponent<CameraComponent>().Camera, camera.GetComponent<TransformComponent>().GetTransform());
}else
{
Renderer2D::BeginScene(m_EditorCamera);
}
if (m_ShowPhysicsColliders)
{
// box collider
auto bcview = m_ActiveScene->GetAllEntitiesWith<TransformComponent, BoxCollider2DComponent>();
for (auto entity : bcview)
{
auto [tc, bc2D] = bcview.get<TransformComponent, BoxCollider2DComponent>(entity);
glm::vec3 translation = tc.Translation + glm::vec3(bc2D.Offset, 0.001f);
glm::vec3 scale = tc.Scale * glm::vec3(bc2D.Size * 2.0f, 1.0f);
glm::mat4 transform = glm::translate(glm::mat4(1.0f), translation) * glm::rotate(
glm::mat4(1.0f), tc.Rotation.z, glm::vec3(0.0f, 0.0f, 1.0f)) * glm::scale(glm::mat4(1.0f), scale);
Renderer2D::DrawRect(transform, glm::vec4(0.2, 1.0f, 0.2f, 1.0f));
}
// circle collider
auto ccview = m_ActiveScene->GetAllEntitiesWith<TransformComponent, CircleCollider2DComponent>();
for (auto entity : ccview)
{
auto [tc, cc2D] = ccview.get<TransformComponent, CircleCollider2DComponent>(entity);
glm::vec3 translation = tc.Translation + glm::vec3(cc2D.Offset, 0.001f);
glm::vec3 scale = tc.Scale * glm::vec3(cc2D.Radius * 2.0f);
glm::mat4 transform = glm::translate(glm::mat4(1.0f), translation) * glm::scale(glm::mat4(1.0f), scale);
Renderer2D::DrawCircle(transform, glm::vec4(0.2, 1.0f, 0.2f, 1.0f), Renderer2D::GetLineWidth() * 0.01f);
}
}
Renderer2D::EndScene();
}
void EditorLayer::OnImGuiRender()
{
static bool showDockspace = true;
@ -229,12 +271,25 @@ namespace Hazel
ImGui::Separator();
auto editorCameraPos = m_EditorCamera.GetPosition();
ImGui::Text("Editor Camera(%.3f, %.3f, %.3f)", editorCameraPos.x, editorCameraPos.y, editorCameraPos.z);
ImGui::Text("Editor Camera Distance: %f", m_EditorCamera.GetDistance());
float lineWidth = Renderer2D::GetLineWidth();
if (ImGui::DragFloat("LineWidth", &lineWidth, 0.01f, 0.0f, 10.0f))
{
Renderer2D::SetLineWidth(lineWidth);
}
// std::string name = "none";
// if (m_HoveredEntity)
// name = m_HoveredEntity.GetComponent<TagComponent>().Tag;
// ImGui::Text("Hovered Entity: %s", name.c_str());
ImGui::Separator();
ImGui::Checkbox("Show Colliders", &m_ShowPhysicsColliders);
ImGui::End();
}
@ -464,11 +519,12 @@ namespace Hazel
#define SHORTCUT_CTRL_D (SDL_KMOD_CTRL + SDLK_D)
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;
if (e.key.down && e.key.repeat == 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_EXIT:
@ -539,7 +595,6 @@ namespace Hazel
}
ImGui::PopStyleVar(2);
ImGui::PopStyleColor(3);
ImGui::End();

View File

@ -41,6 +41,8 @@ namespace Hazel
void ChangeOptMode(unsigned int mode);
void OnOverLayRender();
private:
OrthographicCameraController m_CameraController;
@ -63,12 +65,15 @@ namespace Hazel
glm::vec2 m_ViewPortBounds[2];
Ref<FrameBuffer> m_FrameBuffer;
Ref<FrameBuffer> m_RuntimeFrameBuffer;
SceneHierachyPanel m_SceneHierachyPanel;
ContentBroswerPanel m_ContentBroswerPanel;
int m_GizmoType = -1;
bool m_ShowPhysicsColliders = false;
enum class SceneState
{
Edit = 0, Play = 1

View File

@ -265,13 +265,22 @@ namespace Hazel
if (!m_SelectionContext.HasComponent<SpriteRendererComponent>())
{
if (ImGui::MenuItem("Sprite"))
if (ImGui::MenuItem("Sprite Renderer"))
{
m_SelectionContext.AddComponent<SpriteRendererComponent>();
ImGui::CloseCurrentPopup();
}
}
if (!m_SelectionContext.HasComponent<CircleRendererComponent>())
{
if (ImGui::MenuItem("Circle Renderer"))
{
m_SelectionContext.AddComponent<CircleRendererComponent>();
ImGui::CloseCurrentPopup();
}
}
if (!m_SelectionContext.HasComponent<RigidBody2DComponent>())
{
if (ImGui::MenuItem("RigidBody 2D"))
@ -290,6 +299,15 @@ namespace Hazel
}
}
if (!m_SelectionContext.HasComponent<CircleCollider2DComponent>())
{
if (ImGui::MenuItem("Circle Collider 2D"))
{
m_SelectionContext.AddComponent<CircleCollider2DComponent>();
ImGui::CloseCurrentPopup();
}
}
ImGui::EndPopup();
}
ImGui::PopItemWidth();
@ -385,6 +403,13 @@ namespace Hazel
ImGui::DragFloat("Tiling Color", &component.TilingFactor, 0.1f, 0.0f, 100.f);
});
DrawComponent<CircleRendererComponent>("Circle Renderer", entity, [](auto& component)
{
ImGui::ColorEdit4("Color", glm::value_ptr(component.Color));
// ImGui::DragFloat("Radius", &component.Radius, 0.01f, 0.0f, 1.f);
ImGui::DragFloat("Thickness", &component.Thickness, 0.01f, 0.0f, 1.f);
ImGui::DragFloat("Fade", &component.Fade, 0.00001f, 0.0f, 1.f);
});
DrawComponent<RigidBody2DComponent>("Rigidbody 2D", entity, [](auto& component)
{
@ -415,8 +440,19 @@ namespace Hazel
DrawComponent<BoxCollider2DComponent>("Box Collider 2D", entity, [](auto& component)
{
ImGui::DragFloat2("Offset", glm::value_ptr(component.Offset));
ImGui::DragFloat2("Size", glm::value_ptr(component.Size));
ImGui::DragFloat2("Offset", glm::value_ptr(component.Offset), 0.001f);
ImGui::DragFloat2("Size", glm::value_ptr(component.Size), 0.001f);
ImGui::DragFloat("Density", &component.Density, 0.01f, 0.0f);
ImGui::DragFloat("Friction", &component.Friction, 0.01f, 0.0f, 1.0f);
ImGui::DragFloat("Restitution", &component.Restitution, 0.01f, 0.0f, 1.0f);
ImGui::DragFloat("Restitution Threshold", &component.RestitutionThreshold, 0.01f, 0.0f);
});
DrawComponent<CircleCollider2DComponent>("Circle Collider 2D", entity, [](auto& component)
{
ImGui::DragFloat2("Offset", glm::value_ptr(component.Offset), 0.001f);
ImGui::DragFloat("Radius", &component.Radius, 0.001f);
ImGui::DragFloat("Density", &component.Density, 0.01f, 0.0f);
ImGui::DragFloat("Friction", &component.Friction, 0.01f, 0.0f, 1.0f);

View File

@ -132,8 +132,8 @@ ExampleLayer::ExampleLayer() : Layer("ExampleLayer"), m_CameraController(1280.0f
// Texture.Shader
// m_TextureShader = Hazel::Shader::Create("assets/shaders/Texture.glsl");
auto textureShader = m_ShaderLibrary.Load("assets/shaders/Texture.glsl");
// m_TextureShader = Hazel::Shader::Create("assets/shaders/Renderer2D_quad.glsl");
auto textureShader = m_ShaderLibrary.Load("assets/shaders/Renderer2D_quad.glsl");
// Texture