From 6b6bbc6ac53c236b1930e620aaf877d194b745af Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Tue, 21 Feb 2023 11:37:09 +0800 Subject: [PATCH 1/2] Camera AR now stored in system instead of camera component. --- Assets/Scenes/MainGame.shade | 3 ++- SHADE_Engine/src/Camera/SHCameraComponent.cpp | 23 +++++-------------- SHADE_Engine/src/Camera/SHCameraComponent.h | 7 ++---- SHADE_Engine/src/Camera/SHCameraDirector.cpp | 16 ++++++------- SHADE_Engine/src/Camera/SHCameraDirector.h | 2 +- SHADE_Engine/src/Camera/SHCameraSystem.cpp | 22 ++++-------------- SHADE_Engine/src/Camera/SHCameraSystem.h | 2 ++ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 7 +++++- SHADE_Managed/src/Components/Camera.cxx | 9 +------- SHADE_Managed/src/Components/Camera.hxx | 7 ++---- 10 files changed, 35 insertions(+), 63 deletions(-) diff --git a/Assets/Scenes/MainGame.shade b/Assets/Scenes/MainGame.shade index 438b5a29..aa64d023 100644 --- a/Assets/Scenes/MainGame.shade +++ b/Assets/Scenes/MainGame.shade @@ -9687,6 +9687,7 @@ aimingLength: 1 throwItem: false rayDistance: 0.75 + rayHeight: 0.100000001 - EID: 3 Name: HoldingPoint IsActive: true @@ -9714,10 +9715,10 @@ Yaw: 360 Roll: 1.28065994e-06 Width: 1920 - Height: 1080 Near: 0.00999999978 Far: 10000 Perspective: true + FOV: 90 IsActive: true Camera Arm Component: Arm Pitch: 0 diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.cpp b/SHADE_Engine/src/Camera/SHCameraComponent.cpp index 17378d79..ef3276a7 100644 --- a/SHADE_Engine/src/Camera/SHCameraComponent.cpp +++ b/SHADE_Engine/src/Camera/SHCameraComponent.cpp @@ -10,8 +10,8 @@ namespace SHADE { SHCameraComponent::SHCameraComponent() :yaw(0.0f), pitch(0.0f), roll(0.0f) - , width(1920.0f), height(1080.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(0.5f) - , perspProj(true), dirtyView(true), dirtyProj(true) + , width(1920.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(0.5f) + , perspProj(true), dirtyView(true), dirtyProj(true), followScreenAR(true) , viewMatrix(), perspProjMatrix(), orthoProjMatrix() , position(), offset() { @@ -122,11 +122,6 @@ namespace SHADE } - void SHCameraComponent::SetHeight(float height) noexcept - { - this->height = height; - dirtyProj = true; - } void SHCameraComponent::SetNear(float znear) noexcept { @@ -176,10 +171,7 @@ namespace SHADE return width; } - float SHCameraComponent::GetHeight() const noexcept - { - return height; - } + float SHCameraComponent::GetNear() const noexcept { @@ -191,10 +183,7 @@ namespace SHADE return zFar; } - float SHCameraComponent::GetAspectRatio() const noexcept - { - return width/height; - } + float SHCameraComponent::GetFOV() const noexcept { @@ -251,11 +240,11 @@ RTTR_REGISTRATION .property("Yaw", &SHCameraComponent::GetYaw, &SHCameraComponent::SetYaw) .property("Roll", &SHCameraComponent::GetRoll, &SHCameraComponent::SetRoll) .property("Width", &SHCameraComponent::GetWidth, &SHCameraComponent::SetWidth) - .property("Height", &SHCameraComponent::GetHeight, &SHCameraComponent::SetHeight) .property("Near", &SHCameraComponent::GetNear, &SHCameraComponent::SetNear) .property("Far", &SHCameraComponent::GetFar, &SHCameraComponent::SetFar) .property("Perspective", &SHCameraComponent::GetIsPerspective, &SHCameraComponent::SetIsPerspective) - .property("FOV",&SHCameraComponent::GetFOV, &SHCameraComponent::SetFOV); + .property("FOV", &SHCameraComponent::GetFOV, &SHCameraComponent::SetFOV); + } diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.h b/SHADE_Engine/src/Camera/SHCameraComponent.h index b0999fe9..b1b069c3 100644 --- a/SHADE_Engine/src/Camera/SHCameraComponent.h +++ b/SHADE_Engine/src/Camera/SHCameraComponent.h @@ -19,7 +19,6 @@ namespace SHADE float roll; float width; - float height; float zNear; float zFar; float fov; @@ -27,7 +26,6 @@ namespace SHADE bool dirtyView; bool dirtyProj; - SHMatrix viewMatrix; SHMatrix perspProjMatrix; SHMatrix orthoProjMatrix; @@ -45,6 +43,8 @@ namespace SHADE SHCameraComponent(); virtual ~SHCameraComponent(); + bool followScreenAR; + //Getters and setters. void SetYaw(float yaw) noexcept; @@ -57,7 +57,6 @@ namespace SHADE void SetPosition(SHVec3 pos) noexcept; void SetWidth(float width) noexcept; - void SetHeight(float height) noexcept; void SetNear(float znear) noexcept; void SetFar(float zfar) noexcept; void SetFOV(float fov) noexcept; @@ -70,11 +69,9 @@ namespace SHADE float GetRoll() const noexcept; float GetWidth() const noexcept; - float GetHeight() const noexcept; float GetNear() const noexcept; float GetFar() const noexcept; - float GetAspectRatio() const noexcept; float GetFOV() const noexcept; bool GetIsPerspective() const noexcept; diff --git a/SHADE_Engine/src/Camera/SHCameraDirector.cpp b/SHADE_Engine/src/Camera/SHCameraDirector.cpp index 7d5ec6f2..15255ae8 100644 --- a/SHADE_Engine/src/Camera/SHCameraDirector.cpp +++ b/SHADE_Engine/src/Camera/SHCameraDirector.cpp @@ -99,13 +99,13 @@ namespace SHADE return 0.0f; } - float SHCameraDirector::GetHeight() noexcept - { - SHCameraComponent* camComponent = GetMainCameraComponent(); - if (camComponent) - return camComponent->GetHeight(); - else - return 0.0f; - } + //float SHCameraDirector::GetHeight() noexcept + //{ + // SHCameraComponent* camComponent = GetMainCameraComponent(); + // if (camComponent) + // return camComponent->GetHeight(); + // else + // return 0.0f; + //} } diff --git a/SHADE_Engine/src/Camera/SHCameraDirector.h b/SHADE_Engine/src/Camera/SHCameraDirector.h index 381531d6..48e85fc2 100644 --- a/SHADE_Engine/src/Camera/SHCameraDirector.h +++ b/SHADE_Engine/src/Camera/SHCameraDirector.h @@ -31,7 +31,7 @@ namespace SHADE SHMatrix const& GetOrthoMatrix() noexcept; SHMatrix const& GetPerspectiveMatrix() noexcept; float GetWidth() noexcept; - float GetHeight() noexcept; + //float GetHeight() noexcept; private: SHMatrix viewMatrix; diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index 6154c104..eb3fd72d 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -116,7 +116,6 @@ namespace SHADE editorCamera.SetYaw(0.0f); editorCamera.SetRoll(0.0f); editorCamera.SetWidth(1080.0f); - editorCamera.SetHeight(720.0f); editorCamera.SetFar(10000000.0f); editorCamera.movementSpeed = 2.0f; editorCamera.perspProj = true; @@ -144,18 +143,7 @@ namespace SHADE //std::cout << EVENT_DATA->resizeWidth << std::endl; //std::cout << EVENT_DATA->resizeHeight << std::endl; - - - for (auto director : directorHandleList) - { - auto camera = SHComponentManager::GetComponent_s(director->mainCameraEID); - if (camera) - { - camera->SetWidth(EVENT_DATA->resizeWidth); - camera->SetHeight(EVENT_DATA->resizeHeight); - } - - } + screenAspectRatio = (float)EVENT_DATA->resizeWidth / (float)EVENT_DATA->resizeHeight; return eventPtr->handle; @@ -340,7 +328,7 @@ namespace SHADE if (camera.dirtyProj == true) { //Perspective projection matrix. - const float ASPECT_RATIO = (camera.GetAspectRatio()); + const float ASPECT_RATIO = (screenAspectRatio); const float TAN_HALF_FOV = tan(SHMath::DegreesToRadians(camera.fov) * 0.5f); camera.perspProjMatrix = SHMatrix::Identity; camera.perspProjMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV); @@ -357,7 +345,7 @@ namespace SHADE const float right = camera.GetWidth() * 0.5f; const float left = -right; - const float top = camera.GetHeight() * 0.5f; + const float top = camera.GetWidth() / screenAspectRatio * 0.5f; const float btm = -top; const float n = camera.GetNear(); const float f = camera.GetFar(); @@ -574,11 +562,11 @@ namespace SHADE auto editor = SHSystemManager::GetSystem(); if (editor->editorState != SHEditor::State::PLAY) { - return SHVec2{ GetEditorCamera()->GetWidth(), GetEditorCamera()->GetHeight() }; + return SHVec2{ GetEditorCamera()->GetWidth(), GetEditorCamera()->GetWidth() / screenAspectRatio }; } else { - return SHVec2{ GetDirector(index)->GetWidth(),GetDirector(index)->GetHeight() }; + return SHVec2{ GetDirector(index)->GetWidth(),GetDirector(index)->GetWidth() / screenAspectRatio }; } #else diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.h b/SHADE_Engine/src/Camera/SHCameraSystem.h index 4d6476bf..92728537 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.h +++ b/SHADE_Engine/src/Camera/SHCameraSystem.h @@ -21,6 +21,8 @@ namespace SHADE SHCameraComponent editorCamera; SHCameraArmComponent editorCameraArm; + float screenAspectRatio{16.0f/9.0f}; + SHResourceLibrary directorLibrary; std::vector directorHandleList; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 297a86ae..8f4f30af 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -1177,7 +1177,7 @@ namespace SHADE auto cameraSystem = SHSystemManager::GetSystem(); #ifdef SHEDITOR cameraSystem->GetEditorCamera()->SetWidth(static_cast(resizeWidth)); - cameraSystem->GetEditorCamera()->SetHeight(static_cast(resizeHeight)); + //cameraSystem->GetEditorCamera()->SetAspectRatio(static_cast(resizeWidth) / static_cast(resizeHeight)); // Create new event and broadcast it SHWindowResizeEvent newEvent; @@ -1187,7 +1187,12 @@ namespace SHADE SHEventManager::BroadcastEvent(newEvent, SH_WINDOW_RESIZE_EVENT); #else + // Create new event and broadcast it + SHWindowResizeEvent newEvent; + newEvent.resizeWidth = resizeWidth; + newEvent.resizeHeight = resizeHeight; + SHEventManager::BroadcastEvent(newEvent, SH_WINDOW_RESIZE_EVENT); #endif } diff --git a/SHADE_Managed/src/Components/Camera.cxx b/SHADE_Managed/src/Components/Camera.cxx index 0d0dbced..4640c17a 100644 --- a/SHADE_Managed/src/Components/Camera.cxx +++ b/SHADE_Managed/src/Components/Camera.cxx @@ -50,15 +50,8 @@ namespace SHADE { GetNativeComponent()->SetWidth(val); } - float Camera::Height::get() - { - return (GetNativeComponent()->GetHeight()); - } + - void Camera::Height::set(float val) - { - GetNativeComponent()->SetHeight(val); - } float Camera::Near::get() { return (GetNativeComponent()->GetNear()); diff --git a/SHADE_Managed/src/Components/Camera.hxx b/SHADE_Managed/src/Components/Camera.hxx index c6afeb6d..c793f1ed 100644 --- a/SHADE_Managed/src/Components/Camera.hxx +++ b/SHADE_Managed/src/Components/Camera.hxx @@ -35,11 +35,7 @@ namespace SHADE float get(); void set(float val); } - property float Height - { - float get(); - void set(float val); - } + property float Near { float get(); @@ -60,6 +56,7 @@ namespace SHADE Vector3 get(); void set(Vector3 val); } + void SetMainCamera(size_t directorIndex); From 546ac9bb0008bd0c66bd54481e06dc0e75f44384 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Fri, 24 Feb 2023 15:02:41 +0800 Subject: [PATCH 2/2] Gets the resource manager to pre-Load the textures when we set button texture asset ids --- SHADE_Engine/src/UI/SHButtonComponent.cpp | 3 +++ SHADE_Engine/src/UI/SHToggleButtonComponent.cpp | 5 ++++- SHADE_Engine/src/UI/SHUISystem.cpp | 15 ++++++--------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/SHADE_Engine/src/UI/SHButtonComponent.cpp b/SHADE_Engine/src/UI/SHButtonComponent.cpp index cbc36ce3..5cc31841 100644 --- a/SHADE_Engine/src/UI/SHButtonComponent.cpp +++ b/SHADE_Engine/src/UI/SHButtonComponent.cpp @@ -28,16 +28,19 @@ namespace SHADE void SHButtonComponent::SetDefaultTexture(AssetID texture) noexcept { defaultTexture = texture; + SHResourceManager::LoadOrGet(texture); } void SHButtonComponent::SetHoveredTexture(AssetID texture) noexcept { hoveredTexture = texture; + SHResourceManager::LoadOrGet(texture);\ } void SHButtonComponent::SetClickedTexture(AssetID texture) noexcept { clickedTexture = texture; + SHResourceManager::LoadOrGet(texture); } diff --git a/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp b/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp index 1ae0e9e0..546a2a26 100644 --- a/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp +++ b/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp @@ -1,6 +1,7 @@ #include "SHpch.h" #include "SHToggleButtonComponent.h" - +#include "Resource/SHResourceManager.h" +#include "Graphics/MiddleEnd/Textures/SHTextureLibrary.h" namespace SHADE { @@ -28,11 +29,13 @@ namespace SHADE void SHToggleButtonComponent::SetDefaultTexture(AssetID texture) noexcept { defaultTexture = texture; + SHResourceManager::LoadOrGet(texture); } void SHToggleButtonComponent::SetToggledTexture(AssetID texture) noexcept { toggledTexture = texture; + SHResourceManager::LoadOrGet(texture); } void SHToggleButtonComponent::SetValue(bool value) noexcept diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index b42e71a6..e27c00a2 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -313,8 +313,8 @@ namespace SHADE { auto material = renderable->GetModifiableMaterial(); comp.currentTexture = textureID; - material->SetProperty("data.textureIndex", SHResourceManager::LoadOrGet(textureID)); - loadTexture = true; + material->SetProperty("data.textureIndex", SHResourceManager::Get(textureID)->TextureArrayIndex); + } @@ -368,8 +368,8 @@ namespace SHADE { auto material = renderable->GetModifiableMaterial(); comp.currentTexture = textureID; - material->SetProperty("data.textureIndex", SHResourceManager::LoadOrGet(textureID)); - loadTexture = true; + material->SetProperty("data.textureIndex", SHResourceManager::Get(textureID)->TextureArrayIndex); + } @@ -381,6 +381,8 @@ namespace SHADE { SHUISystem* system = (SHUISystem*)GetSystem(); auto& dense = SHComponentManager::GetDense(); + //We ensure that the textures are loaded before we do the update. + SHResourceManager::FinaliseChanges(); for (auto& comp : dense) { if (SHSceneManager::CheckNodeAndComponentsActive(comp.GetEID())) @@ -393,11 +395,6 @@ namespace SHADE if (SHSceneManager::CheckNodeAndComponentsActive(comp.GetEID())) system->UpdateToggleButtonComponent(comp); } - if (system->loadTexture == true) - { - system->loadTexture = false; - SHResourceManager::FinaliseChanges(); - } } SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept