Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Engine_Master_UPC/ModuleRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ void ModuleRender::renderScene(ID3D12GraphicsCommandList4* commandList, const Re
const float w = static_cast<float>(outputSurface.getWidth());
const float h = static_cast<float>(outputSurface.getHeight());

app->getModuleUI()->buildCommandsForViewport(w, h);

D3D12_VIEWPORT viewport = { 0.0f, 0.0f, w, h, 0.0f, 1.0f };
D3D12_RECT scissorRect = { 0, 0, static_cast<LONG>(w), static_cast<LONG>(h) };

Expand Down
86 changes: 62 additions & 24 deletions Engine_Master_UPC/ModuleUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ void ModuleUI::preRender()
{
m_textCommands.clear();
m_imageCommands.clear();
}

#ifdef GAME_RELEASE
auto viewport = app->getModuleD3D12()->getSwapChain()->getViewport();
const ImVec2 screenSize(viewport.Width, viewport.Height);
#else
const ImVec2 screenSize = app->getModuleEditor()->getWindowSceneEditor()->getSize();
#endif
void ModuleUI::buildCommandsForViewport(float width, float height)
{
m_textCommands.clear();
m_imageCommands.clear();

if (screenSize.x <= 0.0f || screenSize.y <= 0.0f)
if (width <= 0.0f || height <= 0.0f)
{
return;
}

m_rootScreenRect = { 0.0f, 0.0f, screenSize.x, screenSize.y };
m_rootScreenRect = { 0.0f, 0.0f, width, height };

for (GameObject* go : app->getModuleScene()->getScene()->getAllGameObjects())
{
if (!go || !go->GetActive())
{
continue;
}

Canvas* canvas = go->GetComponentAs<Canvas>(ComponentType::CANVAS);
if (!canvas || !canvas->isActive())
Expand All @@ -60,22 +63,22 @@ void ModuleUI::preRender()

if (isScreenSpace)
{
uiScale = UILayoutUtils::CalculateScreenSpaceScale(screenSize.x, screenSize.y);
uiScale = UILayoutUtils::CalculateScreenSpaceScale(width, height);
}
else
{
rootRect = { -0.5f, -0.5f, 1.0f, 1.0f };
}

if (Transform2D* canvasTransform = go->GetComponentAs<Transform2D>(ComponentType::TRANSFORM2D))
{
if (canvasTransform->isActive())
{
rootRect = canvasTransform->getRect(rootRect, { 1.0f, 1.0f });
}
}
if (Transform2D* canvasTransform = go->GetComponentAs<Transform2D>(ComponentType::TRANSFORM2D))
{
if (canvasTransform->isActive())
{
rootRect = canvasTransform->getRect(rootRect, { 1.0f, 1.0f });
}
}

buildUIDrawCommands(go, rootRect, canvas->renderMode, go->GetTransform()->getGlobalMatrix(), canvas->zTest, uiScale);
buildUIDrawCommands(go, rootRect, canvas->renderMode, go->GetTransform()->getGlobalMatrix(), canvas->zTest, uiScale);
}
}

Expand Down Expand Up @@ -226,26 +229,61 @@ void ModuleUI::buildUIImage(GameObject* gameObject, const Rect2D& myRect, Canvas
command.sheetOffset = Vector2(0.5f, 0.5f) * command.uvScale + uiImg->getSheetOffset();

const bool hasSheet = uiImg->getSheetColumns() > 1 || uiImg->getSheetRows() > 1;
if (uiImg->getStretchDrawMode() == UIImage::StretchDrawMode::Tile && !hasSheet)
const UIImage::StretchDrawMode drawMode = uiImg->getStretchDrawMode();

if (drawMode == UIImage::StretchDrawMode::Cover && !hasSheet)
{
TextureAsset* textureAsset = uiImg->getTextureAsset();

if (textureAsset)
{
const float textureWidth = static_cast<float>(textureAsset->getWidth());
const float textureHeight = static_cast<float>(textureAsset->getHeight());

if (textureWidth > 0.0f && textureHeight > 0.0f && myRect.w > 0.0f && myRect.h > 0.0f)
{
const float textureAspect = textureWidth / textureHeight;
const float rectAspect = myRect.w / myRect.h;

if (rectAspect > textureAspect)
{
// if the rectAspect is wider than the texture, fill width and crop top/bottom.
const float visibleV = textureAspect / rectAspect;

command.uvScale = { 1.0f, visibleV };
command.sheetOffset = { 0.5f, 0.5f };
}
else
{
// int he other hand, if the rect is taller/narrower than the texture, fill height and crop left/right.
const float visibleU = rectAspect / textureAspect;

command.uvScale = { visibleU, 1.0f };
command.sheetOffset = { 0.5f, 0.5f };
}
}
}
}
else if (drawMode == UIImage::StretchDrawMode::Tile && !hasSheet)
{
if (t2d->getStretchMode() == StretchMode::HORIZONTAL)
{
command.uvScale.y *= t2d->getScale().y;
}
else if (t2d->getStretchMode() == StretchMode::VERTICAL)
{
command.uvScale.x *= t2d->getScale().x;
}
}
else if (t2d->getStretchMode() == StretchMode::BOTH)
{
command.uvScale *= Vector2(myRect.w / t2d->getBaseSize().x, myRect.h / t2d->getBaseSize().y);
}
{
command.uvScale *= Vector2(myRect.w / t2d->getBaseSize().x, myRect.h / t2d->getBaseSize().y);
}
else
{
command.uvScale *= t2d->getScale();
}
command.sheetOffset = Vector2(0.5f, 0.5f);
}

command.sheetOffset = Vector2(0.5f, 0.5f);
}

command.renderMode = renderMode;
Expand Down
2 changes: 2 additions & 0 deletions Engine_Master_UPC/ModuleUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ModuleUI : public Module
void preRender() override;
bool cleanUp() override;

void buildCommandsForViewport(float width, float height);

const std::vector<UITextCommand>& getTextCommands() const { return m_textCommands; }
const std::vector<UIImageCommand>& getImageCommands() const { return m_imageCommands; }

Expand Down
13 changes: 5 additions & 8 deletions Engine_Master_UPC/RenderSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ RenderSurface::RenderSurface() : m_textures(AttachmentPoint::NUM_ATTACHMENT_POIN
void RenderSurface::attachTexture(AttachmentPoint attachmentPoint, std::shared_ptr<Texture> texture)
{
m_textures[attachmentPoint] = texture;

if (texture && texture->getD3D12Resource())
{
auto desc = texture->getD3D12ResourceDesc();

m_size.x = static_cast<float>(desc.Width);
m_size.y = static_cast<float>(desc.Height);
}
}

std::shared_ptr<Texture> RenderSurface::getTexture(AttachmentPoint attachmentPoint) const
Expand Down Expand Up @@ -88,6 +80,11 @@ void RenderSurface::resize(uint32_t width, uint32_t height)
resize(Vector2(static_cast<float>(width), static_cast<float>(height)));
}

void RenderSurface::setSize(uint32_t width, uint32_t height)
{
m_size = Vector2(static_cast<float>(width), static_cast<float>(height));
}

Vector2 RenderSurface::getSize() const
{
return m_size;
Expand Down
1 change: 1 addition & 0 deletions Engine_Master_UPC/RenderSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class RenderSurface
void resize(Vector2 size);
void resize(uint32_t width, uint32_t height);

void setSize(uint32_t width, uint32_t height);
Vector2 getSize() const;

uint32_t getWidth() const;
Expand Down
8 changes: 7 additions & 1 deletion Engine_Master_UPC/SwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ SwapChain::SwapChain(HWND hWnd, ComPtr<ID3D12Device4> device, CommandQueue* queu

auto* depthTexture = app->getModuleResources()->createDepthBuffer(float(m_windowWidth), float(m_windowHeight));
m_renderSurface.attachTexture( RenderSurface::DEPTH_STENCIL, std::shared_ptr<Texture>(depthTexture));

m_renderSurface.setSize(m_windowWidth, m_windowHeight);
}

SwapChain::~SwapChain()
Expand Down Expand Up @@ -114,7 +116,11 @@ void SwapChain::resize()
DXCall(m_swapChain->ResizeBuffers(FRAMES_IN_FLIGHT, width, height, swapChainDesc.BufferDesc.Format, swapChainDesc.Flags));

createRenderTargetViews(app->getModuleD3D12()->getDevice());
m_renderSurface.resize(m_windowWidth, m_windowHeight);

auto* depthTexture = app->getModuleResources()->createDepthBuffer(float(m_windowWidth), float(m_windowHeight));
m_renderSurface.attachTexture(RenderSurface::DEPTH_STENCIL, std::shared_ptr<Texture>(depthTexture));

m_renderSurface.setSize(m_windowWidth, m_windowHeight);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Engine_Master_UPC/UIImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void UIImage::drawUi()
ImGui::Separator();
ImGui::TextUnformatted("Stretch");
{
const char* modes[] = { "Stretch", "Tile" };
const char* modes[] = { "Stretch", "Tile", "Cover" };
int mode = static_cast<int>(m_stretchDrawMode);
if (ImGui::Combo("When Stretched", &mode, modes, IM_ARRAYSIZE(modes)))
{
Expand Down
15 changes: 12 additions & 3 deletions Engine_Master_UPC/UIImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@ class UIImage : public Component
enum class StretchDrawMode
{
Stretch = 0,
Tile = 1
Tile = 1,
Cover = 2
};

static const char* StretchDrawModeToString(uint32_t v)
{
return v == 0 ? "Stretch" : "Tile";
switch (static_cast<StretchDrawMode>(v))
{
case StretchDrawMode::Stretch: return "Stretch";
case StretchDrawMode::Tile: return "Tile";
case StretchDrawMode::Cover: return "Cover";
}
}

static uint32_t StringToStretchDrawMode(const char* s)
{
return std::strcmp(s, "Tile") == 0 ? 1 : 0;
if (std::strcmp(s, "Tile") == 0) return 1;
if (std::strcmp(s, "Cover") == 0) return 2;
return 0;
}

UIImage(UID id, GameObject* owner);

std::unique_ptr<Component> clone(GameObject* newOwner) const override;
Expand Down