From af5d4d0a02e0794179c3df686df5a642e756eca5 Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Mon, 5 Jan 2026 16:51:17 +0000 Subject: [PATCH] feat: Enhance background opacity handling and optimize pixel rendering for transparency --- scripts/gui.lua | 11 +++++++++++ src/services/impl/gui_renderer.cpp | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/scripts/gui.lua b/scripts/gui.lua index 00c3984..00cf8f0 100644 --- a/scripts/gui.lua +++ b/scripts/gui.lua @@ -196,11 +196,16 @@ function Context:applyBackgroundOpacity(color) -- For full transparency (opacity = 0), set alpha to 0 -- For partial transparency, multiply the existing alpha local result = {color[1], color[2], color[3], color[4] or 1.0} + local originalAlpha = result[4] if self.opacity == 0 then result[4] = 0 else result[4] = result[4] * self.opacity end + if lua_debug then + print(string.format("[GUI] applyBackgroundOpacity: opacity=%.2f, original alpha=%.2f, new alpha=%.2f", + self.opacity, originalAlpha, result[4])) + end return result end @@ -208,6 +213,12 @@ function Context:pushRect(rect, params) params = params or {} -- Only apply opacity to the background color, not borders or other elements local color = params.color and self:applyBackgroundOpacity(params.color) or params.color + + -- Skip drawing background rect entirely if it would be fully transparent + if color and color[4] == 0 and (not params.borderColor or params.borderWidth == 0) then + return + end + local command = { type = "rect", x = rect.x, diff --git a/src/services/impl/gui_renderer.cpp b/src/services/impl/gui_renderer.cpp index 98d29ac..871762e 100644 --- a/src/services/impl/gui_renderer.cpp +++ b/src/services/impl/gui_renderer.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -479,12 +480,22 @@ GuiRenderer::GuiRenderer(VkDevice device, VkPhysicalDevice physicalDevice, VkFor const auto& pixels = canvas_->Pixels(); size_t pixelCount = static_cast(canvasWidth_) * canvasHeight_; uint8_t* dest = reinterpret_cast(stagingMapped_); + + // Clear destination to fully transparent so pixels with alpha=0 don't overwrite the scene + std::memset(dest, 0, pixelCount * 4); + for (size_t i = 0; i < pixelCount; ++i) { size_t offset = i * 4; uint8_t r = pixels[offset]; uint8_t g = pixels[offset + 1]; uint8_t b = pixels[offset + 2]; uint8_t a = pixels[offset + 3]; + + // Skip fully transparent pixels to avoid overwriting the 3D scene + if (a == 0) { + continue; + } + switch (swapchainFormat_) { case VK_FORMAT_B8G8R8A8_UNORM: case VK_FORMAT_B8G8R8A8_SRGB: