From cdf9b0a35db46216ef6429be890e9c7c65662173 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 10 May 2023 12:48:33 -0700 Subject: [PATCH] (Temp) UI only renders to backbuffer --- src/dawn/display/RenderPipeline.cpp | 28 ++++++++++--------- .../scene/components/display/CMakeLists.txt | 1 + .../components/display/CameraTexture.cpp | 24 ++++++++++++++++ .../components/display/CameraTexture.hpp | 22 +++++++++++++++ src/dawn/scene/components/ui/UIComponent.cpp | 2 ++ src/dawnliminal/scenes/HelloWorldScene.hpp | 19 +++++++++++-- 6 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 src/dawn/scene/components/display/CameraTexture.cpp create mode 100644 src/dawn/scene/components/display/CameraTexture.hpp diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index a631a3b2..e44154ff 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -130,21 +130,23 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { } // UI Elements - auto canvases = scene->findComponents(); - auto itCanvas = canvases.begin(); - while(itCanvas != canvases.end()) { - auto canvas = *itCanvas; - glm::mat4 projection; - glm::mat4 view; - canvas->getProjectionAndView(&projection, &view); + if(renderTarget == this->renderManager->getBackBuffer()) { + auto canvases = scene->findComponents(); + auto itCanvas = canvases.begin(); + while(itCanvas != canvases.end()) { + auto canvas = *itCanvas; + glm::mat4 projection; + glm::mat4 view; + canvas->getProjectionAndView(&projection, &view); - auto renderables = canvas->item->findChildrenDeep(); - auto itChild = renderables.begin(); - while(itChild != renderables.end()) { - vectorAppend(&shaderPassItems,(*itChild)->getPassItems(projection, view)); - ++itChild; + auto renderables = canvas->item->findChildrenDeep(); + auto itChild = renderables.begin(); + while(itChild != renderables.end()) { + vectorAppend(&shaderPassItems,(*itChild)->getPassItems(projection, view)); + ++itChild; + } + ++itCanvas; } - ++itCanvas; } // Debug Lines diff --git a/src/dawn/scene/components/display/CMakeLists.txt b/src/dawn/scene/components/display/CMakeLists.txt index 8ab850d7..070a64c1 100644 --- a/src/dawn/scene/components/display/CMakeLists.txt +++ b/src/dawn/scene/components/display/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(${DAWN_TARGET_NAME} PixelPerfectCamera.cpp TiledSprite.cpp SimpleRenderTargetQuad.cpp + CameraTexture.cpp ) add_subdirectory(mesh) \ No newline at end of file diff --git a/src/dawn/scene/components/display/CameraTexture.cpp b/src/dawn/scene/components/display/CameraTexture.cpp new file mode 100644 index 00000000..79e55cca --- /dev/null +++ b/src/dawn/scene/components/display/CameraTexture.cpp @@ -0,0 +1,24 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "CameraTexture.hpp" + +using namespace Dawn; + +CameraTexture::CameraTexture(SceneItem *item) : + SceneItemComponent(item), + camera(nullptr), + renderTarget(32.0f, 32.0f) +{ +} + +void CameraTexture::onStart() { + if(this->camera == nullptr) this->camera = item->getComponent(); + + useEffect([&]{ + if(this->camera == nullptr) return; + this->camera->renderTarget = &this->renderTarget; + }, this->camera)(); +} \ No newline at end of file diff --git a/src/dawn/scene/components/display/CameraTexture.hpp b/src/dawn/scene/components/display/CameraTexture.hpp new file mode 100644 index 00000000..a9f4b640 --- /dev/null +++ b/src/dawn/scene/components/display/CameraTexture.hpp @@ -0,0 +1,22 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/components/display/Camera.hpp" +#include "display/TextureRenderTarget.hpp" + +namespace Dawn { + class CameraTexture : public SceneItemComponent { + protected: + + public: + StateProperty camera; + TextureRenderTarget renderTarget; + + CameraTexture(SceneItem *item); + + void onStart() override; + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIComponent.cpp b/src/dawn/scene/components/ui/UIComponent.cpp index dea34c79..e79daf61 100644 --- a/src/dawn/scene/components/ui/UIComponent.cpp +++ b/src/dawn/scene/components/ui/UIComponent.cpp @@ -37,6 +37,8 @@ void UIComponent::updateAlignment() { auto dimensional = this->getParentDimensional(); auto translate = this->transform->getLocalPosition(); + assertNotNull(dimensional); + float_t parentWidth, parentHeight; parentWidth = dimensional->getWidth(); parentHeight = dimensional->getHeight(); diff --git a/src/dawnliminal/scenes/HelloWorldScene.hpp b/src/dawnliminal/scenes/HelloWorldScene.hpp index 3b24c175..c15cd59f 100644 --- a/src/dawnliminal/scenes/HelloWorldScene.hpp +++ b/src/dawnliminal/scenes/HelloWorldScene.hpp @@ -5,9 +5,11 @@ #pragma once #include "scene/Scene.hpp" -#include "scene/components/display/Camera.hpp" +#include "scene/components/display/CameraTexture.hpp" #include "prefabs/SimpleSpinningCubePrefab.hpp" #include "prefabs/VNTextbox.hpp" +#include "display/TextureRenderTarget.hpp" +#include "scene/components/ui/UIImage.hpp" namespace Dawn { class HelloWorldScene : public Scene { @@ -15,6 +17,7 @@ namespace Dawn { Camera *camera; Camera *camNew; UICanvas *canvas; + TextureRenderTarget *renderTarget; Texture text; int32_t test = 0; @@ -24,17 +27,27 @@ namespace Dawn { camera = Camera::create(this); camera->fov = 0.436332f; - camera->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0)); + camera->transform->lookAt(glm::vec3(900, 900, 900), glm::vec3(800, 800, 800)); camNew = Camera::create(this); + auto camTexture = camNew->item->addComponent(); camNew->transform->lookAt(glm::vec3(10, 10, 10), glm::vec3(0, 0, 0)); - camNew->renderTarget = nullptr; + camTexture->renderTarget.setSize(256, 256); auto cube = SimpleSpinningCubePrefab::create(this); auto textbox = VNTextbox::create(this); textbox->transform.setParent(canvas->transform); + auto uiTest = this->createSceneItem(); + uiTest->transform.setParent(canvas->transform); + auto image = uiTest->addComponent(); + image->texture = camTexture->renderTarget.getTexture(); + image->alignment = glm::vec4(0, 0, 50, 0); + image->alignX = UI_COMPONENT_ALIGN_START; + image->alignUnitRight = UI_COMPONENT_ALIGN_UNIT_PERCENT; + image->alignY = UI_COMPONENT_ALIGN_STRETCH; + struct Color colors[] = { COLOR_BLUE, COLOR_MAGENTA, COLOR_WHITE, COLOR_MAGENTA, COLOR_CORNFLOWER_BLUE, COLOR_MAGENTA,