add MSAA, renderer2D, boundingbox

This commit is contained in:
2025-12-01 22:14:22 +08:00
parent 0d4024be39
commit c96eb34af5
44 changed files with 1185 additions and 289 deletions

View File

@ -4,6 +4,8 @@
#include "EditorLayer.h"
#include "ImGuizmo.h"
#include "Prism/Core/Input.h"
#include "Prism/Renderer/Renderer2D.h"
namespace Prism
{
@ -12,16 +14,18 @@ namespace Prism
None = 0, ColorProperty = 1
};
void Property(const std::string& name, bool& value)
bool Property(const std::string& name, bool& value)
{
ImGui::Text(name.c_str());
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
std::string id = "##" + name;
ImGui::Checkbox(id.c_str(), &value);
const std::string id = "##" + name;
const bool result = ImGui::Checkbox(id.c_str(), &value);
ImGui::PopItemWidth();
ImGui::NextColumn();
return result;
}
void Property(const std::string& name, float& value, float min = -1.0f, float max = 1.0f,
@ -137,14 +141,16 @@ namespace Prism
m_Scene->SetEnvironment(environment);
m_MeshEntity = m_Scene->CreateEntity();
const auto mesh = CreateRef<Mesh>("assets/models/m1911/m1911.fbx");
//auto mesh = CreateRef<Mesh>("assets/meshes/cerberus/CerberusMaterials.fbx");
// auto mesh = CreateRef<Mesh>("assets/models/m1911/M1911Materials.fbx");
m_MeshEntity = m_Scene->CreateEntity("test Entity");
auto mesh = CreateRef<Mesh>("assets/meshes/TestScene.fbx");
m_MeshEntity->SetMesh(mesh);
m_MeshMaterial = mesh->GetMaterial();
auto secondEntity = m_Scene->CreateEntity("Gun Entity");
secondEntity->Transform() = glm::translate(glm::mat4(1.0f), { 5, 5, 5 }) * glm::scale(glm::mat4(1.0f), {10, 10, 10});
mesh = CreateRef<Mesh>("assets/models/m1911/m1911.fbx");
secondEntity->SetMesh(mesh);
}
// Sphere Scene
@ -213,43 +219,55 @@ namespace Prism
void EditorLayer::OnUpdate(const TimeStep deltaTime)
{
// THINGS TO LOOK AT:
// - BRDF LUT
// - Tonemapping and proper HDR pipeline
using namespace Prism;
using namespace glm;
m_MeshMaterial->Set("u_AlbedoColor", m_AlbedoInput.Color);
m_MeshMaterial->Set("u_Metalness", m_MetalnessInput.Value);
m_MeshMaterial->Set("u_Roughness", m_RoughnessInput.Value);
m_MeshMaterial->Set("lights", m_Light);
m_MeshMaterial->Set("u_AlbedoTexToggle", m_AlbedoInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_NormalTexToggle", m_NormalInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_MetalnessTexToggle", m_MetalnessInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_RoughnessTexToggle", m_RoughnessInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_EnvMapRotation", m_EnvMapRotation);
m_SphereBaseMaterial->Set("u_AlbedoColor", m_AlbedoInput.Color);
m_SphereBaseMaterial->Set("lights", m_Light);
m_SphereBaseMaterial->Set("u_RadiancePrefilter", m_RadiancePrefilter ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_AlbedoTexToggle", m_AlbedoInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_NormalTexToggle", m_NormalInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_MetalnessTexToggle", m_MetalnessInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_RoughnessTexToggle", m_RoughnessInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_EnvMapRotation", m_EnvMapRotation);
if (m_AlbedoInput.TextureMap)
m_MeshMaterial->Set("u_AlbedoTexture", m_AlbedoInput.TextureMap);
if (m_NormalInput.TextureMap)
m_MeshMaterial->Set("u_NormalTexture", m_NormalInput.TextureMap);
if (m_MetalnessInput.TextureMap)
m_MeshMaterial->Set("u_MetalnessTexture", m_MetalnessInput.TextureMap);
if (m_RoughnessInput.TextureMap)
m_MeshMaterial->Set("u_RoughnessTexture", m_RoughnessInput.TextureMap);
if (m_AllowViewportCameraEvents)
m_Scene->GetCamera().OnUpdate(deltaTime);
m_ActiveScene->OnUpdate(deltaTime);
if (m_DrawOnTopBoundingBoxes)
{
// THINGS TO LOOK AT:
// - BRDF LUT
// - Tonemapping and proper HDR pipeline
using namespace Prism;
using namespace glm;
m_MeshMaterial->Set("u_AlbedoColor", m_AlbedoInput.Color);
m_MeshMaterial->Set("u_Metalness", m_MetalnessInput.Value);
m_MeshMaterial->Set("u_Roughness", m_RoughnessInput.Value);
m_MeshMaterial->Set("lights", m_Light);
m_MeshMaterial->Set("u_AlbedoTexToggle", m_AlbedoInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_NormalTexToggle", m_NormalInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_MetalnessTexToggle", m_MetalnessInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_RoughnessTexToggle", m_RoughnessInput.UseTexture ? 1.0f : 0.0f);
m_MeshMaterial->Set("u_EnvMapRotation", m_EnvMapRotation);
m_SphereBaseMaterial->Set("u_AlbedoColor", m_AlbedoInput.Color);
m_SphereBaseMaterial->Set("lights", m_Light);
m_SphereBaseMaterial->Set("u_RadiancePrefilter", m_RadiancePrefilter ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_AlbedoTexToggle", m_AlbedoInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_NormalTexToggle", m_NormalInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_MetalnessTexToggle", m_MetalnessInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_RoughnessTexToggle", m_RoughnessInput.UseTexture ? 1.0f : 0.0f);
m_SphereBaseMaterial->Set("u_EnvMapRotation", m_EnvMapRotation);
if (m_AlbedoInput.TextureMap)
m_MeshMaterial->Set("u_AlbedoTexture", m_AlbedoInput.TextureMap);
if (m_NormalInput.TextureMap)
m_MeshMaterial->Set("u_NormalTexture", m_NormalInput.TextureMap);
if (m_MetalnessInput.TextureMap)
m_MeshMaterial->Set("u_MetalnessTexture", m_MetalnessInput.TextureMap);
if (m_RoughnessInput.TextureMap)
m_MeshMaterial->Set("u_RoughnessTexture", m_RoughnessInput.TextureMap);
m_ActiveScene->OnUpdate(deltaTime);
Renderer::BeginRenderPass(SceneRenderer::GetFinalRenderPass(), false);
auto viewProj = m_Scene->GetCamera().GetViewProjection();
Renderer2D::BeginScene(viewProj, false);
// Prism::Renderer2D::DrawQuad({ 0, 0, 0 }, { 4.0f, 5.0f }, { 1.0f, 1.0f, 0.5f, 1.0f });
Renderer::DrawAABB(m_MeshEntity->GetMesh());
Renderer2D::EndScene();
Renderer::EndRenderPass();
}
}
@ -431,6 +449,11 @@ namespace Prism
Property("Radiance Prefiltering", m_RadiancePrefilter);
Property("Env Map Rotation", m_EnvMapRotation, -360.0f, 360.0f);
if (Property("Show Bounding Boxes", m_UIShowBoundingBoxes))
ShowBoundingBoxes(m_UIShowBoundingBoxes, m_UIShowBoundingBoxesOnTop);
if (m_UIShowBoundingBoxes && Property("On Top", m_UIShowBoundingBoxesOnTop))
ShowBoundingBoxes(m_UIShowBoundingBoxes, m_UIShowBoundingBoxesOnTop);
ImGui::Columns(1);
ImGui::End();
@ -620,12 +643,17 @@ namespace Prism
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::Begin("Viewport");
auto viewportSize = ImGui::GetContentRegionAvail();
SceneRenderer::SetViewportSize((uint32_t)viewportSize.x, (uint32_t)viewportSize.y);
m_ActiveScene->GetCamera().SetProjectionMatrix(glm::perspectiveFov(glm::radians(45.0f), viewportSize.x, viewportSize.y, 0.1f, 10000.0f));
m_ActiveScene->GetCamera().SetViewportSize((uint32_t)viewportSize.x, (uint32_t)viewportSize.y);
ImGui::Image((ImTextureRef)SceneRenderer::GetFinalColorBufferRendererID(), viewportSize, { 0, 1 }, { 1, 0 });
auto windowSize = ImGui::GetWindowSize();
ImVec2 minBound = ImGui::GetWindowPos();
ImVec2 maxBound = { minBound.x + windowSize.x, minBound.y + windowSize.y };
m_AllowViewportCameraEvents = ImGui::IsMouseHoveringRect(minBound, maxBound);
// ImGuizmo
if (m_GizmoType != -1)
{
@ -645,8 +673,11 @@ namespace Prism
void EditorLayer::OnEvent(Event& e)
{
if (m_AllowViewportCameraEvents)
m_Scene->GetCamera().OnEvent(e);
EventDispatcher dispatcher(e);
dispatcher.Dispatch<KeyPressedEvent>(HZ_BIND_EVENT_FN(EditorLayer::OnKeyPressedEvent));
dispatcher.Dispatch<KeyPressedEvent>(PM_BIND_EVENT_FN(EditorLayer::OnKeyPressedEvent));
}
bool EditorLayer::OnKeyPressedEvent(KeyPressedEvent& e)
@ -665,7 +696,27 @@ namespace Prism
case PM_KEY_R:
m_GizmoType = ImGuizmo::OPERATION::SCALE;
break;
case PM_KEY_G:
// Toggle grid
if (Input::IsKeyPressed(PM_KEY_LEFT_CONTROL))
SceneRenderer::GetOptions().ShowGrid = !SceneRenderer::GetOptions().ShowGrid;
break;
case PM_KEY_B:
// Toggle bounding boxes
if (Input::IsKeyPressed(PM_KEY_LEFT_CONTROL))
{
m_UIShowBoundingBoxes = !m_UIShowBoundingBoxes;
ShowBoundingBoxes(m_UIShowBoundingBoxes, m_UIShowBoundingBoxesOnTop);
}
break;
}
return false;
}
void EditorLayer::ShowBoundingBoxes(bool show, bool onTop)
{
SceneRenderer::GetOptions().ShowBoundingBoxes = show && !onTop;
m_DrawOnTopBoundingBoxes = show && onTop;
}
}

View File

@ -25,6 +25,8 @@ namespace Prism
private:
bool OnKeyPressedEvent(KeyPressedEvent& e);
void ShowBoundingBoxes(bool show, bool onTop = false);
private:
Scope<SceneHierarchyPanel> m_SceneHierarchyPanel;
@ -54,6 +56,13 @@ namespace Prism
// Imguizmo
int m_GizmoType = -1; // -1 = no gizmo
// configure button
bool m_AllowViewportCameraEvents = false;
bool m_DrawOnTopBoundingBoxes = false;
bool m_UIShowBoundingBoxes = false;
bool m_UIShowBoundingBoxesOnTop = false;
struct AlbedoInput
{

Binary file not shown.

View File

@ -34,7 +34,7 @@ out VertexOutput
void main()
{
vs_Output.WorldPosition = vec3(u_Transform * vec4(a_Position, 1.0));
vs_Output.Normal = a_Normal;
vs_Output.Normal = mat3(u_Transform) * a_Normal;
vs_Output.TexCoord = vec2(a_TexCoord.x, 1.0 - a_TexCoord.y);
vs_Output.WorldNormals = mat3(u_Transform) * mat3(a_Tangent, a_Binormal, a_Normal);
vs_Output.WorldTransform = mat3(u_Transform);
@ -271,7 +271,8 @@ vec3 Lighting(vec3 F0)
vec3 IBL(vec3 F0, vec3 Lr)
{
vec3 irradiance = texture(u_EnvIrradianceTex, m_Params.Normal).rgb;
vec3 F = fresnelSchlickRoughness(F0, m_Params.NdotV, m_Params.Roughness);
// vec3 F = fresnelSchlickRoughness(F0, m_Params.NdotV, m_Params.Roughness);
vec3 F = fresnelSchlick(F0, m_Params.NdotV);
vec3 kd = (1.0 - F) * (1.0 - m_Params.Metalness);
vec3 diffuseIBL = m_Params.Albedo * irradiance;
@ -316,4 +317,5 @@ void main()
vec3 iblContribution = IBL(F0, Lr);
color = vec4(lightContribution + iblContribution, 1.0);
color = vec4(iblContribution, 1.0);
}

View File

@ -0,0 +1,43 @@
// Basic Texture Shader
#type vertex
#version 430 core
layout(location = 0) in vec3 a_Position;
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;
uniform mat4 u_ViewProjection;
out vec4 v_Color;
out vec2 v_TexCoord;
out float v_TexIndex;
out float v_TilingFactor;
void main()
{
v_Color = a_Color;
v_TexCoord = a_TexCoord;
v_TexIndex = a_TexIndex;
v_TilingFactor = a_TilingFactor;
gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
}
#type fragment
#version 430 core
layout(location = 0) out vec4 color;
in vec4 v_Color;
in vec2 v_TexCoord;
in float v_TexIndex;
in float v_TilingFactor;
uniform sampler2D u_Textures[32];
void main()
{
color = texture(u_Textures[int(v_TexIndex)], v_TexCoord * v_TilingFactor) * v_Color;
}

View File

@ -0,0 +1,29 @@
// Basic Texture Shader
#type vertex
#version 430 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
uniform mat4 u_ViewProjection;
out vec4 v_Color;
void main()
{
v_Color = a_Color;
gl_Position = u_ViewProjection * vec4(a_Position, 1.0);
}
#type fragment
#version 430 core
layout(location = 0) out vec4 color;
in vec4 v_Color;
void main()
{
color = v_Color;
}

View File

@ -16,20 +16,34 @@ void main()
#type fragment
#version 430
layout(location = 0) out vec4 o_Color;
in vec2 v_TexCoord;
uniform sampler2D u_Texture;
layout(location=0) out vec4 outColor;
uniform sampler2DMS u_Texture;
uniform float u_Exposure;
uniform int u_TextureSamples;
vec4 MultiSampleTexture(sampler2DMS tex, ivec2 texCoord, int samples)
{
vec4 result = vec4(0.0);
for (int i = 0; i < samples; i++)
result += texelFetch(tex, texCoord, i);
result /= float(samples);
return result;
}
void main()
{
const float gamma = 2.2;
const float pureWhite = 1.0;
vec3 color = texture(u_Texture, v_TexCoord).rgb * u_Exposure;
ivec2 texSize = textureSize(u_Texture);
ivec2 texCoord = ivec2(v_TexCoord * texSize);
vec4 msColor = MultiSampleTexture(u_Texture, texCoord, u_TextureSamples);
vec3 color = msColor.rgb * u_Exposure;//texture(u_Texture, v_TexCoord).rgb * u_Exposure;
// Reinhard tonemapping operator.
// see: "Photographic Tone Reproduction for Digital Images", eq. 4
float luminance = dot(color, vec3(0.2126, 0.7152, 0.0722));
@ -39,5 +53,5 @@ void main()
vec3 mappedColor = (mappedLuminance / luminance) * color;
// Gamma correction.
outColor = vec4(pow(mappedColor, vec3(1.0/gamma)), 1.0);
o_Color = vec4(pow(mappedColor, vec3(1.0 / gamma)), 1.0);
}